-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBilibiliParser.py
224 lines (209 loc) · 14.3 KB
/
BilibiliParser.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import re
import threading
import time
import requests
from ruamel import yaml
from utils import rtext
cp = update_message = ""
VERSION = 3
helpmessage = '''\u00A79=============== \u00A7a[Bilibili Parser]\u00A79 ===============
\u00A7b{cp}blbl video <链接/视频id>\u00A76 : 解析哔哩哔哩视频信息
\u00A7b{cp}blbl checkupdate\u00A76 : 检查更新
\u00A79================================================'''.replace("\n", "\\n")
# 更新检查线程
class updateDetection(threading.Thread):
def __init__(self, server, player):
threading.Thread.__init__(self)
self.server = server
self.name = "Bilibili Parser Update Detection Thread"
self.player = player
def run(self):
global update_message
if self.player != "":
self.server.execute("tellraw " + self.player + " {\"text\":\"开始检查更新\", \"color\":\"yellow\"}")
else:
self.server.logger.info("开始检查更新")
try:
# 走github获取版本号
result = requests.get("http://raw.githubusercontent.com/dogdie233/BilibiliParser/master/version.json")
if result.status_code == 200:
resultJson = result.json()
if resultJson["latestVer"] > VERSION:
update_message = "BilibiliParser 插件有新版本 " + resultJson["verName"]
if self.player != "":
self.server.execute("tellraw " + self.player + " {\"text\":\"" + update_message + "\", \"color\":\"green\"}")
else:
self.server.logger.info("BilibiliParser 有新版本" + resultJson["verName"])
else:
if self.player != "":
self.server.execute("tellraw " + self.player + " {\"text\":\"当前已是最新版本\", \"color\":\"green\"}")
else:
self.server.logger.info("当前已是最新版本")
else:
a = 1 / 0 # 抛出异常走coding获取版本号
except:
if self.player != "":
self.server.execute("tellraw " + self.player + " {\"text\":\"无法连接到 github 服务器,正在连接到 coding\", \"color\":\"red\"}")
else:
self.server.logger.warning("无法连接到 github 服务器,正在连接到 coding")
try:
# 走coding获取版本号
result = requests.get("http://dogdieself.coding.net/p/bilibiliparser/d/bilibiliparser/git/raw/master/version.json")
if result.status_code == 200:
resultJson = result.json()
if resultJson["latestVer"] > VERSION:
update_message = "BilibiliParser 插件有新版本 " + resultJson["verName"]
if self.player != "":
self.server.execute("tellraw " + self.player + " {\"text\":\"" + update_message + "\", \"color\":\"green\"}")
else:
self.server.logger.info("BilibiliParser 有新版本" + resultJson["verName"])
else:
if self.player != "":
self.server.execute("tellraw " + self.player + " {\"text\":\"当前已是最新版本\", \"color\":\"green\"}")
else:
self.server.logger.info("当前已是最新版本")
else:
if self.player != "":
self.server.execute("tellraw " + self.player + " {\"text\":\"无法连接到服务器,检测更新失败\", \"color\":\"red\"}")
else:
self.server.logger.error("无法连接到服务器,检测更新失败")
except:
if self.player != "":
self.server.execute("tellraw " + self.player + " {\"text\":\"无法连接到服务器,检测更新失败\", \"color\":\"red\"}")
else:
self.server.logger.error("无法连接到服务器,检测更新失败")
def on_load(server, old_module):
global cp, helpmessage
with open ("config.yml", "r", encoding="utf8") as f:
cp = yaml.safe_load(f)["console_command_prefix"]
server.add_help_message(cp + "blbl help", "查看 BilibiliParser 插件帮助")
helpmessage = helpmessage.replace("{cp}", cp) # 替换帮助字符中的命令前缀
updateDetectionThread = updateDetection(server, "")
updateDetectionThread.setDaemon(True)
updateDetection.start(updateDetectionThread)
def on_player_joined(server, player):
if update_message != "" and server.get_permission_level(player) >= 3:
server.execute("tellraw " + player + " {\"text\":\"" + update_message + "\",\"color\":\"green\"}")
def on_info(server, info):
if info.is_user:
if info.content.startswith(cp):
if info.content.startswith(cp + "blbl"):
args = info.content.split(" ")
del (args[0])
on_command(server, info.player, args)
else:
vid = get_video_id(info.content)
if vid is not None:
message_checked = rtext.RText("检测到您发送的信息是bilibili视频链接,是否要解析视频内容?", color=rtext.RColor.green)
message_yes = rtext.RText("[是]", color=rtext.RColor.aqua, styles=[rtext.RStyle.bold])
message_yes.set_click_event(rtext.RAction.run_command, cp + "blbl video " + info.content)
message_yes.set_hover_text("点击执行命令" + cp + "blbl video " + info.content)
server.reply(info, rtext.RTextList(message_checked, rtext.RText(" "), message_yes))
def on_command (server, player, args):
if len(args) == 0:
server.execute("tellraw " + player + " {\"text\":\"参数错误,请使用 " + cp + "blbl help 获取帮助\", \"color\":\"red\"}")
return
if args[0].lower() == "video":
# 检查命令参数
if len(args) != 2:
server.execute("tellraw " + player + " {\"text\":\"参数错误,请使用 " + cp + "blbl help 获取帮助\", \"color\":\"red\"}")
return
# 获取视频id
vid = get_video_id(args[1])
if vid is None:
server.execute("tellraw " + player + " {\"text\":\"这不是bilibili视频id或链接\", \"color\":\"red\"}")
return
if "bv" in vid.lower():
response = requests.get("https://api.bilibili.com/x/web-interface/view?byid=" + vid)
else:
response = requests.get("https://api.bilibili.com/x/web-interface/view?aid=" + vid)
# 判断是否成功获取
if response.status_code != 200:
server.execute("tellraw " + player + " {\"text\":\"网络连接错误,请稍后重试,错误码: " + str(response.status_code) + "\", \"color\":\"red\"}")
return
result = response.json()
if result["code"] == 0:
# 获取的code为0冇表示问题
message = rtext.RText("========视频信息获取成功========\n", color=rtext.RColor.green, styles=[rtext.RStyle.bold])
# av bv号和打开链接
message_avtitle = rtext.RText("AV号: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold])
message_av = rtext.RText("av" + str(result["data"]["aid"]), color=rtext.RColor.green)
message_avlink = rtext.RText("[打开链接]", color=rtext.RColor.aqua)
message_avlink.set_click_event(rtext.RAction.open_url, "http://www.bilibili.com/video/av" + str(result["data"]["aid"]))
message_avlink.set_hover_text("打开链接: http://www.bilibili.com/video/av" + str(result["data"]["aid"]))
message_bvtitle = rtext.RText("\nBV号: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold])
message_bv = rtext.RText(result["data"]["bvid"], color=rtext.RColor.green)
message_bvlink = rtext.RText("[打开链接]", color=rtext.RColor.aqua)
message_bvlink.set_click_event(rtext.RAction.open_url, "http://www.bilibili.com/video/" + str(result["data"]["bvid"]))
message_bvlink.set_hover_text("打开链接: http://www.bilibili.com/video/" + str(result["data"]["bvid"]))
message = rtext.RTextList(message, message_avtitle, message_av, message_avlink, message_bvtitle, message_bv, message_bvlink, rtext.RText("\n"))
# 标题信息
message = rtext.RTextList(message, rtext.RText("标题: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(result["data"]["title"] + "\n", color=rtext.RColor.green))
# 封面信息
message = rtext.RTextList(message, rtext.RText("封面: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message_pic = rtext.RText("[查看图片]" + "\n", color=rtext.RColor.aqua)
message_pic.set_click_event(rtext.RAction.open_url, result["data"]["pic"])
message_pic.set_hover_text("打开链接: " + result["data"]["pic"])
message = rtext.RTextList(message, message_pic)
# 发布日期
message = rtext.RTextList(message, rtext.RText("发布日期: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(result["data"]["pubdate"])) + "\n", color=rtext.RColor.green))
# 简介
result["data"]["desc"] = result["data"]["desc"].replace("\n", " \n")
if len(result["data"]["desc"]) <= 60:
message_desc = rtext.RText(result["data"]["desc"] + "\n", color=rtext.RColor.green)
else:
message_desc = rtext.RText(result["data"]["desc"][0:59] + "……", color=rtext.RColor.green)
message_show_full_desc = rtext.RText("[查看完整简介]\n", color=rtext.RColor.aqua)
message_show_full_desc.set_hover_text(result["data"]["desc"])
message_desc = rtext.RTextList(message_desc, message_show_full_desc)
message = rtext.RTextList(message, rtext.RText("简介: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]), message_desc)
# 播放量
message = rtext.RTextList(message, rtext.RText("播放量: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(str(result["data"]["stat"]["view"]) + "\n", color=rtext.RColor.green))
# 弹幕总量
message = rtext.RTextList(message, rtext.RText("弹幕总量: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(str(result["data"]["stat"]["danmaku"]) + "\n", color=rtext.RColor.green))
# 点赞数量
message = rtext.RTextList(message, rtext.RText("点赞数量: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(str(result["data"]["stat"]["like"]) + "\n", color=rtext.RColor.green))
# 收获硬币
message = rtext.RTextList(message, rtext.RText("收获硬币: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(str(result["data"]["stat"]["coin"]) + "\n", color=rtext.RColor.green))
# 收藏数量
message = rtext.RTextList(message, rtext.RText("收藏数量: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(str(result["data"]["stat"]["favorite"]) + "\n", color=rtext.RColor.green))
# 转发数量
message = rtext.RTextList(message, rtext.RText("转发数量: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(str(result["data"]["stat"]["share"]) + "\n", color=rtext.RColor.green))
# 评论数量
message = rtext.RTextList(message, rtext.RText("评论数量: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(str(result["data"]["stat"]["reply"]) + "\n", color=rtext.RColor.green))
# 分part数量
message = rtext.RTextList(message, rtext.RText("分part数量: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(str(len(result["data"]["pages"])) + "\n", color=rtext.RColor.green))
# up主
message = rtext.RTextList(message, rtext.RText("投稿人: ", color=rtext.RColor.blue, styles=[rtext.RStyle.bold]))
message = rtext.RTextList(message, rtext.RText(result["data"]["owner"]["name"], color=rtext.RColor.green))
message_owner_link = rtext.RText("[打开个人主页]", color=rtext.RColor.aqua)
message_owner_link.set_click_event(rtext.RAction.open_url, "http://space.bilibili.com/" + str(result["data"]["owner"]["mid"]))
message_owner_link.set_hover_text("打开链接: http://space.bilibili.com/" + str(result["data"]["owner"]["mid"]))
message = rtext.RTextList(message, message_owner_link)
server.say(message)
else:
server.execute("tellraw " + player + " {\"text\":\"Error发生,错误信息: " + result["message"].replace("\"", "\\\"") + "\", \"color\":\"red\"}")
elif args[0].lower() == "checkupdate":
updateDetectionThread = updateDetection(server, player)
updateDetectionThread.setDaemon(True)
updateDetection.start(updateDetectionThread)
elif args[0].lower() == "help":
server.execute("tellraw " + player + " {\"text\":\"" + helpmessage.replace("\"", "\\\"") + "\"}")
else:
server.execute("tellraw " + player + " {\"text\":\"参数错误,请使用 " + cp + "blbl help 获取帮助\", \"color\":\"red\"}")
def get_video_id(url):
match = re.match(r"(?:https?://)?(?:(?:www\.bilibili\.(?:(?:com)|(?:tv))/video/)|(?:b23\.tv/))?(?:(?:av(\d+))|(bv[0-9A-Za-z]+))$", url, re.I)
if match is None:
return None
else:
return match.group(2) if match.group(1) is None else match.group(1)