challenge-1377.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/python3
  2. import json
  3. import sys
  4. from datetime import datetime
  5. def import_array(filename):
  6. # Load the JSON file
  7. with open(filename, 'r') as f:
  8. data = json.load(f)
  9. # Access the "messages" array
  10. array = data['messages']
  11. return array
  12. def check_challenge_win(message):
  13. # Check that the message has text and that the text is "1337"
  14. if "text" in message and message["text"] == "1337":
  15. # Check if the message was edited, return false
  16. if "edited" in message:
  17. return False
  18. else:
  19. # get the hour and minutes from the date
  20. dt = datetime.fromtimestamp(int(message["date_unixtime"]))
  21. #finally, check if the hour is 13 and minute is 37
  22. if dt.hour == 13 and dt.minute == 37 :
  23. return True
  24. else:
  25. return False
  26. def main():
  27. # Get the file name from the first command line argument
  28. filename = sys.argv[1]
  29. # Import the array from the JSON file
  30. messages = import_array(filename)
  31. # create a dict containing the number of wins for each sender
  32. wins = {}
  33. # check the messages
  34. for message in messages:
  35. if(check_challenge_win(message)):
  36. #print("Message gagnant de " + message["from"] + " le " + message["date"])
  37. #If it's the first win, add the key with value 1
  38. if message["from"] not in wins:
  39. wins[message["from"]] = 1
  40. # Else, add 1 to the number of wins
  41. else:
  42. wins[message["from"]] += 1
  43. # print the resulting wins dict
  44. print(wins)
  45. if __name__ == '__main__':
  46. main()