Skip to content

Commit

Permalink
feat: add new api support
Browse files Browse the repository at this point in the history
Brings support for non-animated danmaku
  • Loading branch information
dyphire committed Jan 26, 2025
1 parent 0ab226f commit 713e551
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 23 deletions.
2 changes: 0 additions & 2 deletions api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ function url_decode(str)
str = str:gsub('%?.+', '')
:gsub('%+', ' ')
return str
else
return
end
end

Expand Down
316 changes: 316 additions & 0 deletions extra.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
local utils = require 'mp.utils'
local msg = require 'mp.msg'

local Source = {
["b 站"] = "bilibili1",
["腾讯"] = "qq",
["爱奇艺"] = "qiyi",
["优酷"] = "youku",
}

local function is_chinese(str)
return string.match(str, "[\228-\233][\128-\191]") ~= nil
end

local function query_tmdb_movies(title)
local encoded_title = url_encode(title)
local url = string.format("https://api.themoviedb.org/3/search/movie?api_key=%s&query=%s&language=zh-CN",
options.tmdb_api_key, encoded_title)

local cmd = {
"curl",
"-s",
"-H", "accept: application/json",
url
}

local res = mp.command_native({
name = "subprocess",
args = cmd,
capture_stdout = true,
capture_stderr = true,
})

if res.status ~= 0 then
local message = "获取 tmdb 中文数据失败"
local menu_type = "menu_anime"
local menu_title = "在此处输入番剧名称"
local footnote = "使用enter或ctrl+enter进行搜索"
local menu_cmd = { "script-message-to", mp.get_script_name(), "search-anime-event" }
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote, menu_cmd, title)
else
show_message(message, 3)
end
msg.error("获取 tmdb 中文数据失败:" .. res.stderr)
end

local data = utils.parse_json(res.stdout)
if data and data.results and #data.results > 0 then
return data.results[1].name
end
end

local function query_tmdb_tv(title)
local encoded_title = url_encode(title)
local url = string.format("https://api.themoviedb.org/3/search/tv?api_key=%s&query=%s&language=zh-CN",
options.tmdb_api_key, encoded_title)

local cmd = {
"curl",
"-s",
"-H", "accept: application/json",
url
}

local res = mp.command_native({
name = "subprocess",
args = cmd,
capture_stdout = true,
capture_stderr = true,
})

if res.status ~= 0 then
local message = "获取 tmdb 中文数据失败"
local menu_type = "menu_anime"
local menu_title = "在此处输入番剧名称"
local footnote = "使用enter或ctrl+enter进行搜索"
local menu_cmd = { "script-message-to", mp.get_script_name(), "search-anime-event" }
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote, menu_cmd, title)
else
show_message(message, 3)
end
msg.error("获取 tmdb 中文数据失败:" .. res.stderr)
end

local data = utils.parse_json(res.stdout)
if data and data.results and #data.results > 0 then
return data.results[1].name
end
end

local function get_episode_number(cat, id, site)
local url = string.format("https://api.web.360kan.com/v1/detail?cat=%s&id=%s&site=%s",
cat, id, site)

local cmd = { "curl", "-s", url }
local res = mp.command_native({
name = "subprocess",
args = cmd,
capture_stdout = true,
capture_stderr = true,
})

if res.status ~= 0 then
msg.error("Failed to fetch data: " .. (res.stderr or "unknown error"))
return nil
end

local result = utils.parse_json(res.stdout)
if result and result.data and result.data.allupinfo then
return tonumber(result.data.allupinfo[site])
end
return nil
end

local function get_details(class, id, site)
local message = "加载数据中..."
local menu_type = "menu_details"
local menu_title = "剧集信息菜单"
local footnote = "使用 / 打开筛选"
update_menu_uosc(menu_type, menu_title, message, footnote)

local cat = 0
if class == "电影" then
cat = 1
elseif class == "电视剧" then
cat = 2
-- elseif class == "综艺" then
-- cat = 3
elseif class == "动漫" then
cat = 4
end

local number = get_episode_number(cat, id, site)
if not number or cat == 0 then
local message = "剧集信息获取失败..."
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote)
else
show_message(message, 3)
end
end

