Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4.  
  5. import sys
  6. import time
  7. import pprint
  8.  
  9. from satori.rtm.client import make_client, SubscriptionMode
  10.  
  11. endpoint = "wss://open-data.api.satori.com"
  12. appkey = "5Ba8Bf20bbAaaf1EDBA997E63AF4a78f"
  13. channel = "new-zealand"
  14. messages = []
  15. ids = []
  16.  
  17.  
  18. def to_conduce(messages):
  19. existing_ents = []
  20. new_ents = []
  21. for message in messages:
  22. ent = {"identity":message['vehicleId'],
  23. "timestamp_ms": int(message['lastLocationUpdateTime']),
  24. "endtime_ms": int(message['lastLocationUpdateTime']+86400000),
  25. "kind": "new-zealand",
  26. "path": [{"y":float(message['location']['lon']),"x":float(message['location']['lat']),"z":float(0.0)}],
  27. "attrs": json.dumps(message)
  28. }
  29. if message['vehicleId'] in vehicle_ids:
  30. existing_ents.append(ent)
  31. else:
  32. new_ents.append(ent)
  33. r = requests.post('some url')
  34. print(r.text)
  35. r = requests.post('some url')
  36. print(r.text)
  37. messages = []
  38.  
  39. def main():
  40. messages = []
  41. with make_client(endpoint=endpoint, appkey=appkey) as client:
  42. print('Connected to Satori RTM!')
  43.  
  44. class SubscriptionObserver(object):
  45. def on_subscription_data(self, data):
  46. for message in data['messages']:
  47. print(message['entity'].keys())
  48. messages.append(message)
  49. if len(messages)>1000:
  50. to_conduce(messages)
  51. messages = []
  52.  
  53. subscription_observer = SubscriptionObserver()
  54. client.subscribe(
  55. channel,
  56. SubscriptionMode.SIMPLE,
  57. subscription_observer)
  58.  
  59. try:
  60. while True:
  61. time.sleep(1)
  62. except KeyboardInterrupt:
  63. pass
  64.  
  65.  
  66. if __name__ == '__main__':
  67. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement