main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import datetime
  2. import os
  3. import time
  4. from Utils.AccessToken import access_token
  5. from Utils.UserId import userid
  6. from Utils.JobId import jobid
  7. from Utils.HikAccessToken import hik_access_token
  8. from Utils.HikGetUserlist import hik_getuserlist
  9. from Utils.HikAdduser import hik_adduser
  10. from Utils.HikDeletevisitor import hik_delete_visitor
  11. from Utils.HikPermissionGroupsPersons import hik_permissiongroupspersons
  12. from Utils.HikAddcard import hik_addcard
  13. from Utils.Hik_isToken import hik_is_token
  14. from Utils.Dingtalk import send_message
  15. import json
  16. def datetime_now():
  17. datetimenow = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  18. return datetimenow
  19. with open("config.json") as f:
  20. data = json.load(f)
  21. appkey = data["appkey"]
  22. appsecret = data["appsecret"]
  23. client_id = data["client_id"]
  24. client_secret = data["client_secret"]
  25. groupId = data["groupId"]
  26. sleeptime = data["sleeptime"]
  27. dingding = data["dingtalk"]
  28. #容器获取环境变量
  29. # appkey = os.getenv("appkey")
  30. # appsecret = os.getenv("appsecret")
  31. # client_id = os.getenv("client_id")
  32. # client_secret = os.getenv("client_secret")
  33. # groupId = os.getenv("groupId")
  34. # sleeptime = os.getenv("sleeptime")
  35. # dingding = os.getenv("dingtalk")
  36. #获取Hik token
  37. hik_accesstoken = hik_access_token(client_id,client_secret)
  38. # print(hik_accesstoken)
  39. while True:
  40. #获取钉钉token
  41. ding_accesstoken = access_token(appkey,appsecret)
  42. print("%s,获取钉钉token成功"%datetime_now())
  43. #获取钉钉用户id
  44. ding_userid_list = userid(ding_accesstoken)
  45. print("%s,获取钉钉用户id成功"%datetime_now())
  46. #获取钉钉用户工号
  47. ding_jobidlists = jobid(ding_userid_list,ding_accesstoken)
  48. print("%s,获取钉钉用户工号成功"%datetime_now())
  49. #判断token是否有效
  50. hik_accesstoken = hik_is_token(hik_accesstoken,client_id,client_secret)
  51. # print(hik_accesstoken)
  52. #获取Hik 用户列表
  53. hik_userlists,expire_visitor = hik_getuserlist(hik_accesstoken)
  54. # print(hik_userlists)
  55. #删除过期访客信息
  56. hik_delete_visitor(hik_accesstoken, expire_visitor)
  57. print("%s,删除过期访客信息成功,共计%s个"%(datetime_now(),len(expire_visitor)))
  58. #钉钉用户数据同步到海康云眸
  59. #查询出海康云眸里没有的钉钉jobid
  60. diff_value = set(ding_jobidlists) - set(hik_userlists)
  61. diff_value = list(diff_value)
  62. #删除jobid为空的
  63. j = 0
  64. for i in range(len(diff_value)):
  65. if diff_value[j] == "":
  66. diff_value.pop(j)
  67. else:
  68. j += 1
  69. if diff_value:
  70. message = "【dingding_hik_cloud】 %s,需要添加用户列表%s,共计%s个"%(datetime_now(),diff_value,len(diff_value))
  71. print(message)
  72. send_message(message,dingding)
  73. #根据以上取出的jobid往海康云眸里添加用户
  74. for id in diff_value:
  75. name = ding_jobidlists[id]
  76. hik_adduser(hik_accesstoken,id,name)
  77. time.sleep(2)
  78. #根据以上取出的jobid在海康云眸里权限组绑定人员
  79. hik_permissiongroupspersons(hik_accesstoken,groupId,diff_value)
  80. #根据以上取出的jobid在海康云眸里创建卡片
  81. hik_addcard(hik_accesstoken,diff_value)
  82. else:
  83. print("%s,没有需要同步的人员"%datetime_now())
  84. time.sleep(int(sleeptime))