Skip to content

Instantly share code, notes, and snippets.

@phwelo
Created August 23, 2019 16:45
Show Gist options
  • Save phwelo/a845e1d7a8c2d9223ee6ada29add95ab to your computer and use it in GitHub Desktop.
Save phwelo/a845e1d7a8c2d9223ee6ada29add95ab to your computer and use it in GitHub Desktop.
Pulls geolocation then weatherdata and returns icon and temperature
#!/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