示例#1
0
def main(departure, arrival, next, transport, verbose):
    """ Search trips with Trainline and returns it in csv """

    # Get current datetime > from_date
    from_date_obj = datetime.now()

    # Decode duration (ex : 1day => timedelta(days=1))
    delta = _decode_next_param(next)

    # Calculate the end date > to_date
    to_date_obj = from_date_obj + delta

    # Convert the datetime objects to strings
    from_date = from_date_obj.strftime("%d/%m/%Y %H:%M")
    to_date = to_date_obj.strftime("%d/%m/%Y %H:%M")

    if transport == "any":
        transport = None

    if verbose:
        print()
        print("Search trips from {} to {}, between {} and {}\n".format(
            departure, arrival, from_date, to_date))

    results = trainline.search(departure_station=departure,
                               arrival_station=arrival,
                               from_date=from_date,
                               to_date=to_date,
                               transportation_mean=transport)

    print(results.csv())

    if verbose:
        print()
        print("{} results".format(len(results)))
示例#2
0
def test_search_3_passengers_and_bicyles():
    Pierre = Passenger(birthdate="01/01/1980")
    Sophie = Passenger(birthdate="01/02/1981")
    Enzo = Passenger(birthdate="01/03/2012", cards=[trainline.ENFANT_PLUS])

    from_date = "{} 08:00".format(_TOMORROW)
    to_date = "{} 19:00".format(_TOMORROW)
    departure_station = "Toulouse Matabiau"
    arrival_station = "Bordeaux St-Jean"

    results = trainline.search(passengers=[Pierre, Sophie, Enzo],
                               departure_station=departure_station,
                               arrival_station=arrival_station,
                               from_date=from_date,
                               to_date=to_date,
                               bicycle_with_or_without_reservation=True)

    print()
    print("Search trips for {} to {}, between {} and {}".format(
        departure_station, arrival_station, from_date, to_date))
    print("{} results".format(len(results)))
    assert len(results) > 0

    display_trips(results)

    csv_header = results.csv().split("\n")[0]
    assert csv_header == "departure_date;arrival_date;duration;\
number_of_segments;price;currency;transportation_mean;bicycle_reservation"

    # Check that the result trips starts at the proper date (tomorrow)
    first_result = results.csv().split("\n")[1]
    assert _TOMORROW in first_result.split(";")[0]

    last_result = results.csv().split("\n")[-2]
    assert _TOMORROW in last_result.split(";")[0]
def test_basic_search_with_bicyle_with_reservation():
    from_date = "{} 20:00".format(_TOMORROW)
    to_date = "{} 21:00".format(_TOMORROW)
    departure_station = "Toulouse Matabiau"
    arrival_station = "Bordeaux"

    results = trainline.search(departure_station=departure_station,
                               arrival_station=arrival_station,
                               from_date=from_date,
                               to_date=to_date,
                               bicycle_with_reservation_only=True)
    print()
    print("Search trips for {} to {}, between {} and {}".format(
        departure_station, arrival_station, from_date, to_date))
    print("{} results".format(len(results)))
    assert len(results) > 0

    display_trips(results)

    csv_header = results.csv().split("\n")[0]
    assert csv_header == "departure_date;arrival_date;duration;\
number_of_segments;price;currency;transportation_mean;bicycle_reservation"

    # Check that the result trips starts at the proper date (tomorrow)
    first_result = results.csv().split("\n")[1]
    assert _TOMORROW in first_result.split(";")[0]

    last_result = results.csv().split("\n")[-2]
    assert _TOMORROW in last_result.split(";")[0]
