From 8816be46f5c11583b1e26c71b78b17ce7a3f150d Mon Sep 17 00:00:00 2001 From: Bezleputh Date: Fri, 3 Oct 2014 15:21:48 +0200 Subject: [PATCH] [leboncoin] fix select values --- modules/leboncoin/browser.py | 43 +++++++++++++++++++++++++++++++++--- modules/leboncoin/module.py | 9 +++++--- modules/leboncoin/pages.py | 37 ++++++++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 9 deletions(-) diff --git a/modules/leboncoin/browser.py b/modules/leboncoin/browser.py index 28443a17..7b47dc4e 100644 --- a/modules/leboncoin/browser.py +++ b/modules/leboncoin/browser.py @@ -18,7 +18,7 @@ # along with weboob. If not, see . from weboob.browser2 import PagesBrowser, URL - +from weboob.capabilities.housing import Query from .pages import CityListPage, HousingListPage, HousingPage @@ -26,10 +26,16 @@ class LeboncoinBrowser(PagesBrowser): BASEURL = 'http://www.leboncoin.fr' city = URL('ajax/location_list.html\?city=(?P.*)&zipcode=(?P.*)', CityListPage) search = URL('(?P.*)/offres/ile_de_france/occasions/\?ps=(?P.*)&pe=(?P.*)&ros=(?P.*)&location=(?P.*)&sqs=(?P.*)&sqe=(?P.*)&ret=(?P.*)&f=(?P.*)', - '(ventes_immobilieres|locations)/offres/ile_de_france/occasions/\?.*', + '(?P<_type>.*)/offres/ile_de_france/occasions.*?', HousingListPage) housing = URL('ventes_immobilieres/(?P<_id>.*).htm', HousingPage) + RET = {Query.HOUSE_TYPES.HOUSE: '1', + Query.HOUSE_TYPES.APART: '2', + Query.HOUSE_TYPES.LAND: '3', + Query.HOUSE_TYPES.PARKING: '4', + Query.HOUSE_TYPES.OTHER: '5'} + def get_cities(self, pattern): city = '' zip_code = '' @@ -40,7 +46,8 @@ class LeboncoinBrowser(PagesBrowser): return self.city.go(city=city, zip=zip_code).get_cities() - def search_housings(self, type, cities, nb_rooms, area_min, area_max, cost_min, cost_max, ret, advert_type): + def search_housings(self, query, advert_type): + type, cities, nb_rooms, area_min, area_max, cost_min, cost_max, ret = self.decode_query(query) return self.search.go(location=cities, ros=nb_rooms, sqs=area_min, @@ -53,3 +60,33 @@ class LeboncoinBrowser(PagesBrowser): def get_housing(self, _id, obj=None): return self.housing.go(_id=_id).get_housing(obj=obj) + + def decode_query(self, query): + cities = [] + for c in query.cities: + cities.append('%s %s' % (c.id, c.name)) + + if len(cities) == 0: + return list() + + ret = [] + for g in query.house_types: + if g in self.RET: + ret.append(self.RET.get(g)) + + if len(ret) == 0: + return list() + + _type = 'ventes_immobilieres' + if query.type == Query.TYPE_RENT: + _type = 'locations' + + self.search.go(_type=_type) + + nb_rooms = '' if not query.nb_rooms else self.page.get_rooms_min(query.nb_rooms) + area_min = '' if not query.area_min else self.page.get_area_min(query.area_min) + area_max = '' if not query.area_max else self.page.get_area_max(query.area_max) + cost_min = '' if not query.cost_min else self.page.get_cost_min(query.cost_min) + cost_max = '' if not query.cost_max else self.page.get_cost_max(query.cost_max) + + return _type, ','.join(cities), nb_rooms, area_min, area_max, cost_min, cost_max, '&ret='.join(ret) diff --git a/modules/leboncoin/module.py b/modules/leboncoin/module.py index 2057fb58..38ddabde 100644 --- a/modules/leboncoin/module.py +++ b/modules/leboncoin/module.py @@ -19,7 +19,7 @@ from weboob.tools.backend import Module, BackendConfig -from weboob.capabilities.housing import CapHousing, Query, Housing, HousingPhoto +from weboob.capabilities.housing import CapHousing, Housing, HousingPhoto from weboob.tools.value import Value from .browser import LeboncoinBrowser @@ -39,13 +39,13 @@ class LeboncoinModule(Module, CapHousing): CONFIG = BackendConfig(Value('advert_type', label='Advert type', choices={'c': 'Agency', 'p': 'Owner', 'a': 'All'}, default='a')) - + """ RET = {Query.HOUSE_TYPES.HOUSE: '1', Query.HOUSE_TYPES.APART: '2', Query.HOUSE_TYPES.LAND: '3', Query.HOUSE_TYPES.PARKING: '4', Query.HOUSE_TYPES.OTHER: '5'} - + """ def get_housing(self, _id): return self.browser.get_housing(_id) @@ -61,6 +61,7 @@ class LeboncoinModule(Module, CapHousing): return self.browser.get_cities(pattern) def search_housings(self, query): + """ cities = [] for c in query.cities: cities.append('%s %s' % (c.id, c.name)) @@ -90,5 +91,7 @@ class LeboncoinModule(Module, CapHousing): area_min, area_max, cost_min, cost_max, '&ret='.join(ret), self.config['advert_type'].get()) + """ + return self.browser.search_housings(query, self.config['advert_type'].get()) OBJECTS = {Housing: fill_housing, HousingPhoto: fill_photo} diff --git a/modules/leboncoin/pages.py b/modules/leboncoin/pages.py index 8246fcd8..b23a1c6f 100644 --- a/modules/leboncoin/pages.py +++ b/modules/leboncoin/pages.py @@ -16,7 +16,6 @@ # # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . - from decimal import Decimal from weboob.browser2.page import HTMLPage, method, pagination from weboob.browser2.elements import ItemElement, ListElement @@ -40,6 +39,38 @@ class CityListPage(HTMLPage): class HousingListPage(HTMLPage): + def get_area_min(self, asked_area): + return self.find_select_value(asked_area, '//select[@id="sqs"]/option') + + def get_area_max(self, asked_area): + return self.find_select_value(asked_area, '//select[@id="sqe"]/option') + + def get_rooms_min(self, asked_rooms): + return self.find_select_value(asked_rooms, '//select[@id="ros"]/option') + + # def get_rooms_max(self, asked_rooms): + # return self.find_select_value(asked_rooms, '//select[@id="roe"]/option') + + def get_cost_min(self, asked_cost): + return self.find_select_value(asked_cost, '//select[@id="ps"]/option') + + def get_cost_max(self, asked_cost): + return self.find_select_value(asked_cost, '//select[@id="pe"]/option') + + def find_select_value(self, ref_value, selector): + select = {} + for item in self.doc.xpath(selector): + if item.attrib['value']: + select[CleanDecimal('.')(item)] = CleanDecimal('./@value')(item) + + select_keys = select.keys() + select_keys.sort() + for select_value in select_keys: + if select_value >= ref_value: + return select[select_value] + + return select[select_keys[-1]] + @pagination @method class get_housing_list(ListElement): @@ -116,8 +147,8 @@ class HousingPage(HTMLPage): obj_area = Env('area') def obj_date(self): - _date = Regexp(CleanText('//div[@class="upload_by"]', replace=[(u'à', '')]), - '.*- Mise en ligne le (.*).')(self) + _date = Regexp(CleanText('//div[@class="upload_by"]', replace=[(u'à', '')]), + '.*- Mise en ligne le (.*).')(self) for fr, en in DATE_TRANSLATE_FR: _date = fr.sub(en, _date)