import requests
import base64
import itchat
from itchat.content import *
def orc_look(filePath):
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=tSeFPh0pvmWbtEY5SbidzNWT&client_secret=q9YwDFUT4y85bglQPLkoFvA0boZBX5C5'
access_token = ""
response = requests.get(host)
if response:
access_token = response.json()["access_token"]
with open(filePath, 'rb') as f:
image_data = f.read()
base64_ima = base64.b64encode(image_data)
data = {
'image': base64_ima
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + str(access_token)
r = requests.post(url, params=headers, data=data).json()
list = []
res = []
for word in r['words_result']:
list.append(word['words'])
if "16计算机科学与技术-4班未打卡学生名单" in list:
for i in list:
length = len(i)
if length <= 3 and length >= 2:
if not '1' in i:
res.append(i)
res.pop(0)
return res
@itchat.msg_register([TEXT, PICTURE], isGroupChat=True)
def getPicture(msg):
print(msg['User']['NickName'])
if msg['User']['NickName'] == "互联网16级大四副班长群":
if msg['Type'] == "Picture":
filePath = "/home/retemin/images" + msg['FileName']
msg.download(filePath)
list = orc_look(filePath)
if len(list)>0:
for i in list:
Nickname = itchat.search_friends(remarkName=i)
if Nickname:
UserName = Nickname[0]["UserName"]
itchat.send_msg("记得打卡",toUserName=UserName)
if __name__ == '__main__':
itchat.auto_login(enableCmdQR=2,hotReload=True)
itchat.run()