From d710c93b363824cf74bddc483afae01ba1f0549c Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sun, 1 Aug 2010 21:08:22 +0200 Subject: [PATCH] fetch thumbnails asynchroneously --- weboob/applications/qvideoob/main_window.py | 2 +- weboob/applications/qvideoob/minivideo.py | 16 ++++++++++++---- weboob/backends/youjizz/backend.py | 12 +++++++++--- weboob/backends/youporn/backend.py | 12 +++++++++--- weboob/backends/youtube/backend.py | 12 +++++++++--- weboob/capabilities/video.py | 20 ++++++++++++++++++-- 6 files changed, 58 insertions(+), 16 deletions(-) diff --git a/weboob/applications/qvideoob/main_window.py b/weboob/applications/qvideoob/main_window.py index a2d9bc31..eace523c 100644 --- a/weboob/applications/qvideoob/main_window.py +++ b/weboob/applications/qvideoob/main_window.py @@ -87,7 +87,7 @@ class MainWindow(QtMainWindow): self.ui.searchEdit.setEnabled(True) self.process = None return - minivideo = MiniVideo(backend, video) + minivideo = MiniVideo(self.weboob, backend, video) self.ui.scrollAreaContent.layout().addWidget(minivideo) self.minivideos.append(minivideo) if (video.nsfw and not self.ui.nsfwCheckBox.isChecked() or diff --git a/weboob/applications/qvideoob/minivideo.py b/weboob/applications/qvideoob/minivideo.py index 5840b29d..df6f9e6c 100644 --- a/weboob/applications/qvideoob/minivideo.py +++ b/weboob/applications/qvideoob/minivideo.py @@ -19,15 +19,17 @@ import urllib2 from PyQt4.QtGui import QFrame, QImage, QPixmap +from weboob.tools.application.qt import QtDo from weboob.applications.qvideoob.ui.minivideo_ui import Ui_MiniVideo from .video import Video class MiniVideo(QFrame): - def __init__(self, backend, video, parent=None): + def __init__(self, weboob, backend, video, parent=None): QFrame.__init__(self, parent) self.ui = Ui_MiniVideo() self.ui.setupUi(self) + self.weboob = weboob self.backend = backend self.video = video self.ui.titleLabel.setText(video.title) @@ -40,9 +42,15 @@ class MiniVideo(QFrame): else: self.ui.ratingLabel.setText('%s' % video.rating) - if video.thumbnail_url: - data = urllib2.urlopen(video.thumbnail_url).read() - img = QImage.fromData(data) + self.process_thumbnail = QtDo(self.weboob, self.gotThumbnail) + self.process_thumbnail.do('fillobj', self.video, ['thumbnail'], backends=backend) + + def gotThumbnail(self, backend, video): + if not backend: + return + + if video.thumbnail.data: + img = QImage.fromData(video.thumbnail.data) self.ui.imageLabel.setPixmap(QPixmap.fromImage(img)) def enterEvent(self, event): diff --git a/weboob/backends/youjizz/backend.py b/weboob/backends/youjizz/backend.py index dc6aa495..7a010e96 100644 --- a/weboob/backends/youjizz/backend.py +++ b/weboob/backends/youjizz/backend.py @@ -57,8 +57,14 @@ class YoujizzBackend(BaseBackend, ICapVideo): return self.browser.iter_search_results(pattern) def fill_video(self, video, fields): - # ignore the fields param: VideoPage.get_video() returns all the information - with self.browser: - return self.browser.get_video(YoujizzVideo.id2url(video.id), video) + if fields != ['thumbnail']: + # if we don't want only the thumbnail, we probably want also every fields + with self.browser: + video = self.browser.get_video(YoujizzVideo.id2url(video.id), video) + if 'thumbnail' in fields: + with self.browser: + video.thumbnail.data = self.browser.openurl(video.thumbnail.url).read() + + return video OBJECTS = {YoujizzVideo: fill_video} diff --git a/weboob/backends/youporn/backend.py b/weboob/backends/youporn/backend.py index 5ea89dda..bd58c398 100644 --- a/weboob/backends/youporn/backend.py +++ b/weboob/backends/youporn/backend.py @@ -56,8 +56,14 @@ class YoupornBackend(BaseBackend, ICapVideo): raise NotImplementedError() def fill_video(self, video, fields): - # ignore the fields param: VideoPage.get_video() returns all the information - with self.browser: - return self.browser.get_video(YoupornVideo.id2url(video.id), video) + if fields != ['thumbnail']: + # if we don't want only the thumbnail, we probably want also every fields + with self.browser: + video = self.browser.get_video(YoupornVideo.id2url(video.id), video) + if 'thumbnail' in fields: + with self.browser: + video.thumbnail.data = self.browser.openurl(video.thumbnail.url).read() + + return video OBJECTS = {YoupornVideo: fill_video} diff --git a/weboob/backends/youtube/backend.py b/weboob/backends/youtube/backend.py index 1c2104df..158853f4 100644 --- a/weboob/backends/youtube/backend.py +++ b/weboob/backends/youtube/backend.py @@ -72,8 +72,14 @@ class YoutubeBackend(BaseBackend, ICapVideo): raise NotImplementedError() def fill_video(self, video, fields): - # ignore the fields param: VideoPage.get_video() returns all the information - with self.browser: - return self.browser.get_video(YoutubeVideo.id2url(video.id), video) + if fields != ['thumbnail']: + # if we don't want only the thumbnail, we probably want also every fields + with self.browser: + video = self.browser.get_video(YoutubeVideo.id2url(video.id), video) + if 'thumbnail' in fields: + with self.browser: + video.thumbnail.data = self.browser.openurl(video.thumbnail.url).read() + + return video OBJECTS = {YoutubeVideo: fill_video} diff --git a/weboob/capabilities/video.py b/weboob/capabilities/video.py index aaa3ca46..8a415594 100644 --- a/weboob/capabilities/video.py +++ b/weboob/capabilities/video.py @@ -22,9 +22,23 @@ from .cap import ICap __all__ = ['BaseVideo', 'ICapVideo'] +class ContactThumbnail(object): + def __init__(self, url): + self.url = url + self.data = None + + def __str__(self): + return '<%s>' % self.url + + def __repr__(self): + return '' % self.url + + def __iscomplete__(self): + return self.data + class BaseVideo(object): def __init__(self, _id, title=None, url=None, author=None, duration=None, date=None, - rating=0.0, rating_max=0.0, thumbnail_url=None, nsfw=False): + rating=0.0, rating_max=0.0, thumbnail=None, thumbnail_url=None, nsfw=False): self.id = unicode(_id) self.title = title self.url = url @@ -33,7 +47,9 @@ class BaseVideo(object): self.date = date self.rating = float(rating) self.rating_max = float(rating_max) - self.thumbnail_url = thumbnail_url + self.thumbnail = thumbnail + if thumbnail_url and not self.thumbnail: + self.thumbnail = ContactThumbnail(thumbnail_url) self.nsfw = nsfw @classmethod