-
Notifications
You must be signed in to change notification settings - Fork 12
/
trainline_cli.py
104 lines (88 loc) · 2.6 KB
/
trainline_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""CLI tool for trainline."""
import click
import trainline
from datetime import datetime, timedelta
# Usage : trainline_cli.py --help
@click.command()
@click.option(
'--departure', '-d',
envvar="PARAM1",
type=str,
help='departure station (example : Toulouse)',
required=True,
)
@click.option(
'--arrival', '-a',
type=str,
help='arrival station (example : Bordeaux)',
required=True,
)
@click.option(
'--next', '-n',
type=str,
help='period of search from now \
(example : 1day, 2days, 3d, 1hour, 2hours, 3h)',
default='3hours',
show_default=True,
)
@click.option(
'--transport', '-t',
type=click.Choice(['train', 'coach', 'any']),
help='get only results for the selected transportation mean',
default='train',
show_default=True,
)
@click.option(
'--verbose', '-v',
is_flag=True,
help='verbose mode',
)
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)))
def _decode_next_param(next_param):
""" From a 'next' string, returns a timedelta object
>>> print(_decode_next_param("1day"))
1 day, 0:00:00
>>> print(_decode_next_param("2d"))
2 days, 0:00:00
>>> print(_decode_next_param("3hours"))
3:00:00
>>> print(_decode_next_param("4h"))
4:00:00
"""
if "d" in next_param:
delta = timedelta(days=int(next_param.split("d")[0]))
elif "h" in next_param:
delta = timedelta(hours=int(next_param.split("h")[0]))
else:
delta = timedelta(hours=3)
return delta
if __name__ == "__main__":
main()