-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
68 lines (56 loc) · 1.69 KB
/
util.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
import requests
import json
import hmac
import hashlib
from base64 import b64encode
from datetime import time
from configparser import ConfigParser
import os
def readConfig():
config = ConfigParser()
config.read("config.ini")
values = config['VALUES']
return {
'OPENAPI_DOMAIN': values['domain'],
'USER_ID': values['userId'],
'USER_SECRET': values['secret'],
'BEGIN_TIME': time.fromisoformat(values['beginTime']),
'END_TIME': time.fromisoformat(values['endTime']),
'CAMERA_IDS': values['cameras'].split(',')
}
OPENAPI_DOMAIN, USER_ID, USER_SECRET = readConfig()['OPENAPI_DOMAIN'], readConfig()['USER_ID'], readConfig()['USER_SECRET']
def iterDate(start, end, step):
ctr = start
end = end
while ctr < end:
next = ctr + step
yield (ctr, next)
ctr = next
def generateSignature(message, secret):
bytes = hmac.new(
key = secret.encode(),
msg = message.encode(),
digestmod = hashlib.sha256).digest()
return b64encode(bytes).decode()
def hikRequest(endpoint, data = {}):
url = f"{OPENAPI_DOMAIN}{endpoint}"
payload = json.dumps(data)
signatureString = f'''POST
*/*
application/json
x-ca-key:{USER_ID}
{endpoint}'''
signature = generateSignature(signatureString, USER_SECRET)
headers = {
'X-Ca-Key': USER_ID,
'X-Ca-Signature-Headers': 'X-Ca-Key',
'X-Ca-Signature': signature,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json()['data']
def downloadFromUrl(filename, url):
bts = requests.request('GET', url, verify=False).content
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'wb') as file:
file.write(bts)