__init__.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #from WeatherData_Clouds import WeatherData_Clouds
  4. #from WeatherData_Coords import WeatherData_Coords
  5. #from WeatherData_Main import WeatherData_Main
  6. #from WeatherData_Rain import WeatherData_Rain
  7. #from WeatherData_Sys import WeatherData_Sys
  8. #from WeatherData_Weather import WeatherData_Weather
  9. #from WeatherData_Wind import WeatherData_Wind
  10. #from WeatherData import WeatherData
  11. from OpenWeatherMap.WeatherData import WeatherData
  12. from OpenWeatherMap.WeatherData_Clouds import WeatherData_Clouds
  13. from OpenWeatherMap.WeatherData_Coords import WeatherData_Coords
  14. from OpenWeatherMap.WeatherData_Main import WeatherData_Main
  15. from OpenWeatherMap.WeatherData_Rain import WeatherData_Rain
  16. from OpenWeatherMap.WeatherData_Sys import WeatherData_Sys
  17. from OpenWeatherMap.WeatherData_Weather import WeatherData_Weather
  18. from OpenWeatherMap.WeatherData_Wind import WeatherData_Wind
  19. import configparser
  20. import OpenWeatherMap.WeatherData_Unit
  21. import requests
  22. import json
  23. import os
  24. class OpenWeatherMap(object):
  25. CurrentLocation = None
  26. ApiKey = ""
  27. Unit = WeatherData_Unit.UNIT_STANDARD
  28. City = ""
  29. def __init__(self):
  30. config = configparser.ConfigParser()
  31. config.read('config.ini')
  32. if "OpenWeatherMap" in config:
  33. self.ApiKey = config["OpenWeatherMap"]["ApiKey"]
  34. unit = config["OpenWeatherMap"]["Unit"]
  35. self.City = config["OpenWeatherMap"]["City"]
  36. if unit=="UNIT_STANDARD" or unit == "0":
  37. self.Unit = WeatherData_Unit.UNIT_STANDARD
  38. elif unit =="UNIT_IMPERIAL" or unit == "1":
  39. self.Unit = WeatherData_Unit.UNIT_IMPERIAL
  40. elif unit == "UNIT_METRIC" or unit == "2":
  41. self.Unit = WeatherData_Unit.UNIT_METRIC
  42. else:
  43. self.Unit = WeatherData_Unit.UNIT_STANDARD
  44. def __del__(self):
  45. print("Del OpenWeaherMap")
  46. del self.City
  47. del self.Unit
  48. del self.ApiKey
  49. del self.CurrentLocation
  50. def Update(self):
  51. self.CurrentLocation = self.GetFromCity(self.City)
  52. def GetFromCity(self, city, country = ""):
  53. if country==None or country=="":
  54. return self._Get("q="+city)
  55. elif city.isnumeric():
  56. return self._Get("zip="+city+","+country)
  57. else:
  58. return self._Get("q="+city+","+country)
  59. def GetFromCityId(self,id):
  60. return self._Get("id="+id)
  61. def GetFromCityCoord(self,lat,lon):
  62. return self._Get("lat=" + lat + "&lon" + lon)
  63. def __fromData(data):
  64. res = WeatherData()
  65. if data is not None:
  66. if "clouds" in data:
  67. res.Clouds = WeatherData_Clouds()
  68. res.Clouds.All = int(data["clouds"]["all"])
  69. if "main" in data:
  70. res.Main = WeatherData_Main()
  71. res.Main.Temperature = float(data["main"]["temp"])
  72. res.Main.Humidity = int(data["main"]["humidity"])
  73. res.Main.Pressure = int(data["main"]["pressure"])
  74. res.Main.FeelsLike = int(data["main"]["feels_like"])
  75. res.Main.TempMax = int(data["main"]["temp_max"])
  76. res.Main.TempMin = int(data["main"]["temp_min"])
  77. if "weather" in data:
  78. res.Weather = WeatherData_Weather()
  79. res.Weather.ID = int(data["weather"][0]["id"])
  80. res.Weather.Main = data["weather"][0]["main"]
  81. res.Weather.Icon = data["weather"][0]["icon"]
  82. res.Weather.Description = data["weather"][0]["description"]
  83. if "wind" in data:
  84. res.Wind = WeatherData_Wind()
  85. if "speed" in data["wind"]:
  86. res.Wind.Speed = float(data["wind"]["speed"])
  87. else:
  88. res.Wind.Speed = 0.0
  89. if "deg" in data["wind"]:
  90. res.Wind.Deg = int(data["wind"]["deg"])
  91. else:
  92. res.Wind.Deg = 0
  93. if "rain" in data:
  94. res.Rain = WeatherData_Rain()
  95. res.Rain.PerHour = float(data["rain"]["1h"])
  96. if "sys" in data:
  97. res.Sys = WeatherData_Sys()
  98. res.Sys.Country = data["sys"]["country"]
  99. res.Sys.ID = data["sys"]["id"]
  100. res.Sys.Type = data["sys"]["type"]
  101. res.Sys.Sunrise = data["sys"]["sunrise"]
  102. res.Sys.Sunset = data["sys"]["sunset"]
  103. res.Sys.Message = data["sys"]["country"]
  104. res.Cod = data["cod"]
  105. res.Source = data["base"]
  106. res.Visibility = data["visibility"]
  107. res.DT = data["dt"]
  108. res.TimeZone = data["timezone"]
  109. res.ID = data["id"]
  110. res.Name = data["name"]
  111. if "message" in data:
  112. res.Message = data["message"]
  113. return res
  114. def _Get(self, query):
  115. response = None
  116. if self.ApiKey != None:
  117. unit = "standard"
  118. if self.Unit == WeatherData_Unit.UNIT_IMPERIAL:
  119. unit = "imperial"
  120. elif self.Unit == WeatherData_Unit.UNIT_METRIC:
  121. unit = "metric"
  122. url = "http://api.openweathermap.org/data/2.5/weather?" + query + "&APPID=" + self.ApiKey + "&units="+unit
  123. response = requests.get(url)
  124. res=None
  125. if response.status_code == 200:
  126. data = json.loads(response.content.decode('utf-8'))
  127. self.__fromData(data)
  128. f = open("cache/weather.json","wb")
  129. f.write(response.content.decode('utf-8'))
  130. f.close()
  131. del data
  132. elif(os.path.exists("cache/weather.json")):
  133. f = open('cache/weather.json')
  134. data=json.load(f)
  135. res = self.__fromData(data)
  136. f.close()
  137. else:
  138. res = WeatherData()
  139. #url = "http://api.openweathermap.org/data/2.5/forecast/daily?" + query + "&cnt=16&APPID=" + self.ApiKey + "&units="+unit
  140. del response
  141. del url
  142. del unit
  143. return res