aboutsummaryrefslogtreecommitdiff
path: root/src/library.py
blob: 38217fccba630ba034a10db20956b64c85b264ea (plain)
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
#!/usr/bin/env python3
import datetime, requests, json, pytz, sys

weather_codes = {0:"Clear Sky", 1:"Mainly Clear",2:"Partly Cloudy",3:"Overcast",45:"Fog",48:"Fog",51:"Light Drizzle",\
                 53:"Moderate Drizzle",55:"Dense Drizzle",56:"Light Freezing Drizzle",57:"Freezing Drizzle",\
                 61:"Light Rain",63:"Moderate Rain",65:"Heavy Rain",66:"Light Freezing Rain",67:"Freezing Rain",\
                 71:"Light Snowfall",73:"Moderate Snowfall",75:"Heavy Snowfall",77:"Snow Grains",80:"Slight Rain Showers",\
                 81:"Moderate Rain Showers",82:"Heavy Rain Showers",85:"Slight Snow Showers",86:"Heavy Snow Showers",\
                 95:"Thunderstorm",96:"Strong Thunderstrom",99:"Heavy Thunderstrom"}

weather_icons = {0:('wi-day-sunny','wi-night-clear'), 1:('wi-day-sunny','wi-night-clear'), 2:('wi-cloudy','wi-night-partly-cloudy'),\
                 3:('wi-day-sunny-overcast','wi-night-cloudy'), 45:('wi-day-fog','wi-night-fog'), 48:('wi-day-fog','wi-night-fog'), \
                 51:('wi-day-rain','wi-night-rain'),53:('wi-day-rain','wi-night-rain'),55:('wi-day-rain','wi-night-rain'),\
                 56:('wi-day-rain','wi-night-rain'),57:('wi-day-rain','wi-night-rain'),61:('wi-day-rain','wi-night-rain'),\
                 63:('wi-day-rain','wi-night-rain'),65:('wi-day-rain','wi-night-rain'),66:('wi-day-rain','wi-night-rain'),\
                 67:('wi-day-rain','wi-night-rain'),71:('wi-day-snow','wi-night-snow'),73:('wi-day-snow','wi-night-snow'),\
                 71:('wi-day-snow','wi-night-snow'),75:('wi-day-snow','wi-night-snow'),77:('wi-day-snow','wi-night-snow'),\
                 80:('wi-day-showers','wi-night-showers'),81:('wi-day-showers','wi-night-showers'),82:('wi-day-showers','wi-night-showers'),\
                 85:('wi-day-snow','wi-night-snow'),86:('wi-day-snow','wi-night-snow'),95:('wi-day-storm-showers','wi-night-storm-showers'),\
                 96:('wi-day-storm-showers','wi-night-storm-showers'),99:('wi-day-storm-showers','wi-night-storm-showers')}


def get_direction_icon(degrees):
    directions = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','NWN','NW','NNW','N']
    icons = {'N':'north','NNE':None,'NE':'north-east','ENE':None,'E':'east','ESE':None,'SE':'south-east','SSE':None,\
            'S':'south','SSW':None,'SW':'south-west','WSW':None,'W':'west','NWN':None,'NW':'north-west','NNW':None,'N':'north'}
    points = 8
    step = 360 / points
    index = (round((degrees + step / 2) / step) * 16) / points;
    direction = directions[int(index)]
    return icons[direction]


def get_lat_long(location):
    # Converts a location into latitude and longitude
    url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json"
    headers = {"User-Agent": "pywttr 0.1"}
    data = requests.get(url, headers=headers).json()
    if len(data["results"]) > 1:
        return 0,0
    else:
        latitude = data["results"][0]["latitude"]
        longitude = data["results"][0]["longitude"]
        return latitude, longitude


def get_data(latitude, longitude):
    headers = {"User-Agent": "pywttr 0.1"}
    now = datetime.datetime.now()
    now_str = now.strftime("%Y-%m-%d")
    end_str = (now + datetime.timedelta(days=7)).strftime("%Y-%m-%d")

    url_base = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}"
    url_weather_params = "&current_weather=true&hourly=temperature_2m&hourly=relativehumidity_2m&hourly=apparent_temperature&hourly=precipitation_probability&hourly=precipitation&hourly=weathercode&hourly=windspeed_10m&hourly=winddirection_10m&daily=weathercode&daily=temperature_2m_max&daily=temperature_2m_min&daily=sunrise&daily=sunset&daily=precipitation_sum&daily=precipitation_probability_max&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch&timeformat=iso8601&past_days=0&forecast_days=7"
    url_date_time = f"&start_date={now_str}&end_date={end_str}&timezone=auto"
    url = url_base + url_weather_params + url_date_time
    data = requests.get(url, headers=headers).json()

    for i in range(len(data["hourly"]["time"])):
        data["hourly"]["time"][i] = datetime.datetime.strptime(data["hourly"]["time"][i], '%Y-%m-%dT%H:%M').strftime('%a %x %I:%M %p')
    for i in range(len(data["daily"]["time"])):
        data["daily"]["time"][i] = datetime.datetime.strptime(data["daily"]["time"][i], '%Y-%m-%d').strftime('%a %x')
    for i in range(len(data["daily"]["sunrise"])):
        data["daily"]["sunrise"][i] = datetime.datetime.strptime(data["daily"]["sunrise"][i], '%Y-%m-%dT%H:%M').strftime('%I:%M %p')
    for i in range(len(data["daily"]["sunset"])):
        data["daily"]["sunset"][i] = datetime.datetime.strptime(data["daily"]["sunset"][i], '%Y-%m-%dT%H:%M').strftime('%I:%M %p')

    data["current_weather"]["time"] = datetime.datetime.strptime(data["current_weather"]["time"], '%Y-%m-%dT%H:%M').strftime('%a %x %I:%M %p')

    return data


def get_current_rounded_time(tz_str):
    # Gets current time rounded down to the hour
    tz = pytz.timezone(tz_str)
    cur_time = datetime.datetime.now(tz=tz)
    cur_time_rounded = cur_time.replace(second=0, microsecond=0, minute=0, hour=cur_time.hour)
    return cur_time_rounded


def make_current(values):
    # Takes a list of weather data values 
    # and removes items from before the current time
    # (to the nearest hour)
    ret = []
    cur_time_rounded = get_current_rounded_time()
    for val in values:
        if val["time"] >= cur_time_rounded:
            ret.append(val)
    return ret


def translate_weather_code(weather_code:int):
    return weather_codes[weather_code]