This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointtcl.py
250 lines (172 loc) · 7.1 KB
/
pointtcl.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from envparse import env, Env
from bot import *
from database import db_session, init_db
from models import *
import sys
import logging
import click
import commands
import grandlyon
def get_bot_instance(available_commands=[]):
return Bot(
name=env('SLACK_BOT_NAME'),
token=env('SLACK_BOT_TOKEN'),
id=env('SLACK_BOT_ID'),
available_commands=available_commands
)
def get_human_line_type_name(type):
if type == TclLineType.SUBWAY:
return 'Métro'
elif type == TclLineType.TRAM:
return 'Tram'
elif type == TclLineType.BUS:
return 'Bus'
elif type == TclLineType.FUNICULAR:
return 'Funiculaire'
else:
return 'Type de ligne inconnu'
@click.group()
def cli():
"""Point TCL Slack bot"""
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%d/%m/%Y %H:%M:%S',
stream=sys.stdout
)
logging.getLogger().setLevel(logging.INFO)
logging.info('Initializing')
Env.read_envfile('.env')
@cli.command()
def run():
"""Run the bot himself"""
available_commands = [getattr(commands, command)() for command in commands.__all__]
bot = get_bot_instance(available_commands)
logging.info('Connecting to Slack')
bot.run()
@cli.command()
def id():
"""Print the bot ID"""
bot = get_bot_instance()
logging.info('Getting bot ID...')
bot_id = bot.get_id()
if bot_id:
logging.info('Bot ID is ' + bot_id)
else:
logging.error('Could not find bot user')
def create_database():
"""Create then seed the database"""
logging.info('Deleting and creating the database')
init_db() # See in models.py
grandlyon_client = grandlyon.Client(env('GRANDLYON_LOGIN'), env('GRANDLYON_PASSWORD'))
logging.info('Seeding the database')
logging.info('Getting all bus lines')
# Bus lines
bus_lines = grandlyon_client.get_all_bus_lines()
all_bus_lines = []
for bus_line in bus_lines:
name = bus_line['ligne'].lower()
if name not in all_bus_lines:
all_bus_lines.append(name)
db_session.add(TclLine(
name=name,
type=TclLineType.BUS
))
db_session.commit()
logging.info('Getting all subway and funicular lines')
# Subway and funicular lines
subway_funicular_lines = grandlyon_client.get_all_subway_funicular_lines()
all_subway_funicular_lines = []
for subway_funicular_line in subway_funicular_lines:
name = subway_funicular_line['ligne'].lower()
if name not in all_subway_funicular_lines:
all_subway_funicular_lines.append(name)
db_session.add(TclLine(
name=name,
type=TclLineType.FUNICULAR if name.startswith('f') else TclLineType.SUBWAY
))
db_session.commit()
logging.info('Getting all tram lines')
# Tram lines
tram_lines = grandlyon_client.get_all_tram_lines()
all_tram_lines = []
for tram_line in tram_lines:
name = tram_line['ligne'].lower()
if name not in all_tram_lines:
all_tram_lines.append(name)
db_session.add(TclLine(
name=name,
type=TclLineType.TRAM
))
db_session.commit()
logging.info('Done')
@cli.command(name='create_database')
def create_database_cmd():
"""Create then seed the database"""
create_database()
def check_lines():
"""Check for disruption on all lines"""
bot = get_bot_instance()
logging.info('Getting all current disruptions')
grandlyon_client = grandlyon.Client(env('GRANDLYON_LOGIN'), env('GRANDLYON_PASSWORD'))
disrupted_lines = grandlyon_client.get_disrupted_lines()
logging.info('Got {} disrupted lines to process'.format(len(disrupted_lines)))
disruption_start_lines = []
disruption_end_lines = []
lines_to_notify = env.list('DISRUPTIONS_LINES', default=[])
logging.info('Processing new or ongoing disruptions')
if disrupted_lines:
for line_name, disruption_infos in disrupted_lines.items():
line_object = TclLine.find_line(line_name)
if not line_object:
logging.warning('Line not found: {}'.format(line_name))
continue
if not line_object.is_disrupted:
logging.info('Line {} of type {}: start of disruption'.format(line_name, line_object.type))
line_object.is_disrupted = True
line_object.latest_disruption_started_at = disruption_infos['started_at']
line_object.latest_disruption_reason = disruption_infos['reason']
db_session.add(line_object)
if line_name in lines_to_notify:
disruption_start_lines.append('*{line_type} {line_name}*{reason}'.format(
line_type=get_human_line_type_name(line_object.type),
line_name=line_name,
reason=' (la raison est : _' + line_object.latest_disruption_reason + '_)' if line_object.latest_disruption_reason else ''
))
else:
logging.info('Line {} of type {} already set as disrupted'.format(line_name, line_object.type))
logging.info('Processing finished disruptions')
disturbed_line_ids_in_db = TclLine.get_disturbed_line_ids()
finished_disruptions = list(set(disturbed_line_ids_in_db) - set(disrupted_lines.keys()))
if finished_disruptions:
for line_name in finished_disruptions:
line_object = TclLine.find_line(line_name)
if not line_object:
logging.warning('Line not found: {}'.format(line_name))
continue
logging.info('Line {} of type {}: end of disruption'.format(line_name, line_object.type))
line_object.is_disrupted = False
db_session.add(line_object)
if line_name in lines_to_notify:
disruption_end_lines.append('*{line_type} {line_name}*{reason}'.format(
line_type=get_human_line_type_name(line_object.type),
line_name=line_name,
reason=' (la raison était : _' + line_object.latest_disruption_reason + '_)' if line_object.latest_disruption_reason else ''
))
else:
logging.info('No finished disruption to process')
recipient_channels = env.list('SEND_DISRUPTION_MESSAGES_TO', default=[])
if recipient_channels: # If there's channels to inform
logging.info('Sending updates to Slack')
for recipient_channel in recipient_channels:
if disruption_start_lines:
bot.say_random('disruption_start', recipient_channel, lines=' - ' + '\n - '.join(disruption_start_lines))
if disruption_end_lines:
bot.say_random('disruption_end', recipient_channel, lines=' - ' + '\n - '.join(disruption_end_lines))
db_session.commit()
logging.info('End of processing')
@cli.command(name='check_lines')
def check_lines_cmd():
"""Check for disruption on all lines"""
check_lines()
if __name__ == '__main__':
cli()