-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (65 loc) · 3.08 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
# Copyright 2020 InfAI (CC SES)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
from datetime import date, timedelta
from typing import List
import schedule
from import_lib.import_lib import ImportLib, get_logger
from lib.station.Station import get_stations_in_bboxes
from lib.station.StationImport import StationImport
if __name__ == '__main__':
lib = ImportLib()
logger = get_logger(__name__)
stationImport = StationImport(lib)
bboxes = lib.get_config("BBOXES", None)
if not isinstance(bboxes, List):
logger.error("Invalid config for BBOXES will not be used")
bboxes = None
date_time, _ = lib.get_last_published_datetime()
state, _ = lib.get_last_published_datetime()
if state is None:
logger.info("Import is starting fresh")
else:
logger.info("Import is continuing previous import")
today = date.today()
stations = get_stations_in_bboxes(bboxes)
if lib.get_config("HISTORIC", False):
if state is None or state.year < today.year - 1:
logger.info(str(len(stations)) + " stations found in area")
logger.info("Importing historic data of " + str(len(stations)) + " stations...")
stationImport.import_historical(stations)
else:
logger.info("Skipping historic data (already done)")
else:
logger.info("Skipping historic data (not configured)")
year_active_stations = [station for station in stations if station.date_to.year == today.year]
if lib.get_config("RECENT", False):
if state is None or state.year < today.year:
logger.info(str(len(year_active_stations)) + " active stations this year")
logger.info("Importing this years data of " + str(len(year_active_stations)) + " stations...")
stationImport.import_recent(year_active_stations)
else:
logger.info("Skipping recent data (already done)")
else:
logger.info("Skipping recent data (not configured)")
active_day = today - timedelta(days=3) # station list might a few days old
today_active_stations = [station for station in year_active_stations if station.date_to > active_day]
logger.info(str(len(today_active_stations)) + " active stations today")
logger.info("Importing todays data of " + str(len(today_active_stations)) + " stations...")
stationImport.import_today(today_active_stations)
logger.info("Setting schedule to run every 30 minutes")
schedule.every(30).minutes.do(stationImport.import_most_recent, today_active_stations)
while True:
schedule.run_pending()
time.sleep(1)