Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # oaklandxposter.py
- # Oakland Cross Poster Bot
- # cross-posts threads from /r/oakland to /r/oaklanduncensored
- # created 2019-02-04 VoinaYMir
- # modified 2019-02-04 VoinaYMir
- import praw
- import time
- CLIENT_ID = 'your client id goes here'
- CLIENT_SECRET = 'your client secret goes here'
- USERNAME = 'your bot username goes here'
- PASSWORD = 'your bot password goes here'
- USER_AGENT = 'Mozilla/5.0' # I just use this but you can change to whatever
- LAST_XPOST_FILE = 'last-xpost.txt' # can change if you like
- # this file is used to save the last post id and timestamp that was crossposted
- # so the bot knows where it left off and doesn't crosspost everything all over
- # again
- SRC_SUB = 'oakland' # change to source subreddit you want to crosspost from
- TGT_SUB = 'oaklanduncensored' # change to target subreddit to crosspost to
- DEFAULT_LAST_ID = 'anampl'
- # change this to the id of the last post you want the crossposter to start from
- # otherwise it will post everything it finds (which could end up being a LOT)
- DEFAULT_LAST_TIME = 1549420519
- # change this to the unix timestamp of the last post you want the crossposter
- # to start from. otherwise it will post everything it finds
- def get_last_xpost():
- try:
- with open(LAST_XPOST_FILE, 'r') as f:
- data = f.readline().rstrip('\n')
- post_id, post_time = data.split(':')
- post_time = float(post_time)
- return post_id, post_time
- except:
- return None, None
- def save_last_xpost(post_id, post_time):
- with open(LAST_XPOST_FILE, 'w') as f:
- f.write(post_id + ':' + str(post_time))
- class CrossPoster:
- def __init__(self):
- self.reddit = praw.Reddit(client_id=CLIENT_ID,
- client_secret=CLIENT_SECRET,
- user_agent=USER_AGENT,
- username=USERNAME,
- password=PASSWORD)
- self.last_xpost = None
- self.last_xpost_time = None
- def get_new_posts(self):
- last_id, last_time = get_last_xpost()
- print('[+] getting new posts since: ' + str(last_time))
- if not last_id:
- last_id = DEFAULT_LAST_ID
- last_time = DEFAULT_LAST_TIME
- new_posts = []
- oakland_posts = self.reddit.subreddit(SRC_SUB).new()
- for post in oakland_posts:
- if not post.id == last_id and post.created_utc > last_time:
- new_posts.append(post)
- else:
- break
- return new_posts
- def crosspost(self, post):
- print('[+] crossposting thread from: ' + str(post.created_utc) + ' id: ' + post.id)
- try:
- post.crosspost(TGT_SUB)
- self.last_xpost = post.id
- self.last_time = post.created_utc
- except praw.exceptions.APIException as e:
- print(str(e))
- raise
- def crosspost_new_posts(self):
- new_posts = self.get_new_posts()
- new_posts.reverse()
- for post in new_posts:
- self.crosspost(post)
- if self.last_xpost:
- save_last_xpost(self.last_xpost, self.last_time)
- def main():
- xposter = CrossPoster()
- xposter.crosspost_new_posts()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement