Browse Source

ajout scripts pour histogramme des messages selon l'heure d'envoi

Théo Ertzscheid 2 years ago
parent
commit
8e731dbd4d
5 changed files with 125 additions and 7 deletions
  1. 1 0
      .gitignore
  2. 1 1
      README.md
  3. 5 6
      challenge-heure.py
  4. 60 0
      histogramme-heure-minute.py
  5. 58 0
      histogramme-heure.py

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
 *.json
+*.png

+ 1 - 1
README.md

@@ -13,4 +13,4 @@ Choisir le format json en cliquant sur "HTML" en bas
 
 ## Comment lancer le script challenge-1337.py : 
 
-`python3 challenge-1337.py <nom_du_fichier.json>`
+`python3 challenge-1337.py <nom_du_fichier.json>`

+ 5 - 6
challenge-heure.py

@@ -51,7 +51,7 @@ def verify_time_string(time):
     print("Erreur : format de chaine non valide", file=sys.stderr)
     return False
   # Check that the hour and minute numbers are valid
-  elif not (0 < int(time[:2]) < 23 and 0 < int(time[2:]) < 59):
+  elif not (0 <= int(time[:2]) < 24 and 0 <= int(time[2:]) < 60):
     print("Erreur : heure non valide", file=sys.stderr)
     return False
   else: 
@@ -71,16 +71,15 @@ def main():
   else: 
     quit()
 
-
   # Import the array from the JSON file
   messages = import_array(filename)
   
   # create a dict containing the number of wins for each sender
   wins = analyze_messages(messages, hour, minute)
   
-  # print the resulting wins dict
-  print(wins)
-
+  # sort and print the resulting wins dict
+  sorted_wins = sorted(wins.items(), key=lambda x:x[1], reverse=True)
+  print(sorted_wins)
 
 if __name__ == '__main__':
-  main()
+  main()

+ 60 - 0
histogramme-heure-minute.py

@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+import json
+import sys
+from datetime import datetime
+import matplotlib.pyplot as plt
+
+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
+
+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):
+      for minute in range(0, 60):
+        hist[str(hour).zfill(2) + str(minute).zfill(2)]=0
+
+    for message in messages:
+      if "date_unixtime" in message :
+        message_hour = str(datetime.fromtimestamp(int(message["date_unixtime"])).hour).zfill(2)
+        message_minute = str(datetime.fromtimestamp(int(message["date_unixtime"])).minute).zfill(2)
+        hist[message_hour + message_minute] += 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)
+  #plt.show()
+  plt.savefig("msg-heureminutes.png")
+
+if __name__ == '__main__':
+  main()

+ 58 - 0
histogramme-heure.py

@@ -0,0 +1,58 @@
+#!/usr/bin/python3
+import json
+import sys
+from datetime import datetime
+import matplotlib.pyplot as plt
+
+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
+
+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()