local url = string.format("https://api.web.360kan.com/v1/detail?cat=%s&id=%s&start=1&end=%s&site=%s",
cat, id, number, site)

local cmd = { "curl", "-s", url }
local res = mp.command_native({
name = "subprocess",
args = cmd,
capture_stdout = true,
capture_stderr = true,
})

if res.status ~= 0 then
local message = "剧集信息获取失败..."
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote)
else
show_message(message, 3)
end
msg.error("Failed to fetch data: " .. (res.stderr or "unknown error"))
end

local result = utils.parse_json(res.stdout)
local items = {}
if result and result.data and result.data.allepidetail then
local data = result.data.allepidetail
for _, item in ipairs(data[site]) do
table.insert(items, {
title = "" .. item.playlink_num .. "",
hint = item.playlink_num,
value = {
"script-message-to",
mp.get_script_name(),
"add-extra-event",
item.url,
},
})
end
end
if #items > 0 then
if uosc_available then
update_menu_uosc(menu_type, menu_title, items, footnote)
else
show_message("", 0)
mp.add_timeout(0.1, function()
open_menu_select(items)
end)
end
end
end

local function search_query(query, class)
local url = string.format("https://api.so.360kan.com/index?force_v=1&kw=%s", query)
if class ~= nil then
url = url .. "&type=" .. class
end
local cmd = { "curl", "-s", url }

local res = mp.command_native({
name = "subprocess",
args = cmd,
capture_stdout = true,
capture_stderr = true,
})

local menu_type = "menu_anime"
local menu_title = "在此处输入番剧名称"
local footnote = "使用enter或ctrl+enter进行搜索"
local menu_cmd = { "script-message-to", mp.get_script_name(), "search-anime-event" }

if res.status ~= 0 then
local message = "获取数据失败"
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote, menu_cmd, query)
else
show_message(message, 3)
end
msg.error("HTTP 请求失败:" .. res.stderr)
end

local result = utils.parse_json(res.stdout)
local items = {}
if result and result.data.longData and result.data.longData.rows then
for _, item in ipairs(result.data.longData.rows) do
if item.playlinks then
for source_name, source_id in pairs(Source) do
if item.playlinks[source_id] then
table.insert(items, {
title = item.titleTxt,
hint = item.cat_name .. " | " .. item.year .. " | 来源:" .. source_name,
value = {
"script-message-to",
mp.get_script_name(),
"get-extra-event",
item.cat_name, item.en_id, utils.format_json(item.playlinks), source_id,
},
})
end
end
end
end
end
if #items > 0 then
if uosc_available then
update_menu_uosc(menu_type, menu_title, items, footnote, menu_cmd, query)
else
show_message("", 0)
mp.add_timeout(0.1, function()
open_menu_select(items)
end)
end
end
return nil
end

function query_extra(name, class)
local title = nil
local class = class and class:lower()
local message = "加载数据中..."
local menu_type = "menu_anime"
local menu_title = "在此处输入番剧名称"
local footnote = "使用enter或ctrl+enter进行搜索"
local menu_cmd = { "script-message-to", mp.get_script_name(), "search-anime-event" }
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote, menu_cmd, name)
else
show_message(message, 30)
end

if is_chinese(name) then
search_query(name, class)
return
end

if class == "dy" then
title = query_tmdb_movies(name)
else
title = query_tmdb_tv(name)
end

if title then
search_query(title, class)
end
end

mp.register_script_message("get-extra-event", function(cat, id, playlinks, source_id)
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_anime")
end
if cat == "电影" then
local playlinks = utils.parse_json(playlinks)
if playlinks[source_id]:match("^.-%.html") then
play_url = playlinks[source_id]:match("^(.-%.html).*")
else
play_url = playlinks[source_id]:gsub("%?bsource=360ogvys$","")
end
add_danmaku_source(play_url, true)
else
get_details(cat, id, source_id)
end
end)

mp.register_script_message("add-extra-event", function(query)
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_details")
end
if query:match("^.-%.html") then
play_url = query:match("^(.-%.html).*")
else
play_url = query:gsub("%?bsource=360ogvys$","")
end
add_danmaku_source(play_url, true)
end)
Loading

0 comments on commit 713e551

Please sign in to comment.