Browse Source

refactoring

Théo Ertzscheid 2 years ago
parent
commit
babb88175e
2 changed files with 58 additions and 0 deletions
  1. 50 0
      classement-reactions.py
  2. 8 0
      outils.py

+ 50 - 0
classement-reactions.py

@@ -0,0 +1,50 @@
+#!/usr/bin/python3
+import json
+import sys
+from datetime import datetime
+import matplotlib.pyplot as plt
+from outils import import_array
+
+def create_hist(messages):
+    # Initialize the dict in which we'll store the 
+    # number of messages for each hour of the day
+    hist = {}
+    for hour in range(0, 24):
+      hist[hour]=0
+
+    for message in messages:
+      if "date_unixtime" in message :
+        message_hour = datetime.fromtimestamp(int(message["date_unixtime"])).hour
+        hist[message_hour] += 1
+    
+    return hist
+
+def main():
+  # Get the file name from the first command line argument
+  filename = sys.argv[1]
+
+  # Import the array from the JSON file
+  messages = import_array(filename)
+  
+  # create a dict containing the number of messages for each hour
+  hist = create_hist(messages)
+  
+  # sort and print the resulting wins dict
+  print(sorted(hist.items(), key=lambda x:x[1], reverse=True))
+
+  hours = list(hist.keys())
+  counts = list(hist.values())
+
+  fig = plt.figure(figsize = (10, 5))
+
+  plt.xlabel("Messages par heure")
+  plt.ylabel("Nb de messages")
+  plt.title("Heure de la journée")
+
+  # creating the bar plot
+  plt.bar(hours, counts,width = 0.4)
+  #plt.show()
+  plt.savefig("msg-heures.png")
+
+if __name__ == '__main__':
+  main()

+ 8 - 0
outils.py

@@ -0,0 +1,8 @@
+def import_array(filename):
+  # Load the JSON file
+  with open(filename, 'r') as f:
+    data = json.load(f)
+
+  # Access the "messages" array
+  array = data['messages']
+  return array