Spaces:
Sleeping
Sleeping
File size: 2,740 Bytes
3067763 320a5a6 |
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 |
import requests
from datetime import datetime, timedelta
def is_time_difference_greater_than_one_hour(time1, time2):
datetime1 = datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
datetime2 = datetime.strptime(time2, "%Y-%m-%d %H:%M:%S")
difference = abs(datetime1 - datetime2)
return difference >= timedelta(hours=1)
API_KEY = '0eab9492e3024ce4969105357241507'
weather_cache = {}
date_check = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def get_weather_data(city = "",lat = "",lon = "",day = 5):
if city !="":
url = f'http://api.weatherapi.com/v1/forecast.json?key={API_KEY}&q={city}&days={day}'
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return None
elif lat != "" and lon != "":
url = f'http://api.weatherapi.com/v1/forecast.json?key={API_KEY}&q={lat},{lon}&days={day}'
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return None
return None
def more_weather_data(city = '',lat = '',lon = ''):
global weather_cache
#print(weather_cache[lat+lon])
if city != '':
wanted_day = weather_cache[city]['days'] + 4
weather_cache.pop(city,None)
data = get_weather_data(city = city, day = wanted_day)
weather_cache[city] ={
'data': data,
'timestamp': datetime.now(),
'days' : wanted_day
}
return data
elif lat != '' and lon != '':
wanted_day = weather_cache[lat+lon]['days'] + 4
weather_cache.pop(lat+lon,None)
data = get_weather_data(lat = lat, lon = lon, day = wanted_day)
weather_cache[lat+lon] ={
'data': data,
'timestamp': datetime.now(),
'days' : wanted_day
}
return data
return None
def save_weather_data(city, data):
global weather_cache
timestamp = datetime.now()
weather_cache[city] = {
'data': data,
'timestamp': timestamp,
'days' : 5
}
def get_cached_weather_data(city):
global weather_cache, date_check
#Remove all weather_cache when more than 50 caches or There's a one-hour time difference.
if len(weather_cache) >= 50 or is_time_difference_greater_than_one_hour(date_check,datetime.now().strftime("%Y-%m-%d %H:%M:%S")):
weather_cache = {}
date_check = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cached_data = weather_cache.get(city)
if cached_data:
# Check if the cached data is from today
if cached_data['timestamp'].date() == datetime.now().date():
return cached_data['data']
return None |