Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # oaklandxposter.py
  4. # Oakland Cross Poster Bot
  5. # cross-posts threads from /r/oakland to /r/oaklanduncensored
  6.  
  7. # created 2019-02-04 VoinaYMir
  8. # modified 2019-02-04 VoinaYMir
  9.  
  10. import praw
  11. import time
  12.  
  13. CLIENT_ID = 'your client id goes here'
  14. CLIENT_SECRET = 'your client secret goes here'
  15. USERNAME = 'your bot username goes here'
  16. PASSWORD = 'your bot password goes here'
  17. USER_AGENT = 'Mozilla/5.0' # I just use this but you can change to whatever
  18. LAST_XPOST_FILE = 'last-xpost.txt' # can change if you like
  19. # this file is used to save the last post id and timestamp that was crossposted
  20. # so the bot knows where it left off and doesn't crosspost everything all over
  21. # again
  22. SRC_SUB = 'oakland' # change to source subreddit you want to crosspost from
  23. TGT_SUB = 'oaklanduncensored' # change to target subreddit to crosspost to
  24. DEFAULT_LAST_ID = 'anampl'
  25. # change this to the id of the last post you want the crossposter to start from
  26. # otherwise it will post everything it finds (which could end up being a LOT)
  27. DEFAULT_LAST_TIME = 1549420519
  28. # change this to the unix timestamp of the last post you want the crossposter
  29. # to start from. otherwise it will post everything it finds
  30.  
  31. def get_last_xpost():
  32.     try:
  33.         with open(LAST_XPOST_FILE, 'r') as f:
  34.             data = f.readline().rstrip('\n')
  35.             post_id, post_time = data.split(':')
  36.             post_time = float(post_time)
  37.             return post_id, post_time
  38.     except:
  39.         return None, None
  40.  
  41. def save_last_xpost(post_id, post_time):
  42.     with open(LAST_XPOST_FILE, 'w') as f:
  43.         f.write(post_id + ':' + str(post_time))
  44.  
  45. class CrossPoster:
  46.     def __init__(self):
  47.         self.reddit = praw.Reddit(client_id=CLIENT_ID,
  48.                                 client_secret=CLIENT_SECRET,
  49.                                 user_agent=USER_AGENT,
  50.                                 username=USERNAME,
  51.                                 password=PASSWORD)
  52.         self.last_xpost = None
  53.         self.last_xpost_time = None
  54.  
  55.     def get_new_posts(self):
  56.         last_id, last_time = get_last_xpost()
  57.         print('[+] getting new posts since: ' + str(last_time))
  58.         if not last_id:
  59.             last_id = DEFAULT_LAST_ID
  60.             last_time = DEFAULT_LAST_TIME
  61.  
  62.         new_posts = []
  63.         oakland_posts = self.reddit.subreddit(SRC_SUB).new()
  64.         for post in oakland_posts:
  65.             if not post.id == last_id and post.created_utc > last_time:
  66.                 new_posts.append(post)
  67.             else:
  68.                 break
  69.  
  70.         return new_posts
  71.  
  72.     def crosspost(self, post):
  73.         print('[+] crossposting thread from: ' + str(post.created_utc) + ' id: ' + post.id)
  74.         try:
  75.             post.crosspost(TGT_SUB)
  76.             self.last_xpost = post.id
  77.             self.last_time = post.created_utc
  78.         except praw.exceptions.APIException as e:
  79.             print(str(e))
  80.             raise
  81.  
  82.     def crosspost_new_posts(self):
  83.         new_posts = self.get_new_posts()
  84.         new_posts.reverse()
  85.         for post in new_posts:
  86.             self.crosspost(post)
  87.         if self.last_xpost:
  88.             save_last_xpost(self.last_xpost, self.last_time)
  89.  
  90.  
  91. def main():
  92.     xposter = CrossPoster()
  93.     xposter.crosspost_new_posts()
  94.  
  95. if __name__ == '__main__':
  96.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement