File size: 1,929 Bytes
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
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 = ""):
    if city !="":
        url = f'http://api.weatherapi.com/v1/forecast.json?key={API_KEY}&q={city}&days=5'
        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=5'
        response = requests.get(url)
        if response.status_code == 200:
            return response.json()
        else:
            return None
    return None

def save_weather_data(city, data):
    global weather_cache
    timestamp = datetime.now()
    weather_cache[city] = {
        'data': data,
        'timestamp': timestamp
    }

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