From a7866aef1e9fe5bcda0051ead0f58f1ea018973c Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:11:18 +0530 Subject: [PATCH 01/22] Update current_weather.py --- web_programming/current_weather.py | 41 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 3ed4c8a95a0c..86ae6d7d046f 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -1,30 +1,31 @@ import requests -APPID = "" # <-- Put your OpenWeatherMap appid here! -URL_BASE = "https://api.openweathermap.org/data/2.5/" - - -def current_weather(q: str = "Chicago", appid: str = APPID) -> dict: - """https://openweathermap.org/api""" - return requests.get(URL_BASE + "weather", params=locals()).json() - - -def weather_forecast(q: str = "Kolkata, India", appid: str = APPID) -> dict: - """https://openweathermap.org/forecast5""" - return requests.get(URL_BASE + "forecast", params=locals()).json() - - -def weather_onecall(lat: float = 55.68, lon: float = 12.57, appid: str = APPID) -> dict: - """https://openweathermap.org/api/one-call-api""" - return requests.get(URL_BASE + "onecall", params=locals()).json() - +# Replace the API_KEY with your free Weatherstack API key +API_KEY = "" # <-- Put your free Weatherstack API key here! +URL_BASE = "http://api.weatherstack.com/current" + +# Function to fetch current weather data for a given location +def current_weather(location: str, api_key: str = API_KEY) -> dict: + # Prepare the parameters to send in the API request + params = { + "access_key": api_key, # API key for authentication + "query": location # The location (city name) for which weather data is requested + } + + # Make an HTTP GET request to the Weatherstack API and parse the response as JSON + response = requests.get(URL_BASE, params=params) + weather_data = response.json() + + return weather_data # Return the weather data as a dictionary if __name__ == "__main__": from pprint import pprint while True: - location = input("Enter a location:").strip() + location = input("Enter a location (city name): ").strip() + if location: - pprint(current_weather(location)) + # Fetch and print the current weather data for the specified location + pprint(current_weather(location, API_KEY)) else: break From d2fcd7e3d542d37dc18254b5bbc134927810c59a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 06:45:55 +0000 Subject: [PATCH 02/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_weather.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 86ae6d7d046f..3107bf1e02a7 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -4,26 +4,28 @@ API_KEY = "" # <-- Put your free Weatherstack API key here! URL_BASE = "http://api.weatherstack.com/current" + # Function to fetch current weather data for a given location def current_weather(location: str, api_key: str = API_KEY) -> dict: # Prepare the parameters to send in the API request params = { "access_key": api_key, # API key for authentication - "query": location # The location (city name) for which weather data is requested + "query": location, # The location (city name) for which weather data is requested } - + # Make an HTTP GET request to the Weatherstack API and parse the response as JSON response = requests.get(URL_BASE, params=params) weather_data = response.json() return weather_data # Return the weather data as a dictionary + if __name__ == "__main__": from pprint import pprint while True: location = input("Enter a location (city name): ").strip() - + if location: # Fetch and print the current weather data for the specified location pprint(current_weather(location, API_KEY)) From 34eb55d29e86f8d7a67551e7f0d4bb5e539a3448 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:45:01 +0530 Subject: [PATCH 03/22] Update current_weather.py --- web_programming/current_weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 3107bf1e02a7..e282ed2b00b5 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -10,7 +10,7 @@ def current_weather(location: str, api_key: str = API_KEY) -> dict: # Prepare the parameters to send in the API request params = { "access_key": api_key, # API key for authentication - "query": location, # The location (city name) for which weather data is requested + "query": location, } # Make an HTTP GET request to the Weatherstack API and parse the response as JSON From a7df8652af1c2d5f450d9d08825cd4adfcd5a2ab Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:45:37 +0530 Subject: [PATCH 04/22] Update current_weather.py --- web_programming/current_weather.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index e282ed2b00b5..3a34184706b4 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -10,8 +10,7 @@ def current_weather(location: str, api_key: str = API_KEY) -> dict: # Prepare the parameters to send in the API request params = { "access_key": api_key, # API key for authentication - "query": location, - } + "query": location, } # Make an HTTP GET request to the Weatherstack API and parse the response as JSON response = requests.get(URL_BASE, params=params) From 34a80750a8bd6c43b613ff3e283024041fc38193 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 07:16:12 +0000 Subject: [PATCH 05/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_weather.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 3a34184706b4..e282ed2b00b5 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -10,7 +10,8 @@ def current_weather(location: str, api_key: str = API_KEY) -> dict: # Prepare the parameters to send in the API request params = { "access_key": api_key, # API key for authentication - "query": location, } + "query": location, + } # Make an HTTP GET request to the Weatherstack API and parse the response as JSON response = requests.get(URL_BASE, params=params) From 4a78eab0b750d992ff81d0855b1034b3935f9d97 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:48:08 +0530 Subject: [PATCH 06/22] Update current_weather.py --- web_programming/current_weather.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index e282ed2b00b5..2e769c438de0 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -9,9 +9,8 @@ def current_weather(location: str, api_key: str = API_KEY) -> dict: # Prepare the parameters to send in the API request params = { - "access_key": api_key, # API key for authentication - "query": location, - } + "access_key": api_key, # API key for authentication +"query": location,} # Make an HTTP GET request to the Weatherstack API and parse the response as JSON response = requests.get(URL_BASE, params=params) From 5a4068e030661306395f8bbb18a7960cb5c12ab9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 07:18:43 +0000 Subject: [PATCH 07/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_weather.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 2e769c438de0..e282ed2b00b5 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -9,8 +9,9 @@ def current_weather(location: str, api_key: str = API_KEY) -> dict: # Prepare the parameters to send in the API request params = { - "access_key": api_key, # API key for authentication -"query": location,} + "access_key": api_key, # API key for authentication + "query": location, + } # Make an HTTP GET request to the Weatherstack API and parse the response as JSON response = requests.get(URL_BASE, params=params) From c80aa7b72a1414c69823c58f73ca28ba9f3e9a37 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:50:38 +0530 Subject: [PATCH 08/22] Update current_weather.py --- web_programming/current_weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index e282ed2b00b5..1e3125a9aba5 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -9,7 +9,7 @@ def current_weather(location: str, api_key: str = API_KEY) -> dict: # Prepare the parameters to send in the API request params = { - "access_key": api_key, # API key for authentication + "access_key": api_key,# API key for authentication "query": location, } From 1020c822183381019f56e59ecc83fa4e9f3d9014 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 07:21:13 +0000 Subject: [PATCH 09/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 1e3125a9aba5..e282ed2b00b5 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -9,7 +9,7 @@ def current_weather(location: str, api_key: str = API_KEY) -> dict: # Prepare the parameters to send in the API request params = { - "access_key": api_key,# API key for authentication + "access_key": api_key, # API key for authentication "query": location, } From 90e66382f0c7fa7b2dd8f36fb634c0ae64f4bb41 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:20:30 +0530 Subject: [PATCH 10/22] Update current_weather.py --- web_programming/current_weather.py | 71 ++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index e282ed2b00b5..f008a08d2500 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -1,33 +1,58 @@ import requests -# Replace the API_KEY with your free Weatherstack API key -API_KEY = "" # <-- Put your free Weatherstack API key here! -URL_BASE = "http://api.weatherstack.com/current" - - -# Function to fetch current weather data for a given location -def current_weather(location: str, api_key: str = API_KEY) -> dict: - # Prepare the parameters to send in the API request - params = { - "access_key": api_key, # API key for authentication - "query": location, - } - - # Make an HTTP GET request to the Weatherstack API and parse the response as JSON - response = requests.get(URL_BASE, params=params) +# Replace these API keys with your respective API keys +OPENWEATHERMAP_API_KEY = "" # <-- Put your OpenWeatherMap API key here! +WEATHERSTACK_API_KEY = "" # <-- Put your Weatherstack API key here! + +# Define the URL for OpenWeatherMap API with placeholders +OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" +WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" + +def current_weather(location: str, api_provider: str, api_key: str) -> dict: + if api_provider == "openweathermap": + # For OpenWeatherMap, we assume that location contains the name of the city + params = { + "q": location, + "appid": api_key + } + elif api_provider == "weatherstack": + # For Weatherstack, location can be a city name or latitude, longitude + if location.replace(',', '').replace('.', '').isdigit(): + # If location is numeric, assume it's latitude, longitude + lat, lon = location.split(',') + params = { + "lat": lat, + "lon": lon, + "appid": api_key + } + else: + params = { + "access_key": api_key, + "query": location + } + else: + raise ValueError("Invalid API provider. Use 'openweathermap' or 'weatherstack'.") + + response = requests.get(OPENWEATHERMAP_URL_BASE, params=params) if api_provider == "openweathermap" else requests.get(WEATHERSTACK_URL_BASE, params=params) weather_data = response.json() - - return weather_data # Return the weather data as a dictionary - + return weather_data if __name__ == "__main__": from pprint import pprint while True: - location = input("Enter a location (city name): ").strip() - - if location: - # Fetch and print the current weather data for the specified location - pprint(current_weather(location, API_KEY)) + location = input("Enter a location (city name or latitude,longitude): ").strip() + api_provider = input("Enter the API provider (openweathermap or weatherstack): ").strip() + + if location and api_provider: + try: + if api_provider == "openweathermap": + pprint(current_weather(location, api_provider, OPENWEATHERMAP_API_KEY)) + elif api_provider == "weatherstack": + pprint(current_weather(location, api_provider, WEATHERSTACK_API_KEY)) + else: + print("Invalid API provider. Use 'openweathermap' or 'weatherstack'.") + except ValueError as e: + print(e) else: break From 50ecb60a7f006cc4368c4de73e4536ba4e9ec894 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:51:20 +0000 Subject: [PATCH 11/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_weather.py | 48 +++++++++++++++++------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index f008a08d2500..a2e22bcf9e4b 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -8,50 +8,56 @@ OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" + def current_weather(location: str, api_provider: str, api_key: str) -> dict: if api_provider == "openweathermap": # For OpenWeatherMap, we assume that location contains the name of the city - params = { - "q": location, - "appid": api_key - } + params = {"q": location, "appid": api_key} elif api_provider == "weatherstack": # For Weatherstack, location can be a city name or latitude, longitude - if location.replace(',', '').replace('.', '').isdigit(): + if location.replace(",", "").replace(".", "").isdigit(): # If location is numeric, assume it's latitude, longitude - lat, lon = location.split(',') - params = { - "lat": lat, - "lon": lon, - "appid": api_key - } + lat, lon = location.split(",") + params = {"lat": lat, "lon": lon, "appid": api_key} else: - params = { - "access_key": api_key, - "query": location - } + params = {"access_key": api_key, "query": location} else: - raise ValueError("Invalid API provider. Use 'openweathermap' or 'weatherstack'.") + raise ValueError( + "Invalid API provider. Use 'openweathermap' or 'weatherstack'." + ) - response = requests.get(OPENWEATHERMAP_URL_BASE, params=params) if api_provider == "openweathermap" else requests.get(WEATHERSTACK_URL_BASE, params=params) + response = ( + requests.get(OPENWEATHERMAP_URL_BASE, params=params) + if api_provider == "openweathermap" + else requests.get(WEATHERSTACK_URL_BASE, params=params) + ) weather_data = response.json() return weather_data + if __name__ == "__main__": from pprint import pprint while True: location = input("Enter a location (city name or latitude,longitude): ").strip() - api_provider = input("Enter the API provider (openweathermap or weatherstack): ").strip() + api_provider = input( + "Enter the API provider (openweathermap or weatherstack): " + ).strip() if location and api_provider: try: if api_provider == "openweathermap": - pprint(current_weather(location, api_provider, OPENWEATHERMAP_API_KEY)) + pprint( + current_weather(location, api_provider, OPENWEATHERMAP_API_KEY) + ) elif api_provider == "weatherstack": - pprint(current_weather(location, api_provider, WEATHERSTACK_API_KEY)) + pprint( + current_weather(location, api_provider, WEATHERSTACK_API_KEY) + ) else: - print("Invalid API provider. Use 'openweathermap' or 'weatherstack'.") + print( + "Invalid API provider. Use 'openweathermap' or 'weatherstack'." + ) except ValueError as e: print(e) else: From f84de33d4c0b97e0513e296b19131ddbe5cebe49 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:24:17 +0530 Subject: [PATCH 12/22] Update current_weather.py --- web_programming/current_weather.py | 48 +++++++++++++----------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index a2e22bcf9e4b..f008a08d2500 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -8,56 +8,50 @@ OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" - def current_weather(location: str, api_provider: str, api_key: str) -> dict: if api_provider == "openweathermap": # For OpenWeatherMap, we assume that location contains the name of the city - params = {"q": location, "appid": api_key} + params = { + "q": location, + "appid": api_key + } elif api_provider == "weatherstack": # For Weatherstack, location can be a city name or latitude, longitude - if location.replace(",", "").replace(".", "").isdigit(): + if location.replace(',', '').replace('.', '').isdigit(): # If location is numeric, assume it's latitude, longitude - lat, lon = location.split(",") - params = {"lat": lat, "lon": lon, "appid": api_key} + lat, lon = location.split(',') + params = { + "lat": lat, + "lon": lon, + "appid": api_key + } else: - params = {"access_key": api_key, "query": location} + params = { + "access_key": api_key, + "query": location + } else: - raise ValueError( - "Invalid API provider. Use 'openweathermap' or 'weatherstack'." - ) + raise ValueError("Invalid API provider. Use 'openweathermap' or 'weatherstack'.") - response = ( - requests.get(OPENWEATHERMAP_URL_BASE, params=params) - if api_provider == "openweathermap" - else requests.get(WEATHERSTACK_URL_BASE, params=params) - ) + response = requests.get(OPENWEATHERMAP_URL_BASE, params=params) if api_provider == "openweathermap" else requests.get(WEATHERSTACK_URL_BASE, params=params) weather_data = response.json() return weather_data - if __name__ == "__main__": from pprint import pprint while True: location = input("Enter a location (city name or latitude,longitude): ").strip() - api_provider = input( - "Enter the API provider (openweathermap or weatherstack): " - ).strip() + api_provider = input("Enter the API provider (openweathermap or weatherstack): ").strip() if location and api_provider: try: if api_provider == "openweathermap": - pprint( - current_weather(location, api_provider, OPENWEATHERMAP_API_KEY) - ) + pprint(current_weather(location, api_provider, OPENWEATHERMAP_API_KEY)) elif api_provider == "weatherstack": - pprint( - current_weather(location, api_provider, WEATHERSTACK_API_KEY) - ) + pprint(current_weather(location, api_provider, WEATHERSTACK_API_KEY)) else: - print( - "Invalid API provider. Use 'openweathermap' or 'weatherstack'." - ) + print("Invalid API provider. Use 'openweathermap' or 'weatherstack'.") except ValueError as e: print(e) else: From 9c9fc6e2bbf959a167d29fe4567f244d5a3a76ae Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:54:59 +0000 Subject: [PATCH 13/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_weather.py | 48 +++++++++++++++++------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index f008a08d2500..a2e22bcf9e4b 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -8,50 +8,56 @@ OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" + def current_weather(location: str, api_provider: str, api_key: str) -> dict: if api_provider == "openweathermap": # For OpenWeatherMap, we assume that location contains the name of the city - params = { - "q": location, - "appid": api_key - } + params = {"q": location, "appid": api_key} elif api_provider == "weatherstack": # For Weatherstack, location can be a city name or latitude, longitude - if location.replace(',', '').replace('.', '').isdigit(): + if location.replace(",", "").replace(".", "").isdigit(): # If location is numeric, assume it's latitude, longitude - lat, lon = location.split(',') - params = { - "lat": lat, - "lon": lon, - "appid": api_key - } + lat, lon = location.split(",") + params = {"lat": lat, "lon": lon, "appid": api_key} else: - params = { - "access_key": api_key, - "query": location - } + params = {"access_key": api_key, "query": location} else: - raise ValueError("Invalid API provider. Use 'openweathermap' or 'weatherstack'.") + raise ValueError( + "Invalid API provider. Use 'openweathermap' or 'weatherstack'." + ) - response = requests.get(OPENWEATHERMAP_URL_BASE, params=params) if api_provider == "openweathermap" else requests.get(WEATHERSTACK_URL_BASE, params=params) + response = ( + requests.get(OPENWEATHERMAP_URL_BASE, params=params) + if api_provider == "openweathermap" + else requests.get(WEATHERSTACK_URL_BASE, params=params) + ) weather_data = response.json() return weather_data + if __name__ == "__main__": from pprint import pprint while True: location = input("Enter a location (city name or latitude,longitude): ").strip() - api_provider = input("Enter the API provider (openweathermap or weatherstack): ").strip() + api_provider = input( + "Enter the API provider (openweathermap or weatherstack): " + ).strip() if location and api_provider: try: if api_provider == "openweathermap": - pprint(current_weather(location, api_provider, OPENWEATHERMAP_API_KEY)) + pprint( + current_weather(location, api_provider, OPENWEATHERMAP_API_KEY) + ) elif api_provider == "weatherstack": - pprint(current_weather(location, api_provider, WEATHERSTACK_API_KEY)) + pprint( + current_weather(location, api_provider, WEATHERSTACK_API_KEY) + ) else: - print("Invalid API provider. Use 'openweathermap' or 'weatherstack'.") + print( + "Invalid API provider. Use 'openweathermap' or 'weatherstack'." + ) except ValueError as e: print(e) else: From 234a6fbeff0c73e7d277aa510d9510b4f2adc8df Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 17:33:47 +0530 Subject: [PATCH 14/22] Update current_weather.py --- web_programming/current_weather.py | 79 +++++++++++++----------------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index a2e22bcf9e4b..895ccbb6644a 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -1,63 +1,54 @@ import requests -# Replace these API keys with your respective API keys -OPENWEATHERMAP_API_KEY = "" # <-- Put your OpenWeatherMap API key here! -WEATHERSTACK_API_KEY = "" # <-- Put your Weatherstack API key here! - -# Define the URL for OpenWeatherMap API with placeholders +# Define the URL for the APIs with placeholders OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" - -def current_weather(location: str, api_provider: str, api_key: str) -> dict: - if api_provider == "openweathermap": - # For OpenWeatherMap, we assume that location contains the name of the city - params = {"q": location, "appid": api_key} - elif api_provider == "weatherstack": - # For Weatherstack, location can be a city name or latitude, longitude - if location.replace(",", "").replace(".", "").isdigit(): - # If location is numeric, assume it's latitude, longitude - lat, lon = location.split(",") - params = {"lat": lat, "lon": lon, "appid": api_key} - else: - params = {"access_key": api_key, "query": location} +def current_weather(location: str) -> tuple[dict]: + weather_data = () + + if OPENWEATHERMAP_API_KEY and WEATHERSTACK_API_KEY: + params_openweathermap = { + "q": location, + "appid": OPENWEATHERMAP_API_KEY + } + params_weatherstack = { + "query": location, + "access_key": WEATHERSTACK_API_KEY + } + response_openweathermap = requests.get(OPENWEATHERMAP_URL_BASE, params=params_openweathermap) + response_weatherstack = requests.get(WEATHERSTACK_URL_BASE, params=params_weatherstack) + weather_data += ({"OpenWeatherMap": response_openweathermap.json()}, {"Weatherstack": response_weatherstack.json()}) + elif OPENWEATHERMAP_API_KEY: + params_openweathermap = { + "q": location, + "appid": OPENWEATHERMAP_API_KEY + } + response_openweathermap = requests.get(OPENWEATHERMAP_URL_BASE, params=params_openweathermap) + weather_data += ({"OpenWeatherMap": response_openweathermap.json()},) + elif WEATHERSTACK_API_KEY: + params_weatherstack = { + "query": location, + "access_key": WEATHERSTACK_API_KEY + } + response_weatherstack = requests.get(WEATHERSTACK_URL_BASE, params=params_weatherstack) + weather_data += ({"Weatherstack": response_weatherstack.json()},) else: - raise ValueError( - "Invalid API provider. Use 'openweathermap' or 'weatherstack'." - ) + raise ValueError("No API keys provided or no valid data returned.") - response = ( - requests.get(OPENWEATHERMAP_URL_BASE, params=params) - if api_provider == "openweathermap" - else requests.get(WEATHERSTACK_URL_BASE, params=params) - ) - weather_data = response.json() return weather_data - if __name__ == "__main__": from pprint import pprint while True: location = input("Enter a location (city name or latitude,longitude): ").strip() - api_provider = input( - "Enter the API provider (openweathermap or weatherstack): " - ).strip() - if location and api_provider: + if location: try: - if api_provider == "openweathermap": - pprint( - current_weather(location, api_provider, OPENWEATHERMAP_API_KEY) - ) - elif api_provider == "weatherstack": - pprint( - current_weather(location, api_provider, WEATHERSTACK_API_KEY) - ) - else: - print( - "Invalid API provider. Use 'openweathermap' or 'weatherstack'." - ) + forecasts = current_weather(location) + for forecast in forecasts: + pprint(forecast) except ValueError as e: print(e) else: From 8bc5d929f4059cb52589d1d8453b3c88c4aa06d4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:04:25 +0000 Subject: [PATCH 15/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_weather.py | 45 +++++++++++++++--------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 895ccbb6644a..896468027372 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -4,40 +4,41 @@ OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" + def current_weather(location: str) -> tuple[dict]: weather_data = () - + if OPENWEATHERMAP_API_KEY and WEATHERSTACK_API_KEY: - params_openweathermap = { - "q": location, - "appid": OPENWEATHERMAP_API_KEY - } - params_weatherstack = { - "query": location, - "access_key": WEATHERSTACK_API_KEY - } - response_openweathermap = requests.get(OPENWEATHERMAP_URL_BASE, params=params_openweathermap) - response_weatherstack = requests.get(WEATHERSTACK_URL_BASE, params=params_weatherstack) - weather_data += ({"OpenWeatherMap": response_openweathermap.json()}, {"Weatherstack": response_weatherstack.json()}) + params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} + params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} + response_openweathermap = requests.get( + OPENWEATHERMAP_URL_BASE, params=params_openweathermap + ) + response_weatherstack = requests.get( + WEATHERSTACK_URL_BASE, params=params_weatherstack + ) + weather_data += ( + {"OpenWeatherMap": response_openweathermap.json()}, + {"Weatherstack": response_weatherstack.json()}, + ) elif OPENWEATHERMAP_API_KEY: - params_openweathermap = { - "q": location, - "appid": OPENWEATHERMAP_API_KEY - } - response_openweathermap = requests.get(OPENWEATHERMAP_URL_BASE, params=params_openweathermap) + params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} + response_openweathermap = requests.get( + OPENWEATHERMAP_URL_BASE, params=params_openweathermap + ) weather_data += ({"OpenWeatherMap": response_openweathermap.json()},) elif WEATHERSTACK_API_KEY: - params_weatherstack = { - "query": location, - "access_key": WEATHERSTACK_API_KEY - } - response_weatherstack = requests.get(WEATHERSTACK_URL_BASE, params=params_weatherstack) + params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} + response_weatherstack = requests.get( + WEATHERSTACK_URL_BASE, params=params_weatherstack + ) weather_data += ({"Weatherstack": response_weatherstack.json()},) else: raise ValueError("No API keys provided or no valid data returned.") return weather_data + if __name__ == "__main__": from pprint import pprint From a47a8cea18f41564f6d590cd31a8eb5c370e0cc8 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 17:39:57 +0530 Subject: [PATCH 16/22] Update current_weather.py --- web_programming/current_weather.py | 47 ++++++++++++++++-------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 896468027372..5bae59cfd56b 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -4,41 +4,44 @@ OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" +# Replace these API keys with your respective API keys +OPENWEATHERMAP_API_KEY = "5e27ef729510b19f5f7c07e6388d8dfe" +WEATHERSTACK_API_KEY = "26c96ba14d56916edf916a4092f22960" def current_weather(location: str) -> tuple[dict]: weather_data = () - + if OPENWEATHERMAP_API_KEY and WEATHERSTACK_API_KEY: - params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} - params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} - response_openweathermap = requests.get( - OPENWEATHERMAP_URL_BASE, params=params_openweathermap - ) - response_weatherstack = requests.get( - WEATHERSTACK_URL_BASE, params=params_weatherstack - ) - weather_data += ( - {"OpenWeatherMap": response_openweathermap.json()}, - {"Weatherstack": response_weatherstack.json()}, - ) + params_openweathermap = { + "q": location, + "appid": OPENWEATHERMAP_API_KEY + } + params_weatherstack = { + "query": location, + "access_key": WEATHERSTACK_API_KEY + } + response_openweathermap = requests.get(OPENWEATHERMAP_URL_BASE, params=params_openweathermap) + response_weatherstack = requests.get(WEATHERSTACK_URL_BASE, params=params_weatherstack) + weather_data += ({"OpenWeatherMap": response_openweathermap.json()}, {"Weatherstack": response_weatherstack.json()}) elif OPENWEATHERMAP_API_KEY: - params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} - response_openweathermap = requests.get( - OPENWEATHERMAP_URL_BASE, params=params_openweathermap - ) + params_openweathermap = { + "q": location, + "appid": OPENWEATHERMAP_API_KEY + } + response_openweathermap = requests.get(OPENWEATHERMAP_URL_BASE, params=params_openweathermap) weather_data += ({"OpenWeatherMap": response_openweathermap.json()},) elif WEATHERSTACK_API_KEY: - params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} - response_weatherstack = requests.get( - WEATHERSTACK_URL_BASE, params=params_weatherstack - ) + params_weatherstack = { + "query": location, + "access_key": WEATHERSTACK_API_KEY + } + response_weatherstack = requests.get(WEATHERSTACK_URL_BASE, params=params_weatherstack) weather_data += ({"Weatherstack": response_weatherstack.json()},) else: raise ValueError("No API keys provided or no valid data returned.") return weather_data - if __name__ == "__main__": from pprint import pprint From 33578f81ffe04cc1daf38b69c5d19af4a2874d5d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:10:32 +0000 Subject: [PATCH 17/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_weather.py | 45 +++++++++++++++--------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 5bae59cfd56b..1f98e1bca5ff 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -8,40 +8,41 @@ OPENWEATHERMAP_API_KEY = "5e27ef729510b19f5f7c07e6388d8dfe" WEATHERSTACK_API_KEY = "26c96ba14d56916edf916a4092f22960" + def current_weather(location: str) -> tuple[dict]: weather_data = () - + if OPENWEATHERMAP_API_KEY and WEATHERSTACK_API_KEY: - params_openweathermap = { - "q": location, - "appid": OPENWEATHERMAP_API_KEY - } - params_weatherstack = { - "query": location, - "access_key": WEATHERSTACK_API_KEY - } - response_openweathermap = requests.get(OPENWEATHERMAP_URL_BASE, params=params_openweathermap) - response_weatherstack = requests.get(WEATHERSTACK_URL_BASE, params=params_weatherstack) - weather_data += ({"OpenWeatherMap": response_openweathermap.json()}, {"Weatherstack": response_weatherstack.json()}) + params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} + params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} + response_openweathermap = requests.get( + OPENWEATHERMAP_URL_BASE, params=params_openweathermap + ) + response_weatherstack = requests.get( + WEATHERSTACK_URL_BASE, params=params_weatherstack + ) + weather_data += ( + {"OpenWeatherMap": response_openweathermap.json()}, + {"Weatherstack": response_weatherstack.json()}, + ) elif OPENWEATHERMAP_API_KEY: - params_openweathermap = { - "q": location, - "appid": OPENWEATHERMAP_API_KEY - } - response_openweathermap = requests.get(OPENWEATHERMAP_URL_BASE, params=params_openweathermap) + params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} + response_openweathermap = requests.get( + OPENWEATHERMAP_URL_BASE, params=params_openweathermap + ) weather_data += ({"OpenWeatherMap": response_openweathermap.json()},) elif WEATHERSTACK_API_KEY: - params_weatherstack = { - "query": location, - "access_key": WEATHERSTACK_API_KEY - } - response_weatherstack = requests.get(WEATHERSTACK_URL_BASE, params=params_weatherstack) + params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} + response_weatherstack = requests.get( + WEATHERSTACK_URL_BASE, params=params_weatherstack + ) weather_data += ({"Weatherstack": response_weatherstack.json()},) else: raise ValueError("No API keys provided or no valid data returned.") return weather_data + if __name__ == "__main__": from pprint import pprint From e725598a4086b37460f32318d7430fc19970f123 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 17:46:02 +0530 Subject: [PATCH 18/22] Update current_weather.py --- web_programming/current_weather.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 1f98e1bca5ff..99cd2cea522a 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -5,8 +5,8 @@ WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" # Replace these API keys with your respective API keys -OPENWEATHERMAP_API_KEY = "5e27ef729510b19f5f7c07e6388d8dfe" -WEATHERSTACK_API_KEY = "26c96ba14d56916edf916a4092f22960" +OPENWEATHERMAP_API_KEY = "Your_OpenWeatherMap_API_Key_Here" +WEATHERSTACK_API_KEY = "Your_Weatherstack_API_Key_Here" def current_weather(location: str) -> tuple[dict]: From ef6b2caf177f0006cfcb1c9388146603d7a6e81e Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 18:22:55 +0530 Subject: [PATCH 19/22] Update current_weather.py --- web_programming/current_weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 99cd2cea522a..875bb4094c74 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -9,7 +9,7 @@ WEATHERSTACK_API_KEY = "Your_Weatherstack_API_Key_Here" -def current_weather(location: str) -> tuple[dict]: +def current_weather(location: str) -> tuple[dict, ...]: weather_data = () if OPENWEATHERMAP_API_KEY and WEATHERSTACK_API_KEY: From bbc2670ef7129c0c371dcd0720dd5446dbed0bfe Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 18:26:13 +0530 Subject: [PATCH 20/22] Update current_weather.py From f00741d664360d4a5d269e380dab1fdaa4930cda Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 24 Oct 2023 15:06:56 +0200 Subject: [PATCH 21/22] Update current_weather.py --- web_programming/current_weather.py | 58 +++++++++++++----------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 875bb4094c74..705d1aa2195d 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -1,60 +1,50 @@ -import requests +# import requests + +# Put your API key(s) here +OPENWEATHERMAP_API_KEY = "" +WEATHERSTACK_API_KEY = "" # Define the URL for the APIs with placeholders OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" -# Replace these API keys with your respective API keys -OPENWEATHERMAP_API_KEY = "Your_OpenWeatherMap_API_Key_Here" -WEATHERSTACK_API_KEY = "Your_Weatherstack_API_Key_Here" - -def current_weather(location: str) -> tuple[dict, ...]: - weather_data = () - - if OPENWEATHERMAP_API_KEY and WEATHERSTACK_API_KEY: - params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} - params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} - response_openweathermap = requests.get( - OPENWEATHERMAP_URL_BASE, params=params_openweathermap - ) - response_weatherstack = requests.get( - WEATHERSTACK_URL_BASE, params=params_weatherstack - ) - weather_data += ( - {"OpenWeatherMap": response_openweathermap.json()}, - {"Weatherstack": response_weatherstack.json()}, - ) - elif OPENWEATHERMAP_API_KEY: +def current_weather(location: str) -> list[dict]: + """ + >>> current_weather("location") + Traceback (most recent call last): + ... + ValueError: No API keys provided or no valid data returned. + """ + weather_data = [] + if OPENWEATHERMAP_API_KEY: params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} response_openweathermap = requests.get( OPENWEATHERMAP_URL_BASE, params=params_openweathermap ) - weather_data += ({"OpenWeatherMap": response_openweathermap.json()},) - elif WEATHERSTACK_API_KEY: + weather_data.append({"OpenWeatherMap": response_openweathermap.json()}) + if WEATHERSTACK_API_KEY: params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} response_weatherstack = requests.get( WEATHERSTACK_URL_BASE, params=params_weatherstack ) - weather_data += ({"Weatherstack": response_weatherstack.json()},) - else: + weather_data.append({"Weatherstack": response_weatherstack.json()}) + if not weather_data: raise ValueError("No API keys provided or no valid data returned.") - return weather_data if __name__ == "__main__": from pprint import pprint - while True: + location = "to be determined..." + while location: location = input("Enter a location (city name or latitude,longitude): ").strip() - if location: try: - forecasts = current_weather(location) - for forecast in forecasts: + weather_data = current_weather(location) + for forecast in weather_data: pprint(forecast) except ValueError as e: - print(e) - else: - break + print(repr(e)) + location = "" From 6c3bd3a5346b03d49c1c87a070d93ac295829a7b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 24 Oct 2023 15:09:02 +0200 Subject: [PATCH 22/22] import requests --- web_programming/current_weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py index 705d1aa2195d..3b6cd177cdfb 100644 --- a/web_programming/current_weather.py +++ b/web_programming/current_weather.py @@ -1,4 +1,4 @@ -# import requests +import requests # Put your API key(s) here OPENWEATHERMAP_API_KEY = ""