├── .gitignore ├── tgv ├── Dockerfile ├── secret_template.json ├── Readme.md └── main.py /.gitignore: -------------------------------------------------------------------------------- 1 | secret.json 2 | -------------------------------------------------------------------------------- /tgv: -------------------------------------------------------------------------------- 1 | docker run -it \ 2 | -v $(pwd):/folder \ 3 | tgvmax "$@" 4 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:2.7.15 2 | 3 | WORKDIR app 4 | COPY . . 5 | 6 | ENTRYPOINT [ "python", "./main.py" ] 7 | -------------------------------------------------------------------------------- /secret_template.json: -------------------------------------------------------------------------------- 1 | { 2 | "SMS": 3 | { 4 | "user": "USERNAME", 5 | "password": "PASSWORD" 6 | }, 7 | "EMAIL": 8 | { 9 | "my_email": "YOUR GMAIL", 10 | "my_password" : "PASSWORD", 11 | "toaddrs" : "TO RECEVEIR" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # TGV MAX ALERT 2 | 3 | ### Usage : 4 | ``` 5 | $> git clone https://github.com/SegFault42/Tgv_Max_Alert 6 | $> cd Tgv_Max_Alert 7 | $> cp secret_template.json secret.json 8 | ``` 9 | Éditer le fichier secret Json avec vos informations. 10 | 11 | Si vous souhaitez être notifie par email remplir les champs email avec un compte mail dans "my email". 12 | 13 | Si vous souhaitez être notifie par SMS, remplir les champs SMS (fonctionne uniquement avec free mobile). 14 | 15 | [Configuration de la notification par SMS](https://www.freenews.fr/freenews-edition-nationale-299/free-mobile-170/nouvelle-option-notifications-par-sms-chez-free-mobile-14817) 16 | 17 | ``` 18 | -h, --help show this help message and exit 19 | --date DATE date format : YYYY-MM-DD 20 | --hour HOUR hour format : 11:18. Monitor between 11h00 to 18h00 21 | --origine ORIGINE train station origine 22 | --destination DESTINATION train station destination 23 | --alert ALERT SMS/EMAIL/NO 24 | 25 | ``` 26 | 27 | Exemple pour un trajet entre Paris et Marseille : 28 | 29 | ``` 30 | python main.py --date="2018-03-26" --hour="6:20" --origine="PARIS (intramuros)" --destination="MARSEILLE ST CHARLES" --alert="EMAIL" 31 | ``` 32 | 33 | [Lien avec tous les noms des différentes gares](https://ressources.data.sncf.com/explore/dataset/tgvmax/?sort=date) 34 | 35 | ### Docker build 36 | ``` 37 | docker build -t tgvmax . 38 | ``` 39 | 40 | ### Docker run 41 | ``` 42 | docker run -it \ 43 | -v $(pwd):/folder \ 44 | tgvmax "@" 45 | ``` 46 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import urllib, json 4 | import smtplib 5 | import argparse 6 | import datetime 7 | import sys 8 | import time 9 | 10 | def parse_arguments(): 11 | parser = argparse.ArgumentParser(description="TGV_Max_Alert") 12 | parser.add_argument('--date', type=str, required=True, help="date format : YYYY-MM-DD") 13 | parser.add_argument('--hour', type=str, required=True, help="hour format : 11:18. Monitor between 11h00 to 18h00") 14 | parser.add_argument('--origine', type=str, required=True, help="train station origine") 15 | parser.add_argument('--destination', type=str, required=True, help="train station destination") 16 | parser.add_argument('--alert', type=str, required=True, help="SMS/EMAIL/NO") 17 | parser.parse_args() 18 | args = parser.parse_args() 19 | return args 20 | 21 | def is_args_valid(args): 22 | try: 23 | datetime.datetime.strptime(args.date, '%Y-%m-%d') 24 | except ValueError: 25 | raise ValueError("\033[31mIncorrect data format, should be YYYY-MM-DD\033[0m") 26 | 27 | if (args.alert != "SMS" and args.alert != "EMAIL" and args.alert != "NO"): 28 | print ("\033[31mAlert bad formatted\033[0m") 29 | sys.exit(-1); 30 | 31 | hour = args.hour.split(':', 1) 32 | if (int(hour[0]) > 0 and int(hour[0]) < 24 and int(hour[1]) > 0 and int(hour[1]) < 60): 33 | return hour 34 | print ("\033[31mHour bad formatted\033[0m") 35 | sys.exit(-1) 36 | 37 | 38 | def prepare_url(args): 39 | url = "https://ressources.data.sncf.com/api/records/1.0/search/?dataset=tgvmax&sort=date&facet=date&facet=origine&facet=destination" 40 | url += "&refine.origine=" + args.origine 41 | url += "&refine.destination=" + args.destination 42 | url += "&refine.date=" + args.date 43 | return url 44 | 45 | def send_sms(message): 46 | credential = json.load(open("./secret.json")) 47 | print json.dumps(credential, indent=4) 48 | sms = "https://smsapi.free-mobile.fr/sendmsg?user=" 49 | sms += credential["SMS"]["user"] 50 | sms += "&pass=" 51 | sms += credential["SMS"]["password"] 52 | sms += "&msg=" 53 | sms += message 54 | urllib.urlopen(sms) 55 | 56 | def send_email(message): 57 | credential = json.load(open("./secret.json")) 58 | fromaddr = credential["EMAIL"]["my_email"] 59 | toaddrs = credential["EMAIL"]["toaddrs"] 60 | subject = "TGV MAX ALERT" 61 | 62 | msg = """From: %s\nTo: %s\nSubject: %s\n\n%s 63 | """ % (fromaddr, ", ".join(toaddrs), subject, message) 64 | 65 | server = smtplib.SMTP('smtp.gmail.com:587') 66 | server.starttls() 67 | server.login(credential["EMAIL"]["my_email"], credential["EMAIL"]["my_password"]) 68 | server.sendmail(fromaddr, toaddrs, msg) 69 | server.quit() 70 | 71 | 72 | def send_alert(data, args): 73 | message = "Train disponible " + data["fields"]["date"] + " !\n" +\ 74 | "Aller : " + data["fields"]["origine"] +\ 75 | "\nDepart a : " + data["fields"]["heure_depart"] +\ 76 | "\nRetour : " + data["fields"]["destination"] +\ 77 | "\nArrive a : " + data["fields"]["heure_arrivee"] 78 | print "\033[32m" + message + "\033[0m\n" 79 | if (args.alert == "SMS"): 80 | send_sms(message) 81 | elif (args.alert == "EMAIL"): 82 | send_email(message) 83 | 84 | def search_train(data, my_hour, args): 85 | alert = False 86 | nb_train = len(data["records"]) 87 | for i in range(0, nb_train): 88 | if (data["records"][i]["fields"]["od_happy_card"] == "OUI"): 89 | hour = data["records"][i]["fields"]["heure_depart"] 90 | hourIn = int(hour.split(':', 1)[0]) 91 | if (int(my_hour[0]) <= hourIn and int(my_hour[1]) >= hourIn): 92 | send_alert(data["records"][i], args) 93 | alert = True 94 | if (alert == True): 95 | return True 96 | return False 97 | 98 | def main(): 99 | args = parse_arguments() 100 | hour = is_args_valid(args) 101 | url = prepare_url(args) 102 | while (True): 103 | response = urllib.urlopen(url) 104 | data = json.loads(response.read()) 105 | if search_train(data, hour, args) == True: 106 | return (1) 107 | else: 108 | print "Aucun train disponible ..." 109 | time.sleep(60) 110 | 111 | if __name__ == '__main__': 112 | main() 113 | 114 | --------------------------------------------------------------------------------