-
Notifications
You must be signed in to change notification settings - Fork 0
/
getWeather.py
41 lines (30 loc) · 1.07 KB
/
getWeather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import requests
import os
from geopy.geocoders import Nominatim
def getWeather(city):
#get city latitude and longitude or None object if city is not real
geolocator = Nominatim(user_agent="FlaskWeatherApp")
location = geolocator.geocode(city, timeout=10, exactly_one=True)
#if city not real quit function
if location == None:
return False
#dark sky api
key = os.environ.get('API_KEY')
print(key)
link = 'https://api.darksky.net/forecast/'
#get data from api
url = link + key + '/' + str(location.latitude) + ',' + str(location.longitude) + '?exclude=[minutely, hourly, daily, alerts, flags]'
#try to make request or quit the function
try:
#send request
r = requests.get(url)
data = r.json()
#get temperature, icon from data
weatherData = {
'summary' : data['currently']['summary'],
'temperature' : data['currently']['temperature'],
'icon' : data['currently']['icon']
}
return weatherData
except:
return False