diff options
author | Sam Chudnick <sam@chudnick.com> | 2023-06-10 09:15:10 -0400 |
---|---|---|
committer | Sam Chudnick <sam@chudnick.com> | 2023-06-10 09:15:10 -0400 |
commit | e1144d41f1924ac61271a0370b45444068c360b5 (patch) | |
tree | 25c9395a5a59f5d1b96027b15d13bff75311efb8 | |
parent | a7b391f1e13d83f3b0b508707a5bbf3a730170fa (diff) |
Run files through autopep8
-rw-r--r-- | src/app.py | 21 | ||||
-rw-r--r-- | src/forms.py | 6 | ||||
-rw-r--r-- | src/library.py | 76 |
3 files changed, 61 insertions, 42 deletions
@@ -1,6 +1,12 @@ | |||
1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
2 | import json, requests, datetime, argparse, pytz, flask | 2 | import json |
3 | import library, forms | 3 | import requests |
4 | import datetime | ||
5 | import argparse | ||
6 | import pytz | ||
7 | import flask | ||
8 | import library | ||
9 | import forms | ||
4 | 10 | ||
5 | app = flask.Flask(__name__) | 11 | app = flask.Flask(__name__) |
6 | app.config['SECRET_KEY'] = "JAnmklasd39u2mnwim" | 12 | app.config['SECRET_KEY'] = "JAnmklasd39u2mnwim" |
@@ -14,7 +20,7 @@ def check_submission(location): | |||
14 | return flask.redirect(flask.url_for('weather', latitude=latitude, longitude=longitude)) | 20 | return flask.redirect(flask.url_for('weather', latitude=latitude, longitude=longitude)) |
15 | 21 | ||
16 | 22 | ||
17 | @app.route('/', methods=('GET','POST')) | 23 | @app.route('/', methods=('GET', 'POST')) |
18 | def index(): | 24 | def index(): |
19 | form = forms.WeatherForm() | 25 | form = forms.WeatherForm() |
20 | if form.validate_on_submit(): | 26 | if form.validate_on_submit(): |
@@ -24,11 +30,12 @@ def index(): | |||
24 | return flask.render_template("index.html", form=form) | 30 | return flask.render_template("index.html", form=form) |
25 | 31 | ||
26 | 32 | ||
27 | @app.route('/weather', methods=('GET','POST')) | 33 | @app.route('/weather', methods=('GET', 'POST')) |
28 | def weather(): | 34 | def weather(): |
29 | latitude = flask.request.args.get('latitude', type=str) | 35 | latitude = flask.request.args.get('latitude', type=str) |
30 | longitude = flask.request.args.get('longitude', type=str) | 36 | longitude = flask.request.args.get('longitude', type=str) |
31 | data = library.get_data(latitude, longitude) | 37 | data = library.get_data(latitude, longitude) |
38 | |||
32 | hour = library.get_current_rounded_time(data["timezone"]).hour | 39 | hour = library.get_current_rounded_time(data["timezone"]).hour |
33 | form = forms.WeatherForm() | 40 | form = forms.WeatherForm() |
34 | if form.validate_on_submit(): | 41 | if form.validate_on_submit(): |
@@ -36,10 +43,10 @@ def weather(): | |||
36 | return check_submission(location) | 43 | return check_submission(location) |
37 | else: | 44 | else: |
38 | return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime, | 45 | return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime, |
39 | weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon) | 46 | weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon) |
40 | 47 | ||
41 | 48 | ||
42 | @app.route('/location', methods=('GET','POST')) | 49 | @app.route('/location', methods=('GET', 'POST')) |
43 | def location(): | 50 | def location(): |
44 | location = flask.request.args.get('location', type=str) | 51 | location = flask.request.args.get('location', type=str) |
45 | url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" | 52 | url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" |
@@ -51,7 +58,7 @@ def location(): | |||
51 | point = data["results"][i] | 58 | point = data["results"][i] |
52 | choice_str = point["name"] + ", " | 59 | choice_str = point["name"] + ", " |
53 | if "admin1" in point: | 60 | if "admin1" in point: |
54 | choice_str += point["admin1"] + ", " | 61 | choice_str += point["admin1"] + ", " |
55 | if "country" in point: | 62 | if "country" in point: |
56 | choice_str += point["country"] | 63 | choice_str += point["country"] |
57 | choices.append((i, choice_str)) | 64 | choices.append((i, choice_str)) |
diff --git a/src/forms.py b/src/forms.py index ee08c0e..a19ce20 100644 --- a/src/forms.py +++ b/src/forms.py | |||
@@ -1,10 +1,12 @@ | |||
1 | import flask_wtf, wtforms | 1 | import flask_wtf |
2 | import wtforms | ||
3 | |||
2 | 4 | ||
3 | class WeatherForm(flask_wtf.FlaskForm): | 5 | class WeatherForm(flask_wtf.FlaskForm): |
4 | location = wtforms.StringField("Location") | 6 | location = wtforms.StringField("Location") |
5 | submit = wtforms.SubmitField("Submit") | 7 | submit = wtforms.SubmitField("Submit") |
6 | 8 | ||
9 | |||
7 | class LocationForm(flask_wtf.FlaskForm): | 10 | class LocationForm(flask_wtf.FlaskForm): |
8 | location = wtforms.SelectField("Location") | 11 | location = wtforms.SelectField("Location") |
9 | submit = wtforms.SubmitField("Submit") | 12 | submit = wtforms.SubmitField("Submit") |
10 | |||
diff --git a/src/library.py b/src/library.py index 38217fc..37ba3eb 100644 --- a/src/library.py +++ b/src/library.py | |||
@@ -1,32 +1,37 @@ | |||
1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
2 | import datetime, requests, json, pytz, sys | 2 | import datetime |
3 | 3 | import requests | |
4 | weather_codes = {0:"Clear Sky", 1:"Mainly Clear",2:"Partly Cloudy",3:"Overcast",45:"Fog",48:"Fog",51:"Light Drizzle",\ | 4 | import json |
5 | 53:"Moderate Drizzle",55:"Dense Drizzle",56:"Light Freezing Drizzle",57:"Freezing Drizzle",\ | 5 | import pytz |
6 | 61:"Light Rain",63:"Moderate Rain",65:"Heavy Rain",66:"Light Freezing Rain",67:"Freezing Rain",\ | 6 | import sys |
7 | 71:"Light Snowfall",73:"Moderate Snowfall",75:"Heavy Snowfall",77:"Snow Grains",80:"Slight Rain Showers",\ | 7 | |
8 | 81:"Moderate Rain Showers",82:"Heavy Rain Showers",85:"Slight Snow Showers",86:"Heavy Snow Showers",\ | 8 | weather_codes = {0: "Clear Sky", 1: "Mainly Clear", 2: "Partly Cloudy", 3: "Overcast", 45: "Fog", 48: "Fog", 51: "Light Drizzle", |
9 | 95:"Thunderstorm",96:"Strong Thunderstrom",99:"Heavy Thunderstrom"} | 9 | 53: "Moderate Drizzle", 55: "Dense Drizzle", 56: "Light Freezing Drizzle", 57: "Freezing Drizzle", |
10 | 10 | 61: "Light Rain", 63: "Moderate Rain", 65: "Heavy Rain", 66: "Light Freezing Rain", 67: "Freezing Rain", | |
11 | weather_icons = {0:('wi-day-sunny','wi-night-clear'), 1:('wi-day-sunny','wi-night-clear'), 2:('wi-cloudy','wi-night-partly-cloudy'),\ | 11 | 71: "Light Snowfall", 73: "Moderate Snowfall", 75: "Heavy Snowfall", 77: "Snow Grains", 80: "Slight Rain Showers", |
12 | 3:('wi-day-sunny-overcast','wi-night-cloudy'), 45:('wi-day-fog','wi-night-fog'), 48:('wi-day-fog','wi-night-fog'), \ | 12 | 81: "Moderate Rain Showers", 82: "Heavy Rain Showers", 85: "Slight Snow Showers", 86: "Heavy Snow Showers", |
13 | 51:('wi-day-rain','wi-night-rain'),53:('wi-day-rain','wi-night-rain'),55:('wi-day-rain','wi-night-rain'),\ | 13 | 95: "Thunderstorm", 96: "Strong Thunderstrom", 99: "Heavy Thunderstrom"} |
14 | 56:('wi-day-rain','wi-night-rain'),57:('wi-day-rain','wi-night-rain'),61:('wi-day-rain','wi-night-rain'),\ | 14 | |
15 | 63:('wi-day-rain','wi-night-rain'),65:('wi-day-rain','wi-night-rain'),66:('wi-day-rain','wi-night-rain'),\ | 15 | weather_icons = {0: ('wi-day-sunny', 'wi-night-clear'), 1: ('wi-day-sunny', 'wi-night-clear'), 2: ('wi-cloudy', 'wi-night-partly-cloudy'), |
16 | 67:('wi-day-rain','wi-night-rain'),71:('wi-day-snow','wi-night-snow'),73:('wi-day-snow','wi-night-snow'),\ | 16 | 3: ('wi-day-sunny-overcast', 'wi-night-cloudy'), 45: ('wi-day-fog', 'wi-night-fog'), 48: ('wi-day-fog', 'wi-night-fog'), |
17 | 71:('wi-day-snow','wi-night-snow'),75:('wi-day-snow','wi-night-snow'),77:('wi-day-snow','wi-night-snow'),\ | 17 | 51: ('wi-day-rain', 'wi-night-rain'), 53: ('wi-day-rain', 'wi-night-rain'), 55: ('wi-day-rain', 'wi-night-rain'), |
18 | 80:('wi-day-showers','wi-night-showers'),81:('wi-day-showers','wi-night-showers'),82:('wi-day-showers','wi-night-showers'),\ | 18 | 56: ('wi-day-rain', 'wi-night-rain'), 57: ('wi-day-rain', 'wi-night-rain'), 61: ('wi-day-rain', 'wi-night-rain'), |
19 | 85:('wi-day-snow','wi-night-snow'),86:('wi-day-snow','wi-night-snow'),95:('wi-day-storm-showers','wi-night-storm-showers'),\ | 19 | 63: ('wi-day-rain', 'wi-night-rain'), 65: ('wi-day-rain', 'wi-night-rain'), 66: ('wi-day-rain', 'wi-night-rain'), |
20 | 96:('wi-day-storm-showers','wi-night-storm-showers'),99:('wi-day-storm-showers','wi-night-storm-showers')} | 20 | 67: ('wi-day-rain', 'wi-night-rain'), 71: ('wi-day-snow', 'wi-night-snow'), 73: ('wi-day-snow', 'wi-night-snow'), |
21 | 71: ('wi-day-snow', 'wi-night-snow'), 75: ('wi-day-snow', 'wi-night-snow'), 77: ('wi-day-snow', 'wi-night-snow'), | ||
22 | 80: ('wi-day-showers', 'wi-night-showers'), 81: ('wi-day-showers', 'wi-night-showers'), 82: ('wi-day-showers', 'wi-night-showers'), | ||
23 | 85: ('wi-day-snow', 'wi-night-snow'), 86: ('wi-day-snow', 'wi-night-snow'), 95: ('wi-day-storm-showers', 'wi-night-storm-showers'), | ||
24 | 96: ('wi-day-storm-showers', 'wi-night-storm-showers'), 99: ('wi-day-storm-showers', 'wi-night-storm-showers')} | ||
21 | 25 | ||
22 | 26 | ||
23 | def get_direction_icon(degrees): | 27 | def get_direction_icon(degrees): |
24 | directions = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','NWN','NW','NNW','N'] | 28 | directions = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', |
25 | icons = {'N':'north','NNE':None,'NE':'north-east','ENE':None,'E':'east','ESE':None,'SE':'south-east','SSE':None,\ | 29 | 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'NWN', 'NW', 'NNW', 'N'] |
26 | 'S':'south','SSW':None,'SW':'south-west','WSW':None,'W':'west','NWN':None,'NW':'north-west','NNW':None,'N':'north'} | 30 | icons = {'N': 'north', 'NNE': None, 'NE': 'north-east', 'ENE': None, 'E': 'east', 'ESE': None, 'SE': 'south-east', 'SSE': None, |
31 | 'S': 'south', 'SSW': None, 'SW': 'south-west', 'WSW': None, 'W': 'west', 'NWN': None, 'NW': 'north-west', 'NNW': None, 'N': 'north'} | ||
27 | points = 8 | 32 | points = 8 |
28 | step = 360 / points | 33 | step = 360 / points |
29 | index = (round((degrees + step / 2) / step) * 16) / points; | 34 | index = (round((degrees + step / 2) / step) * 16) / points |
30 | direction = directions[int(index)] | 35 | direction = directions[int(index)] |
31 | return icons[direction] | 36 | return icons[direction] |
32 | 37 | ||
@@ -37,7 +42,7 @@ def get_lat_long(location): | |||
37 | headers = {"User-Agent": "pywttr 0.1"} | 42 | headers = {"User-Agent": "pywttr 0.1"} |
38 | data = requests.get(url, headers=headers).json() | 43 | data = requests.get(url, headers=headers).json() |
39 | if len(data["results"]) > 1: | 44 | if len(data["results"]) > 1: |
40 | return 0,0 | 45 | return 0, 0 |
41 | else: | 46 | else: |
42 | latitude = data["results"][0]["latitude"] | 47 | latitude = data["results"][0]["latitude"] |
43 | longitude = data["results"][0]["longitude"] | 48 | longitude = data["results"][0]["longitude"] |
@@ -57,15 +62,20 @@ def get_data(latitude, longitude): | |||
57 | data = requests.get(url, headers=headers).json() | 62 | data = requests.get(url, headers=headers).json() |
58 | 63 | ||
59 | for i in range(len(data["hourly"]["time"])): | 64 | for i in range(len(data["hourly"]["time"])): |
60 | data["hourly"]["time"][i] = datetime.datetime.strptime(data["hourly"]["time"][i], '%Y-%m-%dT%H:%M').strftime('%a %x %I:%M %p') | 65 | data["hourly"]["time"][i] = datetime.datetime.strptime( |
66 | data["hourly"]["time"][i], '%Y-%m-%dT%H:%M').strftime('%a %x %I:%M %p') | ||
61 | for i in range(len(data["daily"]["time"])): | 67 | for i in range(len(data["daily"]["time"])): |
62 | data["daily"]["time"][i] = datetime.datetime.strptime(data["daily"]["time"][i], '%Y-%m-%d').strftime('%a %x') | 68 | data["daily"]["time"][i] = datetime.datetime.strptime( |
69 | data["daily"]["time"][i], '%Y-%m-%d').strftime('%a %x') | ||
63 | for i in range(len(data["daily"]["sunrise"])): | 70 | for i in range(len(data["daily"]["sunrise"])): |
64 | data["daily"]["sunrise"][i] = datetime.datetime.strptime(data["daily"]["sunrise"][i], '%Y-%m-%dT%H:%M').strftime('%I:%M %p') | 71 | data["daily"]["sunrise"][i] = datetime.datetime.strptime( |
72 | data["daily"]["sunrise"][i], '%Y-%m-%dT%H:%M').strftime('%I:%M %p') | ||
65 | for i in range(len(data["daily"]["sunset"])): | 73 | for i in range(len(data["daily"]["sunset"])): |
66 | data["daily"]["sunset"][i] = datetime.datetime.strptime(data["daily"]["sunset"][i], '%Y-%m-%dT%H:%M').strftime('%I:%M %p') | 74 | data["daily"]["sunset"][i] = datetime.datetime.strptime( |
75 | data["daily"]["sunset"][i], '%Y-%m-%dT%H:%M').strftime('%I:%M %p') | ||
67 | 76 | ||
68 | data["current_weather"]["time"] = datetime.datetime.strptime(data["current_weather"]["time"], '%Y-%m-%dT%H:%M').strftime('%a %x %I:%M %p') | 77 | data["current_weather"]["time"] = datetime.datetime.strptime( |
78 | data["current_weather"]["time"], '%Y-%m-%dT%H:%M').strftime('%a %x %I:%M %p') | ||
69 | 79 | ||
70 | return data | 80 | return data |
71 | 81 | ||
@@ -74,12 +84,13 @@ def get_current_rounded_time(tz_str): | |||
74 | # Gets current time rounded down to the hour | 84 | # Gets current time rounded down to the hour |
75 | tz = pytz.timezone(tz_str) | 85 | tz = pytz.timezone(tz_str) |
76 | cur_time = datetime.datetime.now(tz=tz) | 86 | cur_time = datetime.datetime.now(tz=tz) |
77 | cur_time_rounded = cur_time.replace(second=0, microsecond=0, minute=0, hour=cur_time.hour) | 87 | cur_time_rounded = cur_time.replace( |
88 | second=0, microsecond=0, minute=0, hour=cur_time.hour) | ||
78 | return cur_time_rounded | 89 | return cur_time_rounded |
79 | 90 | ||
80 | 91 | ||
81 | def make_current(values): | 92 | def make_current(values): |
82 | # Takes a list of weather data values | 93 | # Takes a list of weather data values |
83 | # and removes items from before the current time | 94 | # and removes items from before the current time |
84 | # (to the nearest hour) | 95 | # (to the nearest hour) |
85 | ret = [] | 96 | ret = [] |
@@ -90,6 +101,5 @@ def make_current(values): | |||
90 | return ret | 101 | return ret |
91 | 102 | ||
92 | 103 | ||
93 | def translate_weather_code(weather_code:int): | 104 | def translate_weather_code(weather_code: int): |
94 | return weather_codes[weather_code] | 105 | return weather_codes[weather_code] |
95 | |||