Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. import time
  2. import praw
  3.  
  4. CONFIG_INTERVAL = 30 # Number of seconds between updating
  5. CONFIG_CLIENT_ID = "" # Client ID from /prefs/apps
  6. CONFIG_CLIENT_SECRET = "" # Client secret from /prefs/apps
  7. CONFIG_USERNAME = "" # Username for the bot account
  8. CONFIG_PASSWORD = "" # Password for the bot account
  9. CONFIG_SUBREDDIT = "" # Subreddit(s) the script runs on
  10. CONFIG_SCRIPTHOST = "" # Your Reddit username
  11. CONFIG_USER_AGENT = "postsubscribe 1.0 for /r/%s, hosted by /u/%s" \
  12. %(CONFIG_SUBREDDIT, CONFIG_SCRIPTHOST)
  13. CONFIG_SUB_SUBJECT = 'Subscribe me!' # Subject users put to subscribe
  14. CONFIG_SUB_NOTIFY = 'Subscribed!' # Subject use to confirm subscription
  15. CONFIG_SUB_MSG = 'You are now subscribed to new posts.'
  16. CONFIG_UNSUB_SUBJECT = 'Unsubscribe me!' # Subject users put to unsubscribe
  17. CONFIG_UNSUB_NOTIFY = 'Unsubscribed!' # Subject use to confirm unsubscription
  18. CONFIG_UNSUB_MSG = 'You are now unsubscribed to new posts.'
  19. CONFIG_NOTIFY_SUB = 'New post from /u/%s' # Subject of notification.
  20. CONFIG_NOTIFY_MSG = "Here's my latest post: [%s](%s)" # Body of notification
  21. CONFIG_USERS_FILE = 'users.txt' # file to read usernames from
  22. CONFIG_LASTPOST_FILE = 'last_post.txt' # file to read last post ID from
  23.  
  24. # For CONFIG_NOTIFY_SUB, %s should exist once, where you would like your
  25. # username to show in the subject line.
  26. # For CONFIG_NOTIFY_MSG, the first %s is the title of your post, and the
  27. # second is the direct link. I recommend leaving it as [%s](%s) to create
  28. # a proper markdown link.
  29.  
  30. def log(msg):
  31. print('[%s] %s'%(time.ctime(), msg))
  32.  
  33. def poll_messages(reddit, users):
  34. new_users = set()
  35. new_users.update(users)
  36. unread = []
  37. for item in reddit.inbox.unread(limit=None):
  38. if isinstance(item, praw.models.Message):
  39. unread.append(item)
  40. log('Found %d unread message(s).'%len(unread))
  41. reddit.inbox.mark_read(unread)
  42. for msg in unread:
  43. user = msg.author
  44. usern = user.name
  45. if msg.subject == CONFIG_SUB_SUBJECT and not usern in new_users:
  46. new_users.add(usern)
  47. user.message(CONFIG_SUB_NOTIFY, CONFIG_SUB_MSG)
  48. log('User /u/%s has subscribed!'%usern)
  49. elif msg.subject == CONFIG_UNSUB_SUBJECT and usern in new_users:
  50. new_users.discard(usern)
  51. user.message(CONFIG_UNSUB_NOTIFY, CONFIG_UNSUB_MSG)
  52. log('User /u/%s has unsubscribed.'%usern)
  53. return new_users
  54.  
  55. def send_to_subscribed(reddit, users, post):
  56. msg = CONFIG_NOTIFY_MSG%(post.title, post.url)
  57. sub = CONFIG_NOTIFY_SUB%(CONFIG_SCRIPTHOST)
  58. for u in users:
  59. reddit.redditor(u).message(sub, msg)
  60. log('Notified /u/%s of new post.'%u)
  61.  
  62. def most_recent_post(reddit, subr):
  63. sub = reddit.subreddit(subr)
  64. return list(sub.new(limit=1))[0]
  65.  
  66. def read(path):
  67. data = ''
  68. try:
  69. with open(path, 'r') as f:
  70. data = f.read().strip()
  71. except: pass
  72. return data
  73.  
  74. def write(path, data):
  75. try:
  76. with open(path, 'w') as f:
  77. f.write(data)
  78. except: pass
  79.  
  80. def read_subscribed(path):
  81. users = set()
  82. for u in read(path).splitlines():
  83. users.add(u)
  84. return users
  85.  
  86. def read_last_post(path):
  87. return read(path)
  88.  
  89. def write_subscribed(users):
  90. write(CONFIG_USERS_FILE, '\n'.join(users))
  91.  
  92. def write_last_post(postid):
  93. write(CONFIG_LASTPOST_FILE, postid)
  94.  
  95. def main():
  96. log('Reading subscribed users...')
  97. users = read_subscribed('users.txt')
  98. log('Reading last post...')
  99. last_post = read_last_post('last_post.txt')
  100. log('Authenticating with Reddit...')
  101. reddit = praw.Reddit(client_id = CONFIG_CLIENT_ID,
  102. client_secret = CONFIG_CLIENT_SECRET,
  103. username = CONFIG_USERNAME,
  104. password = CONFIG_PASSWORD,
  105. user_agent = CONFIG_USER_AGENT)
  106. while True:
  107. log('Polling messages...')
  108. new_users = poll_messages(reddit, users)
  109. if users != new_users:
  110. log('Subscribed users changed, re-writing file...')
  111. write_subscribed(new_users)
  112. users = new_users
  113. log('Getting last post...')
  114. recent = most_recent_post(reddit, CONFIG_SUBREDDIT)
  115. if recent.name != last_post:
  116. log('A new post was found! Notifying users...')
  117. send_to_subscribed(reddit, users, recent)
  118. log('Re-writing post file...')
  119. write_last_post(recent.name)
  120. last_post = recent.name
  121. else:
  122. log('No new posts.')
  123. log('Sleeping for %d seconds...'%CONFIG_INTERVAL)
  124. time.sleep(CONFIG_INTERVAL)
  125.  
  126. if __name__ == '__main__':
  127. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement