[weboob] [PATCH 1/3] colisprive: fixes

Matthieu Weber mweber+weboob at free.fr
Fri May 22 19:23:12 CEST 2015


fixed history parsing
rewrite for browser2


Signed-off-by: Matthieu Weber <mweber+weboob at free.fr>
---
 modules/colisprive/browser.py |   21 +++++----------------
 modules/colisprive/module.py  |    5 ++---
 modules/colisprive/pages.py   |   27 ++++++++++++++-------------
 3 files changed, 21 insertions(+), 32 deletions(-)

diff --git a/modules/colisprive/browser.py b/modules/colisprive/browser.py
index 8e645fa..4825a08 100644
--- a/modules/colisprive/browser.py
+++ b/modules/colisprive/browser.py
@@ -17,25 +17,14 @@
 # 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 weboob.deprecated.browser import Browser
+from weboob.browser import PagesBrowser, URL
 from .pages import TrackPage, ErrorPage
 
 
-__all__ = ['ColispriveBrowser']
+class ColispriveBrowser(PagesBrowser):
 
-
-class ColispriveBrowser(Browser):
-    PROTOCOL = 'https'
-    DOMAIN = 'www.colisprive.com'
-    ENCODING = 'utf8'
-
-    PAGES = {'https://www.colisprive.com/moncolis/pages/detailColis.aspx.*': TrackPage,
-             'https://www.colisprive.com/moncolis/Default.aspx.*': ErrorPage,
-             }
+    track_page = URL('https://www.colisprive.com/moncolis/pages/detailColis.aspx\?numColis=(?P<id>.+)', TrackPage)
+    error_page = URL('https://www.colisprive.fr', ErrorPage)
 
     def get_tracking_info(self, _id):
-        self.location('https://www.colisprive.com/moncolis/pages/detailColis.aspx?numColis=%s' % _id)
-        if not self.is_on_page(TrackPage):
-            return None
-
-        return self.page.get_info(_id)
+        return self.track_page.go(id=_id).get_info(_id)
diff --git a/modules/colisprive/module.py b/modules/colisprive/module.py
index f2e37aa..8247d2c 100644
--- a/modules/colisprive/module.py
+++ b/modules/colisprive/module.py
@@ -17,8 +17,8 @@
 # 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 weboob.capabilities.parcel import CapParcel
 from weboob.tools.backend import Module
+from weboob.capabilities.parcel import CapParcel
 
 from .browser import ColispriveBrowser
 
@@ -36,5 +36,4 @@ class ColispriveModule(Module, CapParcel):
     BROWSER = ColispriveBrowser
 
     def get_parcel_tracking(self, _id):
-        with self.browser:
-            return self.browser.get_tracking_info(_id)
+        return self.browser.get_tracking_info(_id)
diff --git a/modules/colisprive/pages.py b/modules/colisprive/pages.py
index b38ac11..92a6b57 100644
--- a/modules/colisprive/pages.py
+++ b/modules/colisprive/pages.py
@@ -18,8 +18,8 @@
 # along with weboob. If not, see <http://www.gnu.org/licenses/>.
 
 from datetime import date
-from weboob.deprecated.browser import Page
-from weboob.capabilities.parcel import Parcel, Event
+from weboob.browser.pages import HTMLPage
+from weboob.capabilities.parcel import Parcel, Event, ParcelNotFound
 
 
 def update_status(p, status):
@@ -27,28 +27,28 @@ def update_status(p, status):
         p.status = status
 
 
-class TrackPage(Page):
+class TrackPage(HTMLPage):
     def get_info(self, _id):
         p = Parcel(_id)
 
-        statustr = self.document.xpath('//tr[@class="bandeauText"]')[0]
-        status = self.parser.tocleanstring(statustr.xpath('td')[1])
+        statustr = self.doc.xpath('//tr[@class="bandeauText"]')[0]
+        status = statustr.xpath('td')[1].text
 
         p.info = status
         p.status = p.STATUS_UNKNOWN
 
         p.history = []
-        for i, tr in enumerate(self.document.xpath('//div[@class="mainbloc4Evt"]//tr')):
+        for i, tr in enumerate(self.doc.xpath('//table[@class="tableHistoriqueColis"]//tr[@class="bandeauText"]')):
             tds = tr.findall('td')
             try:
-                if tds[0].attrib['class'] != "titrestatutdate2":
+                if tds[0].attrib['class'] != "tdText":
                     continue
             except:
                 continue
 
             ev = Event(i)
             ev.location = None
-            ev.activity = self.parser.tocleanstring(tds[1])
+            ev.activity = tds[1].text
             if u"Votre colis a été expédié par votre webmarchand" in ev.activity:
                 update_status(p, p.STATUS_PLANNED)
             elif u"Votre colis est pris en charge par Colis Privé" in ev.activity:
@@ -59,12 +59,12 @@ class TrackPage(Page):
                 update_status(p, p.STATUS_IN_TRANSIT)
             elif u"Votre colis a été livré" in ev.activity:
                 update_status(p, p.STATUS_ARRIVED)
-            ev.date = date(*reversed([int(x) for x in self.parser.tocleanstring(tds[0]).split('/')]))
+            ev.date = date(*reversed([int(x) for x in tds[0].text.split('/')]))
             p.history.append(ev)
 
         try:
-            datelivre = self.document.xpath('//div[@class="NoInstNoRecla"]')
-            clean = self.parser.tocleanstring(datelivre[0])
+            datelivre = self.doc.xpath('//div[@class="NoInstNoRecla"]')
+            clean = datelivre[0].text
             if "Votre colis a déja été livré" in clean:
                 p.status = p.STATUS_ARRIVED
         except:
@@ -72,5 +72,6 @@ class TrackPage(Page):
         return p
 
 
-class ErrorPage(Page):
-    pass
+class ErrorPage(HTMLPage):
+    def get_info(self, _id):
+        raise ParcelNotFound("No such ID: %s" % _id)
-- 
1.7.10.4




More information about the weboob mailing list