Created
August 23, 2019 16:45
-
-
Save phwelo/a845e1d7a8c2d9223ee6ada29add95ab to your computer and use it in GitHub Desktop.
Pulls geolocation then weatherdata and returns icon and temperature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import json | |
import pyowm | |
geolocation_api_uri = "http://ip-api.com/json/?fields=49344" | |
def get_coord(): | |
r = requests.get(geolocation_api_uri) | |
return json.loads(r.text) | |
def weather_client(x,y): | |
owm = pyowm.OWM('APIKEYHERE') | |
return owm.weather_around_coords(x,y, limit=1)[0] | |
def get_weather_icon(client): | |
code = client.get_weather().get_weather_icon_name()[0:-1] | |
if code == '01': | |
return '' | |
elif code == '02': | |
return '' | |
elif code == '03' or code == '04': | |
return '' | |
elif code == '09': | |
return '' | |
elif code == '10': | |
return '' | |
elif code == '11': | |
return '' | |
elif code == '13': | |
return '' | |
elif code == '50': | |
return '' | |
else: | |
return 'an error or something' | |
def get_temp(client): | |
temp_json = client.get_weather().get_temperature(unit="fahrenheit") | |
return int(round(temp_json['temp'])) | |
weather = {} | |
coords = get_coord() | |
client = weather_client(coords['lat'], coords['lon']) | |
weather['text'] = get_weather_icon(client) | |
weather['alt'] = get_temp(client) | |
weather = json.dumps(weather, ensure_ascii=False) | |
print(weather) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment