weboob-devel/modules/ehentai/browser.py
Laurent Bachelier e958c229e6 Move the "empty search for latest" to collections
There is now a "latest" collection (or "latest_nsfw").
The feature didn't look much used, since it didn't work on many
backends.
Using collections will make it easy to support other things
like most viewed, featured, etc.
As a bonus, I added tests for every backend with the feature.
2012-03-16 03:34:22 +01:00

111 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Roger Philibert
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword
from urllib import urlencode
from .pages import IndexPage, GalleryPage, ImagePage, HomePage, LoginPage
from .gallery import EHentaiImage
__all__ = ['EHentaiBrowser']
class EHentaiBrowser(BaseBrowser):
ENCODING = None
PAGES = {
r'http://[^/]+/': IndexPage,
r'http://[^/]+/\?.*': IndexPage,
r'http://[^/]+/g/.+': GalleryPage,
r'http://[^/]+/s/.*': ImagePage,
r'http://[^/]+/home\.php': HomePage,
r'http://e-hentai\.org/bounce_login\.php': LoginPage,
}
def __init__(self, domain, username, password, *args, **kwargs):
self.DOMAIN = domain
self.logged = False
BaseBrowser.__init__(self, parser=('lxmlsoup',), *args, **kwargs)
if password:
self.login(username, password)
def _gallery_url(self, gallery):
return 'http://%s/g/%s/' % (self.DOMAIN, gallery.id)
def _gallery_page(self, gallery, n):
return gallery.url + ('?p=%d' % n)
def search_gallery(self, pattern):
self.location(self.buildurl('/', f_search=pattern.encode('utf-8')))
assert self.is_on_page(IndexPage)
return self.page.iter_galleries()
def latest_gallery(self):
self.location('/')
assert self.is_on_page(IndexPage)
return self.page.iter_galleries()
def iter_gallery_images(self, gallery):
self.location(gallery.url)
assert self.is_on_page(GalleryPage)
i = 0
while True:
n = self.page._next_page_link()
for img in self.page.image_pages():
yield EHentaiImage(img)
if n is None:
break
i += 1
self.location(self._gallery_page(gallery, i))
assert self.is_on_page(GalleryPage)
def get_image_url(self, image):
self.location(image.id)
assert self.is_on_page(ImagePage)
return self.page.get_url()
def gallery_exists(self, gallery):
gallery.url = self._gallery_url(gallery)
self.location(gallery.url)
assert self.is_on_page(GalleryPage)
return self.page.gallery_exists(gallery)
def fill_gallery(self, gallery, fields):
gallery.url = self._gallery_url(gallery)
self.location(gallery.url)
assert self.is_on_page(GalleryPage)
self.page.fill_gallery(gallery)
def login(self, username, password):
assert isinstance(username, basestring)
assert isinstance(password, basestring)
data = {'ipb_login_username': username,
'ipb_login_password': password}
self.location('http://e-hentai.org/bounce_login.php', urlencode(data), no_login=True)
assert self.is_on_page(LoginPage)
if not self.page.is_logged():
raise BrowserIncorrectPassword()
# necessary in order to reach the fjords
self.home()