1. 程式人生 > 實用技巧 >分享一個監測企業微信群人員變化的指令碼...

分享一個監測企業微信群人員變化的指令碼...

使用前提需要每天在企業微信群中,選擇複製所有群成員的按鈕,然後每天儲存成檔案放到指令碼同目錄就可以.

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 import sys
  4 import os
  5 
  6 PRINTRED = "\033[1;31m"
  7 PRINTGREEN = "\033[0;32;47m"   #green color backgroud is white
  8 PRINTGREEN_SIM = "\033[1;32m"  #no backgroud green color
  9 PRINTBLUE = "
\033[1;34m" 10 PRINTCOLOR_END= "\033[0m" 11 12 def getPersonList(fileName): 13 try: 14 fp = open(fileName, 'r') 15 fLines = fp.readlines() 16 fp.close() 17 perList = [] 18 for line in fLines: 19 perSplit = line.split(';') 20 for
item in perSplit: 21 perList.append(item) 22 23 #print(perList) 24 return perList 25 except IOError: 26 print("error: file not found, please check it !!!") 27 sys.exit(0) 28 29 def calPersonDiffResult(orgList, compList): 30 #find delete person
31 countdel = 0 32 countadd = 0 33 for org in orgList: 34 find = 0 35 for comp in compList: 36 if (org == comp): 37 find = 1 38 if (find == 0): 39 countdel += 1 40 print(("減少人員姓名:[{cstart}%s{cend}] !").format(cstart=PRINTRED,cend=PRINTCOLOR_END) % org) 41 print(("總減少人數:[{cstart}%d{cend}] 人!!!").format(cstart=PRINTRED,cend=PRINTCOLOR_END) % countdel) 42 43 #find add person 44 for comp in compList: 45 find = 0 46 for org in orgList: 47 if (comp == org): 48 find = 1 49 if (find == 0): 50 countadd += 1 51 print(("新增人員姓名:[{cstart}%s{cend}] !").format(cstart=PRINTGREEN_SIM,cend=PRINTCOLOR_END) % comp) 52 print(("總新增人員人數:[{cstart}%d{cend}] 人!!!\n\n").format(cstart=PRINTGREEN_SIM,cend=PRINTCOLOR_END) % countadd) 53 54 55 #預設選擇當前路徑人員列表檔案 56 def exploreFiles(curDir = './'): 57 ''' 58 fList = [] 59 for root,dirs,files in os.walk(curDir): 60 for file in files: 61 fileSuffix = os.path.splitext(file)[1] 62 if fileSuffix == '.py' or fileSuffix == '.pyc' or fileSuffix == '.swp': 63 print(("跳過非法檔案 :[{cstart}%s{cend}]").format(cstart=PRINTBLUE,cend=PRINTCOLOR_END) % file) 64 else: 65 #print('append %s' % (file)) 66 fList.append(file) 67 return fList 68 ''' 69 70 #按修改時間獲取排序檔案 71 dir_list = os.listdir(curDir) 72 if dir_list == None: 73 print(("{cstart}路徑未找到{cend}").format(cstart=PRINTRED,cend=PRINTCOLOR_END)) 74 return 75 else: 76 dir_list = sorted(dir_list,key=lambda x: os.path.getmtime(os.path.join(curDir, x))) 77 # print(dir_list) 78 79 #過濾不需要解析的非法格式檔案 80 fList = [] 81 for file in dir_list: 82 fileSuffix = os.path.splitext(file)[1] 83 if fileSuffix == '.py' or fileSuffix == '.pyc' or fileSuffix == '.swp': 84 print(("跳過非法檔案 :[{cstart}%s{cend}]").format(cstart=PRINTBLUE,cend=PRINTCOLOR_END) % file) 85 else: 86 #print('append %s' % (file)) 87 fList.append(file) 88 89 return fList 90 91 def main(): 92 93 fileList = exploreFiles() 94 fileNum = len(fileList) 95 96 for i in range(fileNum-1): 97 for j in range(i+1, fileNum): 98 fNameOrg= fileList[i] 99 fNameComp = fileList[j] 100 orgPersionList = getPersonList(fNameOrg) 101 compPersionList = getPersonList(fNameComp) 102 #delete ' ' person 103 countOrg = len(orgPersionList) - 1 104 countComp = len(compPersionList) - 1 105 print(("%s:原始人數:[{cstart}%d{cend}] 人!!!").format(cstart=PRINTRED,cend=PRINTCOLOR_END) % (fNameOrg, countOrg)) 106 print(("%s:現有人數:[{cstart}%d{cend}] 人!!!").format(cstart=PRINTGREEN_SIM,cend=PRINTCOLOR_END) % (fNameComp, countComp)) 107 calPersonDiffResult(orgPersionList, compPersionList) 108 109 110 if __name__=="__main__": 111 main()