2020-08-02 01:46:20 +00:00
|
|
|
import base64
|
|
|
|
import datetime
|
|
|
|
import hashlib
|
|
|
|
import hmac
|
|
|
|
import json
|
|
|
|
import time
|
2020-08-12 16:01:34 +00:00
|
|
|
|
2021-09-12 12:51:13 +00:00
|
|
|
import aiohttp
|
|
|
|
from tenacity import retry, wait_fixed, stop_after_attempt
|
2020-09-19 10:35:13 +00:00
|
|
|
|
2021-03-21 08:14:28 +00:00
|
|
|
from config import Config
|
2020-08-12 16:01:34 +00:00
|
|
|
|
|
|
|
|
2020-08-02 01:46:20 +00:00
|
|
|
def hash_hmac(key, code, sha1):
|
2020-08-27 13:19:40 +00:00
|
|
|
hmac_code = hmac.new(key.encode(), code.encode(), hashlib.sha1)
|
2020-08-02 01:46:20 +00:00
|
|
|
return base64.b64encode(hmac_code.digest()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
def computeMD5hash(my_string):
|
|
|
|
m = hashlib.md5()
|
|
|
|
m.update(my_string.encode('gb2312'))
|
|
|
|
return m.hexdigest()
|
|
|
|
|
2020-08-12 16:01:34 +00:00
|
|
|
|
2021-09-12 12:51:13 +00:00
|
|
|
@retry(stop=stop_after_attempt(3), wait=wait_fixed(3))
|
2021-02-19 11:18:33 +00:00
|
|
|
async def check(*text):
|
2021-05-22 16:50:16 +00:00
|
|
|
accessKeyId = Config("Check_accessKeyId")
|
|
|
|
accessKeySecret = Config("Check_accessKeySecret")
|
|
|
|
if not accessKeyId or not accessKeySecret:
|
|
|
|
return "\n".join(text)
|
2020-08-02 01:46:20 +00:00
|
|
|
body = {
|
|
|
|
"scenes": [
|
|
|
|
"antispam"
|
|
|
|
],
|
|
|
|
"tasks": list(map(lambda x: {
|
2021-03-05 16:21:07 +00:00
|
|
|
"dataId": "Nullcat is god {}".format(time.time()),
|
2020-08-02 01:46:20 +00:00
|
|
|
"content": x
|
2020-09-11 15:03:01 +00:00
|
|
|
}, text))
|
2020-08-02 01:46:20 +00:00
|
|
|
}
|
|
|
|
clientInfo = '{}'
|
|
|
|
root = 'https://green.cn-shanghai.aliyuncs.com'
|
|
|
|
url = '/green/text/scan?{}'.format(clientInfo)
|
|
|
|
|
|
|
|
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
|
|
|
|
date = datetime.datetime.utcnow().strftime(GMT_FORMAT)
|
2021-03-05 16:21:07 +00:00
|
|
|
nonce = 'LittleC is god forever {}'.format(time.time())
|
2020-08-02 01:46:20 +00:00
|
|
|
contentMd5 = base64.b64encode(hashlib.md5(json.dumps(body).encode('utf-8')).digest()).decode('utf-8')
|
|
|
|
headers = {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-MD5': contentMd5,
|
|
|
|
'Date': date,
|
|
|
|
'x-acs-version': '2018-05-09',
|
2020-08-02 12:27:11 +00:00
|
|
|
'x-acs-signature-nonce': nonce,
|
|
|
|
'x-acs-signature-version': '1.0',
|
|
|
|
'x-acs-signature-method': 'HMAC-SHA1'
|
|
|
|
}
|
|
|
|
tmp = {
|
|
|
|
'x-acs-version': '2018-05-09',
|
|
|
|
'x-acs-signature-nonce': nonce,
|
|
|
|
'x-acs-signature-version': '1.0',
|
|
|
|
'x-acs-signature-method': 'HMAC-SHA1'
|
|
|
|
}
|
|
|
|
sorted_header = {k: tmp[k] for k in sorted(tmp)}
|
|
|
|
step1 = '\n'.join(list(map(lambda x: "{}:{}".format(x, sorted_header[x]), list(sorted_header.keys()))))
|
|
|
|
step2 = url
|
|
|
|
step3 = "POST\napplication/json\n{contentMd5}\napplication/json\n{date}\n{step1}\n{step2}".format(
|
|
|
|
contentMd5=contentMd5,
|
|
|
|
date=headers['Date'], step1=step1, step2=step2)
|
2020-08-27 13:19:40 +00:00
|
|
|
sign = "acs {}:{}".format(accessKeyId, hash_hmac(accessKeySecret, step3, hashlib.sha1))
|
2020-08-02 12:27:11 +00:00
|
|
|
headers['Authorization'] = sign
|
|
|
|
# 'Authorization': "acs {}:{}".format(accessKeyId, sign)
|
2021-09-12 12:51:13 +00:00
|
|
|
async with aiohttp.ClientSession(headers=headers) as session:
|
2020-08-02 12:27:11 +00:00
|
|
|
async with session.post('{}{}'.format(root, url), data=json.dumps(body)) as resp:
|
|
|
|
if resp.status == 200:
|
|
|
|
result = await resp.json()
|
|
|
|
resultUsers = []
|
2021-03-18 15:12:14 +00:00
|
|
|
print(result)
|
2020-08-02 12:27:11 +00:00
|
|
|
for item in result['data']:
|
|
|
|
content = item['content']
|
|
|
|
for itemResult in item['results']:
|
|
|
|
if itemResult['suggestion'] == 'block':
|
|
|
|
for itemDetail in itemResult['details']:
|
|
|
|
if 'contexts' in itemDetail:
|
|
|
|
for itemContext in itemDetail["contexts"]:
|
|
|
|
content = content.replace(itemContext['context'], '<吃掉了>')
|
|
|
|
else:
|
|
|
|
content = "<全部吃掉了>"
|
|
|
|
resultUsers.append(content)
|
2021-02-01 15:13:11 +00:00
|
|
|
return ''.join(resultUsers)
|
2020-08-02 12:27:11 +00:00
|
|
|
|
|
|
|
else:
|
2021-02-19 11:18:33 +00:00
|
|
|
return await resp.text()
|