2022-01-03 09:13:42 +00:00
|
|
|
|
import ujson as json
|
|
|
|
|
|
2022-01-08 14:03:09 +00:00
|
|
|
|
from config import Config
|
2022-01-03 09:13:42 +00:00
|
|
|
|
from core.utils import get_url
|
|
|
|
|
|
|
|
|
|
async def urban(term: str):
|
|
|
|
|
'''查询urban dictionary。
|
|
|
|
|
|
|
|
|
|
:param term: 需要查询的term。
|
|
|
|
|
:returns: 查询结果。'''
|
|
|
|
|
try:
|
|
|
|
|
url = 'http://api.urbandictionary.com/v0/define?term=' + term
|
2022-01-08 14:03:09 +00:00
|
|
|
|
webrender = Config('web_render')
|
|
|
|
|
if webrender:
|
|
|
|
|
url = webrender + 'source?url=' + url
|
2022-01-07 15:01:15 +00:00
|
|
|
|
text = await get_url(url, headers={'accept': '*/*',
|
|
|
|
|
'accept-encoding': 'gzip, deflate',
|
|
|
|
|
'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6',
|
|
|
|
|
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'})
|
2022-01-08 14:03:09 +00:00
|
|
|
|
print(text)
|
2022-01-03 09:13:42 +00:00
|
|
|
|
data = json.loads(text)['list']
|
|
|
|
|
if data == []:
|
|
|
|
|
return '[Urban Dictionary] 没有找到相关结果。'
|
|
|
|
|
else:
|
|
|
|
|
count = data.__len__()
|
|
|
|
|
word = data[0]['word']
|
2022-01-08 14:21:17 +00:00
|
|
|
|
definition = data[0]['definition']
|
2022-01-03 09:13:42 +00:00
|
|
|
|
example = data[0]['example']
|
|
|
|
|
link = data[0]['permalink']
|
2022-01-08 14:21:17 +00:00
|
|
|
|
def_length = definition.split(' ').__len__()
|
|
|
|
|
if def_length > 50:
|
|
|
|
|
definition = ' '.join(definition.split(' ')[0:50]) + '…'
|
|
|
|
|
return f'[Urban Dictionary]({count}个结果):\n{word}\n{definition}\nExample: {example}\n{link}'
|
2022-01-03 09:13:42 +00:00
|
|
|
|
except:
|
|
|
|
|
return '[Urban Dictionary] 查询出错。'
|