From e1144d41f1924ac61271a0370b45444068c360b5 Mon Sep 17 00:00:00 2001 From: Sam Chudnick Date: Sat, 10 Jun 2023 09:15:10 -0400 Subject: Run files through autopep8 --- src/app.py | 21 ++++++++++------ src/forms.py | 6 +++-- src/library.py | 76 +++++++++++++++++++++++++++++++++------------------------- 3 files changed, 61 insertions(+), 42 deletions(-) diff --git a/src/app.py b/src/app.py index 6426b04..6a59ac7 100644 --- a/src/app.py +++ b/src/app.py @@ -1,6 +1,12 @@ #!/usr/bin/env python3 -import json, requests, datetime, argparse, pytz, flask -import library, forms +import json +import requests +import datetime +import argparse +import pytz +import flask +import library +import forms app = flask.Flask(__name__) app.config['SECRET_KEY'] = "JAnmklasd39u2mnwim" @@ -14,7 +20,7 @@ def check_submission(location): return flask.redirect(flask.url_for('weather', latitude=latitude, longitude=longitude)) -@app.route('/', methods=('GET','POST')) +@app.route('/', methods=('GET', 'POST')) def index(): form = forms.WeatherForm() if form.validate_on_submit(): @@ -24,11 +30,12 @@ def index(): return flask.render_template("index.html", form=form) -@app.route('/weather', methods=('GET','POST')) +@app.route('/weather', methods=('GET', 'POST')) def weather(): latitude = flask.request.args.get('latitude', type=str) longitude = flask.request.args.get('longitude', type=str) data = library.get_data(latitude, longitude) + hour = library.get_current_rounded_time(data["timezone"]).hour form = forms.WeatherForm() if form.validate_on_submit(): @@ -36,10 +43,10 @@ def weather(): return check_submission(location) else: return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime, - weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon) + weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon) -@app.route('/location', methods=('GET','POST')) +@app.route('/location', methods=('GET', 'POST')) def location(): location = flask.request.args.get('location', type=str) url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" @@ -51,7 +58,7 @@ def location(): point = data["results"][i] choice_str = point["name"] + ", " if "admin1" in point: - choice_str += point["admin1"] + ", " + choice_str += point["admin1"] + ", " if "country" in point: choice_str += point["country"] 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 @@ -import flask_wtf, wtforms +import flask_wtf +import wtforms + class WeatherForm(flask_wtf.FlaskForm): location = wtforms.StringField("Location") submit = wtforms.SubmitField("Submit") + class LocationForm(flask_wtf.FlaskForm): location = wtforms.SelectField("Location") submit = wtforms.SubmitField("Submit") - 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 @@ #!/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')} +import datetime +import requests +import json +import pytz +import 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'} + 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; + index = (round((degrees + step / 2) / step) * 16) / points direction = directions[int(index)] return icons[direction] @@ -37,7 +42,7 @@ def get_lat_long(location): headers = {"User-Agent": "pywttr 0.1"} data = requests.get(url, headers=headers).json() if len(data["results"]) > 1: - return 0,0 + return 0, 0 else: latitude = data["results"][0]["latitude"] longitude = data["results"][0]["longitude"] @@ -57,15 +62,20 @@ def get_data(latitude, longitude): 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') + 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') + 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') + 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["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') + data["current_weather"]["time"] = datetime.datetime.strptime( + data["current_weather"]["time"], '%Y-%m-%dT%H:%M').strftime('%a %x %I:%M %p') return data @@ -74,12 +84,13 @@ 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) + 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 + # Takes a list of weather data values # and removes items from before the current time # (to the nearest hour) ret = [] @@ -90,6 +101,5 @@ def make_current(values): return ret -def translate_weather_code(weather_code:int): +def translate_weather_code(weather_code: int): return weather_codes[weather_code] - -- cgit v1.2.3