61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
# encoding:utf-8
|
|
import asyncio
|
|
import json
|
|
from urllib.error import URLError
|
|
from urllib.request import Request, urlopen
|
|
import requests
|
|
|
|
from lib.service.wechat_token import WechatToken
|
|
|
|
APPID = 'wxf8c3da4e7dfe00a2'
|
|
APP_SECRET = '8c0a1e88a6b43e4be80ed6a597c0b047'
|
|
|
|
|
|
async def refreshToken(appid, app_secret, refresh=False):
|
|
util = WechatToken()
|
|
token = util.get_token(appid)
|
|
if token is not None and not refresh:
|
|
return token
|
|
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (
|
|
appid, app_secret)
|
|
try:
|
|
response = urlopen(url)
|
|
except URLError as e:
|
|
if hasattr(e, 'reason'):
|
|
print('We failed to reach a server.')
|
|
print('Reason: ', e.reason)
|
|
elif hasattr(e, 'code'):
|
|
print('The server couldn\'t fulfill the request.')
|
|
print('Error code: ', e.code)
|
|
return None
|
|
else:
|
|
res = json.loads(response.read().decode('utf-8'))
|
|
util.update_token(appid, res['access_token'], res['expires_in'])
|
|
print(res)
|
|
return res['access_token']
|
|
|
|
|
|
async def msg_sec_check(content):
|
|
token = await refreshToken(APPID, APP_SECRET)
|
|
url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s' % token
|
|
data = {
|
|
'content': content.encode("utf-8").decode("latin1")
|
|
}
|
|
headers = {'content-type': 'application/json'}
|
|
r = requests.post(url, data=json.dumps(data, ensure_ascii=False), headers=headers)
|
|
if r.status_code != 200:
|
|
return {'errcode': 100, 'errmsg': r.status_code}
|
|
rep_data = r.json()
|
|
print(rep_data)
|
|
if rep_data['errcode'] == 87014:
|
|
result = 1
|
|
else:
|
|
result = 0
|
|
return {'errcode': 0, 'risk': result, 'reason': rep_data['errmsg']}
|
|
|
|
|
|
async def main():
|
|
res = await msg_sec_check('特3456书yuuo莞6543李zxcz蒜7782法fgnv级')
|
|
print(res)
|
|
|