-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
36 lines (26 loc) · 1.17 KB
/
weather.py
File metadata and controls
36 lines (26 loc) · 1.17 KB
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
'''Weather info for inputted lat, long'''
from datetime import datetime
import requests
def main(lat, lng, api_key):
'''Formats data from request and returns'''
url = (f'https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lng}'
f'&exclude=minutely,hourly,alerts&appid={api_key}&units=imperial')
request = requests.get(url, timeout=5).json()
if 'cod' in request: #request failed
code = request["cod"]
print(f'Error code {code}')
print(request["message"])
return ['Something went wrong. Please try again']
curr = request['current']
day = request['daily'][0]
degree_f = '\u00b0F'
low = day['temp']['min']
high = day['temp']['max']
sunrise = datetime.strftime(datetime.fromtimestamp(day['sunrise']), '%-I:%M %p')
sunset = datetime.strftime(datetime.fromtimestamp(day['sunset']), '%-I:%M %p')
out = ['<b><u>Weather for current location:</u></b>']
out.append(f'- {day['summary']}')
out.append(f'- Feels like {curr["feels_like"]} {degree_f}')
out.append(f'- Low: {low} {degree_f}, High: {high} {degree_f}')
out.append(f'- Sunrise: {sunrise}, Sunset: {sunset}')
return out