This repository was archived by the owner on May 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
87 lines (64 loc) · 2.16 KB
/
start.py
File metadata and controls
87 lines (64 loc) · 2.16 KB
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
from src.build import build_data_files, build_templates, build_status_file
from src.data import history, WeatherData
from src.meteohub import fmt_data
from src.env import env
from src.ftp import *
import time
from tempfile import TemporaryDirectory
status = {"code": "operational", "message": "Everything is fine."}
first_run = True
def loop():
global status, first_run
data_fetch_success = False
try:
data, timestamp = fmt_data()
data_fetch_success = True
except Exception as e:
status["code"] = "meteohub_unavailable"
status["message"] = str(e)
print("Exp", e)
try:
history.load(WeatherData(**data), timestamp)
except Exception as e:
if data_fetch_success:
status["code"] = "history_exception"
status["message"] = str(e)
print("Exp", e)
use_sftp = env.get("USE_SFTP")
try:
ftp = FTPLib.get_connector(
env.get("FTP_ADDR"),
env.get("FTP_USER"),
env.get("FTP_PASS"),
use_sftp,
env.get("SFTP_KEY", None),
)
ftp.ensure_directory(env.get("FTP_DIR"))
with TemporaryDirectory() as tmp_dir:
try:
if data_fetch_success:
if first_run:
build_templates(tmp_dir)
first_run = False
build_data_files(tmp_dir)
except Exception as e:
status["code"] = "build_exception"
status["message"] = str(e)
print("Exp", e)
build_status_file(tmp_dir, status)
ftp.upload_directory(tmp_dir, env.get("FTP_DIR"))
ftp.close()
status["code"] = "operational"
status["message"] = "Everything is fine."
print("Uploaded data.")
except Exception as e:
status["code"] = "ftp_exception"
status["message"] = str(e)
print("Exp", e)
print("Failed to upload data due to ftp exception.")
next_post = time.time()
while True:
if time.time() > next_post:
next_post += 60 * env.get("UPLOAD_INTERVAL", 5)
loop()
time.sleep(1)