-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweather.py
58 lines (50 loc) · 2.33 KB
/
weather.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pywapi
import database
def getweather(location, manualweather):
try:
if not manualweather:
locationid = pywapi.get_location_ids(location)
for key in locationid:
locationid = key
else:
locationid = manualweather
weather = pywapi.get_weather_from_weather_com(str(locationid), units='imperial')
condition = str(weather['current_conditions']['text']).lower()
feels_like = str(weather['current_conditions']['feels_like'])
temperature = str(weather['current_conditions']['temperature'])
wind = str(weather['current_conditions']['wind']['speed'])
humidity = str(weather['current_conditions']['humidity'])
location = str(weather['location']['name'])
if wind == "calm":
wind = f"the wind is {wind}"
else:
wind = f"and wind at {wind} mph"
if condition == "":
final = f"It is {temperature}° with humidity at {humidity}% and wind at {wind} mph in {location}. It feels like {feels_like}°\n\n"
else:
final = f"""It is {condition} and {temperature}° with humidity at {humidity}%
{wind} in {location}. It feels like {feels_like}°\n\n"""
return final
except Exception as e:
print(location + " has an invalid location for weather!")
database.log(f"weather", str({location}), None, None, None, None, "Weather error, most likely invalid location.")
return ""
def getallweather(location, manualweather):
try:
if not manualweather:
locationid = pywapi.get_location_ids(location)
for key in locationid:
locationid = key
else:
locationid = manualweather
weather = pywapi.get_weather_from_weather_com(str(locationid), units='imperial')
final = weather['current_conditions']['temperature']
if final == '':
return 50 # For handling exceptions if the site returns null
else:
return final
except Exception as e:
print(location + " Error trying to get all weather location for stats.")
print(str(e))
database.log(f"weather", str({location}), None, None, None, None, "Weather error, most likely invalid location. " + str(e))
return 50 # For handling exceptions if the site returns null