Created
June 29, 2014 19:00
-
-
Save schrobby/c53f65d16998e9cd7ab6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import csv | |
| import time | |
| def send_results(scoreboard): | |
| import praw | |
| message = """ | |
| Hi %s, | |
| thank you for participating in the CSAT edition of the [How Well Can You Name Female Idols](/29cc8h) quiz! | |
| You have achieved __a score of %d out of 20 possible points__. | |
| Don't be upset if you are not satisfied with the result - it was supposed to be a challenge even for most experienced | |
| of nugu-chasers. If you want to find out more about the results, have a look at __[this post](/29esmo)__ for the answer key and | |
| score board. | |
| _This message was generated and delivered automatically. If you didn't expect it, please ignore it. If you believe a mistake | |
| occured while generating your score, please contact me by replying to this message._""" | |
| r = praw.Reddit(user_agent="kpop-quiz.py results delivery by /u/schrobby") | |
| username = raw_input("Username: ") | |
| password = raw_input("Password: ") | |
| r.login(username, password) | |
| print("Logged in as " + username) | |
| for user, score in scoreboard.iteritems(): | |
| print("Sending message with a score of %d to %s" % (score, user)) | |
| try: | |
| r.send_message(user, "How Well Can You Name Female Idols? - Your result", message % (user, score)) | |
| except Exception, e: | |
| print("Couldn't send message to " + user) | |
| time.sleep(2) | |
| def get_results(key, data): | |
| answer_key = [] | |
| scoreboard = dict() | |
| guessed_correctly = None | |
| csv.register_dialect('google', delimiter=',', quotechar='"', skipinitialspace=True) | |
| print("Parsing answers from " + key) | |
| with open(key, 'rb') as f: | |
| reader = csv.reader(f, dialect='google') | |
| for row in reader: | |
| answer_key.append(row) | |
| guessed_correctly = [0] * len(answer_key) | |
| print("Found %d answers" % len(answer_key)) | |
| print("Parsing results from " + data) | |
| with open(data, 'rb') as f: | |
| reader = csv.reader(f, dialect='google') | |
| reader.next() | |
| for row in reader: | |
| scoreboard[row[1]] = 0 | |
| for i, (answers, guess) in enumerate(zip(answer_key, row[2:])): | |
| if sum([s.lower() in guess.lower() for s in answers]): | |
| scoreboard[row[1]] += 1 | |
| guessed_correctly[i] += 1 | |
| return (answer_key, scoreboard, guessed_correctly) | |
| def main(key, data): | |
| (answer_key, scoreboard, guessed_correctly) = get_results(key, data) | |
| print(answer_key) | |
| print(guessed_correctly) | |
| print("\n\nUser | Score\n:-|:-:") | |
| for (user, score) in sorted(scoreboard.items(), key=lambda x:x[1], reverse=True): | |
| print("%s | %d" % (user, score)) | |
| if raw_input("Send results to participants on reddit (y/n)? ") == 'y': | |
| send_results(scoreboard) | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 2: | |
| main(sys.argv[1], sys.argv[2]) | |
| else: | |
| print("Usage: %s <answer_key> <data_file>" % sys.argv[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment