__init__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. Reddit poster posts articles to reddit
  3. You can use the 'subreddit' attribute in you articles to specify which
  4. subbreddit the article should be post in aside of your default sub.
  5. """
  6. from collections import OrderedDict
  7. from pelican import signals
  8. from pelican.generators import Generator
  9. from functools import partial
  10. import logging
  11. import praw
  12. log = logging.getLogger(__name__)
  13. def cross_post(reddit, submission, subs):
  14. subreddits = [] if subs is None else subs.split(' ')
  15. log.debug("Posting in marked subs: %s", subreddits)
  16. for sub in subreddits:
  17. if sub == '':
  18. continue
  19. log.debug("Posting in %s" % sub)
  20. subreddit = reddit.subreddit(sub)
  21. subreddit.subscribe() # must be subscribed to crosspost
  22. submission.crosspost(subreddit)
  23. def make_posts(generator, metadata, url):
  24. """
  25. Make posts on reddit if it's not a draft, on whatever subs are specified
  26. """
  27. reddit = generator.get_reddit()
  28. if reddit is None:
  29. log.info("Reddit plugin not enabled")
  30. return
  31. if metadata.get('status') == "draft": # people don't want to post drafts
  32. log.debug("ignoring draft %s" % metadata['title'])
  33. return
  34. collection = generator.settings['REDDIT_POSTER_COLLECT_SUB']
  35. sub = reddit.subreddit(collection)
  36. results = sub.search(metadata['title'])
  37. if len([result for result in results]) > 0:
  38. log.debug("ignoring %s because it is already on sub %s " % (metadata['title'], collection))
  39. # post already was made to this sub
  40. return
  41. try:
  42. submission = sub.submit(metadata['title'], url=url, resubmit=False)
  43. cross_post(reddit, submission, metadata.get('subreddit'))
  44. except praw.exceptions.APIException as e:
  45. log.error("got an api exception: %s", e)
  46. except AssertionError as e:
  47. log.error("Received an assertion error %s", e)
  48. def init_reddit(generator):
  49. """
  50. this is a hack to make sure the reddit object keeps track of a session
  51. trough article scanning, speeding up networking as the connection can be
  52. kept alive.
  53. """
  54. auth_dict = generator.settings.get('REDDIT_POSTER_AUTH')
  55. if auth_dict is None:
  56. log.info("Could not find REDDIT_POSTER_AUTH key in settings, reddit plugin won't function")
  57. generator.get_reddit = lambda: None
  58. return
  59. reddit = praw.Reddit(**auth_dict)
  60. generator.get_reddit = lambda: reddit # .. openworld has it's merrits
  61. def content_written(generator, content):
  62. """
  63. create a url and call make posts (which has less information)
  64. """
  65. url = "%s/%s" % (generator.settings.get('SITEURL', 'http://localhost:8000'), content.url)
  66. make_posts(generator, content.metadata, url)
  67. def register():
  68. signals.article_generator_write_article.connect(content_written)
  69. signals.article_generator_init.connect(init_reddit)