-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
156 lines (127 loc) · 4.76 KB
/
weather.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# weather.py
# import packages:
import argparse
import json
import sys
from configparser import ConfigParser
from urllib import error, parse, request
# import style.py:
import style
BASE_WEATHER_API_URL = "http://api.openweathermap.org/data/2.5/weather"
# Weather Condition Codes
# https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2
THUNDERSTORM = range(200, 300)
DRIZZLE = range(300, 400)
RAIN = range(500, 600)
SNOW = range(600, 700)
ATMOSPHERE = range(700, 800)
CLEAR = range(800, 801)
CLOUDY = range(801, 900)
def read_user_cli_args():
"""Handles the CLI user interactions.
Returns:
argparse.Namespace : Populated namesoace object
"""
parser = argparse.ArgumentParser(
description="Gets weather and temp info for a selected city."
)
parser.add_argument(
"city", nargs="+", type=str, help="Enter the city name:"
)
parser.add_argument(
"-i",
"--imperial",
action="store_true",
help="display the temp in imperial units"
)
return parser.parse_args()
def build_weather_query(city_input, imperial=False):
"""Builds the URL for an API request to OpenWeather API.
Args:
city_input (List[str]): Name of the city as collected by argparse.
imperial (bool): Whether or not to use imperial units for temperature
Return:
str: URL formatted for a call to OpenWeather's city name endpoint.
"""
api_key = _get_api_key()
city_name = " ".join(city_input)
url_encoded_city_name = parse.quote_plus(city_name)
units = "imperial" if imperial else "metric"
url = (
f"{BASE_WEATHER_API_URL}?q={url_encoded_city_name}"
f"&units={units}&appid={api_key}"
)
return url
def _get_api_key():
"""Fetch the API key from the config file.
Expects a config file named 'secrets.ini' with a structure:
[opoenweather]
api_key=<API_KEY>
"""
config = ConfigParser()
config.read("secrets.ini")
return config["openweather"]["api_key"]
def get_weather_data(query_url):
"""Makes API request to a URL and returns the data as a Python object.
Args:
query_url (str): URL formatted for OpenWeather's city name endpoint
Returns:
dict: Weather information for a specific city
"""
try:
response = request.urlopen(query_url)
except error.HTTPError as http_error:
if http_error.code == 401:
sys.exit("Access Denied. CHeck your API Key..")
elif http_error.code == 404:
sys.exit("Can't find weather data for this city")
else:
sys.exit(f"Something went wrong.. ({http_error.code})")
data = response.read()
try:
return json.loads(data)
except json.JSONDecodeError:
sys.exit("Couldn't read the server response.. Please try again.")
def display_weather_info(weather_data, imperial=False):
"""Prints the formatted weather info about a city.
Args:
weather_data (dict): API response from OpenWeather by city name.
imperial (bool): Whether or not to use inperial units for temperature.
More information at https://openweathermap.org/current#name
"""
city = weather_data["name"]
weather_id = weather_data["weather"][0]["id"]
weather_description = weather_data["weather"][0]["description"]
temperature = weather_data["main"]["temp"]
style.change_color(style.REVERSE)
print(f"{city:^{style.PADDING}}", end="")
style.change_color(style.RESET)
weather_symbol, color = _select_weather_display_params(weather_id)
style.change_color(color)
print(f"\t{weather_symbol}", end="")
print(f"\t{weather_description.capitalize():^{style.PADDING}}", end="")
style.change_color(style.RESET)
print(f"({temperature}°{'F' if imperial else 'C'})")
def _select_weather_display_params(weather_id):
if weather_id in THUNDERSTORM:
display_params = ("💥", style.RED)
elif weather_id in DRIZZLE:
display_params = ("💧", style.CYAN)
elif weather_id in RAIN:
display_params = ("💦", style.BLUE)
elif weather_id in SNOW:
display_params = ("⛄️", style.WHITE)
elif weather_id in ATMOSPHERE:
display_params = ("🌀", style.BLUE)
elif weather_id in CLEAR:
display_params = ("🔆", style.YELLOW)
elif weather_id in CLOUDY:
display_params = ("💨", style.WHITE)
else: # In case the API adds new weather codes
display_params = ("🌈", style.RESET)
return display_params
if __name__ == "__main__":
user_args = read_user_cli_args()
query_url = build_weather_query(user_args.city, user_args.imperial)
weather_data = get_weather_data(query_url)
display_weather_info(weather_data, user_args.imperial)