From 90396044b5b1c4c64b22012ddaf29402a5ef9c67 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 17 Apr 2010 18:08:59 +0200 Subject: [PATCH] implement ICapVideoProvider.get_video() --- weboob/backends/youtube/backend.py | 4 +- weboob/backends/youtube/browser.py | 11 +++++- weboob/backends/youtube/pages/results.py | 2 +- weboob/backends/youtube/pages/video.py | 48 ++++++++++++++++++++++-- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/weboob/backends/youtube/backend.py b/weboob/backends/youtube/backend.py index e938d95d..4bfbd75e 100644 --- a/weboob/backends/youtube/backend.py +++ b/weboob/backends/youtube/backend.py @@ -18,6 +18,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ +import re + from weboob.backend import BaseBackend from weboob.capabilities.video import ICapVideoProvider @@ -44,7 +46,7 @@ class YoutubeBackend(BaseBackend, ICapVideoProvider): def need_url(func): def inner(self, *args, **kwargs): url = args[0] - if isinstance(url, (str,unicode)) and not url.isdigit() and u'youtube.com' not in url: + if (u'youtube.com' not in url) and not re.match('^\w+$', url): return None return func(self, *args, **kwargs) return inner diff --git a/weboob/backends/youtube/browser.py b/weboob/backends/youtube/browser.py index 9fcefc76..42df9afa 100644 --- a/weboob/backends/youtube/browser.py +++ b/weboob/backends/youtube/browser.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ -Copyright(C) 2010 Christophe Benz +Copyright(C) 2010 Christophe Benz, Romain Bignon 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 @@ -49,6 +49,15 @@ class YoutubeBrowser(BaseBrowser): assert self.is_on_page(ResultsPage) return self.page.iter_videos() + def get_video(self, _id): + if re.match('^\w+$', _id): + url = 'http://www.youtube.com/watch?v=%s' % _id + else: + url = _id + + self.location(url) + return self.page.video + def get_video_title(self, page_url): self.location(page_url) return self.page.title diff --git a/weboob/backends/youtube/pages/results.py b/weboob/backends/youtube/pages/results.py index 62abd8f7..63fc0cca 100644 --- a/weboob/backends/youtube/pages/results.py +++ b/weboob/backends/youtube/pages/results.py @@ -24,7 +24,7 @@ from weboob.tools.browser import BasePage from weboob.capabilities.video import Video class ResultsPage(BasePage): - WATCH_RE = re.compile('/watch?v=(\w+)') + WATCH_RE = re.compile('/watch\?v=(\w+)') def iter_videos(self): for div in self.document.getroot().cssselect("div[class^=video-entry]"): a = div.find('a') diff --git a/weboob/backends/youtube/pages/video.py b/weboob/backends/youtube/pages/video.py index 3f856f03..38457298 100644 --- a/weboob/backends/youtube/pages/video.py +++ b/weboob/backends/youtube/pages/video.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ -Copyright(C) 2010 Christophe Benz +Copyright(C) 2010 Christophe Benz, Romain Bignon 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 @@ -18,13 +18,53 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ +import re +from logging import warning + from weboob.tools.browser import BasePage +from weboob.capabilities.video import Video class VideoPage(BasePage): + URL_REGEXP = re.compile("https?://[w\.]*youtube.com/watch\?v=(\w+)") + def on_loaded(self): + self.video = Video(self.get_id()) + self.video.title = self.get_title() + self.video.url = self.get_url() + + self.set_details(self.video) + + def get_id(self): + m = self.URL_REGEXP.match(self.url) + if m: + return m.group(1) + warning("Unable to parse ID") + return 0 + + VIDEO_SIGNATURE_RE = re.compile(r'&t=([^ ,&]*)') + def get_url(self): + video_signature = None + for data in self.document.getiterator('script'): + if not data.text: + continue + for m in re.finditer(self.VIDEO_SIGNATURE_RE, data.text): + video_signature = m.group(1) + return 'http://www.youtube.com/get_video?video_id=%s&t=%s&fmt=18' % (self.video.id, video_signature) + + def get_title(self): found = self.document.getroot().cssselect('meta[name=title]') if found: content = found[0].attrib['content'] - self.title = unicode(content).strip() - else: - self.title = None + return unicode(content).strip() + return u'' + + DATE_REGEXP = re.compile("\w+ (\w+) (\d+) (\d+):(\d+):(\d+) (\d+)") + MONTH2I = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] + + def set_details(self, v): + div = self.document.getroot().cssselect('div[id=watch-description-body]') + if not div: + return + + div = div[0] + v.author = div.find('a').find('strong').text