示例#4
0
    def prix_voyage(self):
        print(self.request.POST)
        resultats = trainline.search(departure_station=self.gare_depart,
                                     arrival_station=self.gare_arrivee,
                                     from_date="15/09/2019 08:00",
                                     to_date="15/09/2019 10:00")

        trajet = dict()
        liste_trajet_prix = list()

        resultats = resultats.csv().split("\n")
        for resultat in resultats:
            resultat = resultat.split(";")
            if resultat != ['']:
                horaire_depart = resultat[0]
                horaire_arrivee = resultat[1]
                temps_trajet = resultat[2]
                prix = resultat[4]
                mode = resultat[6]

                if horaire_depart != 'departure_date' and mode == 'train':
                    trajet['horaire'] = horaire_depart
                    trajet['prix'] = prix.replace(',', '.')
                    liste_trajet_prix.append(trajet)
                    trajet = dict()
        return liste_trajet_prix
示例#5
0
    def get_trainline(self, trip: Trip, bus=True, train=True):
        # for destination in destinations:
        self.logger.info("Finding connections for " + trip.destination)
        transport = None
        if bus and train:
            transport = None
        elif bus:
            transport = 'coach'
        elif train:
            transport = 'train'

        result = trainline.search(departure_station=trip.start_station,
                                  arrival_station=trip.destination,
                                  from_date=trip.start_date_str,
                                  to_date=trip.end_date_str,
                                  transportation_mean=transport)
        # results.append({destination: csv2dict(result.csv())})
        # res_array = result.csv().split('\n')[1:].split(';')
        # results.append(res_array)

        # print("{}: \n{}".format(destination, result.csv()))
        return {
            'Start': trip.start_station,
            'Destination': trip.destination,
            'Connections': self._csv2dict(result.csv())
        }
def find_train(dept_stat, arr_stat, from_date, to_date):
    results = trainline.search(departure_station=dept_stat,
                               arrival_station=arr_stat,
                               from_date=from_date,
                               to_date=to_date)
    x = results.csv()
    data = x.split("\n")
    data = [i.split(";") for i in data]
    data_obj.data = data
    x = ""
    for i in data:
        x = x + str(i) + '\n'
    return str(x)
示例#7
0
def test_basic_search_Paris():
    from_date = "{} 08:00".format(_TOMORROW)
    to_date = "{} 23:00".format(_TOMORROW)
    departure_station = "Toulouse Matabiau"
    arrival_station = "Paris"

    results = trainline.search(departure_station=departure_station,
                               arrival_station=arrival_station,
                               from_date=from_date,
                               to_date=to_date)
    print()
    print("Search trips for {} to {}, between {} and {}".format(
        departure_station, arrival_station, from_date, to_date))
    print("{} results".format(len(results)))
    assert len(results) > 0

    display_trips(results)
示例#8
0
def test_basic_search_with_bicyle_with_reservation():
    from_date = "{} 08:00".format(_TOMORROW)
    to_date = "{} 23:00".format(_TOMORROW)
    departure_station = "Toulouse Matabiau"
    arrival_station = "Bordeaux St-Jean"

    results = trainline.search(departure_station=departure_station,
                               arrival_station=arrival_station,
                               from_date=from_date,
                               to_date=to_date,
                               bicycle_with_reservation_only=True)
    print()
    print("Search trips for {} to {}, between {} and {}".format(
        departure_station, arrival_station, from_date, to_date))
    print("{} results".format(len(results)))
    assert len(results) > 0

    display_trips(results)
def test_basic_search_with_card():
    from_date = "{} 10:00".format(_TOMORROW)
    to_date = "{} 12:00".format(_TOMORROW)
    departure_station = "Toulouse Matabiau"
    arrival_station = "Bordeaux St-Jean"

    p1 = Passenger(birthdate="01/01/1980", cards=[trainline.AVANTAGE_FAMILLE])

    results = trainline.search(departure_station=departure_station,
                               arrival_station=arrival_station,
                               from_date=from_date,
                               to_date=to_date)
    print()
    print("Search trips for {} to {}, between {} and {}".format(
        departure_station, arrival_station, from_date, to_date))
    print("{} results".format(len(results)))
    assert len(results) > 0

    display_trips(results)
