forked from Tony15246/uosc_danmaku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
542 lines (495 loc) · 17.5 KB
/
main.lua
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
local msg = require('mp.msg')
local utils = require("mp.utils")
require("options")
require("api")
require('render')
local input_loaded, input = pcall(require, "mp.input")
local uosc_available = false
local menu_items_config = {
bold = { title = "粗体", query = "bold", hint = options.bold },
fontsize = { title = "大小", query = "fontsize", hint = options.fontsize, scope = { min = "0", max = "inf"} },
outline = { title = "描边", query = "outline", hint = options.outline, scope = { min = "0.0", max = "4.0"} },
shadow = { title = "阴影", query = "shadow", hint = options.shadow, scope = { min = "0", max = "inf"} },
transparency = { title = "透明度", query = "transparency", hint = options.transparency, scope = { min = "0", max = "255"} },
displayarea = { title = "弹幕显示范围", query = "displayarea", hint = options.displayarea, scope = { min = "0.0", max = "1.0"} },
}
local footnote_table = {
bold = "true / false",
fontsize = "请输入整数(>=0)",
outline = "输入范围:(0.0-4.0)",
shadow = "请输入整数(>=0)",
transparency = " 输入范围:0(不透明)到255(完全透明)",
displayarea = "显示范围(0.0-1.0)",
}
-- 创建一个包含键顺序的表,这是样式菜单的排布顺序
local ordered_keys = {"bold", "fontsize", "outline", "shadow", "transparency", "displayarea"}
function get_animes(query)
local encoded_query = url_encode(query)
local url = options.api_server .. "/api/v2/search/episodes"
local params = "anime=" .. encoded_query
local full_url = url .. "?" .. params
local items = {}
local message = "加载数据中..."
if uosc_available then
update_menu(menu_item(message), query)
else
show_message(message, 30)
end
msg.verbose("尝试获取番剧数据:" .. full_url)
local res = get_danmaku_contents(full_url)
if res.status ~= 0 then
local message = "获取数据失败"
if uosc_available then
update_menu(menu_item(message), query)
else
show_message(message, 3)
end
msg.error("HTTP 请求失败:" .. res.stderr)
end
local response = utils.parse_json(res.stdout)
if not response or not response.animes then
local message = "无结果"
if uosc_available then
update_menu(menu_item(message), query)
else
show_message(message, 3)
end
msg.verbose("无结果")
return
end
for _, anime in ipairs(response.animes) do
table.insert(items, {
title = anime.animeTitle,
hint = anime.typeDescription,
value = {
"script-message-to",
mp.get_script_name(),
"search-episodes-event",
anime.animeTitle, utils.format_json(anime.episodes),
},
})
end
if uosc_available then
update_menu(items, query)
elseif input_loaded then
show_message("", 0)
mp.add_timeout(0.1, function()
open_menu_select(items)
end)
end
end
function get_episodes(animeTitle, episodes)
local items = {}
for _, episode in ipairs(episodes) do
table.insert(items, {
title = episode.episodeTitle,
value = { "script-message-to", mp.get_script_name(), "load-danmaku", animeTitle, episode.episodeTitle, episode.episodeId },
keep_open = false,
selectable = true,
})
end
local menu_props = {
type = "menu_episodes",
title = "剧集信息",
search_style = "disabled",
items = items,
}
local json_props = utils.format_json(menu_props)
if uosc_available then
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
elseif input_loaded then
mp.add_timeout(0.1, function()
open_menu_select(items)
end)
end
end
function menu_item(input)
local items = {}
table.insert(items, { title = input, value = "" })
return items
end
function update_menu(items, query)
local menu_props = {
type = "menu_anime",
title = "在此处输入番剧名称",
search_style = "palette",
search_debounce = "submit",
on_search = { "script-message-to", mp.get_script_name(), "search-anime-event" },
footnote = "使用enter或ctrl+enter进行搜索",
search_suggestion = query,
items = items,
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
function open_menu_select(menu_items)
local item_titles, item_values = {}, {}
for i, v in ipairs(menu_items) do
item_titles[i] = v.hint and v.title .. " (" .. v.hint .. ")" or v.title
item_values[i] = v.value
end
mp.commandv('script-message-to', 'console', 'disable')
input.select({
prompt = '筛选:',
items = item_titles,
submit = function(id)
mp.commandv(unpack(item_values[id]))
end,
})
end
-- 打开输入菜单
function open_input_menu_get()
mp.commandv('script-message-to', 'console', 'disable')
local title = get_title(true)
input.get({
prompt = '番剧名称:',
default_text = title,
cursor_position = title and #title + 1,
submit = function(text)
input.terminate()
mp.commandv("script-message-to", mp.get_script_name(), "search-anime-event", text)
end
})
end
function open_input_menu_uosc()
local menu_props = {
type = "menu_danmaku",
title = "在此处输入番剧名称",
search_style = "palette",
search_debounce = "submit",
search_suggestion = get_title(true),
on_search = { "script-message-to", mp.get_script_name(), "search-anime-event" },
footnote = "使用enter或ctrl+enter进行搜索",
items = {
{
value = "",
hint = "使用enter或ctrl+enter进行搜索",
keep_open = true,
selectable = false,
align = "center",
},
},
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
function open_add_menu_get()
mp.commandv('script-message-to', 'console', 'disable')
input.get({
prompt = 'Input url:',
submit = function(text)
input.terminate()
mp.commandv("script-message-to", mp.get_script_name(), "add-source-event", text)
end
})
end
function open_add_menu_uosc()
local menu_props = {
type = "menu_source",
title = "在此输入源地址url",
search_style = "palette",
search_debounce = "submit",
on_search = { "script-message-to", mp.get_script_name(), "add-source-event" },
footnote = "使用enter或ctrl+enter进行搜索",
items = {
{
value = "",
hint = "使用enter或ctrl+enter进行搜索",
keep_open = true,
selectable = false,
align = "center",
},
},
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
function open_input_menu()
if uosc_available then
open_input_menu_uosc()
elseif input_loaded then
open_input_menu_get()
end
end
function open_add_menu()
if uosc_available then
open_add_menu_uosc()
elseif input_loaded then
open_add_menu_get()
end
end
-- 设置弹幕样式菜单
function add_danmaku_setup(actived)
local items = {}
for _, key in ipairs(ordered_keys) do
local config = menu_items_config[key]
table.insert(items, {
title = config.title,
hint = "目前:" .. tostring(config.hint),
value = { "script-message-to", mp.get_script_name(), "setup-danmaku-style", config.query },
active = key == actived,
keep_open = true,
selectable = true,
})
end
local menu_props = {
type = "menu_style",
title = "弹幕样式",
footnote = "样式更改仅在本次播放生效",
items = items,
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
-- 更新弹幕样式设置菜单
function updata_danmaku_setup(actived, status)
local items = {}
for _, key in ipairs(ordered_keys) do
local config = menu_items_config[key]
table.insert(items, {
title = config.title,
hint = "目前:" .. tostring(config.hint),
value = { "script-message-to", mp.get_script_name(), "setup-danmaku-style", config.query },
active = key == actived,
keep_open = true,
selectable = true,
})
end
local menu_props = {
type = "menu_style",
title = footnote_table[actived],
search_style = "palette",
search_debounce = "submit",
footnote = footnote_table[actived] or "",
on_search = { "script-message-to", mp.get_script_name(), "update-danmaku-style", actived },
items = items,
}
local actions = "update-menu"
if status and status == "error" then
menu_props.title = "输入非数字字符或范围出错"
-- 创建一个定时器,在2秒后触发回调函数,删除搜索栏错误信息
mp.add_timeout(2.0, function() updata_danmaku_setup(actived) end)
actions = "open-menu"
elseif status and status == "updata" then
menu_props.title = footnote_table[actived]
actions = "open-menu"
end
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", actions, json_props)
end
-- 总集合弹幕菜单
function open_add_total_menu_uosc()
local items = {}
local total_menu_items_config = {
{ title = "弹幕搜索", action = "open_search_danmaku_menu" },
{ title = "从源添加弹幕", action = "open_add_source_menu" },
{ title = "弹幕设置", action = "open_setup_danmaku_menu" },
}
for _, config in ipairs(total_menu_items_config) do
table.insert(items, {
title = config.title,
value = { "script-message-to", mp.get_script_name(), config.action },
keep_open = false,
selectable = true,
})
end
local menu_props = {
type = "menu_total",
title = "弹幕设置",
search_style = "disabled",
items = items,
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
function open_add_total_menu_select()
local item_titles, item_values = {}, {}
local total_menu_items_config = {
{ title = "弹幕搜索", action = "open_search_danmaku_menu" },
{ title = "从源添加弹幕", action = "open_add_source_menu" },
}
for i, config in ipairs(total_menu_items_config) do
item_titles[i] = config.title
item_values[i] = { "script-message-to", mp.get_script_name(), config.action }
end
mp.commandv('script-message-to', 'console', 'disable')
input.select({
prompt = '选择:',
items = item_titles,
submit = function(id)
mp.commandv(unpack(item_values[id]))
end,
})
end
function open_add_total_menu()
if uosc_available then
open_add_total_menu_uosc()
elseif input_loaded then
open_add_total_menu_select()
end
end
mp.commandv(
"script-message-to",
"uosc",
"set-button",
"danmaku",
utils.format_json({
icon = "search",
tooltip = "弹幕搜索",
command = "script-message open_search_danmaku_menu",
})
)
mp.commandv(
"script-message-to",
"uosc",
"set-button",
"danmaku_source",
utils.format_json({
icon = "add_box",
tooltip = "从源添加弹幕",
command = "script-message open_add_source_menu",
})
)
mp.commandv(
"script-message-to",
"uosc",
"set-button",
"danmaku_menu",
utils.format_json({
icon = "grid_view",
tooltip = "弹幕设置",
command = "script-message open_add_total_menu",
})
)
mp.register_script_message('uosc-version', function()
uosc_available = true
end)
-- 注册函数给 uosc 按钮使用
mp.register_script_message("open_search_danmaku_menu", open_input_menu)
mp.register_script_message("search-anime-event", function(query)
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_danmaku")
end
get_animes(query)
end)
mp.register_script_message("search-episodes-event", function(animeTitle, episodes)
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_anime")
end
get_episodes(animeTitle, utils.parse_json(episodes))
end)
-- Register script message to show the input menu
mp.register_script_message("load-danmaku", function(animeTitle, episodeTitle, episodeId)
danmaku.anime = animeTitle
danmaku.episode = episodeTitle:match("(第%d+[话回集]+)") or episodeTitle
set_episode_id(episodeId, true)
end)
mp.register_script_message("open_add_source_menu", open_add_menu)
mp.register_script_message("add-source-event", function(query)
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_source")
end
add_danmaku_source(query, true)
end)
mp.commandv("script-message-to", "uosc", "set", "show_danmaku", "off")
mp.register_script_message("set", function(prop, value)
if prop ~= "show_danmaku" then
return
end
if value == "on" then
if comments == nil then
local path = mp.get_property("path")
if is_protocol(path) and (path:find('bilibili.com') or path:find('bilivideo.c[nom]+')) then
load_danmaku_for_bilibili(path)
return
end
if is_protocol(path) and (path:find('bahamut.akamaized.net')) then
load_danmaku_for_bahamut(path)
return
end
init(path)
else
if danmaku.anime and danmaku.episode then
show_message("加载弹幕:" .. danmaku.anime .. "-" .. danmaku.episode.. "\\N共计" .. #comments .. "条弹幕", 3)
else
show_message("弹幕加载成功,共计" .. #comments .. "条弹幕", 3)
end
show_danmaku_func()
end
else
show_message("关闭弹幕", 2)
hide_danmaku_func()
end
mp.commandv("script-message-to", "uosc", "set", "show_danmaku", value)
end)
mp.register_script_message("show_danmaku_keyboard", function()
if not enabled then
if comments == nil then
local path = mp.get_property("path")
if is_protocol(path) and (path:find('bilibili.com') or path:find('bilivideo.c[nom]+')) then
load_danmaku_for_bilibili(path)
return
end
if is_protocol(path) and (path:find('bahamut.akamaized.net')) then
load_danmaku_for_bahamut(path)
return
end
init(path)
else
if danmaku.anime and danmaku.episode then
show_message("加载弹幕:" .. danmaku.anime .. "-" .. danmaku.episode.. "\\N共计" .. #comments .. "条弹幕", 3)
else
show_message("弹幕加载成功,共计" .. #comments .. "条弹幕", 3)
end
show_danmaku_func()
mp.commandv("script-message-to", "uosc", "set", "show_danmaku", "on")
end
else
show_message("关闭弹幕", 2)
hide_danmaku_func()
mp.commandv("script-message-to", "uosc", "set", "show_danmaku", "off")
end
end)
mp.register_script_message("open_add_total_menu", open_add_total_menu)
mp.register_script_message("open_setup_danmaku_menu", function()
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_total")
end
add_danmaku_setup()
end)
mp.register_script_message("setup-danmaku-style", function(query)
if type(query) == "string" and menu_items_config[query] then
if query == "bold" then
options.bold = options.bold == "true" and "false" or "true"
menu_items_config.bold.hint = options.bold
end
updata_danmaku_setup(query)
else
add_danmaku_setup()
end
end)
mp.register_script_message("update-danmaku-style", function(query, text)
-- mp.commandv("script-message-to", "uosc", "close-menu", "menu_style")
msg.info(text:gsub("%s",""))
local newText, _ = text:gsub("%s", "") -- 移除所有空白字符
if text == nil or text == "" then
return
elseif query == "bold" then
options.bold = options.bold == "true" and "false" or "true"
menu_items_config.bold.hint = options.bold
updata_danmaku_setup(query, "updata")
elseif tonumber(newText) ~= nil then
local num = tonumber(newText)
local status = "error"
local min_num = tonumber(menu_items_config[query]["scope"]["min"]) or 0
local max_num = tonumber(menu_items_config[query]["scope"]["max"]) or math.huge
if num and min_num <= num and num <= max_num then
options[query] = tonumber(text)
status = "updata"
end
menu_items_config[query]["hint"] = options[query]
updata_danmaku_setup(query, status)
else
updata_danmaku_setup(query, "error")
end
end)