158 lines
5.7 KiB
Lua
158 lines
5.7 KiB
Lua
--[[--
|
|
书架换源:书的身份(stable_id)、进度、阅读时间都不动,只把读取来源指向另一个书源。
|
|
搜索在后台逐个书源进行,进度和结果由 status() 读取;同时只跑一个任务,网页离开后回来仍能接上。
|
|
|
|
@module moonsource_legadorule.switch
|
|
--]]
|
|
|
|
local Sources = require("moonsource_legadorule.sources")
|
|
local Mapper = require("moonsource_legadorule.mapper")
|
|
|
|
local SOURCE_ID = "legadorule"
|
|
local PARALLEL = 6
|
|
|
|
local Switch = {}
|
|
|
|
local job = { running = false, total = 0, done = 0, results = {} }
|
|
|
|
--- 书当前实际读取的书源 key(换过源则是新书源)。
|
|
function Switch.keyOf(stable_id)
|
|
local key = Sources.alias(stable_id)
|
|
if key then return key end
|
|
return (Mapper.splitStableId(stable_id))
|
|
end
|
|
|
|
--- 书架里本书源的书;来源已停用或不存在的排在前面。
|
|
function Switch.books()
|
|
local alive = {}
|
|
for _idx, entry in ipairs(Sources.enabled()) do alive[entry.key] = true end
|
|
local out = {}
|
|
for _idx, book in ipairs(require("book.books_db").listLibrary()) do
|
|
if book.source_id == SOURCE_ID then
|
|
local key = Switch.keyOf(book.stable_id)
|
|
local entry = key and Sources.find(key)
|
|
out[#out + 1] = {
|
|
stable_id = book.stable_id,
|
|
title = book.title,
|
|
authors = book.authors or "",
|
|
source_name = entry and entry.name or key,
|
|
broken = not alive[key or ""],
|
|
switched = Sources.alias(book.stable_id) ~= nil,
|
|
}
|
|
end
|
|
end
|
|
table.sort(out, function(a, b)
|
|
if a.broken ~= b.broken then return a.broken end
|
|
return a.title < b.title
|
|
end)
|
|
return out
|
|
end
|
|
|
|
local function errMessage(err) return require("moonsource_legadorule.unsupported").message(err) end
|
|
|
|
--- 其他书源里同名的排前面,作者也一致的更靠前。
|
|
local function rank(rows, book)
|
|
local function score(r)
|
|
if r.title ~= book.title then return 0 end
|
|
if book.authors ~= "" and r.authors == book.authors then return 2 end
|
|
return 1
|
|
end
|
|
for _idx, r in ipairs(rows) do r.score = score(r) end
|
|
table.sort(rows, function(a, b)
|
|
if a.score ~= b.score then return a.score > b.score end
|
|
return false
|
|
end)
|
|
return rows
|
|
end
|
|
|
|
--- 在其他启用的书源里后台搜同名书;已有任务在跑则返回 false。
|
|
---@param book table { stable_id, title, authors }
|
|
function Switch.start(book)
|
|
if job.running then return false end
|
|
local current = Switch.keyOf(book.stable_id)
|
|
local entries = {}
|
|
for _idx, entry in ipairs(Sources.enabled()) do
|
|
if entry.key ~= current then entries[#entries + 1] = entry end
|
|
end
|
|
local state = { running = #entries > 0, stable_id = book.stable_id, title = book.title,
|
|
total = #entries, done = 0, results = {}, current = {} }
|
|
job = state
|
|
local nextIdx, active = 0, 0
|
|
local handles = {}
|
|
state.cancel = function()
|
|
state.running = false
|
|
for _idx, h in ipairs(handles) do pcall(h.cancel) end
|
|
end
|
|
|
|
local function settle(slot, name, rows)
|
|
if not state.running or slot.settled then return end
|
|
slot.settled = true
|
|
active = active - 1
|
|
state.done = state.done + 1
|
|
for _idx, r in ipairs(rows or {}) do state.results[#state.results + 1] = r end
|
|
rank(state.results, book)
|
|
if state.done >= state.total then state.running = false end
|
|
state.launch()
|
|
end
|
|
|
|
function state.launch()
|
|
while state.running and active < PARALLEL and nextIdx < #entries do
|
|
nextIdx = nextIdx + 1
|
|
local entry = entries[nextIdx]
|
|
local slot = {}
|
|
active = active + 1
|
|
local made, parser = pcall(function()
|
|
return require("moonsource_legadorule.parser").new(Sources.rule(entry))
|
|
end)
|
|
if not made then
|
|
settle(slot, entry.name, nil)
|
|
else
|
|
local ran, handle = pcall(parser.search, parser, book.title, 1, function(books)
|
|
local rows = {}
|
|
for _idx, b in ipairs(type(books) == "table" and books or {}) do
|
|
rows[#rows + 1] = Mapper.book(SOURCE_ID, entry.key, b, entry.name)
|
|
end
|
|
settle(slot, entry.name, rows)
|
|
end)
|
|
if ran then
|
|
handles[#handles + 1] = handle
|
|
else
|
|
settle(slot, entry.name, nil)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
state.launch()
|
|
return true
|
|
end
|
|
|
|
function Switch.cancel()
|
|
if job.running and job.cancel then job.cancel() end
|
|
end
|
|
|
|
--- 进度与目前已找到的结果(排好序,最多 100 条)。
|
|
function Switch.status()
|
|
local rows = {}
|
|
for i, r in ipairs(job.results) do
|
|
if i > 100 then break end
|
|
rows[i] = { stable_id = r.stable_id, title = r.title, authors = r.authors, cover = r.cover,
|
|
source_name = r.source_name, score = r.score }
|
|
end
|
|
return { running = job.running, stable_id = job.stable_id, title = job.title,
|
|
total = job.total, done = job.done, results = rows }
|
|
end
|
|
|
|
--- 把 stable_id 这本书改读 picked(另一书源里的书的 stable_id)。
|
|
---@return boolean ok, string|nil err
|
|
function Switch.apply(stable_id, picked)
|
|
local key, url = Mapper.splitStableId(picked or "")
|
|
if not key or not url then return false, "目标书无效" end
|
|
if not Sources.find(key) then return false, "目标书源不存在" end
|
|
Sources.setAlias(stable_id, key, url)
|
|
-- 目录改用新书源:旧缓存作废,下次打开重新拉取
|
|
pcall(function() require("source.toc").drop(SOURCE_ID, stable_id) end)
|
|
return true
|
|
end
|
|
|
|
return Switch
|