Guest User

Untitled

a guest
Feb 5th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import praw
  2. import config
  3.  
  4. # Gets the correct link flair, None if untagged
  5. def get_flair_id(flair_name, flair_data):
  6. if flair_name is None:
  7. return next(x for x in flair_data
  8. if x['flair_text_editable'])['flair_template_id']
  9. for flair in flair_data:
  10. if flair['flair_css_class'] == (flair_name + 'Dev'):
  11. return flair['flair_template_id']
  12.  
  13. reddit = praw.Reddit(user_agent=config.user_agent,
  14. client_id=config.client_id,
  15. client_secret=config.client_secret,
  16. username=config.username,
  17. password=config.password)
  18.  
  19. # Bot running forever
  20. while True:
  21. # Post-already tagged
  22. with open("tagged.txt", "r") as f:
  23. posts_tagged = f.read()
  24. posts_tagged = posts_tagged.split("\n")
  25. posts_tagged = list(filter(None, posts_tagged))
  26.  
  27. # Goes to subbreddit
  28. robotopia = reddit.subreddit(config.subreddit)
  29.  
  30. # Checks new submissions
  31. for submission in robotopia.new():
  32. # Already tagged, ignore
  33. if submission.id in posts_tagged:
  34. continue
  35. old_flair = submission.link_flair_text
  36. choices = submission.flair.choices()
  37. # Searches in comments
  38. for comment in submission.comments:
  39. author = comment.author
  40. # Checks mod flair
  41. if comment.author_flair_css_class == config.mod_flair:
  42. print('Developer responded')
  43. template_id = get_flair_id(old_flair, choices)
  44. flair_text = old_flair + 'Dev' if old_flair is not None else 'untaggedDev'
  45. print(template_id)
  46. print(flair_text)
  47. # Changes the link flair
  48. submission.mod.flair(text=flair_text,css_class=comment.author_flair_css_class)
  49. # Saves the submission
  50. posts_tagged.append(submission.id)
  51. print('Link flair changed')
  52.  
  53. # Write our updated list back to the file
  54. with open("tagged.txt", "w") as f:
  55. for post_id in posts_tagged:
  56. f.write(post_id + "\n")
Add Comment
Please, Sign In to add comment