#!/usr/bin/env python # # Copyright (c) 2011 Brian House # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # See for details. # """ For more info / discussion: http://blog.brianhouse.net/post/22400908807 """ import sys, os, json, time, datetime, urllib2 API_KEY = "XXXXXXXXXX" FEED_ID = 52901 DATASTREAM = 'Temp' SOURCE = "aquarium_temperature" def main(): t = time.time() start = int(t) - (28 * 6 * 60 * 60) t = time.time() print("NOW %s (%s)" % (t, datetime.datetime.utcfromtimestamp(t).isoformat())) print("START %s (%s)" % (start, datetime.datetime.utcfromtimestamp(start).isoformat())) while float(start) < t: request(start) start += 6 * 60 * 60 # add six hours def request(start): page = 1 while True: try: start_clock = time.time() start_str = "start=%sZ&" % datetime.datetime.utcfromtimestamp(start).isoformat() url = "http://api.pachube.com/v2/feeds/%s/datastreams/%s.json?%sduration=6hours&interval=0&per_page=1000&page=%s" % (FEED_ID, DATASTREAM, start_str, page) print(url) result = net_read(url, headers={'X-PachubeApiKey': API_KEY}) try: data = json.loads(result) if 'datapoints' not in data: # print("No points, got: %s" % data) break datapoints = data['datapoints'] page += 1 for datapoint in datapoints: value = datapoint['value'] date = datapoint['at'] # do something with the data here print("%s: %s" % (date, value)) except Exception as e: print("Parse error (%s), exiting..." % e) exit() # rate limit is 100/minute, so just make sure each request takes a second. elapsed = time.time() - start_clock if elapsed < 1: time.sleep(1 - elapsed) except Exception as e: print(e) def net_read(source, headers=None): request = urllib2.Request(source) if headers: for header, value in headers.items(): request.add_header(header, value) return urllib2.urlopen(request).read() if __name__ == "__main__": main()