-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Change from only weatherstack to both #10882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a7866ae
Update current_weather.py
nababuddin d2fcd7e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 34eb55d
Update current_weather.py
nababuddin a7df865
Update current_weather.py
nababuddin 34a8075
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4a78eab
Update current_weather.py
nababuddin 5a4068e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c80aa7b
Update current_weather.py
nababuddin 1020c82
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 90e6638
Update current_weather.py
nababuddin 50ecb60
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f84de33
Update current_weather.py
nababuddin 9c9fc6e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 234a6fb
Update current_weather.py
nababuddin 8bc5d92
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a47a8ce
Update current_weather.py
nababuddin 33578f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e725598
Update current_weather.py
nababuddin ef6b2ca
Update current_weather.py
nababuddin bbc2670
Update current_weather.py
nababuddin f00741d
Update current_weather.py
cclauss 6c3bd3a
import requests
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,60 @@ | ||
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() | ||
# 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 = "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()}, | ||
) | ||
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("No API keys provided or no valid data returned.") | ||
|
||
return weather_data | ||
|
||
|
||
if __name__ == "__main__": | ||
from pprint import pprint | ||
|
||
while True: | ||
location = input("Enter a location:").strip() | ||
location = input("Enter a location (city name or latitude,longitude): ").strip() | ||
|
||
if location: | ||
pprint(current_weather(location)) | ||
try: | ||
forecasts = current_weather(location) | ||
for forecast in forecasts: | ||
pprint(forecast) | ||
except ValueError as e: | ||
print(e) | ||
else: | ||
break |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.