HikGetUserlist.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import datetime,json
  2. from datetime import timedelta
  3. import requests
  4. import math
  5. from Utils.HikAccessToken import hik_access_token
  6. def is_token(response,client_id,client_secret):
  7. if response["code"] == 401:
  8. hik_accesstoken = hik_access_token(client_id,client_secret)
  9. return hik_accesstoken
  10. def resp(hik_accesstoken,pageNo,person_dict,expire_visitor):
  11. response = requests.get(
  12. url="https://api2.hik-cloud.com/api/v1/open/basic/persons/list?access_token=%s&pageNo=%s&pageSize=999" %(hik_accesstoken,pageNo)
  13. )
  14. response = response.json()
  15. totalsize = response["data"]["total"]
  16. persons = response["data"]["rows"]
  17. for person in persons:
  18. personId = person["employeeNo"]
  19. personName = person["personName"]
  20. person_dict[personId] = personName
  21. if person["personType"] == "visitor":
  22. validEndTime = person["validEndTime"]
  23. validEndTime = datetime.datetime.strptime(validEndTime,"%Y-%m-%dT%H:%M:%S+08:00")
  24. nowTime = datetime.datetime.utcnow()+timedelta(hours=8)
  25. if validEndTime < nowTime:
  26. expire_visitor.append(personId)
  27. return totalsize,person_dict,expire_visitor
  28. def hik_getuserlist(hik_accesstoken):
  29. person_dict = {}
  30. expire_visitor = []
  31. pageNo = 1
  32. totalsize,person_dict,expire_visitor = resp(hik_accesstoken,pageNo,person_dict,expire_visitor)
  33. page = math.ceil(int(totalsize)/999)
  34. while pageNo < page:
  35. pageNo = pageNo +1
  36. resp(hik_accesstoken, pageNo, person_dict,expire_visitor)
  37. # print(person_dict)
  38. return person_dict,expire_visitor
  39. # if __name__ == '__main__':
  40. # with open("../config.json") as f:
  41. # data = json.load(f)
  42. # client_id = data["client_id"]
  43. # client_secret = data["client_secret"]
  44. # groupId = data["groupId"]
  45. # hik_accesstoken = hik_access_token(client_id, client_secret)
  46. # person_dict,expire_visitor = hik_getuserlist(hik_accesstoken)
  47. # print(person_dict)
  48. # print(expire_visitor)