-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwiki_request_1.x.py
103 lines (94 loc) · 3.69 KB
/
wiki_request_1.x.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
import re
import traceback
import requests
from mcdreforged.api.all import *
PLUGIN_METADATA = {
'id': 'wiki_request',
'version': '2.0.0',
'name': 'Wiki Request Plugin',
'description': 'A plugin to look up the Minecraft Wiki',
'author': 'DrLee_lihr',
'link': 'https://github.com/DrLee-lihr/WikiRequest',
'dependencies': {'mcdreforged': '>=1.0.0'}
}
languages_list = ['zh:', 'en:', 'cs:', 'de:', 'el:', 'es:', 'fr:', 'hu:', 'it:',
'ja:', 'ko:', 'nl:', 'pl:', 'pt:', 'ru:', 'th:', 'tr:', 'uk:']
@new_thread
def wiki_request(source: CommandSource, context: dict):
name: str = context["page_name"]
link = "https://minecraft.fandom.com/"
language = 'zh'
temp2 = True
for temp1 in languages_list:
if name.startswith(temp1):
language = name[:2]
name = name[3:]
temp2 = False
link = link + language + "/api.php?action=query&prop=info|extracts&inprop=url&redirects" + \
"&exsentences=1&format=json&titles=" + name
try:
print("requesting " + link + ' for ' + name)
r = requests.get(link)
print("request finished,status:" + str(r.status_code))
if r.status_code == 200:
r.encoding = r.apparent_encoding
result = json.loads(r.text)
page_info = result["query"]["pages"]
try:
temp = page_info["-1"]
except IndexError:
page_number = str(page_info)[2:]
num = 0
for i in page_number:
try:
temp = int(i)
except:
break
else:
num = num + 1
page_number = page_number[:num]
link = page_info[str(page_number)]["fullurl"]
real_title = page_info[str(page_number)]["title"]
extract = page_info[str(page_number)]["extract"]
extract = re.sub(r"<.*?>", "", extract)
if real_title != name:
source.reply(RTextList(
RText("您要的", RColor.gray),
RText(name, RColor.dark_gray),
RText("(重定向到%s)" % real_title, RColor.white),
RText(":", RColor.gray),
"\n",
RText(link, RColor.blue).c(action=RAction.open_url, value=link),
"\n",
RText(extract, RColor.white)
))
else:
source.reply(RTextList(
RText("您要的", RColor.gray),
RText(real_title, RColor.dark_gray),
RText(":", RColor.gray),
"\n",
RText(link, RColor.blue).c(action=RAction.open_url, value=link),
"\n",
RText(extract, RColor.white)
))
else:
source.reply(RText("找不到条目。", RColor.red))
else:
source.reply(RTextList(
RText("发生错误:", RColor.red),
RText("请求出错。", RColor.white)
))
except:
except_info = traceback.format_exc()
source.reply(RTextList(
RText("发生错误:", RColor.red),
RText(except_info, RColor.white)
))
command = Literal("!!wiki").then(
GreedyText("page_name").runs(wiki_request)
)
def on_load(server: ServerInterface, info: Info):
server.register_command(command)
server.register_help_message("!!wiki <pagename>", "查询Minecraft Wiki上的页面。")