示例#10
0
def test_basic_search_with_bicyle_without_reservation():
    from_date = "{} 07:00".format(_TOMORROW)
    to_date = "{} 18:00".format(_TOMORROW)
    departure_station = "Capdenac"
    arrival_station = "Figeac"

    results = trainline.search(
        departure_station=departure_station,
        arrival_station=arrival_station,
        from_date=from_date,
        to_date=to_date,
        bicycle_without_reservation_only=True)
    print()
    print("Search trips for {} to {}, between {} and {}".format(
        departure_station, arrival_station, from_date, to_date))
    print("{} results".format(len(results)))
    assert len(results) > 0

    display_trips(results)
示例#11
0
def test_with_benerail():
    # Added this test to check that "benerail.default" comfort class
    # is handled properly ("options" field is missing in this case)
    # that was causing the issue #1
    from_date = "{} 08:00".format(_TOMORROW)
    to_date = "{} 23:00".format(_TOMORROW)
    departure_station = "Paris"
    arrival_station = "Antwerpen-Centraal"

    results = trainline.search(departure_station=departure_station,
                               arrival_station=arrival_station,
                               from_date=from_date,
                               to_date=to_date)
    print()
    print("Search trips for {} to {}, between {} and {}".format(
        departure_station, arrival_station, from_date, to_date))
    print("{} results".format(len(results)))
    assert len(results) > 0

    display_trips(results)
示例#12
0
def test_search_only_bus():
    from_date = "{} 09:00".format(_TOMORROW)
    to_date = "{} 15:00".format(_TOMORROW)
    departure_station = "Toulouse Matabiau"
    arrival_station = "Bordeaux St-Jean"

    results = trainline.search(departure_station=departure_station,
                               arrival_station=arrival_station,
                               from_date=from_date,
                               to_date=to_date,
                               transportation_mean="coach")
    print()
    print("Search BUS trips for {} to {}, between {} and {}".format(
        departure_station, arrival_station, from_date, to_date))
    print("{} results".format(len(results)))
    assert len(results) > 0

    display_trips(results)

    for folder in results:
        for trip in folder.trips:
            for segment in trip.segments:
                assert (segment.transportation_mean == "coach")
示例#13
0
# -*- coding: utf-8 -*-
# Code from Tducret (https://ww.tducret.com/) that parse trainline in CSV
# More informations on his project here : https://github.com/tducret/trainline-python

import trainline
import sys
import os
from dotenv import load_dotenv
load_dotenv()

if len(sys.argv) != 5:
    sys.exit(
        "usage: python3 main.py departure_station arrival_station from_date to_date\nexemple : python3 main.py Paris Nice '10/05/2019 08:00' '10/05/2019 21:00'"
    )

User = trainline.Passenger(birthdate=os.getenv("BIRTHDATE"))
User.add_special_card(trainline.TGVMAX, os.getenv("TGVMAX"))

results = trainline.search(passengers=[User],
                           departure_station=sys.argv[1],
                           arrival_station=sys.argv[2],
                           from_date=sys.argv[3],
                           to_date=sys.argv[4])

print(results.csv())
示例#14
0
import trainline
import sys

if len(sys.argv) != 7:
    sys.exit(
        "usage: python3 trainline_parser.py birthdate tgvmax_key departure_station arrival_station from_date to_date\nexemple : python3 main.py 04/09/1992 HC123456789 Paris Nice '10/05/2019 08:00' '10/05/2019 21:00'"
    )

birthdate = sys.argv[1]
tgvmax_key = sys.argv[2]
departure_station = sys.argv[3]
arrival_station = sys.argv[4]
from_date = sys.argv[5]
to_date = sys.argv[6]

User = trainline.Passenger(birthdate=birthdate)
User.add_special_card(trainline.TGVMAX, tgvmax_key)
results = trainline.search(
    passengers=[User],
    departure_station=departure_station,
    arrival_station=arrival_station,
    from_date=from_date,
    to_date=to_date,
    # max_price=1.0,
    transportation_mean='train')
print(results.csv())