-
Notifications
You must be signed in to change notification settings - Fork 2
/
MiHive.py
117 lines (96 loc) · 2.89 KB
/
MiHive.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
# MiHive (c) 2014 David Tarrant
# License: GPLv3
import cookielib
import urllib
import urllib2
import json
from csv import DictWriter
import os
import time
from datetime import date
# Open config file
json_data=open('/home/pi/MiHive-dev/config.json')
config = json.load(json_data)
json_data.close()
config = config["config"]
username = config[0]["username"]
password = config[1]["password"]
dataPath = config[2]["dataPath"]
def makeRequest(url,payload):
global urllib2
global opener
if payload:
# Use urllib to encode the payload
data = urllib.urlencode(payload)
# Build our Request object (supplying 'data' makes it a POST)
req = urllib2.Request(url, data)
else:
req = urllib2.Request(url)
# Make the request and read the response
try:
resp = urllib2.urlopen(req)
except urllib2.URLError, e:
print e.code
else:
# 200
body = resp.read()
return body;
return None;
def writeToFile(data):
global dataPath
year = date.today().year
month = date.today().month
day = date.today().day
fullpath = dataPath + "/" + str(year) + "/" + str(month).zfill(2);
file = fullpath + "/" + str(day).zfill(2) + ".csv"
if not os.path.exists(fullpath):
os.makedirs(fullpath)
dicts = data
if os.path.exists(file):
outfile = open(file,"a+")
writer = DictWriter(outfile, dicts.keys())
writer.writerow(dicts)
else:
outfile = open(file,"a+")
writer = DictWriter(outfile, dicts.keys())
writer.writeheader()
writer.writerow(dicts)
outfile.close()
# Store the cookies and create an opener that will hold them
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
# Add our headers
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36')]
# Install our opener (note that this changes the global opener to the one
# we just made, but you can also just call opener.open() if you want)
urllib2.install_opener(opener)
url = 'https://my.hivehome.com/login'
payload = {
'username':username,
'password':password
}
# Login
makeRequest(url,payload)
# Get timestamp for record
timeStamp = time.time()
# Get temporature (weather) data for inside and out
opener.addheaders = [('Accept', 'application/json')];
url = 'https://my.hivehome.com/weather';
body = makeRequest(url,None)
jsonData = json.loads(body)
jsonData['time'] = timeStamp
# Get heating target temporature
opener.addheaders = [('X-Requested-With', 'XMLHttpRequest')];
url = 'https://my.hivehome.com/heating/target';
body = makeRequest(url,None)
target = json.loads(body)
jsonData['target'] = target['target']
# Get hot water status
url = 'https://my.hivehome.com/hotwater/schedule';
body = makeRequest(url,None)
water = json.loads(body)
jsonData['hotWater'] = water['current']
writeToFile(jsonData)
# Logout
url = 'https://my.hivehome.com/login'
makeRequest(url,None)