企業微信郵箱批量指令碼
阿新 • • 發佈:2020-08-05
缺陷:執行速度慢 1s 一條資料,無多執行緒
(如果有更好的方法,可以告知我)
指令碼功能介紹:
-
批量判斷建立不存在部門,更新成員到部門
#!/usr/bin/env python3 # -*- coding:utf8 -* import requests from xlrd import open_workbook import re import pprint from multiprocessing import Process def get_token(): """ 獲取 token :return: """ dict_new = { 'corpid': '', 'corpsecret': '' } url = 'https://api.exmail.qq.com/cgi-bin/gettoken' getr = requests.get(url=url, params=dict_new) access_token_new = getr.json().get('access_token') return access_token_new def get_department_members(department_id_new): """ 獲取所有員工 userid :param department_id_new: :return: """ dict_new = { "access_token": str(access_token), "department_id": str(department_id_new), "fetch_child": int(1) } url = "https://api.exmail.qq.com/cgi-bin/user/simplelist" get_department_members_new = requests.get(url=url, params=dict_new, timeout=300).json()['userlist'] return get_department_members_new def find_department(name_new): """ 精準查詢部門 :param name_new: :return: """ dict_new = { "name": str(name_new), "fuzzy": 0, } url = 'https://api.exmail.qq.com/cgi-bin/department/search?access_token=' + str(access_token) post_find_department = requests.post(url=url, json=dict_new, timeout=300).json()['department'] return post_find_department def find_name_userid(dict_list, name_new): """ 遍歷查詢 userid :param dict_list: :param name_new: :return: """ for dict_new in dict_list: if dict_new['name'] == str(name_new): id_new = dict_new['userid'] return id_new def create_department(name_new, parentid_new): """ 建立部門 :param name_new: :param parentid_new: :return: """ dict_new = { "name": str(name_new), "parentid": int(parentid_new), } url = 'https://api.exmail.qq.com/cgi-bin/department/create?access_token=' + str(access_token) post_create_department = requests.post(url=url, json=dict_new).json() return post_create_department def update_member(userid_new, department_new): """ 更新成員資訊 :param userid_new: :param department_new: :return: """ dict_new = { "userid": str(userid_new), "department": [int(department_new)] } url = 'https://api.exmail.qq.com/cgi-bin/user/update?access_token=' + str(access_token) get_update_member = requests.post(url=url, json=dict_new, timeout=300).json() return get_update_member def update_userid_department(user_in, department_in, members_in): """ 高階 更新成員資訊 :param user_in: :param department_in: :param members_in: :return: """ userid_new = find_name_userid(members_in, user_in) department_new = find_department(department_in)[0]['id'] updata_member_new = update_member(userid_new, department_new) return updata_member_new def open_xlxs(filename_new, sheets_id): data_xlrd = open_workbook(str(filename_new)) table_xlrd_new = data_xlrd.sheets()[int(sheets_id)] return table_xlrd_new def get_members(userid_new): dict_new = { "access_token": access_token, "userid": userid_new } url = 'https://api.exmail.qq.com/cgi-bin/user/get' get_members_new = requests.get(url=url, params=dict_new, timeout=300).json() return get_members_new if __name__ == '__main__': access_token = get_token() members_list = get_department_members(1) table_xlrd = open_xlxs('alist.xlsx', 0) table_xlrd_0 = open_xlxs('alist.xlsx', 1) if table_xlrd.nrows == table_xlrd_0.nrows: ranit_xlrd_nrows = table_xlrd.nrows print('總行數: ' + str(ranit_xlrd_nrows)) for ranit_xlrd in range(ranit_xlrd_nrows): print('##### ' + str(ranit_xlrd) + ' #####') line_tablexlrd = table_xlrd.row_values(ranit_xlrd) # 定義全域性變數 global line_ranint global parentid_line global end_id global end_department global brack_id line_ranint = '' parentid_line = 0 end_id = 0 brack_id = 0 end_department = '' line_tablelist = [] line_findlist = [] # 遍歷列表執行 for ranit in range(table_xlrd.row_len(ranit_xlrd)): if line_tablexlrd[ranit]: line_ranint = line_ranint + line_tablexlrd[ranit] + '/' line_tablelist.append(line_ranint) line_findlist.extend(find_department(line_tablexlrd[ranit])) start_for = find_department(line_tablelist[-1].split('/')[-2]) for statfor in start_for: if statfor['path'] == line_tablelist[-1][:-1]: name_start = table_xlrd_0.row_values(ranit_xlrd)[0] update_start = update_member(get_members(find_name_userid(members_list, name_start))['userid'], statfor['id'])['errmsg'] brack_id = 1 print(name_start + ' 員工更新部門狀態: ' + update_start + "\n" + name_start + ' 員工更新部門路徑:' + statfor['path']) if brack_id == 0: for ranit in range(len(line_tablelist)): for findranit in line_findlist: if findranit['path'] == line_tablelist[ranit][:-1]: parentid_line = findranit['id'] print('路徑絕對匹配: ' + findranit['path']) break elif findranit['path'] == line_tablelist[ranit-1][:-1]: create_status = create_department(line_tablexlrd[ranit], parentid_line) if create_status['errcode'] == 0: findranit_append = find_department(line_tablexlrd[ranit]) print('建立值: ' + line_tablelist[ranit][:-1] + ' / id : ' + str(create_status['id'])) line_findlist.extend(findranit_append) parentid_line = create_status['id'] end_id = create_status['id'] end_department = line_tablelist[ranit][:-1] elif create_status['errcode'] == 60008: findranit_err_path = [] for findranit_err in line_findlist: if findranit_err['name'] == line_tablexlrd[ranit] \ and findranit_err['path'] == line_tablelist[ranit][:-1]: parentid_line = findranit_err['id'] findranit_err_path.append(findranit_err['path']) end_id = findranit_err['id'] end_department = findranit_err['path'] print('存在值: ' + findranit_err_path[0]) break if ranit == (len(line_tablelist)) - 1: name_ranit = table_xlrd_0.row_values(ranit_xlrd)[0] update_status = update_member(get_members(find_name_userid(members_list, name_ranit))['userid'], end_id)['errmsg'] print(name_ranit + ' 員工更新部門狀態: ' + update_status) print(name_ranit + ' 員工更新部門路徑:' + end_department) else: print('錯誤: 表單不一致 對比值:' + str(table_xlrd.nrows) + ' != ' + str(table_xlrd_0.nrows))