/
weather.py
181 lines (164 loc) · 7.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from datetime import datetime
import json
import time
from urllib.request import urlopen, Request
import requests
import utils
import os
emojimap = {'Sunny':':sunny:',
'Mostly Cloudy':':white_sun_cloud:',
'Partly Cloudy':':white_sun_small_cloud:',
'Cloudy':':cloud:',
'Snow':':cloud_snow:',
'Snow Showers':':cloud_snow:',
'Showers':':cloud_rain:',
'Thunderstorms':':thunder_cloud_rain:',
'Severe Thunderstorms':'Severe :thunder_cloud_rain:'}
def _convert_ctof(temp):
'''convert celsius to fahrenheit'''
return 9.0/5.0 * float(temp) + 32
def _get_ET_from_timestamp(timestamp):
# utc = datetime.strptime(timestamp,"%Y-%m-%dT%H:%M:00Z") #"2018-03-31T20:05:00Z",
utc = datetime.strptime(timestamp,"%Y-%m-%dT%H:%M:00+00:00") # 2019-01-10T18:52:00+00:00
nowtime = time.time()
diff = datetime.fromtimestamp(nowtime) - datetime.utcfromtimestamp(nowtime)
utc = utc + diff
return datetime.strftime(utc, "%I:%M ET")
def get_current_weatherbit(text):
url = "https://api.weatherbit.io/v2.0/current"
key = utils.get_keys("weatherbit")['key']
lat,lon,loc = get_lat_lon(text)
url = url + "?key=%s&units=I&lat=%f&lon=%f" % (key, lat, lon)
resp = utils.get_json(url)
if len(resp['data']) > 0:
data = resp['data'][0]
obstime = data['ob_time']
obsdatetime = datetime.strptime(obstime, '%Y-%m-%d %H:%M')
ret = "```python\n" \
"Location search result: %s\n" \
"%s in %s, %s, %s:\n" \
" Temp: %.1f (Feels like: %.1f)\n" \
" Humidity: %d%%\n" \
" Observed: %s at station: %s" % (loc, data['weather']['description'], data['city_name'], data['state_code'], data['country_code'],
data['temp'], data['app_temp'], data['rh'],
utils.prettydate(obsdatetime, utc=True), data['station'])
if data['aqi'] > 50:
ret += "\n AQI: %d```" % (data['aqi'])
else:
ret += "```"
return ret
def get_current_weather(text):
'''get current weather conditions'''
loc = get_lat_lon(text)
url = "https://api.weather.gov/points/%.4f,%.4f" % (loc[0], loc[1])
data = requests.get(url).json()
stations_url = data['properties']['observationStations']
data = requests.get(stations_url).json()
station = data['features'][0]['properties']['stationIdentifier']
station_name = data['features'][0]['properties']['name']
station_url = "https://api.weather.gov/stations/%s/observations?limit=1" % station
print(station_url)
data = requests.get(station_url).json()
data = data['features'][0]['properties']
temp = _convert_ctof(data['temperature']['value'])
text = data['textDescription']
try:
wind_chill = _convert_ctof(data['windChill']['value'])
except:
wind_chill = None
humidity = data['relativeHumidity']['value']
updated = _get_ET_from_timestamp(data['timestamp'])
retval = "At %s:\n%d F, %s\nHumidity: %.0f%%\n" % (station_name, temp, text, float(humidity))
if wind_chill is not None:
retval = "%sWind Chill: %d F\n" % (retval, wind_chill)
retval = "%sUpdated: %s" % (retval, updated)
return "```python\n%s```" % retval
def get_forecast(text):
'''get a 10 day weather forecast'''
loc = get_lat_lon(text)
url = "https://api.weather.gov/points/%.4f,%.4f" % (loc[0], loc[1])
print(url)
req = requests.get(url)
data = req.json()
stations_url = data['properties']['observationStations']
forecast_url = data['properties']['forecast']
data = requests.get(stations_url).json()
station_name = data['features'][0]['properties']['name']
print(forecast_url)
req = requests.get(forecast_url)
data = req.json()
periods = data['properties']['periods']
out = "Forecast for %s:\n\n" % station_name
if len(periods) > 0:
for period in periods[:10]:
out = out + "%s: %d%s\n%s\n\n" % (period['name'], period['temperature'], period['temperatureUnit'], period['detailedForecast'])
return "```python\n%s```" % out
def get_readable_location(geocode_result):
loc_str = ""
for component in geocode_result['address_components']:
if len(loc_str) == 0:
loc_str += component['short_name']
else:
if component['types'][0] in ['locality', 'administrative_area_level_1', 'country']:
loc_str += ", %s" % component['short_name']
return loc_str
def get_lat_lon(search):
cache = "cache/locations.json"
cachedir = "cache"
if not os.path.exists(cache):
if not os.path.exists(cachedir):
os.makedirs(cachedir)
with open(cache, 'w') as f:
f.write("{}")
with open(cache, 'r') as f:
s = f.read()
queries = json.loads(s)
if search in queries: # if cached
result = queries[search]['geometry']['location']
return (result['lat'], result['lng'], get_readable_location(queries[search]))
else: # otherwise use API to find location
query = search.replace(' ','+')
url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + query + "&key="
with open("keys.json",'r') as f:
f = json.loads(f.read())
for k in f['keys']:
if k['name'] == 'google':
key = k['key']
if key is not None:
url = url + key
req = Request(url, headers={'User-Agent' : "ubuntu"})
s = json.loads(urlopen(req).read().decode("utf-8"))
print(s)
lat = s['results'][0]['geometry']['location']['lat']
lon = s['results'][0]['geometry']['location']['lng']
# cache result
queries[search] = s['results'][0]
with open(cache, 'w') as f:
f.write(json.dumps(queries))
return(lat,lon, get_readable_location(s['results'][0]))
def get_current_metar(airport_code):
# url = "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=" + airport_code
# print(url)
# req = Request(url, headers={'User-Agent' : "ubuntu"})
# open = urlopen(req)
# content = open.read().decode('utf-8')
# tag = '<raw_text>'
# close = '</raw_text>'
# metar = content[content.find(tag) + len(tag):content.find(close)]
# return metar
url = 'https://api.weather.gov/stations/%s/observations?limit=25' % (airport_code)
data = utils.get_json(url)
if len(data['features']) > 0:
for i in range(len(data['features'])):
if len(data['features'][i]['properties']['rawMessage']) > 0:
ts = data['features'][i]['properties']['timestamp']
updated = _get_ET_from_timestamp(ts)
return "```Time: %s\n%s```" % (updated, data['features'][i]['properties']['rawMessage'])
else:
return "Airport code not found"
if __name__ == "__main__":
#get_current_weather('%2C'.join(("fairfax,","va")))
# print(get_current_weatherbit('sterling, va'))
# print(get_lat_lon("potomac, md"))
print(get_current_metar('kduj'))
# print(get_forecast("arlington,va"))