use the new URL.id2url decorator

This commit is contained in:
Romain Bignon 2014-03-13 21:21:47 +01:00
commit 2db4646779
6 changed files with 22 additions and 57 deletions

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Roger Philibert # Copyright(C) 2010-2014 Roger Philibert
# #
# This file is part of weboob. # This file is part of weboob.
# #
@ -25,7 +25,6 @@ from weboob.capabilities.collection import ICapCollection, CollectionNotFound
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from .browser import YoujizzBrowser from .browser import YoujizzBrowser
from .video import YoujizzVideo
__all__ = ['YoujizzBackend'] __all__ = ['YoujizzBackend']
@ -52,11 +51,9 @@ class YoujizzBackend(BaseBackend, ICapVideo, ICapCollection):
def fill_video(self, video, fields): def fill_video(self, video, fields):
if fields != ['thumbnail']: if fields != ['thumbnail']:
# if we don't want only the thumbnail, we probably want also every fields # if we don't want only the thumbnail, we probably want also every fields
with self.browser: video = self.browser.get_video(video.id, video)
video = self.browser.get_video(YoujizzVideo.id2url(video.id), video)
if 'thumbnail' in fields and video.thumbnail: if 'thumbnail' in fields and video.thumbnail:
with self.browser: video.thumbnail.data = self.browser.readurl(video.thumbnail.url)
video.thumbnail.data = self.browser.readurl(video.thumbnail.url)
return video return video
@ -77,4 +74,4 @@ class YoujizzBackend(BaseBackend, ICapVideo, ICapCollection):
return return
raise CollectionNotFound(collection.split_path) raise CollectionNotFound(collection.split_path)
OBJECTS = {YoujizzVideo: fill_video} OBJECTS = {BaseVideo: fill_video}

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Roger Philibert # Copyright(C) 2010-2014 Roger Philibert
# #
# This file is part of weboob. # This file is part of weboob.
# #
@ -19,11 +19,9 @@
from weboob.tools.browser2 import PagesBrowser, URL from weboob.tools.browser2 import PagesBrowser, URL
from weboob.tools.browser.decorators import id2url
from .pages.index import IndexPage from .pages.index import IndexPage
from .pages.video import VideoPage from .pages.video import VideoPage
from .video import YoujizzVideo
__all__ = ['YoujizzBrowser'] __all__ = ['YoujizzBrowser']
@ -32,11 +30,13 @@ __all__ = ['YoujizzBrowser']
class YoujizzBrowser(PagesBrowser): class YoujizzBrowser(PagesBrowser):
BASEURL = 'http://www.youjizz.com' BASEURL = 'http://www.youjizz.com'
index = URL(r'/?(index.php)?$', IndexPage) index = URL(r'/?(index.php)?$',
r'/page/\d+.html',
IndexPage)
search = URL(r'/search/(?P<pattern>.+)-(?P<pagenum>\d+).html', IndexPage) search = URL(r'/search/(?P<pattern>.+)-(?P<pagenum>\d+).html', IndexPage)
video = URL(r'/videos/(?P<id>.*).html', VideoPage) video = URL(r'/videos/(?P<id>.*).html', VideoPage)
@id2url(YoujizzVideo.id2url) @video.id2url
def get_video(self, url, video=None): def get_video(self, url, video=None):
self.location(url) self.location(url)
assert self.video.is_here() assert self.video.is_here()

View file

@ -25,8 +25,7 @@ from weboob.tools.browser2 import HTMLPage
from weboob.tools.browser2.page import ListElement, method, ItemElement from weboob.tools.browser2.page import ListElement, method, ItemElement
from weboob.tools.browser2.filters import Filter, Link, CleanText from weboob.tools.browser2.filters import Filter, Link, CleanText
from weboob.capabilities.image import BaseImage from weboob.capabilities.image import BaseImage
from weboob.capabilities.video import BaseVideo
from ..video import YoujizzVideo
__all__ = ['IndexPage'] __all__ = ['IndexPage']
@ -40,7 +39,7 @@ class IndexPage(HTMLPage):
next_page = Link(u'//a[text()="Next »"]') next_page = Link(u'//a[text()="Next »"]')
class item(ItemElement): class item(ItemElement):
klass = YoujizzVideo klass = BaseVideo
class Id(Filter): class Id(Filter):
def filter(self, link): def filter(self, link):
@ -66,6 +65,7 @@ class IndexPage(HTMLPage):
obj_id = Id(Link('.//a')) obj_id = Id(Link('.//a'))
obj_title = CleanText('.//span[@id="title1"]') obj_title = CleanText('.//span[@id="title1"]')
obj_duration = Duration(CleanText('.//span[@class="thumbtime"]//span')) obj_duration = Duration(CleanText('.//span[@class="thumbtime"]//span'))
obj_nsfw = True
def obj_thumbnail(self): def obj_thumbnail(self):
thumbnail = BaseImage(self.xpath('.//img')[0].attrib['data-original']) thumbnail = BaseImage(self.xpath('.//img')[0].attrib['data-original'])

