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/>.
|
# 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 weboob.tools.backend import BaseBackend
|
||||||
|
|
||||||
from .browser import ChronopostBrowser
|
from .browser import ChronopostBrowser
|
||||||
|
|
@ -27,7 +27,7 @@ from .browser import ChronopostBrowser
|
||||||
__all__ = ['ChronopostBackend']
|
__all__ = ['ChronopostBackend']
|
||||||
|
|
||||||
|
|
||||||
class ChronopostBackend(BaseBackend, ICapPackageTracking):
|
class ChronopostBackend(BaseBackend, ICapParcel):
|
||||||
NAME = 'chronopost'
|
NAME = 'chronopost'
|
||||||
DESCRIPTION = u'Chronopost website'
|
DESCRIPTION = u'Chronopost website'
|
||||||
MAINTAINER = u'Romain Bignon'
|
MAINTAINER = u'Romain Bignon'
|
||||||
|
|
@ -36,6 +36,6 @@ class ChronopostBackend(BaseBackend, ICapPackageTracking):
|
||||||
|
|
||||||
BROWSER = ChronopostBrowser
|
BROWSER = ChronopostBrowser
|
||||||
|
|
||||||
def track_package(self, id):
|
def get_parcel_tracking(self, id):
|
||||||
with self.browser:
|
with self.browser:
|
||||||
return self.browser.get_tracking_info(id)
|
return self.browser.get_tracking_info(id)
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ class ChronopostBrowser(BaseBrowser):
|
||||||
|
|
||||||
PAGES = {
|
PAGES = {
|
||||||
'http://www.chronopost.fr/transport-express/livraison-colis': IndexPage,
|
'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):
|
def get_tracking_info(self, _id):
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
import re
|
import re
|
||||||
from dateutil.parser import parse as parse_date
|
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.capabilities import NotAvailable
|
||||||
from weboob.tools.browser import BasePage
|
from weboob.tools.browser import BasePage
|
||||||
|
|
||||||
|
|
@ -37,7 +37,10 @@ class IndexPage(BasePage):
|
||||||
|
|
||||||
class TrackPage(BasePage):
|
class TrackPage(BasePage):
|
||||||
def get_info(self, id):
|
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.arrival = NotAvailable
|
||||||
p.history = []
|
p.history = []
|
||||||
|
|
||||||
|
|
@ -46,14 +49,18 @@ class TrackPage(BasePage):
|
||||||
if len(tds) < 3:
|
if len(tds) < 3:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
loc = Location(i)
|
ev = Event(i)
|
||||||
loc.name = unicode(tds[1].text)
|
ev.location = unicode(tds[1].text)
|
||||||
loc.activity = unicode(tds[1].find('br').tail)
|
ev.activity = unicode(tds[1].find('br').tail)
|
||||||
if tds[-1].text is not None:
|
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('[a-z]+', '', self.parser.tocleanstring(tds[0])).strip()
|
||||||
date = re.sub('(\d+)/(\d+)/(\d+)', r'\3-\2-\1', date)
|
date = re.sub('(\d+)/(\d+)/(\d+)', r'\3-\2-\1', date)
|
||||||
loc.date = parse_date(date)
|
ev.date = parse_date(date)
|
||||||
p.history.append(loc)
|
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
|
return p
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
# 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 weboob.tools.backend import BaseBackend
|
||||||
|
|
||||||
from .browser import UpsBrowser
|
from .browser import UpsBrowser
|
||||||
|
|
@ -27,7 +27,7 @@ from .browser import UpsBrowser
|
||||||
__all__ = ['UpsBackend']
|
__all__ = ['UpsBackend']
|
||||||
|
|
||||||
|
|
||||||
class UpsBackend(BaseBackend, ICapPackageTracking):
|
class UpsBackend(BaseBackend, ICapParcel):
|
||||||
NAME = 'ups'
|
NAME = 'ups'
|
||||||
DESCRIPTION = u'UPS website'
|
DESCRIPTION = u'UPS website'
|
||||||
MAINTAINER = u'Romain Bignon'
|
MAINTAINER = u'Romain Bignon'
|
||||||
|
|
@ -36,6 +36,6 @@ class UpsBackend(BaseBackend, ICapPackageTracking):
|
||||||
|
|
||||||
BROWSER = UpsBrowser
|
BROWSER = UpsBrowser
|
||||||
|
|
||||||
def track_package(self, id):
|
def get_parcel_tracking(self, id):
|
||||||
with self.browser:
|
with self.browser:
|
||||||
return self.browser.get_tracking_info(id)
|
return self.browser.get_tracking_info(id)
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
import re
|
import re
|
||||||
from dateutil.parser import parse as parse_date
|
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
|
from weboob.tools.browser import BasePage
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -30,17 +30,26 @@ __all__ = ['TrackPage']
|
||||||
|
|
||||||
class TrackPage(BasePage):
|
class TrackPage(BasePage):
|
||||||
def get_info(self, id):
|
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'):
|
for dl in self.document.xpath('//dl'):
|
||||||
dt = dl.find('dt')
|
dt = dl.find('dt')
|
||||||
dd = dl.find('dd')
|
dd = dl.find('dd')
|
||||||
if dt is None or dd is None:
|
if dt is None or dd is None:
|
||||||
continue
|
continue
|
||||||
label = self.parser.tocleanstring(dt)
|
label = self.parser.tocleanstring(dt)
|
||||||
if label == 'Scheduled Delivery:' or label == 'Delivered On':
|
if label == 'Scheduled Delivery:':
|
||||||
m = re.search('(\d+/\d+/\d+)', dd.text)
|
p.status = p.STATUS_IN_TRANSIT
|
||||||
if m:
|
elif label == u'Delivered On:':
|
||||||
p.arrival = parse_date(m.group(1))
|
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 = []
|
p.history = []
|
||||||
for i, tr in enumerate(self.document.xpath('//table[@class="dataTable"]//tr')):
|
for i, tr in enumerate(self.document.xpath('//table[@class="dataTable"]//tr')):
|
||||||
|
|
@ -48,10 +57,12 @@ class TrackPage(BasePage):
|
||||||
if len(tds) < 4:
|
if len(tds) < 4:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
loc = Location(i)
|
ev = Event(i)
|
||||||
loc.name = self.parser.tocleanstring(tds[0])
|
ev.location = self.parser.tocleanstring(tds[0])
|
||||||
loc.activity = self.parser.tocleanstring(tds[-1])
|
ev.activity = self.parser.tocleanstring(tds[-1])
|
||||||
loc.date = parse_date('%s %s' % (tds[1].text, tds[2].text))
|
ev.date = parse_date('%s %s' % (tds[1].text, tds[2].text))
|
||||||
p.history.append(loc)
|
p.history.append(ev)
|
||||||
|
|
||||||
|
p.info = self.document.xpath('//a[@id="tt_spStatus"]')[0].text.strip()
|
||||||
|
|
||||||
return p
|
return p
|
||||||
|
|
|
||||||
|
|
@ -21,19 +21,34 @@
|
||||||
from .base import IBaseCap, CapBaseObject, Field, StringField, DateField
|
from .base import IBaseCap, CapBaseObject, Field, StringField, DateField
|
||||||
|
|
||||||
|
|
||||||
class Location(CapBaseObject):
|
class Event(CapBaseObject):
|
||||||
name = StringField('Location name')
|
|
||||||
date = DateField('Date')
|
date = DateField('Date')
|
||||||
activity = StringField('Activity')
|
activity = StringField('Activity')
|
||||||
|
location = StringField('Location')
|
||||||
|
|
||||||
def __repr__(self):
|
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')
|
arrival = DateField('Scheduled arrival date')
|
||||||
|
status = Field('Status of parcel', int, default=STATUS_UNKNOWN)
|
||||||
|
info = StringField('Information about parcel status')
|
||||||
history = Field('History', list)
|
history = Field('History', list)
|
||||||
|
|
||||||
|
|
||||||
class ICapPackageTracking(IBaseCap):
|
class ICapParcel(IBaseCap):
|
||||||
def track_package(self, id):
|
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()
|
raise NotImplementedError()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue