__init__.py 2.8 KB

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