reworking of the ICapParcel API (new name of ICapPackageTracking)
This commit is contained in:
parent
bdf4c13f4e
commit
5c95363ac1
6 changed files with 65 additions and 32 deletions
|
|
@ -18,7 +18,7 @@
|
|||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from weboob.capabilities.tracking import ICapPackageTracking
|
||||
from weboob.capabilities.parcel import ICapParcel
|
||||
from weboob.tools.backend import BaseBackend
|
||||
|
||||
from .browser import ChronopostBrowser
|
||||
|
|
@ -27,7 +27,7 @@ from .browser import ChronopostBrowser
|
|||
__all__ = ['ChronopostBackend']
|
||||
|
||||
|
||||
class ChronopostBackend(BaseBackend, ICapPackageTracking):
|
||||
class ChronopostBackend(BaseBackend, ICapParcel):
|
||||
NAME = 'chronopost'
|
||||
DESCRIPTION = u'Chronopost website'
|
||||
MAINTAINER = u'Romain Bignon'
|
||||
|
|
@ -36,6 +36,6 @@ class ChronopostBackend(BaseBackend, ICapPackageTracking):
|
|||
|
||||
BROWSER = ChronopostBrowser
|
||||
|
||||
def track_package(self, id):
|
||||
def get_parcel_tracking(self, id):
|
||||
with self.browser:
|
||||
return self.browser.get_tracking_info(id)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class ChronopostBrowser(BaseBrowser):
|
|||
|
||||
PAGES = {
|
||||
'http://www.chronopost.fr/transport-express/livraison-colis': IndexPage,
|
||||
'http://www.chronopost.fr/transport-express/livraison-colis/cache/offonce/accueil/suivi.*': TrackPage,
|
||||
'http://www.chronopost.fr/transport-express/livraison-colis/.*accueil/suivi.*': TrackPage,
|
||||
}
|
||||
|
||||
def get_tracking_info(self, _id):
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
import re
|
||||
from dateutil.parser import parse as parse_date
|
||||
|
||||
from weboob.capabilities.tracking import Package, Location
|
||||
from weboob.capabilities.parcel import Parcel, Event
|
||||
from weboob.capabilities import NotAvailable
|
||||
from weboob.tools.browser import BasePage
|
||||
|
||||
|
|
@ -37,7 +37,10 @@ class IndexPage(BasePage):
|
|||
|
||||
class TrackPage(BasePage):
|
||||
def get_info(self, id):
|
||||
p = Package(id)
|
||||
if len(self.document.xpath('//libelle[@nom="MSG_AUCUN_EVT"]')) > 0:
|
||||
return None
|
||||
|
||||
p = Parcel(id)
|
||||
p.arrival = NotAvailable
|
||||
p.history = []
|
||||
|
||||
|
|
@ -46,14 +49,18 @@ class TrackPage(BasePage):
|
|||
if len(tds) < 3:
|
||||
continue
|
||||
|
||||
loc = Location(i)
|
||||
loc.name = unicode(tds[1].text)
|
||||
loc.activity = unicode(tds[1].find('br').tail)
|
||||
ev = Event(i)
|
||||
ev.location = unicode(tds[1].text)
|
||||
ev.activity = unicode(tds[1].find('br').tail)
|
||||
if tds[-1].text is not None:
|
||||
loc.activity += ', ' + self.parser.tocleanstring(tds[-1])
|
||||
ev.activity += ', ' + self.parser.tocleanstring(tds[-1])
|
||||
date = re.sub('[a-z]+', '', self.parser.tocleanstring(tds[0])).strip()
|
||||
date = re.sub('(\d+)/(\d+)/(\d+)', r'\3-\2-\1', date)
|
||||
loc.date = parse_date(date)
|
||||
p.history.append(loc)
|
||||
ev.date = parse_date(date)
|
||||
p.history.append(ev)
|
||||
|
||||
p.info = ' '.join([t.strip() for t in self.document.xpath('//div[@class="numeroColi2"]')[0].itertext()][1:])
|
||||
if u'Livraison effectuée' in p.history[0].activity:
|
||||
p.status = p.STATUS_ARRIVED
|
||||
|
||||
return p
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@
|
|||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from weboob.capabilities.tracking import ICapPackageTracking
|
||||
from weboob.capabilities.parcel import ICapParcel
|
||||
from weboob.tools.backend import BaseBackend
|
||||
|
||||
from .browser import UpsBrowser
|
||||
|
|
@ -27,7 +27,7 @@ from .browser import UpsBrowser
|
|||
__all__ = ['UpsBackend']
|
||||
|
||||
|
||||
class UpsBackend(BaseBackend, ICapPackageTracking):
|
||||
class UpsBackend(BaseBackend, ICapParcel):
|
||||
NAME = 'ups'
|
||||
DESCRIPTION = u'UPS website'
|
||||
MAINTAINER = u'Romain Bignon'
|
||||
|
|
@ -36,6 +36,6 @@ class UpsBackend(BaseBackend, ICapPackageTracking):
|
|||
|
||||
BROWSER = UpsBrowser
|
||||
|
||||
def track_package(self, id):
|
||||
def get_parcel_tracking(self, id):
|
||||
with self.browser:
|
||||
return self.browser.get_tracking_info(id)
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
import re
|
||||
from dateutil.parser import parse as parse_date
|
||||
|
||||
from weboob.capabilities.tracking import Package, Location
|
||||
from weboob.capabilities.parcel import Parcel, Event
|
||||
from weboob.tools.browser import BasePage
|
||||
|
||||
|
||||
|
|
@ -30,17 +30,26 @@ __all__ = ['TrackPage']
|
|||
|
||||
class TrackPage(BasePage):
|
||||
def get_info(self, id):
|
||||
p = Package(id)
|
||||
if len(self.parser.tocleanstring(self.document.xpath('//p[@class="error"]')[0])) > 0:
|
||||
return None
|
||||
|
||||
p = Parcel(id)
|
||||
for dl in self.document.xpath('//dl'):
|
||||
dt = dl.find('dt')
|
||||
dd = dl.find('dd')
|
||||
if dt is None or dd is None:
|
||||
continue
|
||||
label = self.parser.tocleanstring(dt)
|
||||
if label == 'Scheduled Delivery:' or label == 'Delivered On':
|
||||
m = re.search('(\d+/\d+/\d+)', dd.text)
|
||||
if m:
|
||||
p.arrival = parse_date(m.group(1))
|
||||
if label == 'Scheduled Delivery:':
|
||||
p.status = p.STATUS_IN_TRANSIT
|
||||
elif label == u'Delivered On:':
|
||||
p.status = p.STATUS_ARRIVED
|
||||
else:
|
||||
continue
|
||||
|
||||
m = re.search('(\d+/\d+/\d+)', dd.text)
|
||||
if m:
|
||||
p.arrival = parse_date(m.group(1))
|
||||
|
||||
p.history = []
|
||||
for i, tr in enumerate(self.document.xpath('//table[@class="dataTable"]//tr')):
|
||||
|
|
@ -48,10 +57,12 @@ class TrackPage(BasePage):
|
|||
if len(tds) < 4:
|
||||
continue
|
||||
|
||||
loc = Location(i)
|
||||
loc.name = self.parser.tocleanstring(tds[0])
|
||||
loc.activity = self.parser.tocleanstring(tds[-1])
|
||||
loc.date = parse_date('%s %s' % (tds[1].text, tds[2].text))
|
||||
p.history.append(loc)
|
||||
ev = Event(i)
|
||||
ev.location = self.parser.tocleanstring(tds[0])
|
||||
ev.activity = self.parser.tocleanstring(tds[-1])
|
||||
ev.date = parse_date('%s %s' % (tds[1].text, tds[2].text))
|
||||
p.history.append(ev)
|
||||
|
||||
p.info = self.document.xpath('//a[@id="tt_spStatus"]')[0].text.strip()
|
||||
|
||||
return p
|
||||
|
|
|
|||
|
|
@ -21,19 +21,34 @@
|
|||
from .base import IBaseCap, CapBaseObject, Field, StringField, DateField
|
||||
|
||||
|
||||
class Location(CapBaseObject):
|
||||
name = StringField('Location name')
|
||||
class Event(CapBaseObject):
|
||||
date = DateField('Date')
|
||||
activity = StringField('Activity')
|
||||
location = StringField('Location')
|
||||
|
||||
def __repr__(self):
|
||||
return u'<Location name=%r date=%r activity=%r>' % (self.name, self.date, self.activity)
|
||||
return u'<Event date=%r activity=%r location=%r>' % (self.date, self.activity, self.location)
|
||||
|
||||
class Parcel(CapBaseObject):
|
||||
STATUS_UNKNOWN = 0
|
||||
STATUS_PLANNED = 1
|
||||
STATUS_IN_TRANSIT = 2
|
||||
STATUS_ARRIVED = 3
|
||||
|
||||
class Package(CapBaseObject):
|
||||
arrival = DateField('Scheduled arrival date')
|
||||
status = Field('Status of parcel', int, default=STATUS_UNKNOWN)
|
||||
info = StringField('Information about parcel status')
|
||||
history = Field('History', list)
|
||||
|
||||
|
||||
class ICapPackageTracking(IBaseCap):
|
||||
def track_package(self, id):
|
||||
class ICapParcel(IBaseCap):
|
||||
def get_parcel_tracking(self, id):
|
||||
"""
|
||||
Get information abouut a parcel.
|
||||
|
||||
:param id: ID of the parcel
|
||||
:type id: :class:`str`
|
||||
:rtype: :class:`Parcel`
|
||||
"""
|
||||
|
||||
raise NotImplementedError()
|
||||
Loading…
Add table
Add a link
Reference in a new issue