This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
112 lines (96 loc) · 3.43 KB
/
test.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
#!/usr/bin/python
""" Ambient Weather to CSV """
__license__ = "BSD Version 3 License"
from python_library import *
import csv
import json
import requests
import importlib
# parameter file including access keys
## needed due to '-' in import filename
test_secrets = importlib.import_module("test-secrets")
# initialize local variables
json_messages={}
result_d={}
response=""
rec_count=0
output_filename="ambient_CH_"+datetime.datetime.now().strftime("%Y%m%d%H%M")+".csv"
# Set CSV Header & line format
csv_header = ['Temperature (F)','Humidity (%)','Wind Speed (mph)','Wind Gust (mph)','Daily Rainfall Totals (in)','Monthly Rainfall Totals (in)','Yearly Rainfall Totals (in)','UV Radiation Index','Date/Time']
csv_output_elements = [u'tempf',u'humidity',u'windspeedmph',u'windgustmph',u'dailyrainin',u'monthlyrainin',u'yearlyrainin',u'uv',u'date']
# construct api parameters
## Consider moving the URL into test_secrets
url="https://api.ambientweather.net/v1/devices/" + test_secrets.macAddress
api_key="?apiKey="
app_key="&applicationKey="
# number of records to retrieve
## Consider moving this into test_secrets
app_limit="&limit=21"
# build querystring
api_querystring=api_key + test_secrets.apiKey + app_key + test_secrets.appKey + app_limit
api_headers={'Content-Type': 'application/json','Accept': 'application/json'}
# Retrieve content from API
#log_message("sending: " + url + api_querystring)
try:
response = requests.get(url + api_querystring, headers=api_headers)
except:
json_messages['message'] = "get request failed"
json_messages['exit status'] = "error"
log_json_message(json_messages)
exit(1)
if response.status_code != 200:
if response.status_code == 429:
json_messages['message'] = "API Limit Exceeded"
json_messages['return_code'] = response.status
json_messages['exit status'] = "warning"
else:
json_messages['message'] = "Retrieval failed"
json_messages['return_code'] = response.status_code
json_messages['url'] = url + api_querystring
json_messages['exit status'] = "error"
log_json_message(json_messages)
exit(1)
# load content
result_set=json.loads(response.text)
json_messages['records retrieved'] = len(result_set)
if len(result_set) == 0:
json_messages['message'] = "No content retrieved"
json_messages['exit status'] = "error"
log_json_message(json_messages)
exit(1)
# create output file & write header row
try:
output_file = open(output_filename, 'w')
csvwriter = csv.writer(output_file, dialect='excel')
except IOError:
json_messages['message'] = "Output file creation failed"
json_messages['exit status'] = "error"
log_json_message(json_messages)
exit(1)
csvwriter.writerow(csv_header)
# ETL processing result set
for i in range(0, len(result_set)):
# convert to a Dict for key lookup
result_d=result_set[i]
# Translate temperature
if result_d[u'tempf'] < 60:
result_d[u'tempf']="Too cold"
elif result_d[u'tempf'] > 85:
result_d[u'tempf']="Too hot"
else:
result_d[u'tempf']="Goldilocks"
# build the output values in key order
csv_output=['null']*len(csv_output_elements)
j=0
for key in csv_output_elements:
csv_output[j]=result_d[key]
j +=1
csvwriter.writerow(csv_output)
rec_count += 1
# cleanup and exit
output_file.close()
json_messages['rows exported'] = rec_count
json_messages['output file'] = output_filename
json_messages['exit status'] = "normal"
log_json_message(json_messages)
exit(0)