forked from data-for-change/anyway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·441 lines (327 loc) · 11.7 KB
/
main.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/env python
import argparse
import logging
import os
import re
import sys
import click
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.DEBUG,
datefmt="%Y-%m-%d %H:%M:%S",
)
def valid_date(date_string):
date_input_format = "%d-%m-%Y"
date_input_format_alt = "%Y-%m-%dT%H:%M"
from datetime import datetime
try:
return datetime.strptime(date_string, date_input_format)
except ValueError:
try:
return datetime.strptime(date_string, date_input_format_alt)
except ValueError:
msg = "Not a valid date: '{0}'.".format(date_string)
raise argparse.ArgumentTypeError(msg)
@click.group()
def cli():
pass
@cli.command()
@click.option(
"--open",
"open_server",
is_flag=True,
help="Open the server for communication from outside",
default=False,
)
@click.option("--debug-js", is_flag=True, help="Don't minify the JavaScript files")
def testserver(open_server, debug_js):
from anyway.app_and_db import app
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.DEBUG,
datefmt="%Y-%m-%d %H:%M:%S",
)
if debug_js:
app.config["ASSETS_DEBUG"] = True
default_host = "0.0.0.0" if open_server else "127.0.0.1"
app.run(debug=True, host=os.getenv("IP", default_host), port=int(os.getenv("PORT", 5000)))
@cli.group()
def update_news_flash():
pass
@update_news_flash.command()
@click.option("--source", default="", type=str)
@click.option("--news_flash_id", default="", type=str)
def update(source, news_flash_id):
from anyway.parsers import news_flash
if not source:
source = None
if not news_flash_id:
news_flash_id = None
return news_flash.update_all_in_db(source, news_flash_id)
@update_news_flash.command()
def remove_duplicate_news_flash_rows():
from anyway.parsers import news_flash_db_adapter
news_flash_db_adapter.init_db().remove_duplicate_rows()
@cli.group()
def process():
pass
@process.command()
@click.option("--batch_size", type=int, default=5000)
@click.option("--load_start_year", type=str, default=None)
@click.option("--source", type=str, default="s3")
def cbs(batch_size, load_start_year, source):
from anyway.parsers.cbs.executor import main
return main(batch_size=batch_size, load_start_year=load_start_year, source=source)
@process.command()
def news_flash():
from anyway.parsers.news_flash import scrape_all
return scrape_all()
@process.command()
@click.option("--specific_folder", is_flag=True, default=False)
@click.option("--delete_all", is_flag=True)
@click.option("--path", type=str, default="static/data/cbs_vehicles_registered")
def registered_vehicles(specific_folder, delete_all, path):
from anyway.parsers.registered import main
return main(specific_folder=specific_folder, delete_all=delete_all, path=path)
@process.command()
@click.option("--path", type=str, default="static/data/traffic_volume")
def traffic_volume(path):
from anyway.parsers.traffic_volume import main
return main(path)
@process.command()
@click.argument("filename")
def rsa(filename):
from anyway.parsers.rsa import parse
return parse(filename)
@process.command()
@click.argument("filename", type=str, default="static/data/segments/road_segments.xlsx")
def road_segments(filename):
from anyway.parsers.road_segments import parse
return parse(filename)
@process.command()
@click.argument("filepath", type=str, default="static/data/schools/schools.csv")
@click.option("--batch_size", type=int, default=5000)
def schools(filepath, batch_size):
from anyway.parsers.schools import parse
return parse(filepath=filepath, batch_size=batch_size)
@process.command()
@click.argument(
"schools_description_filepath", type=str, default="static/data/schools/schools_description.xlsx"
)
@click.argument(
"schools_coordinates_filepath", type=str, default="static/data/schools/schools_coordinates.xlsx"
)
@click.option("--batch_size", type=int, default=5000)
def schools_with_description(
schools_description_filepath, schools_coordinates_filepath, batch_size
):
from anyway.parsers.schools_with_description import parse
return parse(
schools_description_filepath=schools_description_filepath,
schools_coordinates_filepath=schools_coordinates_filepath,
batch_size=batch_size,
)
@process.command()
@click.argument(
"schools_description_filepath",
type=str,
default="static/data/schools/schools_description_2020.xlsx",
)
@click.argument(
"schools_coordinates_filepath",
type=str,
default="static/data/schools/schools_coordinates_2020.xlsx",
)
@click.option("--batch_size", type=int, default=5000)
def schools_with_description_2020(
schools_description_filepath, schools_coordinates_filepath, batch_size
):
from anyway.parsers.schools_with_description_2020 import parse
return parse(
schools_description_filepath=schools_description_filepath,
schools_coordinates_filepath=schools_coordinates_filepath,
batch_size=batch_size,
)
@process.command()
@click.option(
"--start_date", default="01-07-2017", type=valid_date, help="The Start Date - format DD-MM-YYYY"
)
@click.option(
"--end_date", default="30-06-2022", type=valid_date, help="The End Date - format DD-MM-YYYY"
)
@click.option("--distance", default=0.5, help="float In KM. Default is 0.5 (500m)", type=float)
@click.option("--batch_size", type=int, default=5000)
def injured_around_schools_2022(start_date, end_date, distance, batch_size):
from anyway.parsers.injured_around_schools_2022 import parse
return parse(start_date=start_date, end_date=end_date, distance=distance, batch_size=batch_size)
@process.command()
@click.option(
"--from_s3",
"-f",
is_flag=True,
help="get the data from files, instead of waze api",
)
@click.option(
"--start_date", default="01-01-2019", type=valid_date, help="The Start Date - format DD-MM-YYYY"
)
@click.option(
"--end_date", default="01-01-2020", type=valid_date, help="The End Date - format DD-MM-YYYY"
)
def waze_data(from_s3, start_date, end_date):
"""
Get waze data from existing files or from waze api.
Examples for running the script:
- For getting data from waze RTS HTTP API, run:
python -m main process waze-data
- For getting data from the S3 stored json files, run (change the start and end date as you need):
python -m main process waze-data --from_s3 --start_date=01-01-2020 --end_date=01-01-2020
"""
from anyway.parsers.waze.waze_data_parser import ingest_waze_from_files, ingest_waze_from_api
if from_s3:
return ingest_waze_from_files(
bucket_name="anyway-hasadna.appspot.com", start_date=start_date, end_date=end_date
)
else:
return ingest_waze_from_api()
@process.command()
@click.argument("filename", type=str, default="static/data/embedded_reports/embedded_reports.csv")
def embedded_reports(filename):
from anyway.parsers.embedded_reports import parse
return parse(filename)
@process.command()
@click.option(
"--update",
"update",
is_flag=True,
help="Recalculates the cache (default is False)",
default=False,
)
@click.option(
"--no_info", "info", is_flag=True, help="Prints info on cache (default is True)", default=True
)
def infographics_data_cache(info, update):
"""Will refresh the infographics data cache"""
from anyway.parsers.infographics_data_cache_updater import main
return main(update=update, info=info)
@process.command()
def infographics_data_cache_for_road_segments():
"""Will refresh the infographics data cache"""
from anyway.parsers.infographics_data_cache_updater import main_for_road_segments
return main_for_road_segments()
@process.group()
def cache():
pass
@cache.command()
def update_street():
"""Update street cache"""
from anyway.parsers.infographics_data_cache_updater import main_for_street
main_for_street()
@cache.command()
def update_road_segments():
"""Update road segments cache"""
infographics_data_cache_for_road_segments()
@process.command()
@click.argument("filename", type=str, default="static/data/casualties/casualties_costs.csv")
def update_casualties_costs(filename):
from anyway.parsers.casualties_costs import parse
return parse(filename)
@cli.group()
def preprocess():
pass
@preprocess.command()
@click.option("--path", type=str)
def preprocess_cbs(path):
from anyway.parsers.cbs.preprocessing_cbs_files import update_cbs_files_names
return update_cbs_files_names(path)
@cli.group()
def create_tables():
pass
@create_tables.command()
def create_cbs_tables():
from anyway.parsers.cbs.executor import create_tables
return create_tables()
@create_tables.command()
@click.option(
"--file-name",
type=str,
help="csv file to load from. Default is static/data/cities.csv",
default="%s/static/data/cities.csv" % os.path.abspath(os.path.dirname(__file__)),
)
def update_cities_table(file_name):
from anyway.parsers.cbs.preprocessing_cbs_files import load_cities_data
return load_cities_data(file_name=file_name)
@cli.group()
def update_dictionary_tables():
pass
@update_dictionary_tables.command()
@click.option("--path", type=str, default="static/data/cbs")
def update_cbs(path):
from anyway.parsers.cbs.executor import update_dictionary_tables
return update_dictionary_tables(path)
@cli.group()
def truncate_dictionary_tables():
pass
@truncate_dictionary_tables.command()
@click.option("--path", type=str)
def truncate_cbs(path):
from anyway.parsers.cbs.executor import truncate_dictionary_tables
return truncate_dictionary_tables(path)
@cli.command()
@click.argument("identifiers", nargs=-1)
def load_discussions(identifiers):
from anyway.models import DiscussionMarker
from anyway.app_and_db import db
identifiers = identifiers or sys.stdin
for identifier in identifiers:
identifier = identifier.strip()
m = re.match(r"\((\d+\.\d+),\s*(\d+\.\d+)\)", identifier)
if not m:
logging.error("Failed processing: " + identifier)
continue
(latitude, longitude) = m.group(1, 2)
marker = DiscussionMarker.parse(
{
"latitude": latitude,
"longitude": longitude,
"title": identifier,
"identifier": identifier,
}
)
try:
db.session.add(marker)
db.session.commit()
logging.info(f"Added: {identifier}")
except Exception as e:
db.session.rollback()
logging.warning(f"Failed: {identifier} {e}")
@cli.group()
def scripts():
pass
@scripts.command()
@click.option(
"--start_date", default="01-01-2013", type=valid_date, help="The Start Date - format DD-MM-YYYY"
)
@click.option(
"--end_date", default="31-12-2017", type=valid_date, help="The End Date - format DD-MM-YYYY"
)
@click.option("--distance", default=0.5, help="float In KM. Default is 0.5 (500m)", type=float)
@click.option(
"--output_path", default="output", help="output file of the results. Default is output.csv"
)
def accidents_around_schools(start_date, end_date, distance, output_path):
from anyway.accidents_around_schools import main
return main(
start_date=start_date, end_date=end_date, distance=distance, output_path=output_path
)
@scripts.command()
def test_airflow():
print('my print')
logging.info('info log')
logging.warning('warning log')
logging.debug('debug log')
@scripts.command()
def importemail():
from anyway.parsers.cbs.importmail_cbs import main
return main()
if __name__ == "__main__":
cli(sys.argv[1:]) # pylint: disable=too-many-function-args