Advertisement
Guest User

Untitled

a guest
Nov 28th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import time
  2. import reddit
  3.  
  4. bills = []
  5.  
  6. messageRecipients = ['brown_paper_bag', 'Ben347']
  7.  
  8. user = "Reddica_Bot"
  9. password = removed :P
  10.  
  11. redditWrapper = reddit.Reddit(user_agent="Reddica Bot by /u/Ben347")
  12. redditWrapper.login(user, password)
  13.  
  14. subreddit = redditWrapper.get_subreddit("WorldofPolitics")
  15.  
  16. def log(text):
  17.     print "%s - %s" % (time.time(), text)
  18.  
  19. def isNew(post):
  20.     for timePosted, title, url in bills:
  21.         if title == post:
  22.             return False
  23.     return True
  24.  
  25. def checkForNewBills():
  26.     recentPosts = subreddit.get_new_by_date(limit=10)
  27.     for post in recentPosts:
  28.         if '[bill]' in post.title.lower():
  29.             if isNew(post.title):
  30.                 bills.append((time.time(), post.title, post.short_link))
  31.                 log("Found new bill: %s" % (post.title))
  32.    
  33.    
  34. def sendMessages():
  35.     oldBills = []
  36.    
  37.     for i in range(len(bills)):
  38.         timePosted, title, url = bills[i]
  39.         if time.time() - timePosted >= (60 * 60 * 48):
  40.             oldBills.append(i)
  41.             subject = 'Bill is ready for action'
  42.             message = 'Bill "%s" has been posted for 48 hours: %s.' % (title, url)
  43.             for recipient in messageRecipients:
  44.                 redditWrapper.compose_message(recipient, subject, message)
  45.                 log("Sent alert to user %s" % (recipient))
  46.                 time.sleep(2)
  47.            
  48.     oldBills = sorted(oldBills, reverse=True)
  49.            
  50.     for i in oldBills:
  51.         bills.pop(i)
  52.    
  53. while True:
  54.     time.sleep(10)
  55.    
  56.     checkForNewBills()
  57.    
  58.     sendMessages()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement