| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #from WeatherData_Clouds import WeatherData_Clouds
- #from WeatherData_Coords import WeatherData_Coords
- #from WeatherData_Main import WeatherData_Main
- #from WeatherData_Rain import WeatherData_Rain
- #from WeatherData_Sys import WeatherData_Sys
- #from WeatherData_Weather import WeatherData_Weather
- #from WeatherData_Wind import WeatherData_Wind
- #from WeatherData import WeatherData
- from OpenWeatherMap.WeatherData import WeatherData
- from OpenWeatherMap.WeatherData_Clouds import WeatherData_Clouds
- from OpenWeatherMap.WeatherData_Coords import WeatherData_Coords
- from OpenWeatherMap.WeatherData_Main import WeatherData_Main
- from OpenWeatherMap.WeatherData_Rain import WeatherData_Rain
- from OpenWeatherMap.WeatherData_Sys import WeatherData_Sys
- from OpenWeatherMap.WeatherData_Weather import WeatherData_Weather
- from OpenWeatherMap.WeatherData_Wind import WeatherData_Wind
- import configparser
- import OpenWeatherMap.WeatherData_Unit
- import requests
- import json
- import os
- class OpenWeatherMap(object):
- CurrentLocation = None
- ApiKey = ""
- Unit = WeatherData_Unit.UNIT_STANDARD
- City = ""
- def __init__(self):
- config = configparser.ConfigParser()
- config.read('config.ini')
- if "OpenWeatherMap" in config:
- self.ApiKey = config["OpenWeatherMap"]["ApiKey"]
- unit = config["OpenWeatherMap"]["Unit"]
- self.City = config["OpenWeatherMap"]["City"]
- if unit=="UNIT_STANDARD" or unit == "0":
- self.Unit = WeatherData_Unit.UNIT_STANDARD
- elif unit =="UNIT_IMPERIAL" or unit == "1":
- self.Unit = WeatherData_Unit.UNIT_IMPERIAL
- elif unit == "UNIT_METRIC" or unit == "2":
- self.Unit = WeatherData_Unit.UNIT_METRIC
- else:
- self.Unit = WeatherData_Unit.UNIT_STANDARD
- def __del__(self):
- print("Del OpenWeaherMap")
- del self.City
- del self.Unit
- del self.ApiKey
- del self.CurrentLocation
- def Update(self):
- self.CurrentLocation = self.GetFromCity(self.City)
- def GetFromCity(self, city, country = ""):
- if country==None or country=="":
- return self._Get("q="+city)
- elif city.isnumeric():
- return self._Get("zip="+city+","+country)
- else:
- return self._Get("q="+city+","+country)
- def GetFromCityId(self,id):
- return self._Get("id="+id)
- def GetFromCityCoord(self,lat,lon):
- return self._Get("lat=" + lat + "&lon" + lon)
- def __fromData(data):
- res = WeatherData()
- if data is not None:
- if "clouds" in data:
- res.Clouds = WeatherData_Clouds()
- res.Clouds.All = int(data["clouds"]["all"])
- if "main" in data:
- res.Main = WeatherData_Main()
- res.Main.Temperature = float(data["main"]["temp"])
- res.Main.Humidity = int(data["main"]["humidity"])
- res.Main.Pressure = int(data["main"]["pressure"])
- res.Main.FeelsLike = int(data["main"]["feels_like"])
- res.Main.TempMax = int(data["main"]["temp_max"])
- res.Main.TempMin = int(data["main"]["temp_min"])
- if "weather" in data:
- res.Weather = WeatherData_Weather()
- res.Weather.ID = int(data["weather"][0]["id"])
- res.Weather.Main = data["weather"][0]["main"]
- res.Weather.Icon = data["weather"][0]["icon"]
- res.Weather.Description = data["weather"][0]["description"]
- if "wind" in data:
- res.Wind = WeatherData_Wind()
- if "speed" in data["wind"]:
- res.Wind.Speed = float(data["wind"]["speed"])
- else:
- res.Wind.Speed = 0.0
- if "deg" in data["wind"]:
- res.Wind.Deg = int(data["wind"]["deg"])
- else:
- res.Wind.Deg = 0
- if "rain" in data:
- res.Rain = WeatherData_Rain()
- res.Rain.PerHour = float(data["rain"]["1h"])
- if "sys" in data:
- res.Sys = WeatherData_Sys()
- res.Sys.Country = data["sys"]["country"]
- res.Sys.ID = data["sys"]["id"]
- res.Sys.Type = data["sys"]["type"]
- res.Sys.Sunrise = data["sys"]["sunrise"]
- res.Sys.Sunset = data["sys"]["sunset"]
- res.Sys.Message = data["sys"]["country"]
- res.Cod = data["cod"]
- res.Source = data["base"]
- res.Visibility = data["visibility"]
- res.DT = data["dt"]
- res.TimeZone = data["timezone"]
- res.ID = data["id"]
- res.Name = data["name"]
- if "message" in data:
- res.Message = data["message"]
- return res
- def _Get(self, query):
- response = None
- if self.ApiKey != None:
- unit = "standard"
- if self.Unit == WeatherData_Unit.UNIT_IMPERIAL:
- unit = "imperial"
- elif self.Unit == WeatherData_Unit.UNIT_METRIC:
- unit = "metric"
- url = "http://api.openweathermap.org/data/2.5/weather?" + query + "&APPID=" + self.ApiKey + "&units="+unit
- response = requests.get(url)
- res=None
- if response.status_code == 200:
- data = json.loads(response.content.decode('utf-8'))
- self.__fromData(data)
- f = open("cache/weather.json","wb")
- f.write(response.content.decode('utf-8'))
- f.close()
- del data
- elif(os.path.exists("cache/weather.json")):
- f = open('cache/weather.json')
- data=json.load(f)
- res = self.__fromData(data)
- f.close()
- else:
- res = WeatherData()
- #url = "http://api.openweathermap.org/data/2.5/forecast/daily?" + query + "&cnt=16&APPID=" + self.ApiKey + "&units="+unit
- del response
- del url
- del unit
- return res
|