-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweather.py
60 lines (52 loc) · 1.7 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
import requests
import typer
import inquirer
from yaspin import yaspin
import pyfiglet
import datetime
from tabulate import tabulate
API_KEY="XXXXXXXXXXXXXXXXXX"
units = dict(
metric="°C",
imperial="°F"
)
def weatherDataToTable(data, unit):
day=datetime.datetime.fromtimestamp(data['dt'])
return [f"{day:%d.%m.%Y}", minMaxTemp(data, unit)]
def fetchWeeklyWeather(lon, lat, unit):
params = dict(
lon=lon,
lat=lat,
units= unit,
exclude="current,hourly,minutely,alerts",
appid= API_KEY
)
API_URL = "http://api.openweathermap.org/data/2.5/onecall"
response = requests.get(url=API_URL, params=params)
return response.json()["daily"]
@yaspin(text="Fetching weather data...")
def fetchWeather(city, country, unit):
params = dict(
q=f"{city},{country}",
units= unit,
appid= API_KEY
)
API_URL = "http://api.openweathermap.org/data/2.5/weather"
response = requests.get(url=API_URL, params=params)
lon=response.json()["coord"]["lon"]
lat=response.json()["coord"]["lat"]
return fetchWeeklyWeather(lon, lat, unit)
def minMaxTemp(data, unit):
return "{}{} to {}{}".format(round(data["temp"]["min"]), units[unit], round(data["temp"]["max"]), units[unit])
def main(
city: str = typer.Option(..., prompt=True),
country: str = typer.Option(..., prompt=True)
):
unit = inquirer.list_input("Metric or imperial?", choices=['metric', 'imperial'])
weatherData=fetchWeather(city, country, unit)
print(pyfiglet.figlet_format(minMaxTemp(weatherData[0], unit)))
tabledata = map(lambda x: weatherDataToTable(x, unit), weatherData)
headers = ["Date", "Temperature range"]
print(tabulate(tabledata, headers, tablefmt="fancy_grid"))
if __name__ == "__main__":
typer.run(main)