Created
November 22, 2017 14:18
-
-
Save eliandoran/5bfae19ce048fa2f858f57943670857c to your computer and use it in GitHub Desktop.
A small Python script that uses Open Weather Map to obtain meteorological information for a city
This file contains 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
import requests | |
import datetime | |
import time | |
class OpenWeatherMap: | |
def __init__(self, city, api_key): | |
self.city = city | |
self.api_key = api_key | |
def obtain_data(self): | |
response = requests.get("http://api.openweathermap.org/data/2.5/weather?q={}&appid={}".format( | |
self.city, self.api_key | |
)) | |
self.data = response.json() | |
def pprint(self): | |
print("{}, {} (long. {}, lat. {})\n".format( | |
self.data["name"], | |
self.data["sys"]["country"], | |
self.data["coord"]["lon"], | |
self.data["coord"]["lat"])) | |
for weather in self.data["weather"]: | |
print("{} - {}".format( | |
weather["main"], | |
weather["description"] | |
)) | |
print("Temperature: {:.3} (min. {}, max. {})".format( | |
kelvin_to_celsius(self.data["main"]["temp"]), | |
kelvin_to_celsius(self.data["main"]["temp_min"]), | |
kelvin_to_celsius(self.data["main"]["temp_max"]) | |
)) | |
print("Atmospheric pressure: {} hPa".format( | |
self.data["main"]["pressure"] | |
)) | |
print("Humidity: {}%".format( | |
self.data["main"]["humidity"] | |
)) | |
print("Visibility: {}".format( | |
self.data["visibility"] | |
)) | |
print("Wind: {} at {} deg.".format( | |
self.data["wind"]["speed"], | |
self.data["wind"]["deg"] | |
)) | |
print("Sunrise: {}, Sunset: {}".format( | |
parse_timedelta(self.data["sys"]["sunrise"]), | |
parse_timedelta(self.data["sys"]["sunset"]) | |
)) | |
def kelvin_to_celsius(temp): | |
return temp - 273.15 | |
def parse_timedelta(time_val): | |
return time.strftime("%H:%M:%S", time.localtime(time_val)) | |
weather = OpenWeatherMap("Sibiu", "ce05f34ef4448da9cae1caf24611f656") | |
weather.obtain_data() | |
weather.pprint() | |
#print(weather.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment