-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetodiBot.py
267 lines (230 loc) · 9.19 KB
/
MetodiBot.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
import requests
from dotenv import load_dotenv
import os
from datetime import timedelta
import time, datetime
import MetodiTg
load_dotenv()
api_key = os.getenv("API_BOT_TOKEN")
def unix_to_datetime(unix_timestamp):
"""
Converts a Unix timestamp to a human-readable date/time string.
Args:
unix_timestamp (int or float): The Unix timestamp to be converted.
Returns:
str: A string representing the human-readable date/time.
"""
giorno = time.strftime('%d-%m', time.localtime(unix_timestamp))
ora = time.strftime('%H:%M', time.localtime(unix_timestamp))
return giorno, ora
def get_weather_data(city_name, days_min, days_max):
# API endpoint URL
url1 = f'http://api.openweathermap.org/geo/1.0/direct?'
url2 = f'q={city_name}&limit={1}&appid={api_key}'
url3 = url1+url2
# Send the API request
responseGeo = requests.get(url3)
# Check if the request was successful
if responseGeo.status_code == 200:
# Get the weather data
geo_data = responseGeo.json()
# Extract relevant weather information
name = geo_data[0]['name']
lat = geo_data[0]['lat']
lon = geo_data[0]['lon']
country = geo_data[0]['country']
state = geo_data[0]['state']
else:
print('Error retrieving geographic data')
# API endpoint URL
url = f'https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&lang={"IT"}&units={"metric"}&appid={api_key}'
# Send the API request
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Get the weather data
weather_data = response.json()
else:
print('Error retrieving weather data')
nome_citta = weather_data['city']['name']
text = f"*Ecco le previsioni per {nome_citta}*.\n\n"
for i in range(0, len(weather_data['list'])):
min_epoch = mattina_del_giorno(days_min)
max_epoch = get_future_epoch(days_max)
quando_dt = weather_data['list'][i]['dt']
if quando_dt <= min_epoch:
continue
if quando_dt >= max_epoch:
break
giorno, ora = unix_to_datetime(quando_dt)
# weather
weather = weather_data['list'][i]['weather']
weather_id = weather[0]['id']
weather_main = weather[0]['main']
weather_description = weather[0]['description']
weather_icon = weather[0]['icon']
# misc main
misc_main = weather_data['list'][i]['main']
temp = misc_main['temp']
temp_feels_like = misc_main['feels_like']
humidity = misc_main['humidity']
# probability of precipitation
pop = str(int(weather_data['list'][i]['pop']) * 100) + "%"
# Rain volume for last 3 hours, mm
try:
rain_v = weather_data['list'][i]['rain']['3h']
rain_v = str(rain_v) + str("mm")
except:
rain_v= """non ha piovuto"""
text = text + str(f"""*Il giorno {giorno}, alle ore {ora}*,
e' previsto *{weather_description}*.
Con una temperatura di {temp}°C, percepita come {temp_feels_like}°C.
Probabilita' di precipitazioni: {pop}
Precipitazioni nelle ultime 3 ore: {rain_v}\n
""")
return text
def get_weather_data_single_day(city_name, days_from_now):
# API endpoint URL
url1 = f'http://api.openweathermap.org/geo/1.0/direct?'
url2 = f'q={city_name}&limit={1}&appid={api_key}'
url3 = url1+url2
# Send the API request
responseGeo = requests.get(url3)
# Check if the request was successful
if responseGeo.status_code == 200:
# Get the weather data
geo_data = responseGeo.json()
# Extract relevant weather information
name = geo_data[0]['name']
lat = geo_data[0]['lat']
lon = geo_data[0]['lon']
country = geo_data[0]['country']
state = geo_data[0]['state']
e_country = MetodiTg.escape_special_chars(country)
e_state = MetodiTg.escape_special_chars(state)
else:
print('Error retrieving geographic data')
# API endpoint URL
url = f'https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&lang={"IT"}&units={"metric"}&appid={api_key}'
# Send the API request
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Get the weather data
weather_data = response.json()
else:
print('Error retrieving weather data')
nome_citta = weather_data['city']['name']
e_nome_citta = MetodiTg.escape_special_chars(nome_citta)
text = f"*🪟Ecco le previsioni per _{e_nome_citta}_, {e_state}, {e_country}*🌡️\.\n@meteoSequoiaBot"
for i in range(0, len(weather_data['list'])):
min_epoch = mattina_del_giorno(days_from_now)
max_epoch = mezzanotte_del_giorno(days_from_now)
quando_dt = weather_data['list'][i]['dt']
if quando_dt < min_epoch:
print("dato skippato perche' troppo presto")
continue
if quando_dt >= max_epoch:
print("dato skippato perche' troppo tardi")
break
giorno, ora = unix_to_datetime(quando_dt)
e_giorno = MetodiTg.escape_special_chars(giorno)
# weather
weather = weather_data['list'][i]['weather']
weather_id = weather[0]['id']
emote = emoticon_for_id(weather_id)
weather_main = weather[0]['main']
weather_description = weather[0]['description']
weather_icon = weather[0]['icon']
# misc main
misc_main = weather_data['list'][i]['main']
temp = misc_main['temp']
temp_feels_like = misc_main['feels_like']
e_temp = MetodiTg.escape_special_chars(str(temp))
e_temp_feels_like = MetodiTg.escape_special_chars(str(temp_feels_like))
humidity = misc_main['humidity']
# probability of precipitation
pop = str(int(float(weather_data['list'][i]['pop']) * 100)) + "%"
e_pop = MetodiTg.escape_special_chars(pop)
text = text + str(f"""\n
> *Il giorno {e_giorno}, alle ore {ora}*,
è previsto *{weather_description}{emote}*\.
Con una temperatura di *{e_temp}°C*""")
if abs(float(temp)- float(temp_feels_like)) > 1:
text = text + str(f""", *percepita come {e_temp_feels_like}°C*""")
text = text + str(f"""\.\n_Probabilita' di precipitazioni: *{e_pop}*_""")
# Rain volume for last 3 hours, mm
try:
rain_v = weather_data['list'][i]['rain']['3h']
rain_v = MetodiTg.escape_special_chars(str(rain_v)) + str("mm")
text = text + str(f"""\n_Precipitazioni nelle ultime 3 ore: *{rain_v}*_\.""")
except:
print("""non ha piovuto""")
return text
def get_future_epoch(days):
"""
Returns the Unix epoch time for the future date after adding the specified number of days.
Args:
days (int): The number of days to add to the current time.
Returns:
int: The Unix epoch time for the future date.
"""
# Get the current Unix epoch time
current_epoch = time.time()
# Calculate the number of seconds in the specified number of days
seconds_in_days = days * 24 * 60 * 60
# Add the number of seconds to the current epoch time
future_epoch = current_epoch + seconds_in_days
return int(future_epoch)
def mattina_del_giorno(giorni_da_aspettare):
future_epoch = get_future_epoch(giorni_da_aspettare)
future_date = datetime.datetime.fromtimestamp(future_epoch)
morning_time = datetime.time(7, 50) # 7:50 AM
morning_datetime = datetime.datetime.combine(future_date, morning_time)
morning_timestamp = morning_datetime.timestamp()
return morning_timestamp
def mezzanotte_del_giorno(giorni_da_aspettare):
future_epoch = get_future_epoch(giorni_da_aspettare)
future_date = datetime.datetime.fromtimestamp(future_epoch)
midnight_time = datetime.time(23, 50) # 7:50 AM
midnight_datetime = datetime.datetime.combine(future_date, midnight_time)
midnight_timestamp = midnight_datetime.timestamp()
return midnight_timestamp
def gestioneGiorni():
# Ottieni la data corrente
oggi = datetime.datetime.now()
# Crea un array per i giorni della settimana
giorni_settimana = ['lun', 'mar', 'mer', 'gio', 'ven', 'sab', 'dom']
# Calcola i prossimi 7 giorni
prossimi_giorni = [oggi + timedelta(days=i) for i in range(7)]
# Formatta l'output nel modo desiderato
risultato = [[f"{giorni_settimana[data.weekday()]}_{data.strftime('%d-%m-%y')}"] for data in prossimi_giorni]
return risultato
def emoticon_for_id(id):
id = int(id)
if 200 <= id <= 232:
return "🌩️"
if 300 <= id <= 321:
return "🌧️"
if id == 500 or id == 501:
return "☔"
if id == 511:
return "🌧️🌨️"
if 502 <= id <= 504:
return "🌧️💧"
if 520 <= id <= 531:
return "🌧️☂️"
if 600<= id <= 622:
return "🌨️❄️"
if 701 <= id <= 781:
return "🌫️🌫️"
if id == 800:
return "☀️"
if id == 801:
return "🌤️🌤️"
if id == 802:
return "⛅⛅"
if id == 803:
return "🌥️🌥️"
if id == 804:
return "☁️☁️"