Created
September 10, 2012 16:58
-
-
Save luanlmd/3692126 to your computer and use it in GitHub Desktop.
Cosm python data pusher
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/python | |
# based on http://www.netfluvia.org/layer8/?p=175 | |
import mechanize | |
import json | |
import time | |
class Cosm: | |
_url_base = "http://api.cosm.com/v2/feeds/" | |
_feed_id = None | |
_version = None | |
## the substance of our update - list of dictionaries with keys 'id' and 'current_value' | |
_data = None | |
## the actual object we'll JSONify and send to the API endpoint | |
_payload = None | |
_opener = None | |
def __init__(self, feed_id, apikey): | |
self._version = "1.0.0" | |
self._feed_id = feed_id | |
self._opener = mechanize.build_opener() | |
self._opener.addheaders = [('X-PachubeApiKey',apikey)] | |
self._data = [] | |
self._payload = {} | |
def addDatapoint(self,dp_id,dp_value): | |
self._data.append({'id':dp_id, 'current_value':dp_value}) | |
def buildUpdate(self): | |
self._payload['version'] = self._version | |
self._payload['id'] = self._feed_id | |
self._payload['datastreams'] = self._data | |
def sendUpdate(self): | |
url = self._url_base + self._feed_id + "?_method=put" | |
try: | |
self._opener.open(url,json.dumps(self._payload)) | |
except mechanize.HTTPError as e: | |
print "An HTTP error occurred: %s " % e | |
c = Cosm(feed,key) | |
# do some stuff; gather data, repeating as necessary for any number of datastreams | |
c.addDatapoint(<datastream_id>,<data_value>)) | |
# finish up and submit the data | |
c.buildUpdate() | |
c.sendUpdate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment