Skip to content

tducret/trainline-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e4f88ff Β· Oct 23, 2020

History

75 Commits
Aug 18, 2018
Oct 23, 2020
Oct 19, 2020
Jun 24, 2019
Aug 18, 2018
Jan 1, 2019
Aug 18, 2018
Aug 18, 2018
Jul 5, 2019
Jun 1, 2019
Sep 2, 2018
Jul 6, 2019
Jan 1, 2019
Aug 18, 2018
Oct 23, 2020
Oct 15, 2020
Jan 1, 2019
Sep 5, 2018

Repository files navigation

Trainline

Travis Coveralls github PyPI Docker Image size License

Description

Non-official Python wrapper and CLI tool for Trainline

I wrote a French blog post about it here

🎁 I added a tiny Docker image to use the tool very easily

Requirements

  • Python 3
  • pip3

Installation

pip3 install -U trainline

CLI tool usage

trainline_cli.py --help

Examples :

trainline_cli.py --departure="Toulouse" --arrival="Bordeaux" --next=12hours
trainline_cli.py --departure="Paris" --arrival="Marseille" --next=1day

or the shorter call :

trainline_cli.py -d Toulouse -a Bordeaux -n 12h
trainline_cli.py -d Paris -a Marseille -n 1d

Example output :

departure_date;arrival_date;duration;number_of_segments;price;currency;transportation_mean;bicycle_reservation
15/10/2018 08:19;15/10/2018 10:26;02h07;1;36,0;EUR;train;30,0
15/10/2018 08:19;15/10/2018 10:26;02h07;1;37,5;EUR;train;30,0
15/10/2018 08:19;15/10/2018 10:26;02h07;1;95,5;EUR;train;30,0
[...]

You can then open it with your favorite spreadsheet editor (and play with the filters) :

snapshot trainline_cli.py output in Excel

Package usage

# -*- coding: utf-8 -*-
import trainline

results = trainline.search(
	departure_station="Toulouse",
	arrival_station="Bordeaux",
	from_date="15/10/2018 08:00",
	to_date="15/10/2018 21:00")

print(results.csv())

Example output :

departure_date;arrival_date;duration;number_of_segments;price;currency;transportation_mean;bicycle_reservation
15/10/2018 08:00;15/10/2018 10:55;02h55;1;5,0;EUR;coach;unavailable
15/10/2018 08:00;15/10/2018 10:50;02h50;1;4,99;EUR;coach;unavailable
15/10/2018 08:19;15/10/2018 10:26;02h07;1;20,5;EUR;train;10,0
[...]
# -*- coding: utf-8 -*-
import trainline

Pierre = trainline.Passenger(birthdate="01/01/1980", cards=[trainline.AVANTAGE_FAMILLE])
Sophie = trainline.Passenger(birthdate="01/02/1981")
Enzo = trainline.Passenger(birthdate="01/03/2012")
Nicolas = trainline.Passenger(birthdate="01/01/1996", cards=[trainline.AVANTAGE_JEUNE])
Nicolas.add_special_card(trainline.TGVMAX, "YourCardNumber")

results = trainline.search(
	passengers=[Pierre, Sophie, Enzo, Nicolas],
	departure_station="Toulouse",
	arrival_station="Bordeaux",
	from_date="15/10/2018 08:00",
	to_date="15/10/2018 21:00",
	bicycle_with_or_without_reservation=True)

print(results.csv())

Example output :

departure_date;arrival_date;duration;number_of_segments;price;currency;transportation_mean;bicycle_reservation
15/10/2018 08:19;15/10/2018 10:26;02h07;1;36,0;EUR;train;30,0
15/10/2018 08:19;15/10/2018 10:26;02h07;1;37,5;EUR;train;30,0
15/10/2018 08:19;15/10/2018 10:26;02h07;1;95,5;EUR;train;30,0
[...]

Docker

You can use the trainline tool with the Docker image

You may execute :

docker run -it --rm thibdct/trainline --departure="Toulouse" --arrival="Bordeaux" --next=12hours

The Docker image is built on top of Google Distroless image, so it is tiny :)

🀘 The easy way 🀘

I also built a bash wrapper to execute the Docker container easily.

Install it with :

curl -s https://raw.githubusercontent.com/tducret/trainline-python/master/trainline.sh \
> /usr/local/bin/trainline && chmod +x /usr/local/bin/trainline

You may replace /usr/local/bin with another folder that is in your $PATH

Check that it works :

trainline --help
trainline --departure="Toulouse" --arrival="Bordeaux" --next=12hours

You can upgrade the app with :

trainline --upgrade

and even uninstall with :

trainline --uninstall

TODO

  • Create a sort function in Folders class (to get the cheapest trips first for example)
  • Add filter for class (first, second), for max_duration
  • Implement get_station_id
  • Implement the use of passengers during search
  • Calculate total price with bicycle reservation if search 'with_bicyle' (and export it in csv)
  • Calculate total price for all the passengers (and export it in csv) => may need to create a class for Folder
  • Create the CLI tool and update README