View file

@ -25,10 +25,9 @@ from weboob.tools.browser2 import HTMLPage
from weboob.tools.browser2.page import method, ItemElement from weboob.tools.browser2.page import method, ItemElement
from weboob.tools.browser2.filters import CleanText, Env from weboob.tools.browser2.filters import CleanText, Env
from weboob.capabilities.base import NotAvailable from weboob.capabilities.base import NotAvailable
from weboob.capabilities.video import BaseVideo
from weboob.tools.misc import to_unicode from weboob.tools.misc import to_unicode
from ..video import YoujizzVideo
__all__ = ['VideoPage'] __all__ = ['VideoPage']
@ -36,10 +35,12 @@ __all__ = ['VideoPage']
class VideoPage(HTMLPage): class VideoPage(HTMLPage):
@method @method
class get_video(ItemElement): class get_video(ItemElement):
klass = YoujizzVideo klass = BaseVideo
obj_id = Env('id') obj_id = Env('id')
obj_title = CleanText('//title') obj_title = CleanText('//title')
obj_nsfw = True
obj_ext = u'flv'
def obj_duration(self): def obj_duration(self):
# youjizz HTML is crap, we must parse it with regexps # youjizz HTML is crap, we must parse it with regexps

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon # Copyright(C) 2010-2014 Romain Bignon
# #
# This file is part of weboob. # This file is part of weboob.
# #
@ -18,6 +18,7 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.misc import limit
from weboob.tools.test import BackendTest from weboob.tools.test import BackendTest
from weboob.capabilities.video import BaseVideo from weboob.capabilities.video import BaseVideo
@ -28,15 +29,16 @@ class YoujizzTest(BackendTest):
def test_search(self): def test_search(self):
self.assertTrue(len(self.backend.search_videos('anus', nsfw=False)) == 0) self.assertTrue(len(self.backend.search_videos('anus', nsfw=False)) == 0)
l = list(self.backend.search_videos('anus', nsfw=True)) l = list(limit(self.backend.search_videos('anus', nsfw=True), 100))
self.assertTrue(len(l) > 0) self.assertTrue(len(l) > 0)
v = l[0] v = l[0]
self.backend.fillobj(v, ('url',)) self.backend.fillobj(v, ('url',))
self.assertTrue(v.url and v.url.startswith('http://'), 'URL for video "%s" not found: %s' % (v.id, v.url)) self.assertTrue(v.url and v.url.startswith('http://'), 'URL for video "%s" not found: %s' % (v.id, v.url))
self.backend.browser.openurl(v.url) r = self.backend.browser.open(v.url, stream=True)
self.assertTrue(r.status_code == 200)
def test_latest(self): def test_latest(self):
l = list(self.backend.iter_resources([BaseVideo], [u'latest_nsfw'])) l = list(limit(self.backend.iter_resources([BaseVideo], [u'latest_nsfw']), 100))
self.assertTrue(len(l) > 0) self.assertTrue(len(l) > 0)
v = l[0] v = l[0]
self.backend.fillobj(v, ('url',)) self.backend.fillobj(v, ('url',))

View file

@ -1,35 +0,0 @@
# -*- 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.capabilities.video import BaseVideo
__all__ = ['YoujizzVideo']
class YoujizzVideo(BaseVideo):
def __init__(self, *args, **kwargs):
BaseVideo.__init__(self, *args, **kwargs)
self.nsfw = True
self.ext = u'flv'
@classmethod
def id2url(cls, _id):
return 'http://www.youjizz.com/videos/%s.html' % _id