implement ICapVideoProvider.get_video()
This commit is contained in:
parent
ecb21905a4
commit
90396044b5
4 changed files with 58 additions and 7 deletions
|
|
@ -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.backend import BaseBackend
|
||||||
from weboob.capabilities.video import ICapVideoProvider
|
from weboob.capabilities.video import ICapVideoProvider
|
||||||
|
|
||||||
|
|
@ -44,7 +46,7 @@ class YoutubeBackend(BaseBackend, ICapVideoProvider):
|
||||||
def need_url(func):
|
def need_url(func):
|
||||||
def inner(self, *args, **kwargs):
|
def inner(self, *args, **kwargs):
|
||||||
url = args[0]
|
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 None
|
||||||
return func(self, *args, **kwargs)
|
return func(self, *args, **kwargs)
|
||||||
return inner
|
return inner
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- 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
|
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
|
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)
|
assert self.is_on_page(ResultsPage)
|
||||||
return self.page.iter_videos()
|
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):
|
def get_video_title(self, page_url):
|
||||||
self.location(page_url)
|
self.location(page_url)
|
||||||
return self.page.title
|
return self.page.title
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ from weboob.tools.browser import BasePage
|
||||||
from weboob.capabilities.video import Video
|
from weboob.capabilities.video import Video
|
||||||
|
|
||||||
class ResultsPage(BasePage):
|
class ResultsPage(BasePage):
|
||||||
WATCH_RE = re.compile('/watch?v=(\w+)')
|
WATCH_RE = re.compile('/watch\?v=(\w+)')
|
||||||
def iter_videos(self):
|
def iter_videos(self):
|
||||||
for div in self.document.getroot().cssselect("div[class^=video-entry]"):
|
for div in self.document.getroot().cssselect("div[class^=video-entry]"):
|
||||||
a = div.find('a')
|
a = div.find('a')
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- 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
|
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
|
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.tools.browser import BasePage
|
||||||
|
from weboob.capabilities.video import Video
|
||||||
|
|
||||||
class VideoPage(BasePage):
|
class VideoPage(BasePage):
|
||||||
|
URL_REGEXP = re.compile("https?://[w\.]*youtube.com/watch\?v=(\w+)")
|
||||||
|
|
||||||
def on_loaded(self):
|
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]')
|
found = self.document.getroot().cssselect('meta[name=title]')
|
||||||
if found:
|
if found:
|
||||||
content = found[0].attrib['content']
|
content = found[0].attrib['content']
|
||||||
self.title = unicode(content).strip()
|
return unicode(content).strip()
|
||||||
else:
|
return u''
|
||||||
self.title = None
|
|
||||||
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue