Find file
Fetching contributors…
Cannot retrieve contributors at this time
121 lines (108 sloc) 5 KB
import feedparser
import json
import urllib
import urllib2
from HTMLParser import HTMLParser
from sets import Set
import requests
import time
##using metareddit, responds to reddit comments about commenting history with a link to their redditgraph
##reddit admins will shadowban accounts doing this
def urlOpener(url):
req = urllib2.Request(url, None, {'user-agent':'redditgraphs bot'})
opener = urllib2.build_opener()
f =
#takes rss url and returns comment dic
def commentReader(url):
url = url[:-10] + '.json'
content = urlOpener(url) commentData = json.loads(content)[1]['data']['children'][0]['data']
commentData['body'] = HTMLParser.unescape.__func__(HTMLParser, commentData['body'])
commentData['op'] = json.loads(content)[0]['data']['children'][0]['data']['author']
return commentData
#given comment data, finds the parent's user name
def parentUsername(commentData):
if commentData['parent_id'][0:2] == 't1':
print "comment is parent"
url = "" + commentData['subreddit'] + "/comments/" + commentData['link_id'][3:] + '/threadName/' + commentData['parent_id'][3:] +".json"
content = urlOpener(url)
return json.loads(content)[1].get('data').get('children')[0].get('data').get('author')
print "submission is parent"
url = '' + commentData['parent_id'] + ".json"
content = json.loads(urlOpener(url))
return content['data']['children'][0]['data']['author']
#takes comment string, returns sentance contain searched string and who it refers to
def findQuote(commentString, foundstr):
who = -1
quote = -1
sentences = commentString.replace('\n', ' ').split('. ')
for sentence in sentences:
index = sentence.find(foundstr)
if index != -1:
quote = sentence
if sentence[index-5:index-1].lower()== "OP's".lower():
who = "op"
if sentence[index-5:index-1].lower()== "your":
who = "your"
if sentence[index-3:index-1].lower()== "my":
who = "my"
return {'who': who, 'text': quote}
#returns reddit user name whose comment history will be linked
def findName(commentData, who, text):
if who == "op":
return commentData["op"]
if who == 'your':
return parentUsername(commentData)
if who == 'my':
return commentData["author"]
print "NO VALID WHO: " + text
return -1
#posts comment as a reply
def postComment(commentData, quoteText, name):
if (quoteText != -1) and (name != -1) and (name != '[deleted]'):
text = ">" + quoteText + "\n\n" + "" + name
print text
if not commentData['link_id'] in threadPosted:
data = {'thing_id': 't1_' + commentData['id'], 'text': text, 'uh': modhash, 'api_type': 'json'}
r ='', data=data)
print r.status_code
return data
#process rss url - download feed and try to post to linked comments
def proccessRSS(rssURL, foundStr, hours):
#download list of recent 'comment history' posts from rss
d = feedparser.parse(rssURL)
print "\n" + foundStr + "\n"
for entry in d.entries:
if time.mktime(time.gmtime()) - time.mktime(entry['published_parsed']) < 3600*hours:
surl =
print "\n"+surl
commentData = commentReader(surl)
quote = findQuote(commentData['body'], foundStr)
name = findName(commentData, quote["who"], quote["text"])
postComment(commentData, quote["text"], name)
except Exception:
#download set of thread ids previously posted in
threadPosted = Set()
oldComments = json.loads(urlOpener(''))
for comment in oldComments['data']['children']:
#login to reddit
client = requests.session(headers={'User-Agent': 'redditgraphs bot'})
data = {'user': 'redditgraphs', 'passwd': passwd, 'api_type': 'json'}
r ='', data=data)
modhash = r.json['json']['data']['modhash']
while True:
proccessRSS('', 'comments', 1)
proccessRSS('', 'comment history', 1)
proccessRSS('', 'post history', 1)
proccessRSS('', 'posting history', 1)
proccessRSS('', 'submission history', 1)
print "SLEEPING"