Created
April 13, 2018 22:27
-
-
Save jbdrvl/59bb6b56205edf4562d81d2e9d011080 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import urllib.request, json | |
| import sys | |
| import datetime | |
| args = sys.argv[1:] | |
| dest='' | |
| origine='' | |
| while len(args)!=0: | |
| if args[0] in ['-d', '--destination'] and dest=='': | |
| dest=args[1] | |
| args=args[2:] | |
| elif args[0] in ['-o', '--origine'] and origine=='': | |
| origine=args[1] | |
| args=args[2:] | |
| else: | |
| print('Usage:\n\t./tgvmax.py --destination CITY_1 --origine CITY_2\n\t./tgvmax.py -d CITY_1 -o CITY_2') | |
| exit() | |
| if dest=='': | |
| print('Missing destination!\nUsage:\n\t./tgvmax.py --destination CITY_1 --origine CITY_2\n\t./tgvmax.py -d CITY_1 -o CITY_2') | |
| exit() | |
| if origine=='': | |
| print('Missing origine!\nUsage:\n\t./tgvmax.py --destination CITY_1 --origine CITY_2\n\t./tgvmax.py -d CITY_1 -o CITY_2') | |
| exit() | |
| tmrw = datetime.date.today() + datetime.timedelta(days=1) | |
| with urllib.request.urlopen("https://ressources.data.sncf.com/api/records/1.0/search/?dataset=tgvmax&q=origine%3D{}+destination%3D{}+date%3D{}&sort=date&facet=date".format(origine, dest, tmrw)) as url: | |
| data = json.loads(url.read().decode()) | |
| if len(data['records'])==0: | |
| print('Did not find any TGV max from {} to {}'.format(origine, dest)) | |
| exit() | |
| print('Last TGVmax from tomorrow departing from {} and going to {}:'.format(data['records'][0]['fields']['origine'], data['records'][0]['fields']['destination'])) | |
| print('Train Nbr:', data['records'][0]['fields']['train_no']) | |
| print('Date:', data['records'][0]['fields']['date']) | |
| print('Heure Depart:', data['records'][0]['fields']['heure_depart']) | |
| print('Heure Arrivee:', data['records'][0]['fields']['heure_arrivee']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment