From 06677749528a1dfb0d9a552bc31324bd6e46984f Mon Sep 17 00:00:00 2001 From: Christophe Benz Date: Tue, 27 Apr 2010 12:19:48 +0200 Subject: [PATCH] move id2url to tools --- weboob/backends/youjizz/backend.py | 8 ++++++++ weboob/backends/youjizz/browser.py | 3 --- weboob/backends/youjizz/pages/index.py | 5 ++++- weboob/backends/youjizz/tools.py | 22 ++++++++++++++++++++++ weboob/backends/youporn/backend.py | 11 +++++++++++ weboob/backends/youporn/browser.py | 9 ++------- weboob/backends/youporn/pages/index.py | 4 +++- weboob/backends/youporn/tools.py | 25 +++++++++++++++++++++++++ weboob/backends/youtube/backend.py | 12 +++++++++++- weboob/backends/youtube/browser.py | 6 ++---- weboob/backends/youtube/tools.py | 22 ++++++++++++++++++++++ weboob/capabilities/video.py | 10 +++++++++- 12 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 weboob/backends/youjizz/tools.py create mode 100644 weboob/backends/youporn/tools.py create mode 100644 weboob/backends/youtube/tools.py diff --git a/weboob/backends/youjizz/backend.py b/weboob/backends/youjizz/backend.py index b55fd89f..bb8e6e5c 100644 --- a/weboob/backends/youjizz/backend.py +++ b/weboob/backends/youjizz/backend.py @@ -23,6 +23,10 @@ from weboob.capabilities.video import ICapVideoProvider from .browser import YoujizzBrowser + +__all__ = ['YoujizzBackend'] + + class YoujizzBackend(BaseBackend, ICapVideoProvider): NAME = 'youjizz' MAINTAINER = 'Roger Philibert' @@ -41,6 +45,10 @@ class YoujizzBackend(BaseBackend, ICapVideoProvider): return self._browser raise AttributeError, name + @classmethod + def id2url(cls, _id): + return 'http://www.youjizz.com/videos/%s.html' % _id + def check_url(func): def inner(self, *args, **kwargs): url = args[0] diff --git a/weboob/backends/youjizz/browser.py b/weboob/backends/youjizz/browser.py index ce0a60ba..71efb156 100644 --- a/weboob/backends/youjizz/browser.py +++ b/weboob/backends/youjizz/browser.py @@ -37,9 +37,6 @@ class YoujizzBrowser(BaseBrowser): r'http://.*youjizz\.com/search/.+\.html': IndexPage, } - def id2url(self, _id): - return 'http://www.youjizz.com/videos/%s.html' % _id - def get_video(self, url): self.location(url) return self.page.video diff --git a/weboob/backends/youjizz/pages/index.py b/weboob/backends/youjizz/pages/index.py index 1f2aa018..80203e6e 100644 --- a/weboob/backends/youjizz/pages/index.py +++ b/weboob/backends/youjizz/pages/index.py @@ -23,6 +23,8 @@ import re from weboob.capabilities.video import Video from weboob.tools.browser import BasePage +from .. import tools + __all__ = ['IndexPage'] @@ -59,4 +61,5 @@ class IndexPage(BasePage): title=title, duration=duration, preview_url=preview_url, - nsfw=True) + nsfw=True, + id2url=tools.id2url) diff --git a/weboob/backends/youjizz/tools.py b/weboob/backends/youjizz/tools.py new file mode 100644 index 00000000..6284704d --- /dev/null +++ b/weboob/backends/youjizz/tools.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +""" +Copyright(C) 2010 Roger Philibert + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" + +def id2url(_id): + return 'http://www.youjizz.com/videos/%s.html' % _id diff --git a/weboob/backends/youporn/backend.py b/weboob/backends/youporn/backend.py index fa50c909..0fe3eb9f 100644 --- a/weboob/backends/youporn/backend.py +++ b/weboob/backends/youporn/backend.py @@ -23,6 +23,10 @@ from weboob.capabilities.video import ICapVideoProvider from .browser import YoupornBrowser + +__all__ = ['YoupornBackend'] + + class YoupornBackend(BaseBackend, ICapVideoProvider): NAME = 'youporn' MAINTAINER = 'Romain Bignon' @@ -41,6 +45,13 @@ class YoupornBackend(BaseBackend, ICapVideoProvider): return self._browser raise AttributeError, name + @classmethod + def id2url(cls, _id): + if isinstance(_id, int) or isinstance(_id, (str,unicode)) and _id.isdigit(): + return 'http://www.youporn.com/watch/%d' % int(_id) + else: + return str(_id) + def need_url(func): def inner(self, *args, **kwargs): url = args[0] diff --git a/weboob/backends/youporn/browser.py b/weboob/backends/youporn/browser.py index 07ec5bc4..f9c5a10e 100644 --- a/weboob/backends/youporn/browser.py +++ b/weboob/backends/youporn/browser.py @@ -22,6 +22,7 @@ import urllib from weboob.tools.browser import BaseBrowser +from . import tools from .pages.index import IndexPage from .pages.video import VideoPage @@ -42,12 +43,6 @@ class YoupornBrowser(BaseBrowser): # Disallow arguments BaseBrowser.__init__(self) - def id2url(self, _id): - if isinstance(_id, int) or isinstance(_id, (str,unicode)) and _id.isdigit(): - return 'http://www.youporn.com/watch/%d' % int(_id) - else: - return str(_id) - def iter_search_results(self, pattern, sortby): if not pattern: self.home() @@ -58,5 +53,5 @@ class YoupornBrowser(BaseBrowser): return self.page.iter_videos() def get_video(self, _id): - self.location(self.id2url(_id)) + self.location(tools.id2url(_id)) return self.page.video diff --git a/weboob/backends/youporn/pages/index.py b/weboob/backends/youporn/pages/index.py index d8369d3d..39bd25a9 100644 --- a/weboob/backends/youporn/pages/index.py +++ b/weboob/backends/youporn/pages/index.py @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from weboob.capabilities.video import Video +from .. import tools from .base import PornPage @@ -71,4 +72,5 @@ class IndexPage(PornPage): rating_max=rating_max, duration=duration, preview_url=preview_url, - nsfw=True) + nsfw=True, + id2url=tools.id2url) diff --git a/weboob/backends/youporn/tools.py b/weboob/backends/youporn/tools.py new file mode 100644 index 00000000..cfa6f886 --- /dev/null +++ b/weboob/backends/youporn/tools.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +Copyright(C) 2010 Roger Philibert + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" + +def id2url(_id): + if isinstance(_id, int) or isinstance(_id, (str,unicode)) and _id.isdigit(): + return 'http://www.youporn.com/watch/%d' % int(_id) + else: + return str(_id) diff --git a/weboob/backends/youtube/backend.py b/weboob/backends/youtube/backend.py index 9df3af98..9b05532b 100644 --- a/weboob/backends/youtube/backend.py +++ b/weboob/backends/youtube/backend.py @@ -24,8 +24,13 @@ import re from weboob.backend import BaseBackend from weboob.capabilities.video import ICapVideoProvider, Video +from . import tools from .browser import YoutubeBrowser + +__all__ = ['YoutubeBackend'] + + class YoutubeBackend(BaseBackend, ICapVideoProvider): NAME = 'youtube' MAINTAINER = 'Christophe Benz' @@ -44,6 +49,10 @@ class YoutubeBackend(BaseBackend, ICapVideoProvider): return self._browser raise AttributeError, name + @classmethod + def id2url(cls, _id): + return _id if 'youtube.com' in _id else 'http://www.youtube.com/watch?v=%s' % _id + def get_video(self, _id): return self.browser.get_video(_id) @@ -69,7 +78,8 @@ class YoutubeBackend(BaseBackend, ICapVideoProvider): title=entry.media.title.text.decode('utf-8').strip(), author=author, duration=int(entry.media.duration.seconds.decode('utf-8').strip()), - preview_url=entry.media.thumbnail[0].url.decode('utf-8').strip()) + preview_url=entry.media.thumbnail[0].url.decode('utf-8').strip(), + id2url=tools.id2url) def iter_page_urls(self, mozaic_url): raise NotImplementedError() diff --git a/weboob/backends/youtube/browser.py b/weboob/backends/youtube/browser.py index fb6afec7..2f3dd5b9 100644 --- a/weboob/backends/youtube/browser.py +++ b/weboob/backends/youtube/browser.py @@ -22,6 +22,7 @@ import urllib from weboob.tools.browser import BaseBrowser +from . import tools from .pages import VideoPage __all__ = ['YoutubeBrowser'] @@ -30,9 +31,6 @@ class YoutubeBrowser(BaseBrowser): PAGES = {'.*youtube\.com/watch\?v=(.+)': VideoPage, } - def id2url(self, _id): - return _id if 'youtube.com' in _id else 'http://www.youtube.com/watch?v=%s' % _id - def get_video(self, _id): - self.location(self.id2url(_id)) + self.location(tools.id2url(_id)) return self.page.video diff --git a/weboob/backends/youtube/tools.py b/weboob/backends/youtube/tools.py new file mode 100644 index 00000000..6d19b47c --- /dev/null +++ b/weboob/backends/youtube/tools.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +""" +Copyright(C) 2010 Christophe Benz + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" + +def id2url(_id): + return _id if 'youtube.com' in _id else 'http://www.youtube.com/watch?v=%s' % _id diff --git a/weboob/capabilities/video.py b/weboob/capabilities/video.py index 44e7dd74..51d3e0cf 100644 --- a/weboob/capabilities/video.py +++ b/weboob/capabilities/video.py @@ -26,7 +26,7 @@ __all__ = ['ICapVideoProvider', 'Video'] class Video(object): def __init__(self, _id, title=None, url=None, author=None, duration=0, date=None, - rating=0.0, rating_max=0.0, preview_url=None, nsfw=False): + rating=0.0, rating_max=0.0, preview_url=None, nsfw=False, id2url=None): self.id = _id self.title = title self.url = url @@ -37,11 +37,19 @@ class Video(object): self.rating_max = float(rating_max) self.preview_url = preview_url self.nsfw = nsfw + self.id2url = id2url @property def formatted_duration(self): return '%d:%02d:%02d' % (self.duration / 3600, (self.duration % 3600 / 60), self.duration % 60) + @property + def page_url(self): + if self.id2url: + return self.id2url(self.id) + else: + return None + class ICapVideoProvider(ICap): def iter_page_urls(self, mozaic_url): raise NotImplementedError()