This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
145 lines (121 loc) · 4.59 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
from google.protobuf.json_format import MessageToJson
from google.protobuf.message import DecodeError
import gtfs_realtime_pb2 as gtfsrt
from pathlib import Path
import datetime as dt
import requests
import logging
import json
import pytz
import time
import os
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
data_path = Path('/usr/src/data')
config = None
with open(data_path.joinpath('dataset.json')) as json_file:
config = json.load(json_file)
tz = pytz.timezone(config['timezone'])
logging.info('Job Started')
logging.info('GTFS-RT - %s', config['data_name'])
for setting in ['archive_db','archive_pb','archive_json']:
logging.info('%s --> %s', setting, config[setting])
feeds = os.environ.get('MONITOR', config['gtfsrt'].keys())
if isinstance(feeds, str):
feeds = [feeds]
else:
feeds = list(feeds)
logging.info('Feeds %s', str(feeds))
sleep_time = config['gtfsrt'][feeds[0]]['sleep']
sleep_adaptive_duplicate = False
db_url = os.environ.get('DB_URL', None)
db_init = False
while True:
increase_sleep = False
for feed in feeds:
try:
r = requests.get(config['gtfsrt'][feed]['url'], timeout=10)
fm = gtfsrt.FeedMessage()
fm.ParseFromString(r.content)
data = json.loads(MessageToJson(fm))
timestamp_str = data['header']['timestamp']
timestamp = dt.datetime.fromtimestamp(float(timestamp_str), tz)
logging.debug('PB Timestamp: %s', timestamp_str)
if config['archive_pb'] is True:
week_folder = config['data_name']+"-GTFSRT-"+timestamp.strftime("%G-%V")+".pb"
item_path = data_path.joinpath('gtfsrt', week_folder, feed, timestamp_str).with_suffix('.pb')
logging.debug('PB Path: %s', item_path)
if not item_path.parent.exists():
item_path.parent.mkdir(parents=True, exist_ok=True)
if not item_path.exists():
with open(item_path, 'wb') as output_file:
output_file.write(r.content)
else:
logging.error('Path Already Exists: %s', item_path.relative_to(data_path))
increase_sleep = True
if config['archive_json'] is True:
week_folder = config['data_name']+"-GTFSRT-"+timestamp.strftime("%G-%V")+".json"
item_path = data_path.joinpath('gtfsrt-json', week_folder, feed, timestamp_str).with_suffix('.json')
logging.debug('JSON Path: %s', item_path)
if not item_path.parent.exists():
item_path.parent.mkdir(parents=True, exist_ok=True)
if not item_path.exists():
with open(item_path, 'w') as output_file:
json.dump(data, output_file)
else:
logging.error('Path Already Exists: %s', item_path.relative_to(data_path))
increase_sleep = True
if config['archive_db'] is True:
if not db_init is True:
import pymongo
client = pymongo.MongoClient(db_url)
db = client.get_database()
for table_name in feeds:
timestamp_index_exist = False
if table_name in db.collection_names():
index_info = db[table_name].index_information()
for index in index_info:
for key in index_info[index]['key']:
for field in key:
if field == "header.timestamp":
timestamp_index_exist = True
if not timestamp_index_exist:
logging.warning('No Timestamp Index Located for %s. Creating...', table_name)
db[table_name].create_index(
[("header.timestamp", pymongo.DESCENDING)],
unique=True,background=True
)
db_init = True
data['header']['timestamp'] = int(data['header']['timestamp'])
try:
db[feed].insert_one(data)
except pymongo.errors.DuplicateKeyError:
logging.error('Entry Already Exists. %s %s', feed, str(data['header']['timestamp']))
increase_sleep = True
except requests.exceptions.ReadTimeout as e:
logging.exception('Connection Error to: %s', config['gtfsrt'][feed]['url'])
print(e)
except requests.exceptions.ConnectionError as e:
logging.exception('Connection Error to: %s', config['gtfsrt'][feed]['url'])
print(e)
except requests.exceptions.ChunkedEncodingError as e:
logging.exception('Connection Error to: %s', config['gtfsrt'][feed]['url'])
print(e)
except DecodeError as e:
logging.exception('Unable to decode: %s', config['gtfsrt'][feed]['url'])
print(e)
except KeyError as e:
logging.exception('Missing Value in Protobuffer from: %s', config['gtfsrt'][feed]['url'])
print(e)
if config['sleep_adaptive'] is True:
if increase_sleep:
if not sleep_adaptive_duplicate:
sleep_time = sleep_time + 5
sleep_adaptive_duplicate = True
time.sleep(2)
else:
sleep_time = sleep_time - 1
sleep_adaptive_duplicate = False
logging.info('Sleeping for %s seconds', str(sleep_time))
time.sleep(sleep_time)
else:
time.sleep(sleep_time)