/modules/ehentai/module.py

https://gitlab.com/phyks/weboob · Python · 117 lines · 81 code · 19 blank · 17 comment · 24 complexity · 08d4b8affc27ee1084369297ff14ea01 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. # Copyright(C) 2010-2011 Roger Philibert
  3. #
  4. # This file is part of weboob.
  5. #
  6. # weboob 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. # weboob 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 weboob. If not, see <http://www.gnu.org/licenses/>.
  18. import re
  19. from weboob.capabilities.gallery import CapGallery, BaseGallery
  20. from weboob.capabilities.collection import CapCollection, CollectionNotFound
  21. from weboob.tools.backend import Module, BackendConfig
  22. from weboob.tools.misc import ratelimit
  23. from weboob.tools.value import Value, ValueBackendPassword
  24. from .browser import EHentaiBrowser
  25. from .gallery import EHentaiGallery, EHentaiImage
  26. __all__ = ['EHentaiModule']
  27. class EHentaiModule(Module, CapGallery, CapCollection):
  28. NAME = 'ehentai'
  29. MAINTAINER = u'Roger Philibert'
  30. EMAIL = 'roger.philibert@gmail.com'
  31. VERSION = '1.3'
  32. DESCRIPTION = 'E-Hentai galleries'
  33. LICENSE = 'AGPLv3+'
  34. BROWSER = EHentaiBrowser
  35. CONFIG = BackendConfig(
  36. Value('domain', label='Domain', default='g.e-hentai.org'),
  37. Value('username', label='Username', default=''),
  38. ValueBackendPassword('password', label='Password'))
  39. def create_default_browser(self):
  40. username = self.config['username'].get()
  41. if username:
  42. password = self.config['password'].get()
  43. else:
  44. password = None
  45. return self.create_browser(self.config['domain'].get(), username, password)
  46. def search_galleries(self, pattern, sortby=None):
  47. with self.browser:
  48. return self.browser.search_galleries(pattern)
  49. def iter_gallery_images(self, gallery):
  50. self.fillobj(gallery, ('url',))
  51. with self.browser:
  52. return self.browser.iter_gallery_images(gallery)
  53. ID_REGEXP = r'/?\d+/[\dabcdef]+/?'
  54. URL_REGEXP = r'.+/g/(%s)' % ID_REGEXP
  55. def get_gallery(self, _id):
  56. match = re.match(r'^%s$' % self.URL_REGEXP, _id)
  57. if match:
  58. _id = match.group(1)
  59. else:
  60. match = re.match(r'^%s$' % self.ID_REGEXP, _id)
  61. if match:
  62. _id = match.group(0)
  63. else:
  64. return None
  65. gallery = EHentaiGallery(_id)
  66. with self.browser:
  67. if self.browser.gallery_exists(gallery):
  68. return gallery
  69. else:
  70. return None
  71. def fill_gallery(self, gallery, fields):
  72. if not gallery.__iscomplete__():
  73. with self.browser:
  74. self.browser.fill_gallery(gallery, fields)
  75. def fill_image(self, image, fields):
  76. with self.browser:
  77. image.url = self.browser.get_image_url(image)
  78. if 'data' in fields:
  79. ratelimit("ehentai_get", 2)
  80. image.data = self.browser.readurl(image.url)
  81. def iter_resources(self, objs, split_path):
  82. if BaseGallery in objs:
  83. collection = self.get_collection(objs, split_path)
  84. if collection.path_level == 0:
  85. yield self.get_collection(objs, [u'latest_nsfw'])
  86. if collection.split_path == [u'latest_nsfw']:
  87. for gallery in self.browser.latest_gallery():
  88. yield gallery
  89. def validate_collection(self, objs, collection):
  90. if collection.path_level == 0:
  91. return
  92. if BaseGallery in objs and collection.split_path == [u'latest_nsfw']:
  93. collection.title = u'Latest E-Hentai galleries (NSFW)'
  94. return
  95. raise CollectionNotFound(collection.split_path)
  96. OBJECTS = {
  97. EHentaiGallery: fill_gallery,
  98. EHentaiImage: fill_image}