123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import datetime
- import os
- import time
- from Utils.AccessToken import access_token
- from Utils.UserId import userid
- from Utils.JobId import jobid
- from Utils.HikAccessToken import hik_access_token
- from Utils.HikGetUserlist import hik_getuserlist
- from Utils.HikAdduser import hik_adduser
- from Utils.HikDeletevisitor import hik_delete_visitor
- from Utils.HikPermissionGroupsPersons import hik_permissiongroupspersons
- from Utils.HikAddcard import hik_addcard
- from Utils.Hik_isToken import hik_is_token
- from Utils.Dingtalk import send_message
- import json
- def datetime_now():
- datetimenow = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
- return datetimenow
- with open("config.json") as f:
- data = json.load(f)
- appkey = data["appkey"]
- appsecret = data["appsecret"]
- client_id = data["client_id"]
- client_secret = data["client_secret"]
- groupId = data["groupId"]
- sleeptime = data["sleeptime"]
- dingding = data["dingtalk"]
- #容器获取环境变量
- # appkey = os.getenv("appkey")
- # appsecret = os.getenv("appsecret")
- # client_id = os.getenv("client_id")
- # client_secret = os.getenv("client_secret")
- # groupId = os.getenv("groupId")
- # sleeptime = os.getenv("sleeptime")
- # dingding = os.getenv("dingtalk")
- #获取Hik token
- hik_accesstoken = hik_access_token(client_id,client_secret)
- # print(hik_accesstoken)
- while True:
- #获取钉钉token
- ding_accesstoken = access_token(appkey,appsecret)
- print("%s,获取钉钉token成功"%datetime_now())
- #获取钉钉用户id
- ding_userid_list = userid(ding_accesstoken)
- print("%s,获取钉钉用户id成功"%datetime_now())
- #获取钉钉用户工号
- ding_jobidlists = jobid(ding_userid_list,ding_accesstoken)
- print("%s,获取钉钉用户工号成功"%datetime_now())
- #判断token是否有效
- hik_accesstoken = hik_is_token(hik_accesstoken,client_id,client_secret)
- # print(hik_accesstoken)
- #获取Hik 用户列表
- hik_userlists,expire_visitor = hik_getuserlist(hik_accesstoken)
- # print(hik_userlists)
- #删除过期访客信息
- hik_delete_visitor(hik_accesstoken, expire_visitor)
- print("%s,删除过期访客信息成功,共计%s个"%(datetime_now(),len(expire_visitor)))
- #钉钉用户数据同步到海康云眸
- #查询出海康云眸里没有的钉钉jobid
- diff_value = set(ding_jobidlists) - set(hik_userlists)
- diff_value = list(diff_value)
- #删除jobid为空的
- j = 0
- for i in range(len(diff_value)):
- if diff_value[j] == "":
- diff_value.pop(j)
- else:
- j += 1
- if diff_value:
- message = "【dingding_hik_cloud】 %s,需要添加用户列表%s,共计%s个"%(datetime_now(),diff_value,len(diff_value))
- print(message)
- send_message(message,dingding)
- #根据以上取出的jobid往海康云眸里添加用户
- for id in diff_value:
- name = ding_jobidlists[id]
- hik_adduser(hik_accesstoken,id,name)
- time.sleep(2)
- #根据以上取出的jobid在海康云眸里权限组绑定人员
- hik_permissiongroupspersons(hik_accesstoken,groupId,diff_value)
- #根据以上取出的jobid在海康云眸里创建卡片
- hik_addcard(hik_accesstoken,diff_value)
- else:
- print("%s,没有需要同步的人员"%datetime_now())
- time.sleep(int(sleeptime))
|