This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
retards.py
executable file
·127 lines (111 loc) · 5.26 KB
/
retards.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python
# coding: utf8
import json, urllib.request, base64, argparse, time, re
from datetime import datetime, timedelta
from libsncf.Delay import delay
from libsncf.DisruptionFactory import disruptionFactory
from libsncf.Tools import tools
from libsncf.Stats import stats
from Settings import settings
try:
from outputs.GoogleSheet import googleSheet
google_sheet_not_available = False
except ImportError:
google_sheet_not_available = True
try:
from outputs.Twitter import twitter
twitter_not_available = False
except ImportError:
twitter_not_available = True
try:
from outputs.MySQL import mysql
mysql_not_available = False
except ImportError:
mysql_not_available = True
if __name__ == '__main__':
#Deal with command line arguments
parser = argparse.ArgumentParser(description='Programme permettant de récupérer les retards pour un numéro de train')
parser.add_argument('num_train', action="store", type=int, help="Numéro du train à récupérer")
parser.add_argument('--config', action="store", type=str, default="retards.cfg", help="Emplacement du fichier de configuration")
parser.add_argument('--show-stats', action="store_true", default=False, dest="show_stats", help="Afficher des statistiques pour ce train. Cette option requiert MySQL")
parser.add_argument('--weekly', action="store_true", default=False, dest="weekly_stats", help="Afficher des statistiques hebdomadaires pour ce train.")
parser.add_argument('--monthly', action="store_true", default=False, dest="monthly_stats", help="Afficher des statistiques mensuelles pour ce train.")
parser.add_argument('--no-google-sheets', action="store_true", default=False, dest="no_google_sheet", help="Ne pas envoyer les données sur Google Sheets")
parser.add_argument('--no-twitter', action="store_true", default=False, dest="no_twitter", help="Ne pas envoyer les données sur Twitter")
parser.add_argument('--no-mysql', action="store_true", default=False, dest="no_mysql", help="Ne pas envoyer les données sur MySQL")
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
results = parser.parse_args()
num_train = results.num_train
#Load configuration
settings = settings(results.config)
#Create "tool" object to interact with SNCF API
object_tools = tools(settings)
#If stats must be displayed
if(results.show_stats and not mysql_not_available):
mysql_service = mysql(settings, object_tools)
if results.monthly_stats:
stats_object = stats("MONTHLY", num_train)
else:
stats_object = stats("WEEKLY", num_train)
stats_object = mysql_service.populate_stats(stats_object)
print(str(stats_object))
if(not results.no_twitter and not twitter_not_available):
#Instanciate a Twitter object with settings
twitter_service = twitter(settings)
twitter_service.post_stats(stats_object)
mysql_service.close_connection()
exit(0)
#Get disruptions for this train
response = object_tools.get_disruptions(num_train)
#Create the appropriate event for the disruption
factory = disruptionFactory(response,num_train,object_tools)
#Get this event
event = factory.get_event()
#There are two ways used to browse disruptions. If the first doesn't find the disruption,
#we retrieve disruptions using the second way, just to be sure to not miss anything...
if event is None:
#Get disruptions for this train
response = object_tools.get_disruptions(num_train,True)
#Create the appropriate event for the disruption
factory = disruptionFactory(response,num_train,object_tools)
#Get this event
event = factory.get_event()
mysql_service = None
if event is None:
print("Aucun retard pour ce train")
#If MySQL is not canceled by the user, and the import of required modules succeeded
if(not results.no_mysql and not mysql_not_available):
#Instanciate a MySQL object with settings
mysql_service = mysql(settings, object_tools)
mysql_service.save_normal_trip(num_train)
mysql_service.close_connection()
exit(0)
elif type(event) is delay:
#Sometime, a delay event is reported by SNCF, even if there is 0 minute of delay
if event.get_delay(in_minutes=True) == "0":
print("Aucun retard pour ce train")
#If MySQL is not canceled by the user, and the import of required modules succeeded
if(not results.no_mysql and not mysql_not_available):
#Instanciate a MySQL object with settings
mysql_service = mysql(settings, object_tools)
mysql_service.save_normal_trip(num_train)
mysql_service.close_connection()
exit(0)
#Display the event
print(str(event))
#If Google Sheet is not canceled by the user, and the import of required modules succeeded
if(not results.no_google_sheet and not google_sheet_not_available):
#Instanciate a Google Sheet object with settings
sheet = googleSheet(settings)
sheet.upload_data(event)
#If Twitter is not canceled by the user, and the import of required modules succeeded
if(not results.no_twitter and not twitter_not_available):
#Instanciate a Twitter object with settings
twitter_service = twitter(settings)
twitter_service.post_event(event)
#If MySQL is not canceled by the user, and the import of required modules succeeded
if(not results.no_mysql and not mysql_not_available):
#Instanciate a MySQL object with settings
mysql_service = mysql(settings, object_tools)
mysql_service.save_item(event)
mysql_service.close_connection()