Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import praw
- CONFIG_INTERVAL = 30 # Number of seconds between updating
- CONFIG_CLIENT_ID = "" # Client ID from /prefs/apps
- CONFIG_CLIENT_SECRET = "" # Client secret from /prefs/apps
- CONFIG_USERNAME = "" # Username for the bot account
- CONFIG_PASSWORD = "" # Password for the bot account
- CONFIG_SUBREDDIT = "" # Subreddit(s) the script runs on
- CONFIG_SCRIPTHOST = "" # Your Reddit username
- CONFIG_USER_AGENT = "postsubscribe 1.0 for /r/%s, hosted by /u/%s" \
- %(CONFIG_SUBREDDIT, CONFIG_SCRIPTHOST)
- CONFIG_SUB_SUBJECT = 'Subscribe me!' # Subject users put to subscribe
- CONFIG_SUB_NOTIFY = 'Subscribed!' # Subject use to confirm subscription
- CONFIG_SUB_MSG = 'You are now subscribed to new posts.'
- CONFIG_UNSUB_SUBJECT = 'Unsubscribe me!' # Subject users put to unsubscribe
- CONFIG_UNSUB_NOTIFY = 'Unsubscribed!' # Subject use to confirm unsubscription
- CONFIG_UNSUB_MSG = 'You are now unsubscribed to new posts.'
- CONFIG_NOTIFY_SUB = 'New post from /u/%s' # Subject of notification.
- CONFIG_NOTIFY_MSG = "Here's my latest post: [%s](%s)" # Body of notification
- CONFIG_USERS_FILE = 'users.txt' # file to read usernames from
- CONFIG_LASTPOST_FILE = 'last_post.txt' # file to read last post ID from
- # For CONFIG_NOTIFY_SUB, %s should exist once, where you would like your
- # username to show in the subject line.
- # For CONFIG_NOTIFY_MSG, the first %s is the title of your post, and the
- # second is the direct link. I recommend leaving it as [%s](%s) to create
- # a proper markdown link.
- def log(msg):
- print('[%s] %s'%(time.ctime(), msg))
- def poll_messages(reddit, users):
- new_users = set()
- new_users.update(users)
- unread = []
- for item in reddit.inbox.unread(limit=None):
- if isinstance(item, praw.models.Message):
- unread.append(item)
- log('Found %d unread message(s).'%len(unread))
- reddit.inbox.mark_read(unread)
- for msg in unread:
- user = msg.author
- usern = user.name
- if msg.subject == CONFIG_SUB_SUBJECT and not usern in new_users:
- new_users.add(usern)
- user.message(CONFIG_SUB_NOTIFY, CONFIG_SUB_MSG)
- log('User /u/%s has subscribed!'%usern)
- elif msg.subject == CONFIG_UNSUB_SUBJECT and usern in new_users:
- new_users.discard(usern)
- user.message(CONFIG_UNSUB_NOTIFY, CONFIG_UNSUB_MSG)
- log('User /u/%s has unsubscribed.'%usern)
- return new_users
- def send_to_subscribed(reddit, users, post):
- msg = CONFIG_NOTIFY_MSG%(post.title, post.url)
- sub = CONFIG_NOTIFY_SUB%(CONFIG_SCRIPTHOST)
- for u in users:
- reddit.redditor(u).message(sub, msg)
- log('Notified /u/%s of new post.'%u)
- def most_recent_post(reddit, subr):
- sub = reddit.subreddit(subr)
- return list(sub.new(limit=1))[0]
- def read(path):
- data = ''
- try:
- with open(path, 'r') as f:
- data = f.read().strip()
- except: pass
- return data
- def write(path, data):
- try:
- with open(path, 'w') as f:
- f.write(data)
- except: pass
- def read_subscribed(path):
- users = set()
- for u in read(path).splitlines():
- users.add(u)
- return users
- def read_last_post(path):
- return read(path)
- def write_subscribed(users):
- write(CONFIG_USERS_FILE, '\n'.join(users))
- def write_last_post(postid):
- write(CONFIG_LASTPOST_FILE, postid)
- def main():
- log('Reading subscribed users...')
- users = read_subscribed('users.txt')
- log('Reading last post...')
- last_post = read_last_post('last_post.txt')
- log('Authenticating with Reddit...')
- reddit = praw.Reddit(client_id = CONFIG_CLIENT_ID,
- client_secret = CONFIG_CLIENT_SECRET,
- username = CONFIG_USERNAME,
- password = CONFIG_PASSWORD,
- user_agent = CONFIG_USER_AGENT)
- while True:
- log('Polling messages...')
- new_users = poll_messages(reddit, users)
- if users != new_users:
- log('Subscribed users changed, re-writing file...')
- write_subscribed(new_users)
- users = new_users
- log('Getting last post...')
- recent = most_recent_post(reddit, CONFIG_SUBREDDIT)
- if recent.name != last_post:
- log('A new post was found! Notifying users...')
- send_to_subscribed(reddit, users, recent)
- log('Re-writing post file...')
- write_last_post(recent.name)
- last_post = recent.name
- else:
- log('No new posts.')
- log('Sleeping for %d seconds...'%CONFIG_INTERVAL)
- time.sleep(CONFIG_INTERVAL)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement