-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
151 lines (116 loc) · 4.09 KB
/
app.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
import datetime
import locale
import io
import requests
import logging
import hashlib
import base64
import urllib.parse
from flask import Flask, render_template, send_file, jsonify, request, abort
from browser import make_screenshot
from weather import weather_code_to_info
locale.setlocale(locale.LC_TIME, "fr_FR.UTF-8")
METEO_TOKEN = "685db9ef151cc658e2d46520e693bdc553f516c2b6872a1951acb7e89954d5c7" # noqa: E501
INSEE_CHAMPS_SUR_MARNE = 77083
API_ROUTE = "https://api.meteo-concept.com/api"
API_DAILY = "/forecast/daily"
API_NEXT_HOURS = "/forecast/nextHours"
CACHE_DURATION = datetime.timedelta(hours=1)
WEBSITES = [
"https://weathermap.opengate.space",
"https://screen.opengate.space/clock",
"http://monitoring.mgmt.opengate.space:3000/dashboard/snapshot/w6OlhdMnrWpaWuCiNrSAusvA55G9mGPc?orgId=1&refresh=1m&from=now-1h&to=now",
]
last_weather_refresh: datetime.datetime = datetime.datetime.min
last_image_refresh: datetime.datetime = datetime.datetime.min
weather_cache = {}
app = Flask(__name__)
logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)
def get_weather_data() -> dict:
global last_weather_refresh
global weather_cache
if last_weather_refresh + CACHE_DURATION < datetime.datetime.now():
req = requests.get(API_ROUTE + API_NEXT_HOURS, params={
'token': METEO_TOKEN,
'insee': INSEE_CHAMPS_SUR_MARNE
},
headers={'Accept': 'application/json'}
)
logger.info(str(datetime.datetime.now()) + ' Refresh weather')
weather_cache = req.json()
last_weather_refresh = datetime.datetime.now()
return weather_cache
def transform_weather_data(weather_data: dict) -> tuple:
forecast = weather_data["forecast"]
image, weather = weather_code_to_info(forecast[0]['weather'])
actual = {
'temp': forecast[0]['temp2m'],
'rain': forecast[0]['probarain'],
'humidity': forecast[0]['rh2m'],
'wind': forecast[0]['wind10m'],
'image': image,
'weather': weather,
}
future = []
for i in range(1, 4):
date: str = datetime.datetime.fromisoformat(forecast[i]['datetime'])
image, weather = weather_code_to_info(forecast[i]['weather'])
future.append(
{
'date': date,
'temp': forecast[i]['temp2m'],
'image': image,
'weather': weather,
}
)
return tuple([actual, future])
@app.route("/")
def index():
return render_template(
'index.html',
now=datetime.datetime.now()
)
@app.route("/image_list")
def imageList():
return jsonify(["/screenshot?url=" + urllib.parse.quote(website) + "&date=" +
datetime.datetime.now().strftime("%m%d%Y%H%M") for website in WEBSITES])
@app.route("/screenshot")
def screenshot():
if request.args.get('url') not in WEBSITES:
abort(404)
return send_file(
io.BytesIO(
make_screenshot(
request.args.get('url'),
request.args.get('date'))),
mimetype='image/jpeg',
as_attachment=True,
download_name='%s.jpg' %
image_name)
@app.route("/clock")
def clock():
weather = get_weather_data()
actual_weather, forecast_weather = transform_weather_data(weather)
return render_template(
'clock.html',
actual_weather=actual_weather,
forecast_weather=forecast_weather,
now=datetime.datetime.now()
)
image: bytes = b''
image_name: str = ""
@app.route("/image")
def get_image():
global image, image_name, last_image_refresh
if last_image_refresh + CACHE_DURATION < datetime.datetime.now():
req = requests.get("https://source.unsplash.com/1980x1080/?space")
image = req.content
image_name = base64.b64encode(hashlib.sha256(image).digest()).decode()
logger.info(str(datetime.datetime.now()) + ' Refresh image')
last_image_refresh = datetime.datetime.now()
return send_file(
io.BytesIO(image),
mimetype='image/jpeg',
as_attachment=True,
download_name='%s.jpg' % image_name)