-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsnapshots_uploader.py
46 lines (40 loc) · 1.22 KB
/
snapshots_uploader.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
import json
import requests
import gzip
filename = '1674415503.gz' # snapshot filename
token = 'YOUR_FLESPI_TOKEN_HERE'
id = 0 # set your device id
# if from_timestamp and to_timestamp are zero - republish all messages from the snapshot
from_timestamp = 0
to_timestamp = 0
count = 100 # messages in chunk
def post_messages():
print("\n=======\n")
print(cntr)
print("\n")
messages = requests.post(
'https://flespi.io/gw/devices/' + str(id) + '/messages',
data=json.dumps(arr),
headers={'Authorization': 'FlespiToken ' + token},
stream=True
)
if messages.status_code == 200:
print('Done')
else:
print(json.dumps(arr))
print('Uploading error!')
with gzip.open(filename, 'rb') as f:
data = json.load(f)
cntr = 0
arr = []
for i in data:
if cntr == count:
post_messages() # post messages chunk
cntr = 0
if cntr == 0:
arr = []
if (i['params']['timestamp'] <= to_timestamp and i['params']['timestamp'] >= from_timestamp) or (from_timestamp == 0 and to_timestamp == 0):
arr.append(i['params'])
cntr+=1
if cntr > 0:
post_messages() # post messages chunk