-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.py
41 lines (31 loc) · 1.06 KB
/
cli.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
import click
import requests
SAMPLE_API_KEY = 'b1b15e88fa797225412429c1c50c122a1'
def current_weather(location, api_key=SAMPLE_API_KEY):
url = 'https://api.openweathermap.org/data/2.5/weather'
query_params = {
'q': location,
'appid': api_key,
}
response = requests.get(url, params=query_params)
return response.json()['weather'][0]['description']
@click.command()
@click.argument('location')
@click.option(
'--api-key', '-a',
help='your API key for the OpenWeatherMap API',
)
def main(location, api_key):
"""
A little weather tool that shows you the current weather in a LOCATION of
your choice. Provide the city name and optionally a two-digit country code.
Here are two examples:
1. London,UK
2. Canmore
You need a valid API key from OpenWeatherMap for the tool to work. You can
sign up for a free account at https://openweathermap.org/appid.
"""
weather = current_weather(location, api_key)
print(f"The weather in {location} right now: {weather}.")
if __name__ == "__main__":
main()