Browse Source

Add month mode

pull/1/head
DemiSel 2 years ago
parent
commit
249bcbf4df
1 changed files with 38 additions and 8 deletions
  1. 38
    8
      export_data-v2.py

+ 38
- 8
export_data-v2.py View File

3
 # ------------------------------------------------
3
 # ------------------------------------------------
4
 
4
 
5
 from matplotlib import pyplot as plt
5
 from matplotlib import pyplot as plt
6
+from datetime import datetime
6
 import os
7
 import os
7
 import json
8
 import json
8
 import re
9
 import re
15
 
16
 
16
 DATA_PATH = './data/'
17
 DATA_PATH = './data/'
17
 
18
 
19
+MONTH_MODE = False
20
+MONTH = None
21
+
18
 OTHER_LABEL = 'Les Autres'
22
 OTHER_LABEL = 'Les Autres'
19
 
23
 
20
 # JSON tags
24
 # JSON tags
28
 HELP = """General options :
32
 HELP = """General options :
29
     -h, --help          Consulter l'aide
33
     -h, --help          Consulter l'aide
30
     --path=<path>       Redéfinir le chemin d'accès aux données (par défaut ./data)
34
     --path=<path>       Redéfinir le chemin d'accès aux données (par défaut ./data)
35
+    --month <mm/yyyy>
31
     """
36
     """
32
 
37
 
33
 # ------------------------------------------------
38
 # ------------------------------------------------
34
 #                   Functions
39
 #                   Functions
35
 # ------------------------------------------------
40
 # ------------------------------------------------
36
 
41
 
42
+def printHelp():
43
+    print('Usage:\n '+os.path.basename(__file__)+' <command> [option]\n')
44
+    print(HELP)
45
+    sys.exit(2)
46
+
37
 def handleArguments(argv):
47
 def handleArguments(argv):
48
+    global MONTH_MODE
49
+    global MONTH
50
+    
38
     try:
51
     try:
39
-        opts, args = getopt.getopt(argv, 'h',['help','path='])
52
+        opts, args = getopt.getopt(argv, 'h',['help','path=', 'month='])
40
     except getopt.GetoptError:
53
     except getopt.GetoptError:
41
-        print('Usage:\n '+os.path.basename(__file__)+' <command> [option]\n')
42
-        print(HELP)
43
-        sys.exit(2)
54
+        printHelp()
44
 
55
 
45
     for opt, arg in opts:
56
     for opt, arg in opts:
46
         if opt in ('-h', '--help'):
57
         if opt in ('-h', '--help'):
48
             sys.exit()
59
             sys.exit()
49
         elif opt in ('--path'):
60
         elif opt in ('--path'):
50
             DATA_PATH = arg
61
             DATA_PATH = arg
62
+        elif opt in ('--month'):
63
+            MONTH_MODE = True
64
+            try:
65
+                t = arg.split("/")
66
+                int(t[0])
67
+                int(t[1])
68
+                MONTH = t 
69
+            except:
70
+                printHelp()
51
 
71
 
52
 def readBrokenFbJson(datafile_path):
72
 def readBrokenFbJson(datafile_path):
53
     # ntm facebook
73
     # ntm facebook
65
     datafiles_path = [DATA_PATH + filename for filename in os.listdir(DATA_PATH)]
85
     datafiles_path = [DATA_PATH + filename for filename in os.listdir(DATA_PATH)]
66
     messages, participants = [], []
86
     messages, participants = [], []
67
 
87
 
68
-    print(datafiles_path)
69
     
88
     
70
     for datafile_path in datafiles_path:
89
     for datafile_path in datafiles_path:
71
         datacontent = readBrokenFbJson(datafile_path)
90
         datacontent = readBrokenFbJson(datafile_path)
82
     return set([participant[NAME] for participant in rawParticipants])
101
     return set([participant[NAME] for participant in rawParticipants])
83
 
102
 
84
 def cleanMessages(rawMessages):
103
 def cleanMessages(rawMessages):
85
-    cleanMessages = [message for message in rawMessages if CONTENT in message]
104
+    if MONTH_MODE:
105
+        cleanMessages = [
106
+            message for message in rawMessages if \
107
+            CONTENT in message and \
108
+            datetime.fromtimestamp(message[TIMESTAMP]/1000).month == int(MONTH[0]) and \
109
+            datetime.fromtimestamp(message[TIMESTAMP]/1000).year == int(MONTH[1])
110
+        ]
111
+    else:
112
+        cleanMessages = [message for message in rawMessages if CONTENT in message]
86
     return sorted(cleanMessages, key = lambda x: x[TIMESTAMP])
113
     return sorted(cleanMessages, key = lambda x: x[TIMESTAMP])
87
 
114
 
88
 # TODO tester l'approche en recherche incrémentale
115
 # TODO tester l'approche en recherche incrémentale
96
 
123
 
97
     for message in messages:
124
     for message in messages:
98
         sender = message[SENDER]
125
         sender = message[SENDER]
99
-        result[sender] = result[sender]+1 if sender in result else 1
126
+        countParticipation(result, sender, message)
100
 
127
 
101
     return sorted(result.items(), key = lambda x: x[1])
128
     return sorted(result.items(), key = lambda x: x[1])
102
 
129
 
130
+def countParticipation(participations, sender, message):
131
+    participations[sender] = participations[sender] + 1 if sender in participations else 1
132
+
103
 def mergeSmallParticipation(rawParticipation, threshold = 1):
133
 def mergeSmallParticipation(rawParticipation, threshold = 1):
104
     values = [e[1] for e in rawParticipation]
134
     values = [e[1] for e in rawParticipation]
105
     labels = [e[0] for e in rawParticipation]
135
     labels = [e[0] for e in rawParticipation]
106
 
136
 
107
     totalValues = sum(values)
137
     totalValues = sum(values)
108
-
138
+    idx = 0
109
     for idx, value in enumerate(values):
139
     for idx, value in enumerate(values):
110
         if 100 * value / totalValues >= threshold: break
140
         if 100 * value / totalValues >= threshold: break
111
         
141
         

Loading…
Cancel
Save