[blablacar] New CapTravel module that helps to find available trips on blablacar website

This commit is contained in:
Bezleputh 2015-09-04 15:42:49 +02:00 committed by Florent
commit 2e8f5023eb
5 changed files with 222 additions and 0 deletions

View file

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2015 Bezleputh
#
# 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 .module import BlablacarModule
__all__ = ['BlablacarModule']

View file

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2015 Bezleputh
#
# 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 weboob.browser import PagesBrowser, URL
from .pages import DeparturesPage
from datetime import datetime
import urllib
class BlablacarBrowser(PagesBrowser):
BASEURL = 'https://www.blablacar.fr'
departures = URL('/search_xhr\?(?P<qry>.*)', DeparturesPage)
def get_roadmap(self, departure, arrival, filters):
pass
def get_station_departures(self, station_id, arrival_id, date):
query = {'fn': station_id}
if arrival_id:
query['tn'] = arrival_id
if date:
_date = datetime.strftime(date, "%d/%m/%Y")
query['db'] = _date
_heure = datetime.strftime(date, "%H")
query['hb'] = _heure
query['he'] = '24'
return self.departures.open(qry=urllib.urlencode(query)).get_station_departures()

View file

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2015 Bezleputh
#
# 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 weboob.tools.backend import Module
from weboob.capabilities.travel import CapTravel
from .browser import BlablacarBrowser
__all__ = ['BlablacarModule']
class BlablacarModule(Module, CapTravel):
NAME = 'blablacar'
DESCRIPTION = u'blablacar website'
MAINTAINER = u'Bezleputh'
EMAIL = 'carton_ben@yahoo.fr'
LICENSE = 'AGPLv3+'
VERSION = '1.1'
BROWSER = BlablacarBrowser
def iter_roadmap(self, departure, arrival, filters):
return self.browser.get_roadmap(departure, arrival, filters)
def iter_station_departures(self, station_id, arrival_id, date):
return self.browser.get_station_departures(station_id, arrival_id, date)

View file

@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2015 Bezleputh
#
# 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 weboob.capabilities.travel import Departure
from weboob.browser.filters.standard import CleanText, Format, Regexp, CleanDecimal
from weboob.browser.filters.html import Link
from weboob.browser.elements import ListElement, ItemElement, method
from weboob.browser.pages import JsonPage
from weboob.capabilities.base import Currency
from io import StringIO
import lxml.html as html
from datetime import datetime
class DeparturesPage(JsonPage):
ENCODING = None
def __init__(self, browser, response, *args, **kwargs):
super(DeparturesPage, self).__init__(browser, response, *args, **kwargs)
self.encoding = self.ENCODING or response.encoding
parser = html.HTMLParser(encoding=self.encoding)
if 'results' in self.doc['html']:
self.doc = html.parse(StringIO(self.doc['html']['results']), parser)
else:
self.doc = html.Element('brinbrin')
@method
class get_station_departures(ListElement):
item_xpath = '//ul[@class="trip-search-results"]/li/a'
class item(ItemElement):
klass = Departure
obj_id = Regexp(Link('.'), '/(.*)')
def obj_time(self):
_date = CleanText('./article/div/h3[@itemprop="startDate"]/@content')(self).split('-')
_time = Regexp(CleanText('./article/div/h3[@itemprop="startDate"]'),
u'.* à (\d+)h')(self)
return datetime(int(_date[0]), int(_date[1]), int(_date[2]), int(_time), 0)
obj_type = Format('%s (%s)',
CleanText('./article/div/dl[@class="car-type"]/dt/strong'),
CleanText('./article/div/dl[@class="car-type"]/dd/span/@title'))
obj_departure_station = CleanText('./article/div/dl[@class="geo-from"]/dd',
replace=[(": voir avec le conducteur", "")])
obj_arrival_station = CleanText('./article/div/dl[@class="geo-to"]/dd',
replace=[(": voir avec le conducteur", "")])
obj_price = CleanDecimal(CleanText('./article/div/div[@itemprop="location"]/strong/span[last()]'))
def obj_currency(self):
txt = CleanText('./article/div/div[@itemprop="location"]')(self)
return Currency.get_currency(txt)
obj_information = CleanText('./article/div/div[@class="availability"]')

30
modules/blablacar/test.py Normal file
View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2015 Bezleputh
#
# 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 weboob.tools.test import BackendTest
from datetime import datetime
class BlablacarTest(BackendTest):
MODULE = 'blablacar'
def test_blablacar(self):
departures = list(self.backend.iter_station_departures('lille', 'lens', datetime.now()))
self.assertTrue(len(departures) > 0)