majorcornwallace

Step 1: Starter Python Class for AQE

Feb 3rd, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. import json
  2. import httplib2
  3. #
  4. # AQE Utility Class
  5. #
  6.  
  7.  
  8. #
  9. # Configuration Data
  10. #
  11. aqe_feeds = {'1703619548':'EsTjqWQlHyj69kBKBYbtoanldlKxAb7HalVxzW5ef2V5x296'}
  12. xivelyurl = 'http://api.xively.com/v2/feeds/'
  13. stream_keys = ('CO','Dust','NO2','Temperature','Humidity')
  14.  
  15. class AQEgg(object):
  16.     def __init__(self, host_url, api_key, api_feed):
  17.         self.api_key = api_key
  18.         self.host_url = host_url
  19.         self.api_feed = api_feed
  20.         self.json_headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "application/json", "X-ApiKey": self.api_key}
  21.         self.feed_url = self.host_url + self.api_feed
  22.        
  23.     def get_json_data(self):
  24.         jsondata = {}
  25.         http = httplib2.Http()
  26.         response, content = http.request(self.feed_url, 'GET', headers=self.json_headers)
  27.         jsondata = json.loads(content)
  28.         if ('OK' in response.reason):
  29.             print(response.status, response.reason)
  30.         return jsondata
  31.        
  32.     def parse_egg_data(self,datastream_keys, eggdata):
  33.         eggdict = {datastream['id'] : datastream['current_value'] for datastream in eggdata['datastreams'] if (datastream['id'] in datastream_keys)}
  34.         return eggdict
  35.  
  36. # Main Loop
  37. for feed_id in aqe_feeds:
  38.     aqe = AQEgg(xivelyurl, aqe_feeds[feed_id], feed_id)
  39.  
  40. aqe_json = aqe.get_json_data()
  41. aqe_data = aqe.parse_egg_data(stream_keys,aqe_json)
  42.  
  43. for key in aqe_data:
  44.     print("%s: %s" % (key, aqe_data[key]))
  45.  
  46. #print(" ")
  47. #print(aqe_json)
  48.  
  49. #for datastream in aqe_json['datastreams']:
  50. #   print("{")
  51. #   for key in datastream:
  52. #       print (("\t%s: %s") % (key,datastream[key]))
  53. #   print("}")
  54.  
  55.  
  56. ###############################
Advertisement
Add Comment
Please, Sign In to add comment