-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
59 lines (50 loc) · 2.16 KB
/
scrape.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
import requests
from xmljson import badgerfish as bf
from xml.etree.ElementTree import fromstring
from json import dumps, loads
class EnergyData:
def __init__(self):
self.appliances = [
"http://152.3.3.210/cgi-bin/egauge?noteam",
"http://152.3.3.214/cgi-bin/egauge?noteam",
"http://152.3.3.235/cgi-bin/egauge?noteam",
"http://152.3.3.213/cgi-bin/egauge?noteam"]
self.utilities = ["http://152.3.3.246/cgi-bin/egauge?noteam",
"http://152.3.3.236/cgi-bin/egauge?noteam"]
self.prevUtilities = dict()
self.prevAppliances = dict()
def subDict(self,a, b):
for i in range(len(a)):
a[i]["energy"] = a[i]["energy"] - b[i]["energy"]
return a
def getUtilities(self):
if any(self.prevUtilities):
latest = self.getInfo(self.utilities)
diff = self.subDict(latest, self.prevUtilities)
return diff
self.prevUtilities = self.getInfo(self.utilities)
return dict()
def getCumulativeUtilities(self):
return self.getInfo(self.utilities)
def getCumulativeAppliances(self):
return self.getInfo(self.appliances)
def getAppliances(self):
if any(self.prevAppliances):
latest = self.getInfo(self.appliances)
diff = self.subDict(latest, self.prevAppliances)
return diff
self.prevAppliances = self.getInfo(self.appliances)
return dict()
def getInfo(self, urls):
rt = []
for i in urls: #
try:
r = requests.get(i) # r is a request object
d = loads(dumps(bf.data(fromstring(r.text)), indent = 4)) # r.text is the xml, bf.data returns OrderedDicts, load+dumps converts to Dict
timestamp = d["measurements"]["timestamp"]["$"]
for i in d["measurements"]["meter"]:
# print("{:<40} {:<20} {:<20}".format(i["@title"],i["energyWs"]["$"],i["power"]["$"]))
rt.append({"timestamp":timestamp, "source":i["@title"], "energy":i["energyWs"]["$"],"power":i["power"]["$"] })
except:
print("network error")
return rt