/modules/reddit/module.py

https://github.com/laurentb/weboob · Python · 125 lines · 82 code · 26 blank · 17 comment · 12 complexity · bd1085eabf87709c8884ffd8c320d2ee MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. # Copyright(C) 2017 Vincent A
  3. #
  4. # This file is part of a weboob module.
  5. #
  6. # This weboob module is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This weboob module is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
  18. from __future__ import unicode_literals
  19. from weboob.tools.backend import Module, BackendConfig
  20. from weboob.tools.value import Value
  21. from weboob.capabilities.image import CapImage, BaseImage, Thumbnail
  22. from weboob.capabilities.messages import CapMessages, Thread
  23. from weboob.capabilities.collection import CapCollection, Collection
  24. from .browser import RedditBrowser
  25. __all__ = ['RedditModule']
  26. def register_resources_handler(d, *path):
  27. def decorator(func):
  28. d[path] = func
  29. return func
  30. return decorator
  31. class RedditModule(Module, CapImage, CapCollection, CapMessages):
  32. NAME = 'reddit'
  33. DESCRIPTION = u'reddit website'
  34. MAINTAINER = u'Vincent A'
  35. EMAIL = 'dev@indigo.re'
  36. LICENSE = 'AGPLv3+'
  37. VERSION = '2.1'
  38. CONFIG = BackendConfig(
  39. Value('subreddit', label='Name of the sub-reddit', regexp='[^/]+', default='pics'),
  40. )
  41. BROWSER = RedditBrowser
  42. def create_default_browser(self):
  43. return self.create_browser(self.config['subreddit'].get())
  44. def get_file(self, _id):
  45. raise NotImplementedError()
  46. def get_image(self, id):
  47. return self.browser.get_image(id)
  48. def search_file(self, pattern, sortby=CapImage.SEARCH_RELEVANCE):
  49. return self.browser.search_images(pattern, sortby, True)
  50. def search_image(self, pattern, sortby=CapImage.SEARCH_RELEVANCE, nsfw=False):
  51. sorting = {
  52. CapImage.SEARCH_RELEVANCE: 'relevance',
  53. CapImage.SEARCH_RATING: 'top',
  54. CapImage.SEARCH_VIEWS: 'top', # not implemented
  55. CapImage.SEARCH_DATE: 'new',
  56. }
  57. sortby = sorting[sortby]
  58. return self.browser.search_images(pattern, sortby, nsfw)
  59. def iter_threads(self):
  60. return self.browser.iter_threads()
  61. def get_thread(self, id):
  62. return self.browser.get_thread(id)
  63. def iter_resources(self, objs, split_path):
  64. for k in self.RESOURCES:
  65. if len(k) == len(split_path) and all(a is None or a == b for a, b in zip(k, split_path)):
  66. f = self.RESOURCES[k]
  67. return f(self, objs, *split_path)
  68. RESOURCES = {}
  69. @register_resources_handler(RESOURCES)
  70. def iter_resources_root(self, objs):
  71. return [
  72. Collection(['hot'], 'Hot threads'),
  73. Collection(['new'], 'New threads'),
  74. Collection(['rising'], 'Rising threads'),
  75. Collection(['controversial'], 'Controversial threads'),
  76. Collection(['top'], 'Top threads'),
  77. ]
  78. @register_resources_handler(RESOURCES, None)
  79. def iter_resources_dir(self, objs, key):
  80. if key == 'hot':
  81. key = ''
  82. if Thread in objs:
  83. return self.iter_threads(cat=key)
  84. if BaseImage in objs:
  85. return self.browser.iter_images(cat=key)
  86. return []
  87. def fill_data(self, obj, fields):
  88. if 'thumbnail' in fields and not obj.thumbnail.data:
  89. obj.thumbnail.data = self.browser.open(obj.thumbnail.url).content
  90. if 'data' in fields:
  91. obj.data = self.browser.open(obj.url).content
  92. def fill_thread(self, obj, fields):
  93. if 'root' in fields:
  94. self.browser.fill_thread(obj)
  95. OBJECTS = {
  96. BaseImage: fill_data,
  97. Thumbnail: fill_data,
  98. Thread: fill_thread,
  99. }