Browse Source

Ajouté une gestion de paramètres en ligne de commande

nom.py -h pour voir l'aide
pull/1/head
Figg 3 years ago
parent
commit
fddef4ae4c
1 changed files with 37 additions and 12 deletions
  1. 37
    12
      export_data-v2.py

+ 37
- 12
export_data-v2.py View File

6
 import os
6
 import os
7
 import json
7
 import json
8
 import re
8
 import re
9
-
9
+import sys, getopt
10
 
10
 
11
 
11
 
12
 # ------------------------------------------------
12
 # ------------------------------------------------
13
-#                   Constants
13
+#                   Globals
14
 # ------------------------------------------------
14
 # ------------------------------------------------
15
 
15
 
16
-DATA_PATH = 'D:/Files/Data/Messenger/'
16
+DATA_PATH = './data/'
17
 
17
 
18
 OTHER_LABEL = 'Les Autres'
18
 OTHER_LABEL = 'Les Autres'
19
 
19
 
25
 TIMESTAMP = 'timestamp_ms'
25
 TIMESTAMP = 'timestamp_ms'
26
 SENDER = 'sender_name'
26
 SENDER = 'sender_name'
27
 
27
 
28
+HELP = """General options :
29
+    -h, --help          Consulter l'aide
30
+    --path=<path>       Redéfinir le chemin d'accès aux données (par défaut ./data)
31
+    """
28
 
32
 
29
 # ------------------------------------------------
33
 # ------------------------------------------------
30
 #                   Functions
34
 #                   Functions
31
 # ------------------------------------------------
35
 # ------------------------------------------------
32
 
36
 
37
+def handleArguments(argv):
38
+    try:
39
+        opts, args = getopt.getopt(argv, 'h',['help','path='])
40
+    except getopt.GetoptError:
41
+        print('Usage:\n '+os.path.basename(__file__)+' <command> [option]\n')
42
+        print(HELP)
43
+        sys.exit(2)
44
+
45
+    for opt, arg in opts:
46
+        if opt in ('-h', '--help'):
47
+            print(HELP)
48
+            sys.exit()
49
+        elif opt in ('--path'):
50
+            DATA_PATH = arg
51
+
33
 def readBrokenFbJson(datafile_path):
52
 def readBrokenFbJson(datafile_path):
34
     # ntm facebook
53
     # ntm facebook
35
     # https://stackoverflow.com/questions/50008296/facebook-json-badly-encoded
54
     # https://stackoverflow.com/questions/50008296/facebook-json-badly-encoded
42
         return json.loads(repaired.decode('utf8'))
61
         return json.loads(repaired.decode('utf8'))
43
 
62
 
44
 def computeData():
63
 def computeData():
45
-    # Tous les fichiers du dossier sont traités sans validation
46
-    datafiles_path = [DATA_PATH + filename for filename in os.listdir(DATA_PATH) if filename [-4:] == "json"]
47
-    print(datafiles_path)
64
+    # Tous les fichiers du dossier sont traités sans distinction
65
+    datafiles_path = [DATA_PATH + filename for filename in os.listdir(DATA_PATH)]
48
     messages, participants = [], []
66
     messages, participants = [], []
49
 
67
 
68
+    print(datafiles_path)
69
+    
50
     for datafile_path in datafiles_path:
70
     for datafile_path in datafiles_path:
51
         datacontent = readBrokenFbJson(datafile_path)
71
         datacontent = readBrokenFbJson(datafile_path)
52
         if datacontent is None : continue
72
         if datacontent is None : continue
81
     return sorted(result.items(), key = lambda x: x[1])
101
     return sorted(result.items(), key = lambda x: x[1])
82
 
102
 
83
 def mergeSmallParticipation(rawParticipation, threshold = 1):
103
 def mergeSmallParticipation(rawParticipation, threshold = 1):
84
-    values = [e[1] for e in participation]
85
-    labels = [e[0] for e in participation]
104
+    values = [e[1] for e in rawParticipation]
105
+    labels = [e[0] for e in rawParticipation]
86
 
106
 
87
     totalValues = sum(values)
107
     totalValues = sum(values)
88
 
108
 
107
 #                   Main Code
127
 #                   Main Code
108
 # ------------------------------------------------
128
 # ------------------------------------------------
109
 
129
 
110
-participants, messages = computeData()
111
-messages = filterMessages(messages)
130
+def main(argv):
131
+    handleArguments(argv)
132
+
133
+    participants, messages = computeData()
134
+    messages = filterMessages(messages)
112
 
135
 
113
-participation = computeParticipation(messages)
114
-displayParticipation(participation)
136
+    participation = computeParticipation(messages)
137
+    displayParticipation(participation)
115
 
138
 
139
+if __name__ == "__main__":
140
+    main(sys.argv[1:])

Loading…
Cancel
Save