renix1

acess to open weather api

Jan 29th, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. """
  4.     Test with API Open Weather (current data)
  5. """
  6.  
  7. import requests
  8. import sys
  9. import json
  10.  
  11. class Weather(object):
  12.     """ CLASS TO GET INFOS FROM OPEN WEATHER """
  13.  
  14.  
  15.     def __init__(self, city='Osasco'):
  16.         self.city = city
  17.         self.key = "5b318944fb61cfbae7a2dc89f20b9d5d"
  18.         self.check_current_weather()
  19.  
  20.     def check_current_weather(self):
  21.         """ CHECK CURRENT WEATHER WITH ALL INFOS """
  22.         try:
  23.             req = requests.get("http://api.openweathermap.org/data/2.5/weather?q=%s&APPID=%s" % (self.city, self.key))
  24.             self.info = json.loads(req.text)
  25.             return self.info
  26.         except Exception as e:
  27.             print(e)
  28.             sys.exit(1)
  29.  
  30.     def get_name_city(self):
  31.         """ NAME OF CITY """
  32.         try:
  33.             return self.info['name']
  34.         except KeyError:
  35.             print("Cidade não encontrada")
  36.             sys.exit(1)
  37.  
  38.     def get_country(self):
  39.         """ NAME OF COUNTRY """
  40.         return str(self.info['sys']['country']).encode('utf-8')
  41.  
  42.     def get_wind_speed(self):
  43.         """ SHOW AS METERS/SEC OR MILES/HOUR (KM/H too) """
  44.         return(self.info['wind']['speed'])
  45.  
  46.     def get_current_temp(self):
  47.         """ SHOW AS YOU WANT """
  48.         current_temp = self.info['main']['temp']
  49.         return (self.k_to_c(current_temp))
  50.  
  51.     def get_min_temp(self):
  52.         """ SHOW AS YOU WANT """
  53.         min_temp = self.info['main']['temp_min']
  54.         return (self.k_to_c(min_temp))
  55.  
  56.     def get_max_temp(self):
  57.         """ SHOW AS YOU WANT """
  58.         max_temp = self.info['main']['temp_max']
  59.         return (self.k_to_c(max_temp))
  60.  
  61.     def get_humidity(self):
  62.         """ USE % TO SHOW """
  63.         return "%d" % (self.info['main']['humidity'])
  64.  
  65.     def get_pressure(self):
  66.         """ SHOW AS hPa """
  67.         return "%d" % (self.info['main']['pressure'])
  68.  
  69.     def verify_rain(self):
  70.         """ OBVIOUS """
  71.         descrip = str(self.info['weather'])
  72.         descrip = descrip.replace('u', '').replace('{', '').replace('}', '')
  73.         if 'rain' in descrip:
  74.             return 'há previsão de chuva'
  75.         else:
  76.             return 'não há previsão de chuva'
  77.  
  78.     def k_to_c(self, current_temp):
  79.         """ KELVIN TO CELSIUS """
  80.         return round((current_temp - 273.15), 2)
  81.  
  82. if __name__ == '__main__':
  83.     if len(sys.argv) > 2:
  84.         if sys.argv[1].startswith('-c'):
  85.             cidade = sys.argv[2]
  86.             clima = Weather(cidade)
  87.     clima = Weather()
  88.     # --------- SHOW ------------
  89.     print("Na cidade de {} - ({}) está {}°C (max - {}, min - {})\
  90. , a humidade está em {}%. Pressão em {}hPa e {}."
  91.         .format(clima.get_name_city(), clima.get_country(), clima.get_current_temp(),
  92.         clima.get_max_temp(), clima.get_min_temp(), clima.get_humidity(), clima.get_pressure(), clima.verify_rain()))
Advertisement
Add Comment
Please, Sign In to add comment