forked from cymplecy/scratch_gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgh_cheerlights.py
executable file
·34 lines (29 loc) · 1.03 KB
/
sgh_cheerlights.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
#!/usr/bin/env python
import urllib2
import json
class CheerLights():
def __init__(self):
self.lastID = 0
self.urlRoot = "http://api.thingspeak.com/channels/1417/"
self.colours = []
# retrieve and load the JSON data into a JSON object
def getJSON(self, url):
jsonFeed = urllib2.urlopen(self.urlRoot + url)
feedData = jsonFeed.read()
# print feedData
jsonFeed.close()
data = json.loads(feedData)
# data = feedData
return data
# read the last entry_id
def getEntryID(self, feed):
return int(feed["entry_id"])
def get_colours(self):
last = self.getJSON("field/1/last.json")
if self.getEntryID(last) > self.lastID: # Have processed this entry_id before?
self.colours = []
data = self.getJSON("feed.json")
for feed in data["feeds"]:
self.colours = [str(feed["field1"])] + self.colours
self.lastID = self.getEntryID(feed)
return self.colours