Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import datetime
- # The name of the city you want to get the weather for
- city_name = "Manchester"
- #city_name = "Manchester,England,GB"
- #city_name = "Manchester,New Hampshire,US"
- #city_name = "Manchester,NH,US"
- # Make the API request
- response = requests.get(f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={OPENWEATHERMAP_KEY}")
- # Check if the request was successful
- if response.status_code == 200:
- # Parse the JSON data
- data = response.json()
- # Convert timezone from seconds to hours and minutes
- timezone_hours = data["timezone"] // 3600
- timezone_minutes = (data["timezone"] % 3600) // 60
- timezone_str = f"GMT{'+' if timezone_hours >= 0 else ''}{timezone_hours}:{timezone_minutes:02d}"
- # Print the weather data in a nicely formatted way
- print("City: {}, ({})".format(data["name"], data["sys"]["country"]))
- print("ID: {}".format(data["id"]))
- print("Coordinates: ({}, {})".format(data["coord"]["lon"], data["coord"]["lat"]))
- print("Weather: {}".format(data["weather"][0]["description"]))
- print("Temperature: {:.2f}°C".format(data["main"]["temp"] - 273.15))
- print("Feels like: {:.2f}°C".format(data["main"]["feels_like"] - 273.15))
- print("Minimum temperature: {:.2f}°C".format(data["main"]["temp_min"] - 273.15))
- print("Maximum temperature: {:.2f}°C".format(data["main"]["temp_max"] - 273.15))
- print("Pressure: {} hPa".format(data["main"]["pressure"]))
- print("Humidity: {}%".format(data["main"]["humidity"]))
- print("Visibility: {} m".format(data["visibility"]))
- print("Wind speed: {} m/s".format(data["wind"]["speed"]))
- print("Wind direction: {}°".format(data["wind"]["deg"]))
- print("Clouds: {}%".format(data["clouds"]["all"]))
- print("Sunrise: {}".format(datetime.datetime.fromtimestamp(data["sys"]["sunrise"])))
- print("Sunset: {}".format(datetime.datetime.fromtimestamp(data["sys"]["sunset"])))
- #print("Timezone: {}".format(data["timezone"]))
- print("Timezone: {}".format(timezone_str))
- else:
- # Print an error message
- print(f"Error: {response.status_code}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement