import datetime,json from datetime import timedelta import requests import math from Utils.HikAccessToken import hik_access_token def is_token(response,client_id,client_secret): if response["code"] == 401: hik_accesstoken = hik_access_token(client_id,client_secret) return hik_accesstoken def resp(hik_accesstoken,pageNo,person_dict,expire_visitor): response = requests.get( url="https://api2.hik-cloud.com/api/v1/open/basic/persons/list?access_token=%s&pageNo=%s&pageSize=999" %(hik_accesstoken,pageNo) ) response = response.json() totalsize = response["data"]["total"] persons = response["data"]["rows"] for person in persons: personId = person["employeeNo"] personName = person["personName"] person_dict[personId] = personName if person["personType"] == "visitor": validEndTime = person["validEndTime"] validEndTime = datetime.datetime.strptime(validEndTime,"%Y-%m-%dT%H:%M:%S+08:00") nowTime = datetime.datetime.utcnow()+timedelta(hours=8) if validEndTime < nowTime: expire_visitor.append(personId) return totalsize,person_dict,expire_visitor def hik_getuserlist(hik_accesstoken): person_dict = {} expire_visitor = [] pageNo = 1 totalsize,person_dict,expire_visitor = resp(hik_accesstoken,pageNo,person_dict,expire_visitor) page = math.ceil(int(totalsize)/999) while pageNo < page: pageNo = pageNo +1 resp(hik_accesstoken, pageNo, person_dict,expire_visitor) # print(person_dict) return person_dict,expire_visitor # if __name__ == '__main__': # with open("../config.json") as f: # data = json.load(f) # client_id = data["client_id"] # client_secret = data["client_secret"] # groupId = data["groupId"] # hik_accesstoken = hik_access_token(client_id, client_secret) # person_dict,expire_visitor = hik_getuserlist(hik_accesstoken) # print(person_dict) # print(expire_visitor)