/modules/tumblr/module.py

https://github.com/laurentb/weboob · Python · 80 lines · 46 code · 17 blank · 17 comment · 9 complexity · 3527d6b261e0ae493ec0d9646fd27faa 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.browser.exceptions import ClientError, HTTPNotFound
  20. from weboob.capabilities.gallery import CapGallery, BaseGallery, BaseImage, Thumbnail
  21. from weboob.tools.backend import Module, BackendConfig
  22. from weboob.tools.compat import urlparse
  23. from weboob.tools.value import Value
  24. from .browser import TumblrBrowser
  25. __all__ = ['TumblrModule']
  26. class TumblrModule(Module, CapGallery):
  27. NAME = 'tumblr'
  28. DESCRIPTION = 'images in tumblr blogs'
  29. MAINTAINER = 'Vincent A'
  30. EMAIL = 'dev@indigo.re'
  31. LICENSE = 'AGPLv3+'
  32. VERSION = '2.1'
  33. CONFIG = BackendConfig(Value('url', label='URL of the tumblr', regexp='https?://.+'))
  34. BROWSER = TumblrBrowser
  35. def create_default_browser(self):
  36. return self.create_browser(self.url())
  37. def url(self):
  38. return self.config['url'].get()
  39. def get_gallery(self, _id):
  40. title, icon = self.browser.get_title_icon()
  41. if icon:
  42. icon = Thumbnail(icon)
  43. return BaseGallery(_id, title=title, url=self.url(), thumbnail=icon)
  44. def search_galleries(self, pattern, sortby=CapGallery.SEARCH_RELEVANCE):
  45. pattern = pattern.lower()
  46. url = self.url()
  47. if pattern in url or pattern in self.browser.get_title_icon()[0].lower():
  48. yield self.get_gallery(urlparse(url).netloc)
  49. def iter_gallery_images(self, gallery):
  50. for img in self.browser.iter_images(gallery):
  51. yield img
  52. def fill_img(self, img, fields):
  53. if 'data' in fields:
  54. try:
  55. img.data = self.browser.open(img.url).content
  56. except (ClientError, HTTPNotFound):
  57. img.data = b''
  58. if 'thumbnail' in fields and img.thumbnail:
  59. self.fill_img(img.thumbnail, ('data',))
  60. OBJECTS = {
  61. BaseImage: fill_img,
  62. BaseGallery: fill_img,
  63. }