Skip to content

Instantly share code, notes, and snippets.

@leegao
Created February 16, 2012 03:35
Show Gist options
  • Save leegao/1841580 to your computer and use it in GitHub Desktop.
Save leegao/1841580 to your computer and use it in GitHub Desktop.
Ask Reddit bot
__author__ = 'Lee'
import urllib
import json
import time
import re
seen = {}
info_dict = {}
agent = urllib.URLopener()
def login(user, passwd):
u = agent.open('http://www.reddit.com/api/login', urllib.urlencode({'user':user,'passwd':passwd}));
cookies = u.info()['set-cookie']
cookie = re.search(r"(reddit_session=.+?);", cookies).group(1)
agent.addheader('Cookie', cookie)
def info(t3):
if t3 in info_dict:
return info_dict[t3]
url = "http://www.reddit.com/api/info.json?id=%s"%t3
data = json.loads(agent.open(url).read())
info_dict[t3] = data
return data
def api():
# returns 'worthwhile' comments
url = "http://www.reddit.com/r/AskReddit/comments/.json"
data = json.loads(agent.open(url).read())
data = filter(lambda e: e['data']['parent_id'].startswith('t3') and len(e['data']['body']) > 300 and e['data']['name'] not in seen, data['data']['children'])
for e in data:
seen[e['data']['name']] = True
# filter out those comments from threads whose time stamp is less than 5/6 hours or > 100 score
def info_filter(e):
data = info(e['data']['parent_id'])['data']['children'][0]
#print e['data']['parent_id'], info(e['data']['name'])['data']['children']
score = data['data']['score']
timestamp = data['data']['created_utc']
return score > 300 or time.time() < timestamp + 4*3600
return filter(info_filter, data)
def permalink(data):
link_id = data['data']['link_id'].replace("t3_","")
id = data['data']['id']
url = "http://www.reddit.com/r/AskReddit/comments/%s/a/%s"%(link_id, id)
print url
return url
uh = ""
def save(id, uh):
return json.loads(agent.open("http://www.reddit.com/api/save", urllib.urlencode({'id':id,'uh':uh})).read())
def post(uh, sr, title, url):
return json.loads(agent.open("http://www.reddit.com/api/submit", urllib.urlencode({'uh':uh,'kind':'link','title':title,'url':url,'r':sr,'sr':sr})).read())
def whoami():
data = json.loads(agent.open("http://www.reddit.com/api/me.json").read())
global uh
uh = data['data']['modhash']
login("username","password")
whoami()
import webbrowser
first = True
def save_this_post(data):
url = permalink(data)
print post(uh, 'lee_saved_comments', data['data']['link_title'], url)
def browse(url):
global first
if first:
webbrowser.open_new(url)
first = False
else:
webbrowser.open_new_tab(url)
n = 1
while True:
print n
n += 1
data_set = api()
for data in data_set:
save_this_post(data)
browse(permalink(data))
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment