Skip to content

Instantly share code, notes, and snippets.

@nikolak
Created October 13, 2013 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikolak/6960736 to your computer and use it in GitHub Desktop.
Save nikolak/6960736 to your computer and use it in GitHub Desktop.
import json
import urllib2
from random import randint
req = urllib2.Request("http://www.reddit.com/comments/1oa44z.json",None, {'User-Agent': "wub_wub's script",})
json_response=json.loads(urllib2.urlopen(req).read())
all_comments=json_response[1]["data"]["children"]
valid_comments={}
winners=0
given_games=[]
for comment in all_comments:
try:
number,g1,g2,g3=[line for line in comment["data"]["body"].replace("\n\n","\n").replace("*","").split("\n") if line.strip()!=""][:4]
valid_comments[number]={"author":comment["data"]["author"],"game1":g1.strip(),"game2":g2.strip(),"game3":g3.strip()}
except:
print "{} disqualified, invalid comment content".format(comment["data"]["author"])
while winners<20:
winner_number=randint(0,100)
winner_comment=valid_comments.get(str(winner_number),False)
if winner_comment:
for g in ["game1","game2","game3"]:
if winner_comment[g] not in given_games:
winners+=1;given_games.append(winner_comment[g])
valid_comments[winner_number]=None
print "{} - won {}".format(winner_comment["author"],winner_comment[g].encode('utf-8'))
break
else:
print "{} - {} already given".format(winner_comment["author"],winner_comment[g].encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment