| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #!/usr/bin/python3
- import json
- import sys
- from datetime import datetime
- def check_challenge_win(message, hour, minute):
- # Check that the message has text and that the text is "1337"
- if "text" in message and message["text"] == hour + minute:
- # Check if the message was edited, return false
- if "edited" in message:
- return False
- else:
- # get the hour and minutes from the date
- dt = datetime.fromtimestamp(int(message["date_unixtime"]))
- #finally, check if the hour and minute corresponds to the text of the message
- if (dt.hour == int(hour) and dt.minute == int(minute)):
- return True
- else:
- return False
- def analyze_messages(messages, hour, minute):
- # Initialize the dict that we will return
- wins = {}
- # check the messages
- for message in messages:
- if(check_challenge_win(message, hour, minute)):
- #print("Message gagnant de " + message["from"] + " le " + message["date"])
- #If it's the first win, add the key with value 1
- if message["from"] not in wins:
- wins[message["from"]] = 1
- # Else, add 1 to the number of wins
- else:
- wins[message["from"]] += 1
- return wins
- def verify_time_string(time):
- # Check that the time string is made of 4 digits
- if not(time.isdigit() and len(time) ==4):
- 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]) < 24 and 0 <= int(time[2:]) < 60):
- print("Erreur : heure non valide", file=sys.stderr)
- return False
- else:
- return True
- def main():
- # Get the file name from the first command line argument
- filename = sys.argv[1]
- # Get the time chosen for the challenge (1337,1312, 2222, etc.)
- time = sys.argv[2]
- # Verify that the time string is a valid time of day
- if verify_time_string(time):
- hour = time[:2]
- minute = time[2:]
- 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)
-
- # 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()
|