hk_video_api.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import requests
  4. import json
  5. import time
  6. import uuid
  7. import hmac
  8. import hashlib
  9. import base64
  10. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  11. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  12. HKSDK_BASE_URL = f"https://19.155.243.66/artemis"
  13. def __hksdk_generate_signature(path):
  14. app_key = "28132932"
  15. app_secret = "vPUDL4ilt2mENr8XEfa7"
  16. # 设置请求头
  17. timestamp = str(int(time.time() * 1000))
  18. nonce = str(uuid.uuid4())
  19. headers = {
  20. "Accept": "*/*",
  21. "Content-Type": "application/json",
  22. "x-ca-key": app_key,
  23. "x-ca-nonce": nonce,
  24. "x-ca-timestamp": timestamp,
  25. "x-ca-signature-headers": "x-ca-key,x-ca-nonce,x-ca-timestamp"
  26. }
  27. # 拼接签名字符串
  28. nonce = headers.get("x-ca-nonce")
  29. timestamp = headers.get("x-ca-timestamp")
  30. sign_str = "POST\n*/*\napplication/json" + "\nx-ca-key:" + app_key + "\nx-ca-nonce:" + \
  31. nonce + "\nx-ca-timestamp:" + timestamp + "\n" + \
  32. "/artemis"+path
  33. # 生成签名
  34. signature = hmac.new(app_secret.encode(), sign_str.encode(), hashlib.sha256).digest()
  35. headers["x-ca-signature"]=base64.b64encode(signature).decode()
  36. return headers
  37. def get_video_url(id: str, protocol: str):
  38. video_url = ""
  39. path = "/api/video/v1/cameras/previewURLs"
  40. url = f"{HKSDK_BASE_URL}{path}"
  41. body = {
  42. "cameraIndexCode": id,
  43. "streamType":1, # 子码流
  44. "protocol": protocol,
  45. "transmode": 0, # 使用TCP传输
  46. "expand": "transcode=1&videotype=h264&resolution=D1"
  47. }
  48. if protocol == 'hlss':
  49. body['streamType'] = 0
  50. headers = __hksdk_generate_signature(path)
  51. response = requests.post(url, headers=headers, json=body, verify=False)
  52. if response.status_code == 200:
  53. data = response.json()
  54. if isinstance(data, dict) and data['code'] == '0':
  55. video_url = data['data']['url']
  56. return video_url
  57. def indexCode(id: str):
  58. # api_url = "http://10.181.7.236:8081/indexCode"
  59. # params = {
  60. # "cameraIndexCode": id
  61. # }
  62. # # print('param', params)
  63. # response = requests.post(url=api_url, params=params, timeout=15)
  64. # # print(response.text)
  65. # if response.status_code == 200:
  66. # result = response.json()
  67. # if result['errcode'] == 0:
  68. # data = result['data']
  69. # if isinstance(data,str):
  70. # data = json.loads(data)
  71. # return data
  72. path = "/api/resource/v1/cameras/indexCode"
  73. url = f"{HKSDK_BASE_URL}{path}"
  74. body = {
  75. "cameraIndexCode": id
  76. }
  77. headers = __hksdk_generate_signature(path)
  78. response = requests.post(url, headers=headers, json=body, verify=False)
  79. print(response.text)
  80. if response.status_code == 200:
  81. data = response.json()
  82. if isinstance(data, dict) and data['code'] == '0':
  83. return data['data']
  84. return ""
  85. def controlling(id: str, action: int, command: str, speed: int, presetIndex: str):
  86. # api_url = "http://10.181.7.236:8081/controlling"
  87. # params = {
  88. # "cameraIndexCode": id,
  89. # "action": action,
  90. # "command": command,
  91. # "speed": speed,
  92. # "presetIndex": presetIndex
  93. # }
  94. # print('param', params)
  95. # response = requests.post(url=api_url, params=params, timeout=15)
  96. # print(response.text)
  97. # if response.status_code == 200:
  98. # result = response.json()
  99. # if result['errcode'] == 0:
  100. # data = result['data']
  101. # return data
  102. path = "/api/video/v1/ptzs/controlling"
  103. url = f"{HKSDK_BASE_URL}{path}"
  104. body = {
  105. "cameraIndexCode": id,
  106. "action": action,
  107. "command": command,
  108. "speed": speed,
  109. "presetIndex": presetIndex
  110. }
  111. headers = __hksdk_generate_signature(path)
  112. response = requests.post(url, headers=headers, json=body, verify=False)
  113. print(response.text)
  114. if response.status_code == 200:
  115. data = response.json()
  116. if isinstance(data, dict) and data['code'] == '0':
  117. return data['data']
  118. return ""
  119. class HikvisionAPI:
  120. def __init__(self):
  121. self.app_key = "20034027"
  122. self.app_secret = "tfa3PNeYeN2MOrbRj3wS"
  123. self.base_url = f"https://19.155.243.66/artemis"
  124. # "/artemis/api/resource/v1/cameras"
  125. def _generate_signature(self, path):
  126. # 设置请求头
  127. timestamp = str(int(time.time() * 1000))
  128. nonce = str(uuid.uuid4())
  129. headers = {
  130. "Accept": "*/*",
  131. "Content-Type": "application/json",
  132. "x-ca-key": self.app_key,
  133. "x-ca-nonce": nonce,
  134. "x-ca-timestamp": timestamp,
  135. "x-ca-signature-headers": "x-ca-key,x-ca-nonce,x-ca-timestamp"
  136. }
  137. # 拼接签名字符串
  138. nonce = headers.get("x-ca-nonce")
  139. timestamp = headers.get("x-ca-timestamp")
  140. sign_str = "POST\n*/*\napplication/json" + "\nx-ca-key:" + self.app_key + "\nx-ca-nonce:" + \
  141. nonce + "\nx-ca-timestamp:" + timestamp + "\n" + \
  142. "/artemis"+path
  143. # 生成签名
  144. signature = hmac.new(self.app_secret.encode(), sign_str.encode(), hashlib.sha256).digest()
  145. headers["x-ca-signature"]=base64.b64encode(signature).decode()
  146. return headers
  147. def get_cameras(self,pageNo=1,pageSize=1):
  148. path = "/api/resource/v1/cameras"
  149. url = f"{self.base_url}{path}"
  150. body = {
  151. "pageNo": pageNo,"pageSize":pageSize
  152. }
  153. headers =self._generate_signature(path)
  154. response = requests.post(url, headers=headers, json=body, verify=False)
  155. if response.status_code == 200:
  156. return response.json()
  157. else:
  158. raise Exception(f"Failed to get preview URL: {response.text}")
  159. def get_regions(self,pageNo=1,pageSize=1):
  160. path = "/api/resource/v1/regions"
  161. url = f"{self.base_url}{path}"
  162. body = {
  163. "pageNo": pageNo,"pageSize":pageSize,
  164. "treeCode": "0"
  165. }
  166. headers =self._generate_signature(path)
  167. response = requests.post(url, headers=headers, json=body, verify=False)
  168. if response.status_code == 200:
  169. return response.json()
  170. else:
  171. raise Exception(f"Failed to get preview URL: {response.text}")