From cba809a8d024c9d3a9eda37a4b4020ca007f05a7 Mon Sep 17 00:00:00 2001 From: Roger Philibert Date: Wed, 21 Mar 2012 21:37:12 +0100 Subject: [PATCH] [okc] Photos handling in profile --- modules/okc/backend.py | 18 ++++++++++-------- modules/okc/browser.py | 15 +++++++++++---- modules/okc/pages.py | 5 +++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/modules/okc/backend.py b/modules/okc/backend.py index e5c3339d..1e2dd27a 100644 --- a/modules/okc/backend.py +++ b/modules/okc/backend.py @@ -242,13 +242,13 @@ class OkCBackend(BaseBackend, ICapMessages, ICapContact): data = self.browser.openurl(photo.thumbnail_url).read() contact.set_photo(name, thumbnail_data=data) - #def fill_photo(self, photo, fields): - # with self.browser: - # if 'data' in fields and photo.url and not photo.data: - # photo.data = self.browser.readurl(photo.url) - # if 'thumbnail_data' in fields and photo.thumbnail_url and not photo.thumbnail_data: - # photo.thumbnail_data = self.browser.readurl(photo.thumbnail_url) - # return photo + def fill_photo(self, photo, fields): + with self.browser: + if 'data' in fields and photo.url and not photo.data: + photo.data = self.browser.readurl(photo.url) + if 'thumbnail_data' in fields and photo.thumbnail_url and not photo.thumbnail_data: + photo.thumbnail_data = self.browser.readurl(photo.thumbnail_url) + return photo def get_contact(self, contact): with self.browser: @@ -280,6 +280,8 @@ class OkCBackend(BaseBackend, ICapMessages, ICapContact): contact.status = Contact.STATUS_OFFLINE contact.status_msg = contact.profile['details']['last_online'].value + for no, photo in enumerate(self.browser.get_photos(_id)): + contact.set_photo('image_%i' % no, url=photo, thumbnail_url=photo) return contact #def _get_partial_contact(self, contact): @@ -355,5 +357,5 @@ class OkCBackend(BaseBackend, ICapMessages, ICapContact): OBJECTS = {Thread: fill_thread, Contact: fill_contact, - #ContactPhoto: fill_photo + ContactPhoto: fill_photo } diff --git a/modules/okc/browser.py b/modules/okc/browser.py index 1b99585e..c8278aa1 100644 --- a/modules/okc/browser.py +++ b/modules/okc/browser.py @@ -32,7 +32,7 @@ except ImportError: from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword, BrowserUnavailable from weboob.tools.ordereddict import OrderedDict -from .pages import LoginPage, ThreadPage, MessagesPage, ProfilePage +from .pages import LoginPage, ThreadPage, MessagesPage, ProfilePage, PhotosPage __all__ = ['OkCBrowser'] @@ -48,7 +48,8 @@ class OkCBrowser(BaseBrowser): ('https://%s/login.*' % DOMAIN, LoginPage), ('http://%s/messages' % DOMAIN, ThreadPage), ('http://%s/messages\?.*' % DOMAIN, MessagesPage), - ('http://%s/profile/.*' % DOMAIN, ProfilePage), + ('http://%s/profile/.*/photos' % DOMAIN, PhotosPage), + ('http://%s/profile/[^/]*' % DOMAIN, ProfilePage), )) @@ -200,9 +201,15 @@ class OkCBrowser(BaseBrowser): # return set(ids) @check_login - def get_profile(self, id, with_pics=True): + def get_profile(self, id): self.location(self.absurl('/profile/%s' % id)) - return self.page.get_profile() + profile = self.page.get_profile() + return profile + + @check_login + def get_photos(self, id): + self.location(self.absurl('/profile/%s/photos' % id)) + return self.page.get_photos() #def _get_chat_infos(self): # try: diff --git a/modules/okc/pages.py b/modules/okc/pages.py index 4be4e4ae..10addeb0 100644 --- a/modules/okc/pages.py +++ b/modules/okc/pages.py @@ -129,3 +129,8 @@ class ProfilePage(BasePage): profile['data']['details'].value[key] = ProfileNode(key, label, val) return profile + +class PhotosPage(BasePage): + def get_photos(self): + imgs = self.parser.select(self.document.getroot(), "//div[@class='pic clearfix']//img", method='xpath') + return [img.get('src') for img in imgs]