|
1 | 1 | import requests
|
2 | 2 |
|
3 |
| -APPID = "" # <-- Put your OpenWeatherMap appid here! |
4 |
| -URL_BASE = "https://api.openweathermap.org/data/2.5/" |
5 |
| - |
6 |
| - |
7 |
| -def current_weather(q: str = "Chicago", appid: str = APPID) -> dict: |
8 |
| - """https://openweathermap.org/api""" |
9 |
| - return requests.get(URL_BASE + "weather", params=locals()).json() |
10 |
| - |
11 |
| - |
12 |
| -def weather_forecast(q: str = "Kolkata, India", appid: str = APPID) -> dict: |
13 |
| - """https://openweathermap.org/forecast5""" |
14 |
| - return requests.get(URL_BASE + "forecast", params=locals()).json() |
15 |
| - |
16 |
| - |
17 |
| -def weather_onecall(lat: float = 55.68, lon: float = 12.57, appid: str = APPID) -> dict: |
18 |
| - """https://openweathermap.org/api/one-call-api""" |
19 |
| - return requests.get(URL_BASE + "onecall", params=locals()).json() |
| 3 | +API_KEY = "" # <-- Put your free Weatherstack API key here! |
| 4 | +URL_BASE = "http://api.weatherstack.com/current" |
20 | 5 |
|
| 6 | +def current_weather(location: str, api_key: str = API_KEY) -> dict: |
| 7 | + params = { |
| 8 | + "access_key": api_key, |
| 9 | + "query": location |
| 10 | + } |
| 11 | + return requests.get(URL_BASE, params=params).json() |
21 | 12 |
|
22 | 13 | if __name__ == "__main__":
|
23 | 14 | from pprint import pprint
|
24 | 15 |
|
25 | 16 | while True:
|
26 |
| - location = input("Enter a location:").strip() |
| 17 | + location = input("Enter a location (city name): ").strip() |
27 | 18 | if location:
|
28 |
| - pprint(current_weather(location)) |
| 19 | + pprint(current_weather(location, API_KEY)) |
29 | 20 | else:
|
30 | 21 | break
|
0 commit comments