Created
July 14, 2016 06:59
-
-
Save rw950431/d4118dea9f786a4bdc0fcc860f2cb84e to your computer and use it in GitHub Desktop.
script to extract data from thingspeak API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib2,json | |
import datetime | |
from time import sleep | |
import sys | |
READ_API_KEY='YOUR_API_KEY' | |
CHANNEL_ID=YOUR_CHANNEL | |
start_time=datetime.datetime.strptime('2014-04-16 07:25:08 UTC','%Y-%m-%d %H:%M:%S %Z') | |
results=1000 # fetch this many results (max 8000) | |
#extract data from thingspeak API, using multiple requests if necessary | |
#extracts from newest to oldest due to a quirk in the API | |
# 'results' parameter gives the last n results in time range rather than the first | |
# so you will have to re-sort the results again afterwards.. | |
def main(): | |
end_time=datetime.datetime.utcnow() # or set to desired value like above | |
err_count=0 | |
while end_time > start_time: | |
end_timestr=urllib2.quote(datetime.datetime.strftime(end_time,'%Y-%m-%d %H:%M:%S %Z')) | |
conn = urllib2.urlopen("https://api.thingspeak.com/channels/%d/feeds.csv?results=%d&api_key=%s&end=%s" \ | |
% (CHANNEL_ID,results,READ_API_KEY,end_timestr)) | |
if (conn.getcode()!=200): | |
sys.stderr.write("http status code=%d" % (conn.getcode())) | |
conn.close() | |
err_count+=1 | |
if err_count>5: | |
sys.stderr.write("too many errors, giving up") | |
break | |
sleep(10) | |
continue | |
err_count=0 | |
response = conn.read() | |
data=response.split('\n') | |
# print in reverse order | |
data2=sorted(filter((lambda x:x!=''),data[1:]),reverse=True) | |
if len(data2)<=0: | |
sys.stderr.write( "no more data" ) | |
break | |
for d in data2: | |
print d | |
#get the last (earliest) timestamp | |
dtime=data2[-1].split(',')[0] | |
# subtract 1 second to avoid duplicates | |
end_time=datetime.datetime.strptime(dtime,'%Y-%m-%d %H:%M:%S %Z')-datetime.timedelta(seconds=1) | |
#sys.stderr.write( "end_time= %s" % end_time) | |
conn.close() | |
sleep(5) | |
sys.stderr.write("finished") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment