xtao-some 新增 wiki 功能 (#4)

Co-authored-by: omg-xtao <100690902+omg-xtao@users.noreply.github.com>
This commit is contained in:
zkysimon 2022-06-10 22:29:18 +08:00 committed by GitHub
parent 3cf21d366e
commit c332bb9bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -2,7 +2,7 @@
"list": [ "list": [
{ {
"name": "xtao-some", "name": "xtao-some",
"version": "1.01", "version": "1.02",
"section": "daily", "section": "daily",
"maintainer": "xtaodada", "maintainer": "xtaodada",
"size": "12.9 kb", "size": "12.9 kb",

View File

@ -1,14 +1,13 @@
import json import json
from pyrogram import Client from pyrogram import Client
from pagermaid.listener import listener from pagermaid.listener import listener
from pagermaid.utils import alias_command, Message, client from pagermaid.utils import Message, client
@listener(is_plugin=True, outgoing=True, command=alias_command("guess"), @listener(is_plugin=True, command="guess",
description="能不能好好说话? - 拼音首字母缩写释义工具(需要回复一句话)") description="能不能好好说话? - 拼音首字母缩写释义工具(需要回复一句话)")
async def guess(c: Client, message: Message): async def guess(_: Client, message: Message):
text = message.arguments text = message.arguments
if not text: if not text:
return await message.edit("请先输入一个缩写。") return await message.edit("请先输入一个缩写。")
@ -32,3 +31,32 @@ async def guess(c: Client, message: Message):
await message.edit("\n\n".join(guess_res)) await message.edit("\n\n".join(guess_res))
else: else:
await message.edit("没有匹配到拼音首字母缩写") await message.edit("没有匹配到拼音首字母缩写")
@listener(is_plugin=True, command="wiki",
description="查询维基百科词条",
parameters="<词组>")
async def wiki(_: Client, message: Message):
message = await message.edit("获取中 . . .")
text = message.arguments
if not text:
return await message.edit("请先输入一个关键词。")
try:
req = await client.get("https://zh.wikipedia.org/w/api.php?action=query&list=search&format=json&formatversion=2&srsearch=" + message)
wiki_json = json.loads(req.content.decode("utf-8"))
except:
return await message.edit("出错了呜呜呜 ~ 无法访问到维基百科。")
try:
if not len(wiki_json['query']['search']) == 0:
wiki_title = wiki_json['query']['search'][0]['title']
wiki_content = wiki_json['query']['search'][0]['snippet'].replace('<span class=\"searchmatch\">',
'**').replace(
'</span>', '**')
wiki_time = wiki_json['query']['search'][0]['timestamp'].replace('T', ' ').replace('Z', ' ')
text = '词条: [' + wiki_title + '](https://zh.wikipedia.org/zh-cn/' + wiki_title + ')\n\n' + \
wiki_content + '...\n\n此词条最后修订于 ' + wiki_time
else:
text = "没有匹配到相关词条"
except KeyError:
text = wiki_json['error']['info']
await message.edit(text)