-
Notifications
You must be signed in to change notification settings - Fork 2
/
service
executable file
·67 lines (52 loc) · 1.64 KB
/
service
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
#!/usr/bin/env python
import traceback
from datetime import datetime
from time import sleep
from time import time as timestamp
from pytz import timezone, utc
from hafas_fetcher import HAFASFetcher
from helper import Helper, log
from hosted import CONFIG, DEVICE, NODE
CONFIG.restart_on_update()
TIMEZONE = "Europe/Berlin"
def _now():
tz = timezone(TIMEZONE)
now = datetime.utcnow()
now = now.replace(tzinfo=utc)
now = now.astimezone(tz)
now = now.replace(tzinfo=None)
return now
def idle(seconds):
log("idling for {} seconds".format(seconds))
timeout = timestamp() + seconds
while timestamp() < timeout:
NODE["/time"](Helper.to_unixtimestamp(_now()))
sleep(1)
def main():
idle(2)
while True:
try:
stops = CONFIG["stop_ids"].split(",")
data_sources = CONFIG["data_sources"]
hafas = HAFASFetcher()
for stop in stops:
hafas.fetch_and_parse(stop)
hafas.sort_and_deduplicate()
hafas.write_json()
if CONFIG["api_key"].startswith("http://") or CONFIG["api_key"].startswith(
"https://"
):
idle(15)
else:
requests_per_stop = 2 if data_sources == "both" else 1
request_count = len(stops) * requests_per_stop
sleep_time = max(
86400 / (CONFIG["requests_max_per_day"] / request_count),
30,
)
idle(sleep_time)
except Exception:
traceback.print_exc()
sleep(30)
if __name__ == "__main__":
main()