-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weather.py
54 lines (45 loc) · 1.93 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
from pyowm.owm import OWM
import os
import requests
import json
import pytz
from geopy.geocoders import Nominatim
owm = OWM(os.environ['OWM_API_KEY'])
mgr = owm.weather_manager()
geolocator = Nominatim(user_agent="geoapiExercises")
def toCentralTime(timestamp):
cst = pytz.timezone('US/Central')
fmt = '%d-%m-%Y %H:%M:%S'
return timestamp.astimezone(cst).strftime(fmt)
def sunriseSunsetTime(timestamp):
cst = pytz.timezone('US/Central')
fmt = '%H:%M:%S'
return timestamp.astimezone(cst).strftime(fmt)
def getWeather(zipcode):
""" Returns a weather object"""
api_key = os.environ["OWM_API_KEY"]
location = geolocator.geocode(zipcode)
data = location.raw
lat = data['lat']
lon = data['lon']
response = requests.get(f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={api_key}&units=imperial")
weather = json.loads(response.text)
return weather
def getRadar():
""" Returns a radar gif for the KLSX radar station"""
# TODO: Grab closest station's radar for a given zipcode
response = requests.get('https://radblast-aa.wunderground.com/cgi-bin/radar/WUNIDS_map?station=LSX&brand=wui&num=10&delay=60&type=NCR&frame=0&scale=4.999&noclutter=0&t=1621025493&showstorms=0&map.x=400&map.y=240¢erx=400¢ery=240&transx=0&transy=0&showlabels=1&severe=1&rainsnow=1&lightning=1')
with open('radar.gif','wb') as f:
f.write(response.content)
return 'radar.gif'
def getForecast(zipcode):
""" Returns a forecast object"""
api_key = os.environ["OWM_API_KEY"]
location = geolocator.geocode(zipcode)
data = location.raw
lat = data['lat']
lon = data['lon']
#url = 'http://api.openweathermap.org/data/2.5/forecast?zip=' + zipcode + '&units=imperial&appid=' + api_key
url = f'http://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={api_key}&units=imperial'
forecast = requests.get(url).json()
return forecast