-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarmapi.py
70 lines (58 loc) · 2.54 KB
/
swarmapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
class SwarmAPI:
def __init__(self):
self.recent_data = {}
self.msg_cached = False
self.recent_telemetry = {}
self.tel_cached = False
self.authVal = self._login()
def _login(self):
url = "https://bumblebee.hive.swarm.space/hive/login"
payload = "username=ceti&password=1pE%5ELBAO8z%24kdg%239nTQ5WnMKXERrI%5EG%24M2POTuyy9A"
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
response = requests.request("POST", url, headers=headers, data=payload)
print("SWARM Login succeeded? %s" % str(response.status_code == 200))
return "Bearer %s" % response.json()["token"]
def _get_messages(self, count=100):
url = (
"https://bumblebee.hive.swarm.space/hive/api/v1/messages?organizationId=65466&userApplicationId=65535&devicetype=1&deviceid=0x01d27&direction=fromdevice&count=%s"
% count
)
payload = {}
headers = {"accept": "application/json", "Authorization": self.authVal}
response = requests.request("GET", url, headers=headers, data=payload)
if response.status_code == 200:
self.recent_data = response.json()
self.msg_cached = True
return None
print("Message GET failed.")
def _get_telemetry(self, count=100):
url = (
"https://bumblebee.hive.swarm.space/hive/api/v1/telemetry/devices?organizationId=65466&srcDeviceType=1&count=%s&sortAsc=false"
% count
)
payload = {}
headers = {
"accept": "application/json",
"Authorization": self.authVal,
}
response = requests.request("GET", url, headers=headers, data=payload)
if response.status_code == 200:
self.recent_telemetry = response.json()
self.tel_cached = True
return None
print("Telemetry GET failed.")
def get_recent_data(self, count=100, use_cached=True):
if not (use_cached and self.msg_cached and self.tel_cached):
self._get_messages(count=count)
self._get_telemetry(count=count)
print("No caches?")
return [self.recent_data, self.recent_telemetry]
def logout(self):
url = "https://bumblebee.hive.swarm.space/hive/logout"
payload = {}
headers = {"Authorization": self.authVal}
response = requests.request("GET", url, headers=headers, data=payload)
print("SWARM Logout succeeded? %s" % str(response.status_code == 204))