File: pages.py

package info (click to toggle)
weboob 0.c-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,008 kB
  • sloc: python: 28,678; perl: 244; sh: 198; makefile: 111; sql: 17
file content (104 lines) | stat: -rw-r--r-- 3,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-

# Copyright(C) 2012 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from decimal import Decimal
from dateutil.parser import parse as parse_date

from weboob.tools.browser import BasePage
from weboob.capabilities.base import NotAvailable
from weboob.capabilities.housing import Housing, HousingPhoto


__all__ = ['SearchResultsPage', 'HousingPage']


class SearchResultsPage(BasePage):
    def next_page_url(self):
        urls = self.document.getroot().xpath('//pagesuivante')
        if len(urls) == 0:
            return None
        else:
            return urls[0].text

    def iter_housings(self):
        for a in self.document.getroot().xpath('//annonce'):
            housing = Housing(a.find('idannonce').text)
            housing.title = a.find('titre').text
            housing.date = parse_date(a.find('dtfraicheur').text)
            housing.cost = Decimal(a.find('prix').text)
            housing.currency = u'€'
            housing.area = Decimal(a.find('surface').text)
            housing.text = a.find('descriptif').text.strip()
            housing.location = a.find('ville').text
            try:
                housing.station = a.find('proximite').text
            except AttributeError:
                housing.station = NotAvailable

            housing.photos = []
            for photo in a.xpath('./photos/photo'):
                url = photo.find('stdurl').text
                housing.photos.append(HousingPhoto(url))

            yield housing

class HousingPage(BasePage):
    def get_housing(self, housing=None):
        if housing is None:
            housing = Housing(self.groups[0])

        details = self.document.getroot().xpath('//detailannonce')[0]
        if details.find('titre') is None:
            return None

        housing.title = details.find('titre').text
        housing.text = details.find('descriptif').text.strip()
        housing.cost = Decimal(details.find('prix').text)
        housing.currency = u'€'
        housing.date = parse_date(details.find('dtfraicheur').text)
        housing.area = Decimal(details.find('surface').text)
        housing.phone = details.find('contact').find('telephone').text

        try:
            housing.station = details.find('proximite').text
        except AttributeError:
            housing.station = NotAvailable

        housing.location = details.find('adresse').text
        if not housing.location and details.find('quartier') is not None:
            housing.location = details.find('quartier').text
        if not housing.location:
            housing.location = NotAvailable

        housing.photos = []
        for photo in details.xpath('./photos/photo'):
            if photo.find('bigurl').text:
                url = photo.find('bigurl').text
            else:
                url = photo.find('stdurl').text
            housing.photos.append(HousingPhoto(url))

        housing.details = {}
        for detail in details.xpath('./details/detail'):
            housing.details[detail.find('libelle').text.strip()] = detail.find('valeur').text or 'N/A'

        housing.details['Reference'] = details.find('reference').text

        return housing