Advertisement
Guest User

Lvbot2

a guest
Nov 24th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.16 KB | None | 0 0
  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
  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
  91.     replyTo = processCongratulate(user, number)
  92.     postNotice(username, password, random.choice(normalMessages) % (user, number), 'Bot Lounge with Welcome Pony', replyTo)
  93.     return None
  94.  
  95. #Adds a user to userDict with their post count.
  96. def newUserDict(user, replyTo):
  97.     global userDict
  98.     userLink = 'http://rainbowdash.net/' + str(user)
  99.     userDict[str(user)] = getNoticeCount(userLink)
  100.     print 'Added ' + str(user) + ' to userDict. Post count: ' + str(userDict[str(user)])
  101.     if(userDict[str(user)] % 1000 == 0):
  102.         normalCongratulate(user, userDict[str(user)], replyTo)
  103.     return None
  104.  
  105. #Increments a user's post count in userDict rather than checking their profile each time.
  106. def updateUserDict(user, replyTo):
  107.     global userDict
  108.     userDict[str(user)] += 1
  109.     if(userDict[str(user)] % 1000 == 0 and userDict[str(user)] != 0):
  110.         normalCongratulate(user, userDict[str(user)], replyTo)
  111.     print str(user) + ' posted. New count: ' + str(userDict[str(user)])
  112.     return None
  113.  
  114. #Gets newest post from the timeline and returns its ID.
  115. def newestNoticeID():
  116.     newNoticeID = 0
  117.     statusList = getStatuses()
  118.     for i in statusList:
  119.         if(i[0] > newNoticeID):
  120.             newNoticeID = i[0]
  121.     return newNoticeID
  122.  
  123. #Runs checks on each status it hasn't seen before.
  124. #Knows if it's seen a message before based on the message's ID.
  125. #Higher ID, newer post.
  126. def findNewStatus():
  127.     global currentNoticeID, userDict, username, password
  128.     newMessage = False
  129.     newNoticeID = currentNoticeID
  130.     statusList = getStatuses()
  131.     for i in statusList:
  132.         if(i[0] > currentNoticeID):
  133.             if(i[1] in userDict):
  134.                 updateUserDict(i[1], i[0])
  135.             elif(i[1] not in userDict):
  136.                 newUserDict(i[1], i[0])
  137.             newMessage = True
  138.             if(i[0] > newNoticeID):
  139.                 newNoticeID = i[0]
  140.         if(i[0] % 100000 == 0 and i[0] > currentNoticeID):
  141.             specialCongratulate(i[1], i[0])
  142.     currentNoticeID = newNoticeID
  143.     return newMessage
  144.  
  145.        
  146. #currentNoticeID represents the most recent notice it read,
  147. #userDict stores how many posts each user has.
  148. #Special and normal messages for when people reach a 100,000th RDN message,
  149. #or 1,000th personal message respectively.
  150. specialMessages = ['@%s Wow. That was RDN\'s %sth dash. That\'s a bunch.',
  151.                    '@%s You made RDN\'s %sth dash. Almost as many dashes as there are owls Ross has killed.']
  152. normalMessages = ['@%s Wow, you reached your %sth message. Guess you don\'t get out much.',
  153.                   '@%s Whoah now. %s posts? Isn\'t that how many raccoons and skunks @papyrus has killed?',
  154.                   '@%s Whoah. %s posts? That\'s about how many arguments RDN has. Each hour.']
  155.  
  156. currentNoticeID = newestNoticeID()
  157. print currentNoticeID
  158. userDict = {'username':'int'}
  159. username = str(raw_input('Bot username: '))
  160. password = str(raw_input('Bot password: '))
  161. #Say yes you monster.
  162. raw_input('Do you love bots?\n')
  163. wait = 6
  164. findNewStatus()
  165. while(1):
  166.     try:
  167.         newMessage = findNewStatus()
  168.         if(wait <= 18 and not newMessage):
  169.             wait += 2
  170.         elif(newMessage):
  171.             wait = 6
  172.         time.sleep(wait)
  173.     except Exception as e:
  174.         print 'The programmer must suck.'
  175.         print e
  176.         sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement