-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage_api_get_events.py
72 lines (61 loc) · 2.04 KB
/
storage_api_get_events.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
71
72
import argparse
import requests
import sys
import json
import base64
# Function for executing HTTP request
def storage_api_get_events(token, start, end, limit):
# HTTP header. Indicates the MIME document type (json)
headers = {
'accept': 'application/json',
}
# Parameters of the HTTP request
params = (
('limit', limit),
('order_by', 'time'),
('events', 'motion,sound'),
('start', start),
('end', end),
('token', token),
)
# Exception block
try:
response = requests.get('https://web.skyvr.videoexpertsgroup.com/api/v2/storage/events/', headers=headers, params=params, timeout=15) # Функция выполняющая HTTP запрос. Метод GET
except requests.exceptions.RequestException as e:
print (e)
sys.exit(1)
return response.status_code, response.text # The function returns the status of the HTTP request
# Parser for arguments
parser = argparse.ArgumentParser()
parser.add_argument('-access_token', '--access_token', help = 'Access token', required=True)
parser.add_argument('-start', '--start', help = 'Start time', required=True)
parser.add_argument('-end', '--end', help = 'End time', required=True)
parser.add_argument('-limit', '--limit', help = 'Limit', required=True)
# Checking parameters
try:
param = parser.parse_args()
except IOError as msg:
parser.error(str(msg))
# Parsing of access token
token_json=''
try:
token = base64.b64decode(param.access_token) # Decode access token
token_json = json.loads(token)
except Exception:
print("Error access token")
sys.exit(1)
# Makes HTTP request
code, data = storage_api_get_events(token_json["token"], str(param.start), str(param.end), str(param.limit))
# Returns the status of the HTTP request.
print ('Request completed. HTTP status code: ' + str(code)+'\n')
# Prints a list of events.
try:
data_json = json.loads(data)
except Exception:
pass
print ("Events:")
try:
for obj in data_json["objects"]:
print (obj)
except Exception:
print ("-------------")