-
Notifications
You must be signed in to change notification settings - Fork 0
/
acqua.py
314 lines (244 loc) · 10.9 KB
/
acqua.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import datetime
# import datetime
import json
import locale
import tempfile
import httpx
from bs4 import BeautifulSoup
from dateparser.search import search_dates
# from pprint import pprint
from rich import print
from telegram import Update
from telegram.ext import ContextTypes
from uniplot import plot_to_string
import config
from utils import get_now, no_can_do, printlog, code_screenshot
locale.setlocale(locale.LC_ALL, "it_IT.utf8")
def get_links(year=2023, month=6) -> list:
"""Get all the daily links for a specific month
Args:
year (int, optional): year. Defaults to 2023.
month (int, optional): month. Defaults to 6.
Returns:
list: a list of links
"""
# base url = https://www.comune.palmadimontechiaro.ag.it/flex/cm/pages/ServeBLOB.php/L/IT/IDPagina/3683?YY=2023&MM=6
url = f"https://www.comune.palmadimontechiaro.ag.it/flex/cm/pages/ServeBLOB.php/L/IT/IDPagina/3683?YY={year}&MM={month}"
r = httpx.get(url)
soup = BeautifulSoup(r.text, features="lxml")
links = soup.find("div", class_="ElencoCanale")
links = links.find_all("li")
links = [link.find("a").get("href") for link in links]
# pprint(links)
return links
def analyze_day(url: str, debug=False) -> tuple:
"""Scape a single day link and returns a tuple containing the day in ISO format and a list of quartieri
Args:
url (str): the url for the daily page
debug (bool, optional): print debug informations. Defaults to False.
Returns:
tuple: a tuple containing the day in ISO format and a list of quartieri
"""
r = httpx.get(url)
soup = BeautifulSoup(r.text, features="lxml")
breadcrumb = soup.find("div", class_="BreadCrumb")
data = breadcrumb.find("span").text
# data = ' '.join(data.split()[-3:])
text_data = data.replace("Turni erogazione acqua del ", "").strip()
try:
data = datetime.datetime.strptime(text_data, "%d %B %Y").date()
except ValueError:
date_found = search_dates(
text_data,
languages=["it", "en"],
settings={"PREFER_DATES_FROM": "past", "DEFAULT_LANGUAGES": ["it", "en"]},
)
if date_found:
data = date_found[0][1].date()
# print(data)
table = soup.find("table", id="blobTable-2")
if table:
celle = table.find_all("td", headers="th_c_2_0")
else:
table = soup.find("table", id="blobTable-3")
celle = table.find_all("td", headers="th_c_3_0")
celle = [cella.text for cella in celle]
# erogazione = any(to_search.casefold() in cella.casefold() for cella in celle)
# if erogazione:
# pprint(f"{data}: {erogazione}")
if debug:
lista_quartieri = []
for quartiere in celle:
if len(quartiere) < 3:
continue
quartiere = quartiere.strip().split(":")[0].strip()
quartiere = quartiere.strip().split("(")[0].strip()
if quartiere not in lista_quartieri:
lista_quartieri.append(quartiere)
print(f"[{data}]\t· {', '.join(lista_quartieri)}")
return (data, celle)
def get_erogazioni(json_file="db/erogazioni.json", quartiere="Garda") -> list:
"""Analyze the json and return a list of days where the quartiere is erogato
Args:
json_file (str, optional): json file to analyze. Defaults to "data.json".
quartiere (str, optional): quartiere to search for. Defaults to "Garda".
Returns:
list: a list of strings (dates in ISO format)
"""
with open(json_file, "r") as f:
data = json.load(f)
lista_erogazioni = [key for key in data if erogato_in_quartiere(data[key]["quartieri"], to_search=quartiere)]
return lista_erogazioni
def erogato_in_quartiere(lista_quartieri, to_search="Garda") -> bool:
return any(to_search.casefold() in cella.casefold() for cella in lista_quartieri)
def fancy_stats(erogazioni: list, print_plot=True, limit_erogazioni=150, rolling_avg=5, only_data=True):
if limit_erogazioni > len(erogazioni):
limit_erogazioni = len(erogazioni)
erogazioni = erogazioni[-limit_erogazioni:]
data = {"n_erogazioni": limit_erogazioni}
last_erogazione = datetime.datetime.strptime(erogazioni[0], "%Y-%m-%d").date()
data["first_erogazione"] = erogazioni[0]
data["last_erogazione"] = erogazioni[-1]
media_erogazione = 0
n_erogazioni = 0
last_media = 0
last_media_roll = 0
roll_avg = []
avg = []
max_erog = {"day": last_erogazione, "delta": 0}
media_erogazione_roll_avg = [0 for _ in range(rolling_avg)]
i = 0
for day in erogazioni:
i += 1
this_erogazione = datetime.datetime.strptime(day, "%Y-%m-%d").date()
delta = this_erogazione - last_erogazione
n_erogazioni += 1
media_erogazione += delta.days
media_erogazione_roll_avg.append(delta.days)
media_erogazione_roll_avg.pop(0)
media = media_erogazione / n_erogazioni
media_roll: float = sum(media_erogazione_roll_avg) / rolling_avg
roll_avg.append(media_roll)
avg.append(media)
if delta.days > max_erog["delta"]:
max_erog["day"] = day
max_erog["delta"] = delta.days
simbolo = "↓" if last_media > media else "↑"
simbolo_roll = "↓" if last_media_roll > media_roll else "↑"
last_media = media
last_media_roll = media_roll
last_erogazione = this_erogazione
if not only_data:
print(
f"{i}\t{day} | Ultima erogazione: {delta.days} giorni prima\t| {simbolo} Media: {round(media, 2)} giorni\t| {simbolo_roll} Media mobile ({rolling_avg}): {round(media_roll, 2)} giorni"
)
data["delta_ultima_erogazione"] = delta.days
data["max_erogazione_date"] = max_erog["day"]
data["max_erogazione_delta"] = max_erog["delta"]
data["rolling_avg_last"] = last_media_roll # type: ignore
data["avg_last"] = last_media # type: ignore
# print()
# print(roll_avg)
plot_roll_avg = plot_to_string(
roll_avg[-50:],
erogazioni[-50:],
title=f"Media mobile ({rolling_avg} erogazioni) - ultime 50 erogazioni",
width=70,
lines=True,
)
# plot_avg = plot_to_string(
# avg[-100:],
# erogazioni[-100:],
# title="Media erogazioni - ultime 100 erogazioni",
# width=100,
# lines=True,
# )
# print(plot_roll_avg)
data['plot_roll_avg'] = '\n'.join(plot_roll_avg)
# print(data['plot_roll_avg'])
# data['plot_avg'] = '\n'.join(plot_avg)
if only_data:
return data
async def acqua_stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
if await no_can_do(update, context):
return
if update.message.from_user.id not in config.ADMINS:
return
quartiere = "Garda"
rolling_avg = 5
send_plot = False
await printlog(update, "chiede informazioni sui turni dell'acqua", quartiere)
erogazioni = get_erogazioni(json_file=config.DB_ACQUA, quartiere=quartiere)
stats = fancy_stats(erogazioni, print_plot=False, limit_erogazioni=1000, rolling_avg=rolling_avg)
message = ""
message += f"Ultima erogazione il <code>{stats['last_erogazione']}</code>, dopo <code>{stats['delta_ultima_erogazione']}</code> giorni.\n\n"
message += f"Media mobile (<code>{rolling_avg}</code> erogazioni) all'ultima erogazione: <code>{round(stats['rolling_avg_last'], 2)}</code> giorni.\n"
message += f"Media assoluta di tutte le erogazioni: <code>{round(stats['avg_last'], 2)}</code> giorni.\n\n"
message += f"Massima distanza tra due erogazioni: <code>{stats['max_erogazione_delta']}</code> giorni il <code>{stats['max_erogazione_date']}</code>.\n"
message += f"Totale erogazioni analizzate: <code>{stats['n_erogazioni']}</code>, dal <code>{stats['first_erogazione']}</code>."
await update.message.reply_html(message)
if send_plot:
code: str = stats['plot_roll_avg']
with tempfile.NamedTemporaryFile(suffix=".png") as f:
# This can take 10-20 seconds, sadly
await code_screenshot(code, f.name)
await update.message.reply_photo(photo=open(f.name, "rb"))
async def manual_update_acqua_db(update: Update, context: ContextTypes.DEFAULT_TYPE):
if await no_can_do(update, context):
return
if update.message.from_user.id not in config.ADMINS:
return
return await update_acqua_db(context)
# Runs every morning at 3:00 (Europe/Rome)
async def update_acqua_db(context: ContextTypes.DEFAULT_TYPE) -> None:
print(f"{get_now()} [AUTO] Aggiorno turni acqua dal sito del comune")
today = datetime.datetime.today()
year = today.year
month = today.month
db = json.load(open(config.DB_ACQUA, "r"))
links = get_links(year=year, month=month)
for link in links:
data, celle = analyze_day(link, debug=False)
if data.strftime("%Y-%m-%d") in db:
continue
else:
db[data.strftime("%Y-%m-%d")] = {"quartieri": celle}
if 'Garda' in celle:
try:
data_str = data.strftime("%Y-%m-%d")
date_obj = datetime.datetime.strptime(data_str, "%Y-%m-%d")
unix_timestamp = datetime.datetime.timestamp(date_obj)
fixed_timestamp = int(unix_timestamp) + 10
await send_date_to_infludb(fixed_timestamp)
except Exception as e:
print(f"{get_now()} [AUTO] C'era garda ma si è spaccato")
# Sorta il db per data, lo salva di nuovo sul json
db = dict(sorted(db.items()))
j = json.dumps(db)
with open(config.DB_ACQUA, "w") as f:
f.write(j)
print(f"{get_now()} [AUTO] Turni acqua aggiornati.")
async def send_date_to_infludb(timestamp):
import influxdb_client
from influxdb_client import Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
token = config.INFLUXDB_TOKEN
org = "Casa Trifase"
url = "http://192.168.1.222:8086"
write_client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)
bucket="turni-acqua"
write_api = write_client.write_api(write_options=SYNCHRONOUS)
point = Point("erogazione").field("fieldKey", True).time(timestamp, write_precision=WritePrecision.S)
write_api.write(bucket=bucket, org="Casa Trifase", record=point)
if __name__ == "__main__":
quartiere = 'Garda'
erogazioni = get_erogazioni(json_file=config.DB_ACQUA, quartiere=quartiere)
for erogazione in erogazioni:
#convert a YYYY-MM-DD string into a nanosecond timestamp
date_obj = datetime.datetime.strptime(erogazione, "%Y-%m-%d")
# Step 2: Convert the datetime object to a Unix timestamp in seconds
unix_timestamp = datetime.datetime.timestamp(date_obj)
# Step 3: Convert the Unix timestamp to nanoseconds
print(f'erogazione fieldKey=TRUE {int(unix_timestamp) + 10}')
# stats = fancy_stats(erogazioni, print_plot=False, limit_erogazioni=1000, only_data=False)
# print(stats)