From 0c522d948a804032370775351ad27d37aa083a8c Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 11 Feb 2012 19:06:39 +0100 Subject: [PATCH] add housing photos --- modules/seloger/backend.py | 8 +++++++- modules/seloger/browser.py | 11 +++++++++-- modules/seloger/pages.py | 11 +++++++++-- weboob/capabilities/housing.py | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/modules/seloger/backend.py b/modules/seloger/backend.py index 30de0f79..065c1d78 100644 --- a/modules/seloger/backend.py +++ b/modules/seloger/backend.py @@ -68,7 +68,13 @@ class SeLogerBackend(BaseBackend, ICapHousing): def fill_housing(self, housing, fields): with self.browser: - return self.browser.get_housing(housing.id) + if fields != ['photos'] or not housing.photos: + housing = self.browser.get_housing(housing.id) + if 'photos' in fields: + for photo in housing.photos: + if not photo.data: + photo.data = self.browser.readurl(photo.url) + return housing OBJECTS = {Housing: fill_housing, } diff --git a/modules/seloger/browser.py b/modules/seloger/browser.py index af371462..b4bf3516 100644 --- a/modules/seloger/browser.py +++ b/modules/seloger/browser.py @@ -20,9 +20,10 @@ import json +from weboob.capabilities.base import NotAvailable from weboob.tools.browser import BaseBrowser -from .pages import SearchResultsPage, HousingPage +from .pages import SearchResultsPage, HousingPage, HousingPhotosPage __all__ = ['SeLogerBrowser'] @@ -36,6 +37,7 @@ class SeLogerBrowser(BaseBrowser): 'http://www.seloger.com/(pre)?recherche.htm.*': SearchResultsPage, 'http://www.seloger.com/annonces.htm.*': SearchResultsPage, 'http://www.seloger.com/annonces/.*': HousingPage, + 'http://www.seloger.com/\d+/incl_detail_annonce_load_diapo_new.htm': HousingPhotosPage, } def search_geo(self, pattern): @@ -62,4 +64,9 @@ class SeLogerBrowser(BaseBrowser): self.location('/%d/detail_new.htm' % int(id)) assert self.is_on_page(HousingPage) - return self.page.get_housing(obj) + housing = self.page.get_housing(obj) + + self.location('/%d/incl_detail_annonce_load_diapo_new.htm' % int(id)) + housing.photos = list(self.page.iter_photos()) or NotAvailable + + return housing diff --git a/modules/seloger/pages.py b/modules/seloger/pages.py index d8d2924e..5f7284b4 100644 --- a/modules/seloger/pages.py +++ b/modules/seloger/pages.py @@ -23,10 +23,10 @@ from datetime import date from weboob.tools.browser import BasePage, BrokenPageError from weboob.capabilities.base import NotAvailable -from weboob.capabilities.housing import Housing +from weboob.capabilities.housing import Housing, HousingPhoto -__all__ = ['SearchResultsPage', 'HousingPage'] +__all__ = ['SearchResultsPage', 'HousingPage', 'HousingPhotosPage'] def sanitarize_cost(t): @@ -98,3 +98,10 @@ class HousingPage(BasePage): housing.location = NotAvailable return housing + +class HousingPhotosPage(BasePage): + def iter_photos(self): + for i, li in enumerate(self.document.getroot().xpath('//li')): + photo = HousingPhoto(i) + photo.url = li.attrib['rel'].split('|')[0] + yield photo diff --git a/weboob/capabilities/housing.py b/weboob/capabilities/housing.py index f1e3f981..7d52abe3 100644 --- a/weboob/capabilities/housing.py +++ b/weboob/capabilities/housing.py @@ -26,6 +26,21 @@ from .base import IBaseCap, CapBaseObject __all__ = ['ICapHousing'] +class HousingPhoto(CapBaseObject): + def __init__(self, id): + CapBaseObject.__init__(self, id) + self.add_field('url', basestring) + self.add_field('data', str) + + def __iscomplete__(self): + return self.data + + def __str__(self): + return self.url + + def __repr__(self): + return u'' % (self.id, len(self.data) if self.data else 0) + class Housing(CapBaseObject): def __init__(self, id): CapBaseObject.__init__(self, id) @@ -38,6 +53,7 @@ class Housing(CapBaseObject): self.add_field('station', basestring) self.add_field('text', basestring) self.add_field('phone', basestring) + self.add_field('photos', list) class Query(CapBaseObject): def __init__(self):