Now fetches the temp for current location based on ip address. Need to make own lib for yr because we need some of the return data from yr that is stripped from this lib.

This commit is contained in:
2017-07-27 23:40:54 +02:00
parent c3010de6ae
commit 125ef97f4c

View File

@@ -3,7 +3,7 @@
# @Author: KevinMidboe # @Author: KevinMidboe
# @Date: 2017-07-27 21:26:53 # @Date: 2017-07-27 21:26:53
# @Last Modified by: KevinMidboe # @Last Modified by: KevinMidboe
# @Last Modified time: 2017-07-27 23:03:19 # @Last Modified time: 2017-07-27 23:39:00
# TOD LIST # TOD LIST
# Get coordinates from IP # Get coordinates from IP
@@ -21,45 +21,46 @@ from pprint import pprint
class Location(object): class Location(object):
def __init__(self): def __init__(self):
self.reader = geoip2.database.Reader('conf/GeoLite2-City.mmdb') self.reader = geoip2.database.Reader('conf/GeoLite2-City.mmdb')
self.lat = None self.getIP()
self.long = None
def getIP(self): def getIP(self):
ip = get('https://api.ipify.org').text ip = get('https://api.ipify.org').text
return ip self.ip = self.reader.city(ip)
def getCoordinates(self): def getCoordinates(self):
ip = self.getIP() lat = self.ip.location.latitude
ip_locaiton = self.reader.city(ip) long = self.ip.location.longitude
return [lat, long]
self.lat = ip_locaiton.location.latitude
self.long = ip_locaiton.location.longitude
class WeatherForcast(object): class WeatherForcast(object):
"""docstring for WeatherForcast""" def __init__(self, area=None):
def __init__(self, location): # TODO search for area coordinates in a map
self.location = location self.area = area
def now(self): def now(self):
lat = self.location.lat location = Location()
long = self.location.long lat, long = location.getCoordinates()
print(lat, long) print('Coords: ', lat, long)
print(' - - - - - - - - ')
weather = Yr(coordinates=(lat, long, 10)) weather = Yr(coordinates=(lat, long, 10))
now = json.loads(weather.now(as_json=True)) now = json.loads(weather.now(as_json=True))
pprint(now)
temp = now['location']['temperature']
print(temp['@value'] + ' ' + temp['@unit'])
class TermWeather(object): class TermWeather(object):
# Add now, forcast as args # Add now, forcast as args
def auto(self): def auto(self):
location = Location() WeatherForcast.now(self)
location.getCoordinates()
weatherForcast = WeatherForcast(location) def fetch(self, area=None):
weatherForcast = WeatherForcast(area)
weatherForcast.now() weatherForcast.now()
if __name__ == '__main__': if __name__ == '__main__':
ssl._create_default_https_context = ssl._create_unverified_context ssl._create_default_https_context = ssl._create_unverified_context