-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
250 lines (182 loc) · 6.64 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/user/bin/env python3.6
import os
from io import BytesIO
from datetime import datetime, timezone
import pytz
from feedgen.feed import FeedGenerator
from flask import Flask, Response, request, json, send_file, send_from_directory
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
from unwetter import db, generate, wina as wina_gen, slack, map, sentry
from unwetter.config import SEVERITY_FILTER, STATES_FILTER, URGENCY_FILTER
from unwetter.generate import urls
sentry.init()
app = Flask(__name__, static_folder="website/build")
auth = HTTPBasicAuth()
users = {
"wdr": generate_password_hash(os.environ.get("BASIC_AUTH_PASSWORD", "")),
"uwa": generate_password_hash(os.environ.get("BASIC_AUTH_PASSWORD", "")),
}
@auth.verify_password
def verify_password(username, password):
if username in users and check_password_hash(users.get(username), password):
return username
@app.route("/feed.rss")
def feed():
fg = FeedGenerator()
fg.id(urls.URL_BASE)
fg.title("Unwetter Testfeed")
fg.link(
href="https://www.dwd.de/DE/wetter/warnungen/warnWetter_node.html",
rel="alternate",
)
fg.subtitle("This is a test feed!")
fg.link(href=f"{urls.URL_BASE}feed.rss", rel="self")
fg.language("de")
# Iterate over the most recent 5 events matching filter
for event in db.load_published(limit=10):
fe = fg.add_entry(order="append")
fe.id(event["id"])
fe.title(generate.title(event, variant="wina_headline"))
fe.link(href=urls.wina(event))
fe.published(event["sent"].replace(tzinfo=pytz.UTC))
fe.description(generate.description(event).replace("\n", "<br>"))
r = Response(fg.rss_str(pretty=True), mimetype="application/rss+xml")
r.headers["Content-Type"] = "text/xml; charset=utf-8"
return r
@app.route("/wina/<id>")
def wina(id):
r = Response(wina_gen.from_id(id), mimetype="application/xml")
r.headers["Content-Type"] = "text/xml; charset=iso-8859-1"
return r
def send_pil(img):
bio = BytesIO()
img.save(bio, "PNG")
bio.seek(0)
return send_file(bio, "image/png")
@app.route("/map/<id>.png")
def map_single(id):
img = map.generate_map([db.by_id(id)])
return send_pil(img)
@app.route("/map")
@auth.login_required
def map_multi():
ids = request.args.getlist("id")
disabled_ids = request.args.getlist("disabled")
mode = request.args.get("mode", "square")
title = request.args.get("title")
title_size = int(request.args.get("titleSize", 130))
subtitle = request.args.get("subtitle")
subtitle_size = int(request.args.get("subtitleSize", 110))
img = map.generate_map(
list(db.by_ids(ids)),
mode=map.Mode(mode),
disabled_events=list(db.by_ids(disabled_ids)),
title=title,
title_size=title_size,
subtitle=subtitle,
subtitle_size=subtitle_size,
)
return send_pil(img)
@app.route("/basemap")
def map_base_square():
img = map.generate_base_map(mode=map.Mode.SQUARE)
return send_pil(img)
@app.route("/basemap_wide")
def map_base_wide():
img = map.generate_base_map(mode=map.Mode.WIDE)
return send_pil(img)
@app.route("/slack/event", methods=["GET", "POST"])
def slack_event():
data = request.json or request.form
if not data:
return ""
if data.get("challenge"):
return data.get("challenge")
payload = data.get("payload")
if payload:
data = json.loads(payload)
print(data)
actions = data.get("actions")
for action in actions:
id = data["callback_id"]
channel_id = data["channel"]["id"]
user_id = data["user"]["id"]
event = db.by_id(id)
response = None
if action["name"] == "twitter":
response = "*Vorschlag Tweet*:\n" + generate.tweet(event)
elif action["name"] == "radio":
response = "*Vorschlag Radionachrichten*:\n" + generate.radio(event)
elif action["name"] == "crawl":
response = "*Vorschlag TV-Crawl:*\n" + generate.crawl(event)
elif action["name"] == "dwd":
response = "Offizielle Meldung des DWD:\n" + event["description"]
elif action["name"] == "info":
if "references" in event:
references = f'Referenzen: {", ".join(event["references"])}'
else:
references = ""
response = f"""
Diese Meldung basiert auf offiziellen Informationen des Deutschen Wetterdienstes:
https://www.dwd.de/DE/wetter/warnungen/warnWetter_node.html
Die Bereitstellung dieser Information ist ein Projekt des Digitalen Wandels und wird aktiv weiterentwickelt.
Informationen und Kontakt: {os.environ["WDR_PROJECT_INFO_URL"]}
Event ID: {data["callback_id"]}
{references}
""".strip()
if response:
slack.post_message(
response,
mrkdwn=True,
private=user_id,
channel=channel_id,
thread_ts=data["original_message"]["thread_ts"],
)
return ""
@app.route("/slack/command/show", methods=["POST"])
def slack_command_show():
data = request.form
if not data:
print("No Data :(")
return ""
event = db.by_id(data["text"])
if not event:
return f'No event found with ID {data["text"]}'
slack.post_message(f'Sending test event with ID {data["text"]}')
slack.post_event(event)
return f'Sent event with ID {data["text"]}'
@app.route("/test")
def test():
return "OK"
@app.route("/error")
def error():
raise Exception("AHHHHHHHH")
@app.route("/api/v1/events/current", methods=["GET"])
def api_v1_current_events():
at = request.args.get("at")
if at:
at = datetime.utcfromtimestamp(int(at))
current_events = db.current_events(at=at, all_severities=True)
filtered_events = []
for event in current_events:
if event["severity"] == "Minor":
continue
del event["_id"]
del event["geometry"]
for field in ("sent", "effective", "onset", "expires"):
event[field] = event[field].replace(tzinfo=timezone.utc).timestamp()
filtered_events.append(event)
return json.dumps(sorted(filtered_events, key=map.severity_key, reverse=True))
# Serve React App
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
@auth.login_required
def serve(path):
full_path = os.path.join(app.static_folder, path)
if path != "" and os.path.exists(full_path):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, "index.html")
if __name__ == "__main__":
app.run()