pax_global_header 0000666 0000000 0000000 00000000064 13436457030 0014517 g ustar 00root root 0000000 0000000 52 comment=3863a14eedf17551743a8bbccf0237874d7f4a16
woob-3863a14eedf17551743a8bbccf0237874d7f4a16-modules-trainline/ 0000775 0000000 0000000 00000000000 13436457030 0023043 5 ustar 00root root 0000000 0000000 woob-3863a14eedf17551743a8bbccf0237874d7f4a16-modules-trainline/modules/ 0000775 0000000 0000000 00000000000 13436457030 0024513 5 ustar 00root root 0000000 0000000 woob-3863a14eedf17551743a8bbccf0237874d7f4a16-modules-trainline/modules/trainline/ 0000775 0000000 0000000 00000000000 13436457030 0026500 5 ustar 00root root 0000000 0000000 woob-3863a14eedf17551743a8bbccf0237874d7f4a16-modules-trainline/modules/trainline/__init__.py 0000664 0000000 0000000 00000001523 13436457030 0030612 0 ustar 00root root 0000000 0000000 # -*- coding: utf-8 -*-
# Copyright(C) 2016 Edouard Lambert
#
# This file is part of a weboob module.
#
# This weboob module 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.
#
# This weboob module 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 this weboob module. If not, see .
from .module import TrainlineModule
__all__ = ['TrainlineModule']
woob-3863a14eedf17551743a8bbccf0237874d7f4a16-modules-trainline/modules/trainline/browser.py 0000664 0000000 0000000 00000007447 13436457030 0030551 0 ustar 00root root 0000000 0000000 # -*- coding: utf-8 -*-
# Copyright(C) 2016 Edouard Lambert
#
# This file is part of a weboob module.
#
# This weboob module 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.
#
# This weboob module 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 this weboob module. If not, see .
from datetime import datetime
from dateutil.relativedelta import relativedelta
from weboob.browser.browsers import APIBrowser
from weboob.exceptions import BrowserIncorrectPassword
from weboob.browser.filters.standard import CleanDecimal, Date
from weboob.browser.exceptions import ClientError
from weboob.capabilities.bill import DocumentTypes, Bill, Subscription
class TrainlineBrowser(APIBrowser):
BASEURL = 'https://www.trainline.fr/api/v5/'
def __init__(self, email, password, *args, **kwargs):
super(TrainlineBrowser, self).__init__(*args, **kwargs)
self.session.headers['X-Requested-With'] = 'XMLHttpRequest'
try:
me = self.request('account/signin', data={'email': email, 'password': password})
except ClientError:
raise BrowserIncorrectPassword
self.session.headers['Authorization'] = 'Token token="%s"' % me['meta']['token']
def get_subscription_list(self):
me = self.request('user')['user']
sub = Subscription()
sub.subscriber = '%s %s' % (me['first_name'], me['last_name'])
sub.id = me['id']
sub.label = me['email']
yield sub
def iter_documents(self, subscription):
docs, docs_len, check, month_back, date = list(), -1, 0, 6, None
# First request is known
bills = self.request('pnrs')
while check < month_back:
# If not first
if docs_len > -1 and date:
if check > 0:
# If nothing, we try 4 weeks back
date = (datetime.strptime(date, '%Y-%m-%d') - relativedelta(weeks=4)).strftime('%Y-%m-%d')
else:
# Add 8 weeks to last date to be sure to get all
date = (datetime.combine(date, datetime.min.time()) + relativedelta(weeks=8)).strftime('%Y-%m-%d')
bills = self.request('pnrs?date=%s' % date)
docs_len = len(docs)
for proof, pnr, trip in zip(bills['proofs'], bills['pnrs'], bills['trips']):
# Check if not already in docs list
for doc in docs:
if vars(doc)['id'].split('_', 1)[1] == pnr['id']:
break
else:
b = Bill()
b.id = '%s_%s' % (subscription.id, pnr['id'])
b._url = proof['url']
b.date = Date().filter(proof['created_at'])
b.format = u"pdf"
b.label = u'Trajet du %s' % Date().filter(trip['departure_date'])
b.type = DocumentTypes.BILL
b.vat = CleanDecimal().filter('0')
if pnr['cents']:
b.price = CleanDecimal().filter(format(pnr['cents']/float(100), '.2f'))
b.currency = pnr['currency']
docs.append(b)
check += 1
# If a new bill is found, we reset check
if docs_len < len(docs):
date = b.date
check = 0
return iter(docs)
woob-3863a14eedf17551743a8bbccf0237874d7f4a16-modules-trainline/modules/trainline/module.py 0000664 0000000 0000000 00000005207 13436457030 0030343 0 ustar 00root root 0000000 0000000 # -*- coding: utf-8 -*-
# Copyright(C) 2016 Edouard Lambert
#
# This file is part of a weboob module.
#
# This weboob module 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.
#
# This weboob module 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 this weboob module. If not, see .
from weboob.capabilities.bill import DocumentTypes, CapDocument, Subscription, Document, SubscriptionNotFound, DocumentNotFound
from weboob.capabilities.base import find_object, NotAvailable
from weboob.tools.backend import Module, BackendConfig
from weboob.tools.value import ValueBackendPassword, Value
from .browser import TrainlineBrowser
__all__ = ['TrainlineModule']
class TrainlineModule(Module, CapDocument):
NAME = 'trainline'
DESCRIPTION = u'trainline website'
MAINTAINER = u'Edouard Lambert'
EMAIL = 'elambert@budget-insight.com'
LICENSE = 'AGPLv3+'
VERSION = '1.6'
CONFIG = BackendConfig(Value('login', label='Adresse email'),
ValueBackendPassword('password', label='Mot de passe'))
BROWSER = TrainlineBrowser
accepted_document_types = (DocumentTypes.BILL,)
def create_default_browser(self):
return self.create_browser(self.config['login'].get(), self.config['password'].get())
def iter_subscription(self):
return self.browser.get_subscription_list()
def get_subscription(self, _id):
return find_object(self.iter_subscription(), id=_id, error=SubscriptionNotFound)
def get_document(self, _id):
subid = _id.rsplit('_', 1)[0]
subscription = self.get_subscription(subid)
return find_object(self.iter_documents(subscription), id=_id, error=DocumentNotFound)
def iter_documents(self, subscription):
if not isinstance(subscription, Subscription):
subscription = self.get_subscription(subscription)
return self.browser.iter_documents(subscription)
def download_document(self, document):
if not isinstance(document, Document):
document = self.get_document(document)
if document._url is NotAvailable:
return
return self.browser.open(document._url, headers={'Authorization': ''}).content
woob-3863a14eedf17551743a8bbccf0237874d7f4a16-modules-trainline/modules/trainline/test.py 0000664 0000000 0000000 00000001671 13436457030 0030036 0 ustar 00root root 0000000 0000000 # -*- coding: utf-8 -*-
# Copyright(C) 2016 Edouard Lambert
#
# This file is part of a weboob module.
#
# This weboob module 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.
#
# This weboob module 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 this weboob module. If not, see .
from weboob.tools.test import BackendTest
class TrainlineTest(BackendTest):
MODULE = 'trainline'
def test_trainline(self):
raise NotImplementedError()