Guest User

Untitled

a guest
Mar 8th, 2014
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. import praw
  2. import time
  3. import urllib
  4. import urllib2
  5. import random
  6.  
  7. botText = ""
  8.  
  9. def cleanResponse(responseText):
  10.     cleaned = responseText.replace("<em>","*")
  11.     cleaned = cleaned.replace("</em>","*")
  12.     cleaned = cleaned.replace("<wbr>","")
  13.     cleaned = cleaned.replace("<b>....</b>"," ")
  14.     cleaned = cleaned.replace("<b>.....</b>"," ")
  15.     cleaned = cleaned.replace("<b>......</b>"," ")
  16.     cleaned = cleaned.replace("<b>.......</b>"," ")
  17.     cleaned = cleaned.replace("<b>........</b>"," ")
  18.     cleaned = cleaned.replace("<b>.........</b>"," ")
  19.     return cleaned
  20.  
  21. def cleanComment(comment):
  22.     cleaned = comment.replace('"',' ')
  23.     cleaned = cleaned.replace('-',' ')
  24.     cleaned = cleaned.replace('+',' ')
  25.     return cleaned
  26.  
  27. def getResponse(text):
  28.     url1 = "https://www.google.com/search?q="
  29.     encodedData = urllib.quote_plus(text)
  30.     url2 = url1 + encodedData
  31.     url2 = url2 + "&tbm=dsc"
  32.     ##print url2
  33.     url = url2
  34.     opener = urllib2.build_opener()
  35.     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')]
  36.     response = opener.open(url)
  37.     the_page = response.read()
  38.     anyResults = the_page.find("Try different keywords")
  39.     if(anyResults == -1):
  40.         startPosition = the_page.find('<span class="st">')
  41.         endPosition = the_page.find("</span>",startPosition + 17)
  42.         result = the_page[startPosition+17:endPosition]
  43.         ##print result
  44.         checkSpace = len(result)-2
  45.         if(result[checkSpace] == "-"):
  46.             ##print "Search Result is padded with a date...Updating text location"
  47.             startPosition = endPosition+7
  48.             endPosition = the_page.find("</span>",startPosition)
  49.             result = the_page[startPosition:endPosition]
  50.             ##print result
  51.         result = cleanResponse(result)
  52.         responseArray = result.split("<b>...</b>")
  53.         print len(responseArray)
  54.         ##for x in range(0, len(responseArray)):
  55.             ##print responseArray[x]
  56.         if(len(responseArray) == 1):
  57.             print "RESPONSE: " + responseArray[0]
  58.             s = raw_input('a/s/d-? ')
  59.             if(s == "a"):
  60.                 return responseArray[0]
  61.             else:
  62.                 return -1
  63.         else:
  64.             for x in range(0, len(responseArray)):
  65.                 print "RESPONSE: " + responseArray[x]
  66.                 s = raw_input('a/s/d-? ')
  67.                 if(s == "a"):
  68.                     return responseArray[x]
  69.                 if(s == "d"):
  70.                     return -1
  71.     else:
  72.         print "No Valid Responses"
  73.         return -1
  74.  
  75. print "Logging in..."
  76. r = praw.Reddit('Drunken IBM Watson AI')
  77. r.login('USERNAME', 'PASSWORD')
  78. already_done = set()
  79. while True:
  80.     subreddit = r.get_subreddit('all')
  81.     subreddit_comments = subreddit.get_comments()
  82.     for comment in subreddit_comments:
  83.         print "Scanning Comment..."
  84.             if len(comment.body) > 25 and comment.id not in already_done:
  85.             print "COMMENT: " + comment.body
  86.             commentResponse = getResponse(cleanComment(comment.body))
  87.             if(commentResponse != -1):
  88.                 print "Comment Sleeping..."
  89.                 print "Commenting..."
  90.                 try:
  91.                     comment.reply(commentResponse + botText)
  92.                         already_done.add(comment.id)
  93.                 except:
  94.                     print "Error in Commenting...Sleeping"
  95.                     time.sleep(60)
  96.         print "Sleeping..."
  97.         time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment