Created
September 5, 2015 15:05
-
-
Save datafatmunger/18a1a2ea50b80dc37bb7 to your computer and use it in GitHub Desktop.
Shaney Reddit Bot
This file contains 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 cookielib | |
import urllib | |
import urllib2 | |
import re | |
import mechanize | |
import string | |
import sys | |
from bs4 import BeautifulSoup | |
from random import randint | |
import random | |
import json | |
url = 'https://www.reddit.com' | |
def post_comment(comments_url, form_id, thing_id, text): | |
print 'post_comment' | |
headers = { | |
'User-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0', | |
'Accept': '*/*', | |
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', | |
'Accept-Language': 'en-US,en;q=0.5', | |
'Connection': 'keep-alive', | |
'Referer': 'https://reddit.com/' | |
} | |
req = urllib2.Request(url + '/api/me.json', None, headers) | |
print req.get_method() | |
res = br.open(req) | |
objs = json.loads(res.read()) | |
modhash = objs['data']['modhash'] | |
print modhash | |
headers = { | |
'Content-Type': 'application/json; charset=UTF-8', | |
'User-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0', | |
'Accept': '*/*', | |
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', | |
'Accept-Language': 'en-US,en;q=0.5', | |
'Connection': 'keep-alive', | |
'Referer': comments_url.encode('ascii','ignore'), | |
'X-Modhash': modhash.encode('ascii','ignore') | |
} | |
#parameters = { | |
# 'api_type': 'json', | |
# 'text': text, | |
# 'thing_id': thing_id, | |
# 'uh': modhash | |
#} | |
#data = urllib.urlencode(parameters) | |
data = { 'api_type': 'json', 'text': text, 'thing_id': thing_id, 'uh': modhash, 'id': form_id } | |
data_str = json.dumps(data) | |
print data_str | |
req = urllib2.Request('https://www.reddit.com/api/comment', data_str, headers) | |
print req.headers | |
print req.get_method() | |
print br._ua_handlers['_cookies'].cookiejar | |
try: | |
res = br.open(req) | |
print res.geturl() | |
print res.info() # headers | |
print res.read() # body | |
except Exception, e: | |
print e.info() | |
print e.read() | |
def choice(words): | |
#assumes words is non-empty | |
random.seed | |
index = random.randint(0,len(words)-1) | |
return words[index] | |
def do_shaney(text): | |
output = '' | |
words = string.split(text) | |
end_sentence = [] | |
dict = {} | |
prev1 = '' | |
prev2 = '' | |
for word in words: | |
if prev1 != '' and prev2 != '': | |
key = (prev2, prev1) | |
if dict.has_key(key): | |
dict[key].append(word) | |
else: | |
dict[key] = [word] | |
if (prev1[-1:] == '.' or prev1[-1:] == '?' or prev1[-1:] == '!'): | |
end_sentence.append(key) | |
prev2 = prev1 | |
prev1 = word | |
if end_sentence == []: | |
print 'Sorry, there are no sentences in the text.' | |
return | |
key = () | |
count = 10 | |
while 1: | |
if dict.has_key(key): | |
word = choice(dict[key]) | |
#print word, | |
output += word + ' ' | |
key = (key[1], word) | |
if key in end_sentence: | |
count = count - 1 | |
key = choice(end_sentence) | |
if count <= 0: | |
break | |
else: | |
key = choice(end_sentence) | |
return output | |
def goto_comments(comments_url): | |
text = '' | |
res = br.open(comments_url) | |
soup = BeautifulSoup(res.read(), 'html.parser') | |
comments = soup.findAll('div', class_='usertext-body') | |
for comment in comments: | |
try: | |
comment_text = comment.find('p').contents[0] | |
text += ' ' + str(comment_text) | |
except Exception, e: | |
pass | |
comment = do_shaney(text) | |
print comment | |
path_elms = comments_url.split('/') | |
thing_id = path_elms[len(path_elms) - 3] | |
thing_id = 't3_' + thing_id | |
print thing_id | |
form_id = soup.find('form', class_='usertext cloneable')['id'] | |
print form_id | |
post_comment(comments_url, form_id, thing_id, comment) | |
def goto_all(): | |
all_url = url + '/r/all' | |
res = br.open(all_url) | |
soup = BeautifulSoup(res.read(), 'html.parser') | |
things = soup.findAll('div', class_='thing') | |
n = randint(0, len(things)-1) | |
thing = things[n] | |
link = thing.find('a', class_='comments')['href'] | |
print link | |
goto_comments(link) | |
def list_forms(): | |
for form in br.forms(): | |
try: | |
print form | |
except Exception, e: | |
pass | |
def do_login(): | |
#list_forms() | |
br.select_form(nr=4) | |
br['user'] = '<USERNAME>' | |
br['passwd'] = '<PASSWORD>' | |
res = br.submit() | |
print res.info() | |
goto_all() | |
br = mechanize.Browser() | |
br.addheaders = [ | |
('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0'), | |
('Accept', '*/*'), | |
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'), | |
('Accept-Language', 'en-US,en;q=0.5'), | |
('Connection', 'keep-alive') | |
] | |
# Browser options | |
br.set_handle_equiv(True) | |
br.set_handle_gzip(True) | |
br.set_handle_redirect(True) | |
br.set_handle_referer(True) | |
br.set_handle_robots(False) | |
# Want debugging messages? | |
br.set_debug_http(True) | |
br.set_debug_redirects(True) | |
br.set_debug_responses(True) | |
# Enable cookie support for urllib2 | |
cookiejar = cookielib.LWPCookieJar() | |
br.set_cookiejar(cookiejar) | |
res = br.open(url) | |
print br.title() | |
print res.geturl() | |
#print res.info() # headers | |
#print res.read() # body | |
do_login() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment