Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import time
- import urllib
- import urllib2
- import random
- botText = ""
- def cleanResponse(responseText):
- cleaned = responseText.replace("<em>","*")
- cleaned = cleaned.replace("</em>","*")
- cleaned = cleaned.replace("<wbr>","")
- cleaned = cleaned.replace("<b>....</b>"," ")
- cleaned = cleaned.replace("<b>.....</b>"," ")
- cleaned = cleaned.replace("<b>......</b>"," ")
- cleaned = cleaned.replace("<b>.......</b>"," ")
- cleaned = cleaned.replace("<b>........</b>"," ")
- cleaned = cleaned.replace("<b>.........</b>"," ")
- return cleaned
- def cleanComment(comment):
- cleaned = comment.replace('"',' ')
- cleaned = cleaned.replace('-',' ')
- cleaned = cleaned.replace('+',' ')
- return cleaned
- def getResponse(text):
- url1 = "https://www.google.com/search?q="
- encodedData = urllib.quote_plus(text)
- url2 = url1 + encodedData
- url2 = url2 + "&tbm=dsc"
- ##print url2
- url = url2
- opener = urllib2.build_opener()
- opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 Chrome/25.0.1364.160 Safari/537.22')]
- response = opener.open(url)
- the_page = response.read()
- anyResults = the_page.find("Try different keywords")
- if(anyResults == -1):
- startPosition = the_page.find('<span class="st">')
- endPosition = the_page.find("</span>",startPosition + 17)
- result = the_page[startPosition+17:endPosition]
- ##print result
- checkSpace = len(result)-2
- if(result[checkSpace] == "-"):
- ##print "Search Result is padded with a date...Updating text location"
- startPosition = endPosition+7
- endPosition = the_page.find("</span>",startPosition)
- result = the_page[startPosition:endPosition]
- ##print result
- result = cleanResponse(result)
- responseArray = result.split("<b>...</b>")
- print len(responseArray)
- ##for x in range(0, len(responseArray)):
- ##print responseArray[x]
- if(len(responseArray) == 1):
- print "RESPONSE: " + responseArray[0]
- s = raw_input('a/s/d-? ')
- if(s == "a"):
- return responseArray[0]
- else:
- return -1
- else:
- for x in range(0, len(responseArray)):
- print "RESPONSE: " + responseArray[x]
- s = raw_input('a/s/d-? ')
- if(s == "a"):
- return responseArray[x]
- if(s == "d"):
- return -1
- else:
- print "No Valid Responses"
- return -1
- print "Logging in..."
- r = praw.Reddit('Drunken IBM Watson AI')
- r.login('USERNAME', 'PASSWORD')
- already_done = set()
- while True:
- subreddit = r.get_subreddit('all')
- subreddit_comments = subreddit.get_comments()
- for comment in subreddit_comments:
- print "Scanning Comment..."
- if len(comment.body) > 25 and comment.id not in already_done:
- print "COMMENT: " + comment.body
- commentResponse = getResponse(cleanComment(comment.body))
- if(commentResponse != -1):
- print "Comment Sleeping..."
- print "Commenting..."
- try:
- comment.reply(commentResponse + botText)
- already_done.add(comment.id)
- except:
- print "Error in Commenting...Sleeping"
- time.sleep(60)
- print "Sleeping..."
- time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment