-
Notifications
You must be signed in to change notification settings - Fork 4
/
api_utils.py
58 lines (53 loc) · 2.13 KB
/
api_utils.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 http_utils
import mrequests as requests
from ujson import dumps
TEMPERATURE_KEY = "Temperature"
WIND_KEY = "Wind"
WIND_GUST_KEY = "Gust"
WIND_SPEED_KEY = "Speed"
WIND_DIRECTION_KEY = "Direction"
RAIN_KEY = "Rain"
RAIN_COUNT_DAILY_KEY = "Daily"
RAIN_COUNT_HOURLY_KEY = "Hourly"
PRESSURE_KEY = "Pressure"
HUMIDITY_KEY = "Humidity"
DEW_POINT_KEY = "DewPoint"
def get_data_str(id, key, data):
return ''.join(
[
'ID=', id,
'&PASSWORD=', key,
'&dateutc=now',
'&winddir=', str(data[WIND_KEY][WIND_DIRECTION_KEY]),
'&windspeedmph=', str(data[WIND_KEY][WIND_SPEED_KEY]),
'&windgustmph=', str(data[WIND_KEY][WIND_GUST_KEY]),
'&tempf=', str(data[TEMPERATURE_KEY]),
'&rainin=', str(data[RAIN_KEY][RAIN_COUNT_HOURLY_KEY]),
'&dailyrainin=', str(data[RAIN_KEY][RAIN_COUNT_DAILY_KEY]),
'&baromin=', str(data[PRESSURE_KEY]),
'&humidity=', str(data[HUMIDITY_KEY]),
'&dewptf=', str(data[DEW_POINT_KEY]),
'&softwaretype=custom',
'&action=updateraw'
]
)
def update_weather_api(host, path, station_id, station_key, weather):
url_str = "https://{}{}{}".format(host, path, get_data_str(station_id, station_key, weather))
try:
get_response(url_str, requests.get(url=url_str))
except Exception as e:
print("url: {}\nLooks like Wunderground is not responding -> {}".format(url_str, e))
def send_json_to_telegraf_api(host, port, path, weather_dict):
url_str = "http://{}:{}{}".format(host, port, path)
try:
get_response(url_str, requests.post(url=url_str, data=dumps(weather_dict)))
except Exception as e:
print("url: {}\nLooks like Telegraf is not responding -> {}".format(url_str, e))
def get_response(url_str, http_res):
http_parser = http_utils.HttpParser()
http_res_code = http_parser.parse_http(http_res)
if http_res_code:
http_res_text = http_parser.get_http_response()
print("Response from {} --> {}".format(url_str, http_res_text))
else:
print("Error; no response from host: {}.".format(url_str))