Files
Garfield 93c0413424 🐛 修复目录倒序导致开书落到未购买章
ep_list 最新话在前,startIndex 默认取第一项于是开书总落在锁定章弹「本章未购买」。改为按 ord 升序排、加 toc_version 强制旧缓存重拉;首话 title 为空时回落 short_title。
2026-11-27 08:49:17 +08:00

243 lines
7.8 KiB
Lua

local _ = require("moonsource_bilimanga.l10n")
local SOURCE_ID = "bilimanga"
local function clean(s)
if not s then return "" end
return tostring(s)
end
-- ============================================================
-- Book 映射
-- ============================================================
local function authorOf(info)
local author = info.author_name or info.author
if type(author) == "string" and author ~= "" then return author end
if type(author) ~= "table" then return "未知作者" end
if type(author.name) == "string" and author.name ~= "" then return author.name end
local names = {}
for i = 1, #author do
local item = author[i]
if type(item) == "string" and item ~= "" then
names[#names + 1] = item
elseif type(item) == "table" and type(item.name) == "string" and item.name ~= "" then
names[#names + 1] = item.name
end
end
if #names == 0 then return "未知作者" end
return table.concat(names, " / ")
end
local function plainTitle(info)
local title = info.org_title or info.title or ""
return tostring(title):gsub("<.->", "")
end
local Mapper = {}
function Mapper.book(info)
if not info then return nil end
local comic_id = info.comic_id or info.season_id or info.id
if comic_id == nil or comic_id == "" then return nil end
local cover = info.vertical_cover or info.vcover or info.horizontal_cover or info.square_cover or info.cover or ""
local styles = info.styles
local category = ""
if styles and type(styles) == "table" and #styles > 0 and type(styles[1]) == "string" then
category = table.concat(styles, " · ")
end
local status_str = _("连载")
if info.is_finish == 1 or info.is_finish == true or info.status == 2 then
status_str = _("完结")
end
return {
source_id = SOURCE_ID,
stable_id = tostring(comic_id),
title = clean(plainTitle(info)),
authors = authorOf(info),
cover = cover,
intro = clean(info.desc ~= "" and info.desc or info.evaluate),
category = category,
meta = {
status = status_str,
fans = info.fans or "",
total_eps = info.ep_count or info.total or info.ord_count or 0,
},
}
end
function Mapper.bookFromItem(item)
return Mapper.book(item)
end
-- ============================================================
-- Chapter 映射
-- ============================================================
-- 目录缓存格式。ep_list 是倒序(最新话在前),版本 1 起按 ord 升序;旧缓存一律重拉。
Mapper.TOC_VERSION = 1
function Mapper.chapters(comic)
if not comic then return {} end
local eps = {}
for i, ep in ipairs(comic.ep_list or {}) do eps[i] = ep end
table.sort(eps, function(a, b)
if a.ord ~= b.ord then return (a.ord or 0) < (b.ord or 0) end
return (a.id or 0) < (b.id or 0)
end)
local chapters = {}
for i, ep in ipairs(eps) do
local title = clean(ep.title):match("^%s*(.-)%s*$")
if title == "" then title = clean(ep.short_title):match("^%s*(.-)%s*$") end
chapters[#chapters + 1] = {
idx = i,
uid = tostring(ep.id),
title = title ~= "" and title or ("第" .. i .. "话"),
vip = ep.is_locked == true,
}
end
if chapters[1] then chapters[1].toc_version = Mapper.TOC_VERSION end
return chapters
end
-- ============================================================
-- Image 映射
-- ============================================================
function Mapper.images(wire)
if not wire then return nil end
local images = wire.images
if not images or type(images) ~= "table" or #images == 0 then
-- 尝试 image_list
images = wire.image_list
if not images or type(images) ~= "table" or #images == 0 then
return nil
end
end
local urls = {}
for _, img in ipairs(images) do
if type(img) == "table" and type(img.url) == "string" and img.url ~= "" then
urls[#urls + 1] = img.url
end
end
return #urls > 0 and urls or nil
end
-- ============================================================
-- Search 映射
-- ============================================================
function Mapper.rows(wire)
if type(wire) ~= "table" then return nil end
if wire[1] ~= nil or next(wire) == nil then return wire end
if type(wire.list) == "table" then return wire.list end
local data = wire.data
if type(data) ~= "table" then return nil end
if type(data.list) == "table" then return data.list end
if data[1] ~= nil or next(data) == nil then return data end
return nil
end
local function choices(list)
if type(list) ~= "table" then return nil end
local out = {}
for _idx, item in ipairs(list) do
local id = type(item) == "table" and tonumber(item.id)
local name = type(item) == "table" and item.name
if not id or id ~= math.floor(id) or type(name) ~= "string" or name == "" then return nil end
out[#out + 1] = { id = id, name = name }
end
return #out > 0 and out or nil
end
function Mapper.labels(wire)
local data = type(wire) == "table" and wire.data or nil
if type(data) ~= "table" then return nil end
local styles, orders = choices(data.styles), choices(data.orders)
if not styles or not orders then return nil end
return { styles = styles, orders = orders }
end
function Mapper.storeChoice(node, labels)
local order, style = tostring(node or ""):match("^(-?%d+):(-?%d+)$")
order, style = tonumber(order), tonumber(style)
local function has(list, id)
for _idx, item in ipairs(list) do
if item.id == id then return true end
end
end
if not has(labels.orders, order) then order = labels.orders[1].id end
if style ~= -1 and not has(labels.styles, style) then style = -1 end
return order, style
end
function Mapper.storeGroups(labels, order, style)
local Browse = require("source.browse")
local node = order .. ":" .. style
local orders, styles = {}, { { id = order .. ":-1", title = _("全部") } }
for _idx, item in ipairs(labels.orders) do
orders[#orders + 1] = { id = item.id .. ":" .. style, title = item.name }
end
for _idx, item in ipairs(labels.styles) do
styles[#styles + 1] = { id = order .. ":" .. item.id, title = item.name }
end
local sort_group = Browse.group(orders, node)
local style_group = Browse.group(styles, node)
sort_group.title = _("排序")
style_group.title = _("题材")
return { sort_group, style_group }
end
function Mapper.books(wire)
local rows = Mapper.rows(wire)
if not rows then return nil end
local books = {}
for i = 1, #rows do
local book = Mapper.book(rows[i])
if book then books[#books + 1] = book end
end
if #rows > 0 and #books == 0 then return nil end
return books
end
function Mapper.searchResults(wire)
local items = Mapper.rows(wire) or {}
local books = {}
for i = 1, #items do
local book = Mapper.book(items[i])
if book then books[#books + 1] = book end
end
return books
end
-- ============================================================
-- Shelf 映射
-- ============================================================
function Mapper.favoriteItems(wire)
return Mapper.rows(wire)
end
-- ============================================================
-- Progress 映射
-- ============================================================
function Mapper.historyItem(wire, comic_id)
local list = Mapper.rows(wire)
if not list then return nil end
for _, item in ipairs(list) do
if tostring(item.comic_id or item.season_id) == tostring(comic_id) then
return item
end
end
return nil
end
return Mapper