SHARE
TWEET

Lvbot2 The Second

a guest Nov 25th, 2015 25 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import urllib, urllib2, sys, pycurl, time, random
  2. from BeautifulSoup import BeautifulSoup
  3.  
  4. #Post a notice. Source for 'posted from', replyTo represents message ID you're replying to.
  5. #Leave replyTo blank if not a reply.
  6. def postNotice(username, password, message, source, replyTo):
  7.     try:
  8.         c = pycurl.Curl()
  9.         dataArray = {'status':message, 'source':source, 'in_reply_to_status_id':replyTo}
  10.         data = urllib.urlencode(dataArray)
  11.         c.setopt(pycurl.HTTPHEADER, ['User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.'])
  12.         c.setopt(pycurl.POST, True)
  13.         c.setopt(pycurl.WRITEFUNCTION, lambda x: None)
  14.         c.setopt(pycurl.USERPWD, str(username)+':'+str(password))
  15.         c.setopt(pycurl.URL, 'http://rainbowdash.net/api/statuses/update.xml')
  16.         c.setopt(pycurl.POSTFIELDS, data)
  17.         text = c.perform()
  18.         c.close()
  19.         return None
  20.     except Exception as e:
  21.         print e
  22.         sys.exit()
  23.        
  24. #Check how many posts a user has made.
  25. def getNoticeCount(userLink):
  26.     try:
  27.         request = urllib2.Request(userLink, headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.'})
  28.         response = urllib2.urlopen(request)
  29.         data = response.read()
  30.         soup = BeautifulSoup(data)
  31.         count = int(soup.findAll('dl', attrs={'class':'entity_notices'})[0].find('dd').text)
  32.         return count
  33.     except Exception as e:
  34.         print 'You screwed something up.'
  35.         print e
  36.         sys.exit()
  37.        
  38. #Gets HTML for the timeline RSS feed.
  39. def getTimeline():
  40.     try:
  41.         url = 'http://rainbowdash.net/api/statuses/public_timeline.rss'
  42.         request = urllib2.Request(url, headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.'})
  43.         response = urllib2.urlopen(request)
  44.         data = response.read()
  45.         return data
  46.     except Exception as e:
  47.         print 'Could not get timeline. Check site status or tell the programmer he\'s horrible.'
  48.         print 'Send him this, if you do:'
  49.         print e
  50.         sys.exit()
  51.  
  52. #Gets a list of messages, (noticeID, username) format, and returns it.
  53. def getStatuses():
  54.     statusList = []
  55.     soup = BeautifulSoup(getTimeline())
  56.     try:
  57.         for i in soup.findAll('item'):
  58.             username = str(i.find('title').text.split(':')[0])
  59.             noticeID = int(str(i.find('guid').text).split('/')[-1])
  60.             statusList.append((noticeID, username))
  61.     except Exception as e:
  62.         print 'Could not parse statuses. Tell the programmer he\'s horrible.'
  63.         print 'Send him this, too:'
  64.         print e
  65.         sys.exit()
  66.     return statusList
  67.  
  68. #Processes a congratulatory message by checking their page,
  69. #and getting the correct message to respond to.
  70. #Otherwise it may respond to the wrong message if someone posts multiple times in a few seconds.
  71. def processCongratulate(user, number):
  72.     url = 'http://rainbowdash.net/'+str(user)+'/rss'
  73.     request = urllib2.Request(url, headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.'})
  74.     response = urllib2.urlopen(request)
  75.     data = response.read()
  76.     soup = BeautifulSoup(data)
  77.     currentCount = getNoticeCount('http://rainbowdash.net/'+str(user))
  78.     targetNotice = (currentCount - number)
  79.     replyTo = int(str(soup.findAll('item')[targetNotice].attrs[0][1]).split('/')[-1])
  80.     return replyTo
  81.  
  82. #Congratulates people for every 100,000th message it sees on the timeline.
  83. def specialCongratulate(user, number):
  84.     global specialMessages, username, password
  85.     postNotice(username, password, random.choice(specialMessages) % (user, number), 'Bot Lounge with Welcome Pony', number)
  86.     return None
  87.  
  88. #Congratulates people for every 1,000th message they've posted.
  89. def normalCongratulate(user, number, replyTo):
  90.     global normalMessages, redMessages, username, password
  91.     replyTo = processCongratulate(user, number)
  92.     if(str(user) == 'redenchilada'):
  93.         postNotice(username, password, random.choice(redMessages) % (user, number), 'Bot Lounge with Welcome Pony', replyTo)
  94.     else:
  95.         postNotice(username, password, random.choice(normalMessages) % (user, number), 'Bot Lounge with Welcome Pony', replyTo)
  96.     return None
  97.  
  98. #Adds a user to userDict with their post count.
  99. def newUserDict(user, replyTo):
  100.     global userDict
  101.     userLink = 'http://rainbowdash.net/' + str(user)
  102.     userDict[str(user)] = getNoticeCount(userLink)
  103.     print 'Added ' + str(user) + ' to userDict. Post count: ' + str(userDict[str(user)])
  104.     if(userDict[str(user)] % 1000 == 0):
  105.         normalCongratulate(user, userDict[str(user)], replyTo)
  106.     elif(userDict[str(user)] == 1):
  107.         welcomeNewUser(str(user), replyTo)
  108.     return None
  109.  
  110. #Increments a user's post count in userDict rather than checking their profile each time.
  111. def updateUserDict(user, replyTo):
  112.     global userDict
  113.     userDict[str(user)] += 1
  114.     if(userDict[str(user)] % 1000 == 0 and userDict[str(user)] != 0):
  115.         normalCongratulate(user, userDict[str(user)], replyTo)
  116.     print str(user) + ' posted. New count: ' + str(userDict[str(user)])
  117.     return None
  118.  
  119. #Gets newest post from the timeline and returns its ID.
  120. def newestNoticeID():
  121.     newNoticeID = 0
  122.     statusList = getStatuses()
  123.     for i in statusList:
  124.         if(i[0] > newNoticeID):
  125.             newNoticeID = i[0]
  126.     return newNoticeID
  127.  
  128. #Runs checks on each status it hasn't seen before.
  129. #Knows if it's seen a message before based on the message's ID.
  130. #Higher ID, newer post.
  131. def findNewStatus():
  132.     global currentNoticeID, userDict, username, password
  133.     newMessage = False
  134.     newNoticeID = currentNoticeID
  135.     statusList = getStatuses()
  136.     for i in statusList:
  137.         if(i[0] > currentNoticeID):
  138.             if(i[1] in userDict):
  139.                 updateUserDict(i[1], i[0])
  140.             elif(i[1] not in userDict):
  141.                 newUserDict(i[1], i[0])
  142.             newMessage = True
  143.             if(i[0] > newNoticeID):
  144.                 newNoticeID = i[0]
  145.         if(i[0] % 100000 == 0 and i[0] > currentNoticeID):
  146.             specialCongratulate(i[1], i[0])
  147.     currentNoticeID = newNoticeID
  148.     return newMessage
  149.  
  150. #Welcomes new users to be better than WelcomePony
  151. def welcomeNewUser(user, replyTo):
  152.     global welcomeMessages, username, password
  153.     postNotice(username, password, random.choice(welcomeMessages) % user, 'Bot Lounge with Welcome Pony', replyTo)
  154.  
  155. #currentNoticeID represents the most recent notice it read,
  156. #userDict stores how many posts each user has.
  157. #Special and normal messages for when people reach a 100,000th RDN message,
  158. #or 1,000th personal message respectively.
  159. welcomeMessages = ['@%s Welcome to RDN! I\'m a better bot than @WelcomePony, but don\'t tell anyone or she might break up with me.',
  160.                    '@%s Welcome! You know, to this website. It\'s pretty inactive, and has very much drama, but it is a good site. I hope you enjoy your stay, cutie booty. Also I\'m better than @WelcomePony.']
  161. specialMessages = ['@%s Wow. That was RDN\'s %sth dash. That\'s a bunch.',
  162.                    '@%s You made RDN\'s %sth dash. Almost as many dashes as there are owls Ross has killed.']
  163. normalMessages = ['@%s Wow, you reached your %sth message. Guess you don\'t get out much.',
  164.                   '@%s Whoah now. %s posts? Isn\'t that how many raccoons and skunks @papyrus has killed?',
  165.                   '@%s Whoah. %s posts? That\'s about how many arguments RDN has. Each hour.',
  166.                   '@%s Nice job. You reached your %sth post. Great. Even if you\'re just compensating.',
  167.                   '@%s Astounding. %s posts. That\'s maybe a hundredth of how many times Red cried when he played To The Moon.']
  168. redMessages = ['@%s OMG, Red-senpai, you just reached your %sth post. So many messages of delicious goodness from you.',
  169.                '@%s You know what, Red? Take my %s \'I love you\'s, because that\'s how many posts you\'ve made.',
  170.                '@%s Red, hey, hey Red, you just hit %s posts. That\'s about how many times I\'ve smiled because of you.']
  171. currentNoticeID = newestNoticeID()
  172. userDict = {'username':'int'}
  173. username = str(raw_input('Bot username: '))
  174. password = str(raw_input('Bot password: '))
  175. #Say yes you monster.
  176. raw_input('Do you love bots?\n')
  177. wait = 6
  178. findNewStatus()
  179. while(1):
  180.     try:
  181.         newMessage = findNewStatus()
  182.         if(wait <= 38 and not newMessage):
  183.             wait += 2
  184.         elif(newMessage):
  185.             wait = 6
  186.         time.sleep(wait)
  187.     except Exception as e:
  188.         print 'The programmer must suck.'
  189.         print e
  190.         sys.exit()
RAW Paste Data
Top