Get the area names from long and lat coordinates from google geocode API. Then it is compiled in string for YR and a GET request for the weather API is called with the location names.

This commit is contained in:
2017-07-28 17:33:52 +02:00
parent 7916b0c6bc
commit afef70a7b1

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-28 13:58:43 # @Last Modified time: 2017-07-28 17:30:12
# TODO LIST # TODO LIST
# Get coordinates from IP ✔ # Get coordinates from IP ✔
@@ -33,6 +33,31 @@ class Location(object):
long = self.ip.location.longitude long = self.ip.location.longitude
return [lat, long] return [lat, long]
def getAreaName(self):
lat, long = self.getCoordinates()
coordinates = ','.join([str(lat), str(long)])
areaURL = 'https://maps.googleapis.com/maps/api/geocode/json?&latlng='
areaAPIResponse = json.loads(get(areaURL + coordinates).text)
closestArea = areaAPIResponse['results'][0]['address_components']
area = {}
for item in closestArea:
if 'route' in item['types']:
area['street'] = item['long_name']
if 'locality' in item['types']:
area['town'] = item['long_name']
if 'administrative_area_level_1' in item['types']:
area['municipality'] = item['long_name']
if 'country' in item['types']:
area['country'] = item['long_name']
return area
class WeatherForcast(object): class WeatherForcast(object):
def __init__(self, area=None): def __init__(self, area=None):
@@ -41,14 +66,17 @@ class WeatherForcast(object):
def now(self): def now(self):
location = Location() location = Location()
lat, long = location.getCoordinates() self.area = location.getAreaName()
print('Coords: ', lat, long) print('Area: ', self.area)
print(' - - - - - - - - ') print(' - - - - - - - - ')
weather = Yr(coordinates=(lat, long, 10)) # Create seperate function for formatting
locationName = '/'.join([self.area['country'], self.area['municipality'], self.area['town'], self.area['street']])
weather = Yr(location_name=locationName)
now = json.loads(weather.now(as_json=True)) now = json.loads(weather.now(as_json=True))
temp = now['location']['temperature'] temp = now['temperature']
print(temp['@value'] + ' ' + temp['@unit']) print(temp['@value'] + ' ' + temp['@unit'])