Make skript handle case of verdi page failing gracefully

This commit is contained in:
Felix Blanke 2023-08-27 23:54:16 +02:00
parent dcbec17064
commit c4f3fb2120

47
wsgi.py
View File

@ -8,7 +8,7 @@ import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np
import pandas as pd
from flask import Flask, render_template, request
from flask import Flask, Markup, render_template, request
from flask_caching import Cache
from download_digital import construct_dataframe, get_bez_data
@ -166,16 +166,6 @@ def plot(
def tables(
url: str = "https://beschaeftigtenbefragung.verdi.de/",
):
df, df_state = get_tables(url)
df = df.sort_values(
["Digitale Befragung", "Bundesland", "Bezirk"], ascending=[False, True, True]
)
df_state = df_state.sort_values("Landesbezirk")
output_str = []
def _print_as_html(df: pd.DataFrame):
df = df.astype({"Digitale Befragung": "Int32"})
with pd.option_context("display.max_rows", None):
@ -206,15 +196,44 @@ def tables(
output_str.append(tfoot)
output_str.append(table[idx:])
output_str = []
try:
df, df_state = get_tables(url)
df = df.sort_values(
["Digitale Befragung", "Bundesland", "Bezirk"],
ascending=[False, True, True],
)
df_state = df_state.sort_values("Landesbezirk")
image = plot(df_state)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
except Exception:
last_file = sorted(Path("data").iterdir())[-1]
key = last_file.name[:10]
with (Path("data") / f"{key}_data.ods").open("rb") as ff:
df = pd.read_excel(ff, sheet_name="digital").astype(
{"Digitale Befragung": "Int32"}
)
with (Path("data") / f"{key}_state_data.ods").open("rb") as ff:
df_state = pd.read_excel(ff, sheet_name="digital").astype(
{"Digitale Befragung": "Int32"}
)
image = plot()
timestamp = Markup(f'<font color="red">{key} 10:00:00</font>')
_print_as_html(df_state)
_print_as_html(df)
image = plot(df_state, plot_all=True)
return render_template(
"base.html",
tables="\n".join(output_str),
timestamp=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
timestamp=timestamp,
image=image,
)