Compare commits
7
Commits
v0.0.1
...
82fce6005c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
82fce6005c | ||
|
|
93c0413424 | ||
|
|
5efe13834e | ||
|
|
875dc70861 | ||
|
|
4a031e00c5 | ||
|
|
455891353c | ||
|
|
430962a5d0 |
No files matched your search
@@ -1,5 +1,14 @@
|
|||||||
# 更新日志
|
# 更新日志
|
||||||
|
|
||||||
|
## 0.3.0
|
||||||
|
|
||||||
|
- 🐛 修复目录倒序导致开书落到未购买章
|
||||||
|
- :lipstick: (setting): 账号页使用书源自己的图标
|
||||||
|
- 🐛 修复书架不同步、章节打不开与登录状态误判
|
||||||
|
- 🐛 修复书源加载失败与描述格式
|
||||||
|
- ✨ 迁移到现行 Web 协议:签名请求、浏览器登录与分类书城
|
||||||
|
- 🔥 去掉书源自己的在线更新
|
||||||
|
|
||||||
## 0.0.1
|
## 0.0.1
|
||||||
|
|
||||||
- 「哔哩哔哩漫画」书源拆成独立仓库,独立发版、独立更新;原来所有书源同版本一起更新的做法取消。
|
- 「哔哩哔哩漫画」书源拆成独立仓库,独立发版、独立更新;原来所有书源同版本一起更新的做法取消。
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
# 哔哩哔哩漫画
|
||||||
|
|
||||||
|
| 能力 | 是否支持 | 备注 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 浏览器登录 | 已实现,未实测 | 打开漫画账号页采集适用于该站的 Cookie,再用主站 nav 校验后才保存 |
|
||||||
|
| 粘贴 Cookie | 已实现,未实测 | 须是完整 Cookie;校验通过才写入。旧的未校验 SESSDATA 仍可继续用,直到重新登录 |
|
||||||
|
| 账号切换 | 部分支持 | 同账号续期保留本地记录;换号按月读账号边界清理未同步数据 |
|
||||||
|
| 书架拉取 | 支持 | 需要已登录会话;已用真实账号拉取收藏并写入书库 |
|
||||||
|
| 书架新增与移出 | 已实现,未实测 | 收藏接口已接,未做写后回读 |
|
||||||
|
| 搜索 | 支持 | 公开搜索 |
|
||||||
|
| 分类书城 | 部分支持 | 题材和排序来自站点分类接口。公开分类页已取到 15 本;筛选翻页已核对,设备上的书城页未点过 |
|
||||||
|
| 详情与目录 | 支持 | 目录顺序与话 ID 来自详情 |
|
||||||
|
| 免费话 | 部分支持 | 站点会按页加密少数页(免费话也有),这些页解不了,用提示页占位,其余页照常阅读 |
|
||||||
|
| 已购与大会员话 | 部分支持 | 只认登录后详情里的 is_locked。未锁定才下载,锁定报本章未购买。未用真实大会员会话实测 |
|
||||||
|
| 阅读进度同步 | 已实现,未实测 | 需要已登录会话,按话记录 |
|
||||||
|
| 书评、章评、段评 | 不支持 | 当前版本未接 |
|
||||||
|
| 离线阅读 | 支持 | 已缓存的话可离线打开 |
|
||||||
@@ -1,9 +1,5 @@
|
|||||||
local PLUGIN_DIR = debug.getinfo(1, "S").source:match("^@(.*)/[^/]*$") or "."
|
local _ = require("gettext")
|
||||||
if not package.path:find(PLUGIN_DIR .. "/?.lua", 1, true) then
|
|
||||||
package.path = PLUGIN_DIR .. "/?.lua;" .. package.path
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fullname = "哔哩哔哩漫画书源",
|
fullname = _("月读书源:哔哩哔哩漫画"),
|
||||||
description = "moonsource-bilimanga",
|
description = _("给月读增加「哔哩哔哩漫画」书源。需要同时安装月读,与平台官方无关。"),
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,6 @@
|
|||||||
local PLUGIN_DIR = debug.getinfo(1, "S").source:match("^@(.*)/[^/]*$") or "."
|
--[[--
|
||||||
if not package.path:find(PLUGIN_DIR .. "/?.lua", 1, true) then
|
KOReader 只认带 main.lua 的插件目录;书源本身不需要任何插件钩子,月读读取旁边的 moon_source.lua。
|
||||||
package.path = PLUGIN_DIR .. "/?.lua;" .. package.path
|
--]]
|
||||||
end
|
|
||||||
|
|
||||||
local _ = require("moonsource_bilimanga.l10n")
|
|
||||||
|
|
||||||
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||||
|
|
||||||
return WidgetContainer:extend{
|
return WidgetContainer:extend{
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ return {
|
|||||||
provides = {
|
provides = {
|
||||||
source = true,
|
source = true,
|
||||||
},
|
},
|
||||||
meta = function() return require("moonsource_bilimanga.loadmeta") end,
|
meta = function() return require("moonsource_bilimanga.source").meta() end,
|
||||||
new = function() return require("moonsource_bilimanga.source").new() end,
|
new = function() return require("moonsource_bilimanga.source").new() end,
|
||||||
setting = lazy("moonsource_bilimanga.setting"),
|
setting = lazy("moonsource_bilimanga.setting"),
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ return {
|
|||||||
description = "哔哩哔哩漫画书源",
|
description = "哔哩哔哩漫画书源",
|
||||||
abilities = {
|
abilities = {
|
||||||
search = true,
|
search = true,
|
||||||
browse = false,
|
browse = true,
|
||||||
sync = true,
|
sync = true,
|
||||||
progress = true,
|
progress = true,
|
||||||
cover = true,
|
cover = true,
|
||||||
|
|||||||
@@ -2,11 +2,24 @@ local Cbz = require("source.cbz")
|
|||||||
local Image = require("utils.image")
|
local Image = require("utils.image")
|
||||||
local Paths = require("utils.paths")
|
local Paths = require("utils.paths")
|
||||||
local Client = require("moonsource_bilimanga.client")
|
local Client = require("moonsource_bilimanga.client")
|
||||||
local Mapper = require("moonsource_bilimanga.mapper")
|
|
||||||
local _ = require("moonsource_bilimanga.l10n")
|
local _ = require("moonsource_bilimanga.l10n")
|
||||||
|
|
||||||
local Chapter = {}
|
local Chapter = {}
|
||||||
|
|
||||||
|
-- 站点按页加密(首字节 0x08,免费章也有),解不了;这页换成提示图,其余页照常成章。
|
||||||
|
local ENCRYPTED_PAGE = debug.getinfo(1, "S").source:sub(2):match("^(.*)/") .. "/encrypted.png"
|
||||||
|
|
||||||
|
local function copyFile(from, to)
|
||||||
|
local src = assert(io.open(from, "rb"))
|
||||||
|
local data = src:read("*a")
|
||||||
|
src:close()
|
||||||
|
local dst = io.open(to, "wb")
|
||||||
|
if not dst then return false end
|
||||||
|
dst:write(data)
|
||||||
|
dst:close()
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
function Chapter.archivePath(identity, chapter)
|
function Chapter.archivePath(identity, chapter)
|
||||||
return Paths.bookWorkDir(identity.stable_id, identity.source_id)
|
return Paths.bookWorkDir(identity.stable_id, identity.source_id)
|
||||||
.. "/" .. chapter.uid .. ".cbz"
|
.. "/" .. chapter.uid .. ".cbz"
|
||||||
@@ -29,24 +42,64 @@ function Chapter.materializeAsync(identity, chapter, on_progress, cb)
|
|||||||
return { cancel = function() state.cancelled = true end }
|
return { cancel = function() state.cancelled = true end }
|
||||||
end
|
end
|
||||||
|
|
||||||
local function log(msg)
|
if chapter.vip then
|
||||||
pcall(require("logger").getLogger, "bilimanga")
|
require("ui/uimanager"):nextTick(function()
|
||||||
|
if not state.cancelled then cb(nil, _("本章未购买")) end
|
||||||
|
end)
|
||||||
|
return { cancel = function() state.cancelled = true end }
|
||||||
end
|
end
|
||||||
|
|
||||||
Client.imageIndexAsync(chapter.uid, function(wire, err)
|
local function withCode(url)
|
||||||
|
if url:find("code=DanmakuInfo", 1, true) then return url end
|
||||||
|
if url:find("?", 1, true) then return url .. "&code=DanmakuInfo" end
|
||||||
|
return url .. "?code=DanmakuInfo"
|
||||||
|
end
|
||||||
|
|
||||||
|
state.active = Client.imageIndexAsync(chapter.uid, function(wire, err)
|
||||||
|
if state.cancelled then return end
|
||||||
|
state.active = nil
|
||||||
if err then cb(nil, err); return end
|
if err then cb(nil, err); return end
|
||||||
local urls = Mapper.images(wire)
|
local paths = {}
|
||||||
if not urls or #urls == 0 then
|
local images = wire and wire.images or {}
|
||||||
|
for i = 1, #images do
|
||||||
|
local path = images[i] and images[i].path
|
||||||
|
if type(path) == "string" and path ~= "" then
|
||||||
|
paths[#paths + 1] = path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if #paths == 0 then
|
||||||
cb(nil, _("章节图片解析失败"))
|
cb(nil, _("章节图片解析失败"))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
state.active = Client.imageTokenAsync(paths, function(items, token_err)
|
||||||
|
if state.cancelled then return end
|
||||||
|
state.active = nil
|
||||||
|
if token_err then cb(nil, token_err); return end
|
||||||
|
if type(items) ~= "table" or #items ~= #paths then
|
||||||
|
cb(nil, _("章节图片解析失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local urls = {}
|
||||||
|
for i = 1, #items do
|
||||||
|
local item = items[i]
|
||||||
|
if type(item) ~= "table" then
|
||||||
|
cb(nil, _("章节图片解析失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if type(item.complete_url) ~= "string" or item.complete_url == "" then
|
||||||
|
cb(nil, _("章节图片解析失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
urls[i] = withCode(item.complete_url)
|
||||||
|
end
|
||||||
|
|
||||||
local image_dir = archive_path:gsub("%.cbz$", "")
|
local image_dir = archive_path:gsub("%.cbz$", "")
|
||||||
Paths.ensureDir(image_dir)
|
Paths.ensureDir(image_dir)
|
||||||
|
|
||||||
local paths = {}
|
local files = {}
|
||||||
for i, url in ipairs(urls) do
|
for i, url in ipairs(urls) do
|
||||||
paths[i] = image_dir .. "/" .. string.format("%04d%s", i, Image.suffix(url) or ".jpg")
|
files[i] = image_dir .. "/" .. string.format("%04d%s", i, Image.suffix(url) or ".jpg")
|
||||||
end
|
end
|
||||||
|
|
||||||
local current = 1
|
local current = 1
|
||||||
@@ -54,16 +107,14 @@ function Chapter.materializeAsync(identity, chapter, on_progress, cb)
|
|||||||
if state.cancelled then return end
|
if state.cancelled then return end
|
||||||
local url = urls[current]
|
local url = urls[current]
|
||||||
if not url then
|
if not url then
|
||||||
Cbz.build(paths, archive_path, state, function(path, cbz_err)
|
Cbz.build(files, archive_path, state, function(path, cbz_err)
|
||||||
if state.cancelled then return end
|
if state.cancelled then return end
|
||||||
if cbz_err then log("Cbz.build error: " .. tostring(cbz_err)) end
|
if path then cb(path) else cb(nil, cbz_err or _("图片下载失败")) end
|
||||||
cb(path and { title = chapter.title, file = path } or nil,
|
|
||||||
cbz_err or (state.cancelled and nil or _("图片下载失败")))
|
|
||||||
end)
|
end)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local path = paths[current]
|
local path = files[current]
|
||||||
if Image.ok(path) then
|
if Image.ok(path) then
|
||||||
if on_progress then on_progress(current, #urls) end
|
if on_progress then on_progress(current, #urls) end
|
||||||
current = current + 1
|
current = current + 1
|
||||||
@@ -83,7 +134,17 @@ function Chapter.materializeAsync(identity, chapter, on_progress, cb)
|
|||||||
downloaded = true
|
downloaded = true
|
||||||
state.active = nil
|
state.active = nil
|
||||||
if state.cancelled then return end
|
if state.cancelled then return end
|
||||||
if not ok or not Image.ok(path) then
|
local fh = io.open(path, "rb")
|
||||||
|
local magic = fh and fh:read(1)
|
||||||
|
if fh then fh:close() end
|
||||||
|
if magic == "\8" then
|
||||||
|
os.remove(path)
|
||||||
|
files[current] = path:gsub("%.[^./]+$", "") .. ".png"
|
||||||
|
if not copyFile(ENCRYPTED_PAGE, files[current]) then
|
||||||
|
cb(nil, _("图片下载失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
elseif not ok or not Image.ok(path) then
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
cb(nil, download_err and tostring(download_err) or _("图片下载失败"))
|
cb(nil, download_err and tostring(download_err) or _("图片下载失败"))
|
||||||
return
|
return
|
||||||
@@ -96,6 +157,7 @@ function Chapter.materializeAsync(identity, chapter, on_progress, cb)
|
|||||||
end
|
end
|
||||||
nextImage()
|
nextImage()
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cancel = function()
|
cancel = function()
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
local Request = require("http.request")
|
local Request = require("http.request")
|
||||||
local JSON = require("json")
|
local JSON = require("json")
|
||||||
|
local Crypto = require("moonsource_bilimanga.crypto")
|
||||||
local _ = require("moonsource_bilimanga.l10n")
|
local _ = require("moonsource_bilimanga.l10n")
|
||||||
|
|
||||||
local BASE = "https://manga.bilibili.com"
|
local BASE = "https://manga.bilibili.com"
|
||||||
local NOV = "27"
|
local NOV = "27"
|
||||||
|
local QUERY = "device=pc&platform=web&nov=27&a=810"
|
||||||
|
local DATA_SN = "1E74C20E5720FBF3BB351965D7A9DFC1"
|
||||||
|
local session_buvid3, session_buvid4
|
||||||
|
local apiCode
|
||||||
|
|
||||||
local function getCookie()
|
local function getCookie()
|
||||||
local cfg = require("utils.settings").getSource("bilimanga")
|
local cfg = require("utils.settings").getSource("bilimanga")
|
||||||
@@ -30,7 +35,7 @@ local function makeHeaders(extra)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function twirpAsync(service, method, body, cb)
|
local function twirpAsync(service, method, body, cb)
|
||||||
local url = BASE .. "/twirp/" .. service .. "." .. method .. "?nov=" .. NOV
|
local url = BASE .. "/twirp/" .. service .. "/" .. method .. "?" .. QUERY
|
||||||
local state = { cancelled = false }
|
local state = { cancelled = false }
|
||||||
|
|
||||||
local function finish(data, err)
|
local function finish(data, err)
|
||||||
@@ -57,8 +62,10 @@ local function twirpAsync(service, method, body, cb)
|
|||||||
if not Request.ok(code) then
|
if not Request.ok(code) then
|
||||||
if code == 401 or code == 403 then
|
if code == 401 or code == 403 then
|
||||||
local settings = require("utils.settings")
|
local settings = require("utils.settings")
|
||||||
settings.saveSource("bilimanga", { cookie = "" })
|
local cfg = settings.getSource("bilimanga")
|
||||||
cb(nil, _("登录已过期,请重新输入 Cookie"))
|
cfg.cookie, cfg.account_id = nil, nil
|
||||||
|
settings.saveSource("bilimanga")
|
||||||
|
finish(nil, require("source.auth").expire("bilimanga", _("登录已过期,请重新登录")))
|
||||||
else
|
else
|
||||||
finish(nil, _("请求失败"))
|
finish(nil, _("请求失败"))
|
||||||
end
|
end
|
||||||
@@ -70,6 +77,11 @@ local function twirpAsync(service, method, body, cb)
|
|||||||
finish(nil, _("响应无效"))
|
finish(nil, _("响应无效"))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
local bad = apiCode(data)
|
||||||
|
if bad then
|
||||||
|
finish(nil, bad)
|
||||||
|
return
|
||||||
|
end
|
||||||
finish(data)
|
finish(data)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -93,51 +105,214 @@ local function twirpAsyncSimple(service, method, body, cb)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function apiCode(wire)
|
||||||
|
local code = wire and wire.code
|
||||||
|
if code == nil or tonumber(code) == 0 then return nil end
|
||||||
|
if type(wire.msg) == "string" and wire.msg ~= "" then return wire.msg end
|
||||||
|
return _("请求失败")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function cookieValue(cookie, key)
|
||||||
|
if not cookie or cookie == "" then return nil end
|
||||||
|
return cookie:match(key .. "=([^;]*)")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function nowMs()
|
||||||
|
local ok, socket = pcall(require, "socket")
|
||||||
|
if ok and socket and socket.gettime then
|
||||||
|
return math.floor(socket.gettime() * 1000)
|
||||||
|
end
|
||||||
|
return os.time() * 1000
|
||||||
|
end
|
||||||
|
|
||||||
|
local function ensureBuvid(cookie, state, cb)
|
||||||
|
local from_cookie = cookieValue(cookie, "buvid3")
|
||||||
|
if from_cookie and from_cookie ~= "" then
|
||||||
|
session_buvid3 = from_cookie
|
||||||
|
session_buvid4 = cookieValue(cookie, "buvid4") or session_buvid4
|
||||||
|
cb(session_buvid3, session_buvid4)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if session_buvid3 then
|
||||||
|
cb(session_buvid3, session_buvid4)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
state.job = Request.request({
|
||||||
|
url = "https://api.bilibili.com/x/frontend/finger/spi",
|
||||||
|
method = "GET",
|
||||||
|
headers = {
|
||||||
|
["User-Agent"] = "Mozilla/5.0",
|
||||||
|
["Accept"] = "application/json",
|
||||||
|
},
|
||||||
|
timeout = require("http.timeout").net(1),
|
||||||
|
}, function(res, err)
|
||||||
|
if state.cancelled then return end
|
||||||
|
state.job = nil
|
||||||
|
if err or not res or not Request.ok(tonumber(res.code)) then
|
||||||
|
cb(nil, nil, _("请求失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local ok, wire = pcall(JSON.decode, res.body)
|
||||||
|
local data = ok and wire and wire.data
|
||||||
|
if type(data) ~= "table" or type(data.b_3) ~= "string" or data.b_3 == "" then
|
||||||
|
cb(nil, nil, _("响应无效"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
session_buvid3 = data.b_3
|
||||||
|
session_buvid4 = data.b_4
|
||||||
|
cb(session_buvid3, session_buvid4)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function signedAsync(service, method, body, decrypt_id, cb)
|
||||||
|
local state = { cancelled = false, job = nil }
|
||||||
|
local cookie = getCookie()
|
||||||
|
|
||||||
|
local function finish(data, err)
|
||||||
|
if state.cancelled then return end
|
||||||
|
state.job = nil
|
||||||
|
cb(data, err)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function post(b3, b4)
|
||||||
|
if state.cancelled then return end
|
||||||
|
local body_str = JSON.encode(body or {})
|
||||||
|
local sign = Crypto.sign(QUERY, body_str, nowMs())
|
||||||
|
local headers = makeHeaders()
|
||||||
|
headers["x-bili-data-sn"] = DATA_SN
|
||||||
|
if not cookieValue(cookie, "buvid3") then
|
||||||
|
local extra = "buvid3=" .. b3
|
||||||
|
if type(b4) == "string" and b4 ~= "" then
|
||||||
|
extra = extra .. "; buvid4=" .. b4
|
||||||
|
end
|
||||||
|
if cookie and cookie ~= "" then
|
||||||
|
headers.Cookie = cookie .. "; " .. extra
|
||||||
|
else
|
||||||
|
headers.Cookie = extra
|
||||||
|
end
|
||||||
|
end
|
||||||
|
state.job = Request.request({
|
||||||
|
url = BASE .. "/twirp/" .. service .. "/" .. method
|
||||||
|
.. "?" .. QUERY .. "&ultra_sign=" .. Crypto.urlencode(sign),
|
||||||
|
method = "POST",
|
||||||
|
body = body_str,
|
||||||
|
headers = headers,
|
||||||
|
timeout = require("http.timeout").net(1),
|
||||||
|
}, function(res, err)
|
||||||
|
if state.cancelled then return end
|
||||||
|
state.job = nil
|
||||||
|
if err or not res then
|
||||||
|
finish(nil, err and tostring(err) or _("请求失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not Request.ok(tonumber(res.code)) then
|
||||||
|
finish(nil, _("请求失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local ok, wire = pcall(JSON.decode, res.body)
|
||||||
|
if not ok or type(wire) ~= "table" then
|
||||||
|
finish(nil, _("响应无效"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local bad = apiCode(wire)
|
||||||
|
if bad then
|
||||||
|
finish(nil, bad)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if type(wire.bytesData) == "string" and wire.bytesData ~= "" then
|
||||||
|
if decrypt_id == nil then
|
||||||
|
finish(nil, _("响应无效"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local plain = Crypto.open(wire.bytesData, b3, decrypt_id)
|
||||||
|
if type(plain) ~= "string" then
|
||||||
|
finish(nil, _("响应无效"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local decoded, obj = pcall(JSON.decode, plain)
|
||||||
|
if not decoded or type(obj) ~= "table" then
|
||||||
|
finish(nil, _("响应无效"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
finish(obj)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
finish(wire.data or wire)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
ensureBuvid(cookie, state, function(b3, b4, err)
|
||||||
|
if err or not b3 then
|
||||||
|
finish(nil, err or _("请求失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
post(b3, b4)
|
||||||
|
end)
|
||||||
|
|
||||||
|
return {
|
||||||
|
cancel = function()
|
||||||
|
state.cancelled = true
|
||||||
|
if state.job and state.job.cancel then state.job.cancel() end
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
-- ============================================================
|
-- ============================================================
|
||||||
-- Comic 服务
|
-- Comic 服务
|
||||||
-- ============================================================
|
-- ============================================================
|
||||||
|
|
||||||
|
local Client = {}
|
||||||
|
|
||||||
function Client.searchAsync(keyword, page, page_size, cb)
|
function Client.searchAsync(keyword, page, page_size, cb)
|
||||||
return twirpAsync("comic.v1.Comic", "Search", {
|
return signedAsync("comic.v1.Comic", "Search", {
|
||||||
keyword = keyword,
|
keyWord = keyword,
|
||||||
page = page or 1,
|
pageNum = page or 1,
|
||||||
page_size = page_size or 20,
|
pageSize = page_size or 20,
|
||||||
}, function(wire, err)
|
}, nil, function(wire, err)
|
||||||
if not wire then cb(nil, err); return end
|
if not wire then cb(nil, err); return end
|
||||||
cb(wire)
|
cb(wire)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Client.comicDetailAsync(comic_id, cb)
|
function Client.comicDetailAsync(comic_id, cb)
|
||||||
return twirpAsync("comic.v1.Comic", "ComicDetail", {
|
local id = tonumber(comic_id)
|
||||||
comic_id = tonumber(comic_id),
|
return signedAsync("comic.v1.Comic", "ComicDetail", {
|
||||||
}, function(wire, err)
|
comic_id = id,
|
||||||
|
}, tostring(id), function(wire, err)
|
||||||
if not wire then cb(nil, err); return end
|
if not wire then cb(nil, err); return end
|
||||||
cb(wire.comic)
|
cb(wire.comic or wire)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Client.comicDetailLiteAsync(comic_id, cb)
|
function Client.comicDetailLiteAsync(comic_id, cb)
|
||||||
return twirpAsync("comic.v1.Comic", "ComicDetailLite", {
|
return Client.comicDetailAsync(comic_id, cb)
|
||||||
comic_id = tonumber(comic_id),
|
|
||||||
}, function(wire, err)
|
|
||||||
if not wire then cb(nil, err); return end
|
|
||||||
cb(wire.comic)
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Client.imageIndexAsync(ep_id, cb)
|
function Client.imageIndexAsync(ep_id, cb)
|
||||||
return twirpAsync("comic.v1.Comic", "GetImageIndex", {
|
local id = tonumber(ep_id)
|
||||||
ep_id = tonumber(ep_id),
|
return signedAsync("comic.v1.Comic", "GetImageIndex", {
|
||||||
mode = 2,
|
ep_id = id,
|
||||||
quality = 5,
|
}, tostring(id), function(wire, err)
|
||||||
type = 1,
|
|
||||||
}, function(wire, err)
|
|
||||||
if not wire then cb(nil, err); return end
|
if not wire then cb(nil, err); return end
|
||||||
cb(wire)
|
cb(wire)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- 站点用这个公钥决定要不要把原图包成密文。没有配对私钥,解不了 0x08 那种页。
|
||||||
|
local M1 = "BB0d+pwvfKzW0hUAiBNsnSLRFVnZ0S8pxSYtuF9kScmWvdWuxvmXDikDKTG5M/0zeTR7konA9ImBpO1FqccgvwI="
|
||||||
|
|
||||||
|
function Client.imageTokenAsync(paths, cb)
|
||||||
|
local parts = {}
|
||||||
|
for i = 1, #paths do
|
||||||
|
parts[i] = '"' .. tostring(paths[i]):gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
|
||||||
|
end
|
||||||
|
local urls = "[" .. table.concat(parts, ",") .. "]"
|
||||||
|
return signedAsync("comic.v1.Comic", "ImageToken", {
|
||||||
|
urls = urls,
|
||||||
|
m1 = M1,
|
||||||
|
}, urls, cb)
|
||||||
|
end
|
||||||
|
|
||||||
function Client.homeHotAsync(cb)
|
function Client.homeHotAsync(cb)
|
||||||
return twirpAsync("comic.v1.Comic", "HomeHot", {
|
return twirpAsync("comic.v1.Comic", "HomeHot", {
|
||||||
tab = 0,
|
tab = 0,
|
||||||
@@ -150,16 +325,26 @@ function Client.homeHotAsync(cb)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Client.classPageAsync(cb)
|
function Client.allLabelAsync(cb)
|
||||||
return twirpAsync("comic.v1.Comic", "ClassPage", {
|
return twirpAsync("comic.v1.Comic", "AllLabel", {}, function(wire, err)
|
||||||
page = 1,
|
|
||||||
page_size = 20,
|
|
||||||
}, function(wire, err)
|
|
||||||
if not wire then cb(nil, err); return end
|
if not wire then cb(nil, err); return end
|
||||||
cb(wire)
|
cb(wire)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Client.classPageAsync(choice, page, cb)
|
||||||
|
return signedAsync("comic.v1.Comic", "ClassPage", {
|
||||||
|
style_id = choice.style_id,
|
||||||
|
area_id = -1,
|
||||||
|
is_finish = -1,
|
||||||
|
order = choice.order,
|
||||||
|
special_tag = 0,
|
||||||
|
page_num = page,
|
||||||
|
page_size = 15,
|
||||||
|
is_free = -1,
|
||||||
|
}, tostring(choice.style_id), cb)
|
||||||
|
end
|
||||||
|
|
||||||
function Client.recommendAsync(comic_id, cb)
|
function Client.recommendAsync(comic_id, cb)
|
||||||
return twirpAsync("comic.v1.Comic", "Recommend", {
|
return twirpAsync("comic.v1.Comic", "Recommend", {
|
||||||
comic_id = tonumber(comic_id),
|
comic_id = tonumber(comic_id),
|
||||||
@@ -177,8 +362,23 @@ function Client.listFavoriteAsync(page_num, page_size, cb)
|
|||||||
return twirpAsync("bookshelf.v1.Bookshelf", "ListFavorite", {
|
return twirpAsync("bookshelf.v1.Bookshelf", "ListFavorite", {
|
||||||
page_num = page_num or 1,
|
page_num = page_num or 1,
|
||||||
page_size = page_size or 20,
|
page_size = page_size or 20,
|
||||||
|
order = 0,
|
||||||
|
wait_free = 0,
|
||||||
|
time_limit_free = 0,
|
||||||
|
type = 0,
|
||||||
from = "web",
|
from = "web",
|
||||||
source = "web",
|
source = "web",
|
||||||
|
filter = {
|
||||||
|
reading_progress = -1,
|
||||||
|
is_finish = -1,
|
||||||
|
area = -1,
|
||||||
|
comic_type = -1,
|
||||||
|
wait_free = -1,
|
||||||
|
style = -1,
|
||||||
|
out_label = -1,
|
||||||
|
out_label_type = 0,
|
||||||
|
},
|
||||||
|
show_update_group = false,
|
||||||
}, function(wire, err)
|
}, function(wire, err)
|
||||||
if not wire then cb(nil, err); return end
|
if not wire then cb(nil, err); return end
|
||||||
cb(wire)
|
cb(wire)
|
||||||
@@ -259,6 +459,48 @@ end
|
|||||||
-- 公共工具
|
-- 公共工具
|
||||||
-- ============================================================
|
-- ============================================================
|
||||||
|
|
||||||
|
function Client.loginCheckAsync(cookie, cb)
|
||||||
|
if type(cookie) ~= "string" or cookie == "" then
|
||||||
|
cb(false, _("没有读到 Cookie"))
|
||||||
|
return { cancel = function() end }
|
||||||
|
end
|
||||||
|
local state = { cancelled = false }
|
||||||
|
state.job = Request.request({
|
||||||
|
url = "https://api.bilibili.com/x/web-interface/nav",
|
||||||
|
method = "GET",
|
||||||
|
headers = {
|
||||||
|
["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||||
|
["Accept"] = "application/json",
|
||||||
|
["Referer"] = BASE .. "/",
|
||||||
|
Cookie = cookie,
|
||||||
|
},
|
||||||
|
timeout = require("http.timeout").net(1),
|
||||||
|
}, function(res, err)
|
||||||
|
if state.cancelled then return end
|
||||||
|
state.job = nil
|
||||||
|
if err or not res or not Request.ok(tonumber(res.code)) then
|
||||||
|
cb(false, _("请求失败"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local ok, wire = pcall(JSON.decode, res.body)
|
||||||
|
local data = ok and type(wire) == "table" and wire.data
|
||||||
|
local mid = type(data) == "table" and data.mid
|
||||||
|
local id = type(mid) == "number" and tostring(math.floor(mid)) or mid
|
||||||
|
if type(data) == "table" and data.isLogin == true and tonumber(wire.code) == 0
|
||||||
|
and type(id) == "string" and id:match("^%d+$") then
|
||||||
|
cb(true, nil, { id = id })
|
||||||
|
return
|
||||||
|
end
|
||||||
|
cb(false, _("登录校验失败,请确认已经登录哔哩哔哩"))
|
||||||
|
end)
|
||||||
|
return {
|
||||||
|
cancel = function()
|
||||||
|
state.cancelled = true
|
||||||
|
if state.job and state.job.cancel then state.job.cancel() end
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
function Client.hasAccount()
|
function Client.hasAccount()
|
||||||
local cfg = require("utils.settings").getSource("bilimanga")
|
local cfg = require("utils.settings").getSource("bilimanga")
|
||||||
return cfg.cookie ~= nil and cfg.cookie ~= ""
|
return cfg.cookie ~= nil and cfg.cookie ~= ""
|
||||||
@@ -266,12 +508,13 @@ end
|
|||||||
|
|
||||||
function Client.setCookie(cookie)
|
function Client.setCookie(cookie)
|
||||||
local cfg = require("utils.settings").getSource("bilimanga")
|
local cfg = require("utils.settings").getSource("bilimanga")
|
||||||
cfg.cookie = cookie or ""
|
cfg.cookie = (type(cookie) == "string" and cookie ~= "") and cookie or nil
|
||||||
require("utils.settings").saveSource("bilimanga", cfg)
|
cfg.account_id = nil
|
||||||
|
require("utils.settings").saveSource("bilimanga")
|
||||||
end
|
end
|
||||||
|
|
||||||
function Client.clearCookie()
|
function Client.clearCookie()
|
||||||
Client.setCookie("")
|
Client.setCookie(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 分页遍历
|
-- 分页遍历
|
||||||
|
|||||||
@@ -0,0 +1,404 @@
|
|||||||
|
local bit = require("bit")
|
||||||
|
local band, bor, bxor = bit.band, bit.bor, bit.bxor
|
||||||
|
local lshift, rshift = bit.lshift, bit.rshift
|
||||||
|
|
||||||
|
local Crypto = {}
|
||||||
|
|
||||||
|
local ALPHA62 = "br5NH9CxpvJzmEt3OqoF7jgWMIZ2UnASfc8K6XaLYylT1eVBGh4sdQkDwRP0ui"
|
||||||
|
local B64 = {
|
||||||
|
"UalVqQk8T7+5oJxC6MzIniWDrc3XERGN/wdLyp90mf2tHuvsgeSjZbBF1KAOPhY4",
|
||||||
|
"zI3XZb8T7+5opF1KJxUaBqQk90mf6MlVCEceSjAOPhY42RGN/wdLytHuvsgniWDr",
|
||||||
|
}
|
||||||
|
|
||||||
|
local function u8(x)
|
||||||
|
return band(x, 0xff)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function xtime(x)
|
||||||
|
x = u8(x)
|
||||||
|
local hi = band(x, 0x80)
|
||||||
|
local y = u8(lshift(x, 1))
|
||||||
|
if hi ~= 0 then y = bxor(y, 0x1b) end
|
||||||
|
return y
|
||||||
|
end
|
||||||
|
|
||||||
|
local function mul(a, b)
|
||||||
|
local p = 0
|
||||||
|
a, b = u8(a), u8(b)
|
||||||
|
for _ = 1, 8 do
|
||||||
|
if band(b, 1) ~= 0 then p = bxor(p, a) end
|
||||||
|
local hi = band(a, 0x80)
|
||||||
|
a = u8(lshift(a, 1))
|
||||||
|
if hi ~= 0 then a = bxor(a, 0x1b) end
|
||||||
|
b = rshift(b, 1)
|
||||||
|
end
|
||||||
|
return p
|
||||||
|
end
|
||||||
|
|
||||||
|
local function rotl8(x, n)
|
||||||
|
x = u8(x)
|
||||||
|
return u8(bor(lshift(x, n), rshift(x, 8 - n)))
|
||||||
|
end
|
||||||
|
|
||||||
|
local SBOX, INV = {}, {}
|
||||||
|
for x = 0, 255 do
|
||||||
|
local inv = 0
|
||||||
|
if x ~= 0 then
|
||||||
|
for i = 1, 255 do
|
||||||
|
if mul(x, i) == 1 then inv = i; break end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local y = bxor(inv, rotl8(inv, 1), rotl8(inv, 2), rotl8(inv, 3), rotl8(inv, 4), 0x63)
|
||||||
|
SBOX[x] = y
|
||||||
|
INV[y] = x
|
||||||
|
end
|
||||||
|
|
||||||
|
local function expand192(key)
|
||||||
|
local words = {}
|
||||||
|
for i = 0, 5 do
|
||||||
|
words[i] = {
|
||||||
|
key:byte(i * 4 + 1),
|
||||||
|
key:byte(i * 4 + 2),
|
||||||
|
key:byte(i * 4 + 3),
|
||||||
|
key:byte(i * 4 + 4),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
local rcon = 0x01
|
||||||
|
for i = 6, 51 do
|
||||||
|
local prev = words[i - 1]
|
||||||
|
local t1, t2, t3, t4 = prev[1], prev[2], prev[3], prev[4]
|
||||||
|
if i % 6 == 0 then
|
||||||
|
t1, t2, t3, t4 = SBOX[t2], SBOX[t3], SBOX[t4], SBOX[t1]
|
||||||
|
t1 = bxor(t1, rcon)
|
||||||
|
rcon = xtime(rcon)
|
||||||
|
end
|
||||||
|
local old = words[i - 6]
|
||||||
|
words[i] = { bxor(old[1], t1), bxor(old[2], t2), bxor(old[3], t3), bxor(old[4], t4) }
|
||||||
|
end
|
||||||
|
return words
|
||||||
|
end
|
||||||
|
|
||||||
|
local function addRound(s, words, round)
|
||||||
|
for c = 0, 3 do
|
||||||
|
local w = words[round * 4 + c]
|
||||||
|
for r = 0, 3 do
|
||||||
|
local i = c * 4 + r
|
||||||
|
s[i] = bxor(s[i], w[r + 1])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function mod4(x)
|
||||||
|
return (x % 4 + 4) % 4
|
||||||
|
end
|
||||||
|
|
||||||
|
local function invShift(s)
|
||||||
|
local t = {}
|
||||||
|
for i = 0, 15 do t[i] = s[i] end
|
||||||
|
for r = 1, 3 do
|
||||||
|
for c = 0, 3 do
|
||||||
|
s[c * 4 + r] = t[mod4(c - r) * 4 + r]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function invMix(s)
|
||||||
|
for c = 0, 3 do
|
||||||
|
local o = c * 4
|
||||||
|
local a, b, d, e = s[o], s[o + 1], s[o + 2], s[o + 3]
|
||||||
|
s[o] = bxor(mul(a, 14), mul(b, 11), mul(d, 13), mul(e, 9))
|
||||||
|
s[o + 1] = bxor(mul(a, 9), mul(b, 14), mul(d, 11), mul(e, 13))
|
||||||
|
s[o + 2] = bxor(mul(a, 13), mul(b, 9), mul(d, 14), mul(e, 11))
|
||||||
|
s[o + 3] = bxor(mul(a, 11), mul(b, 13), mul(d, 9), mul(e, 14))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function decryptBlock(block, words)
|
||||||
|
local s = {}
|
||||||
|
for i = 0, 15 do s[i] = block:byte(i + 1) end
|
||||||
|
addRound(s, words, 12)
|
||||||
|
for round = 11, 1, -1 do
|
||||||
|
invShift(s)
|
||||||
|
for i = 0, 15 do s[i] = INV[s[i]] end
|
||||||
|
addRound(s, words, round)
|
||||||
|
invMix(s)
|
||||||
|
end
|
||||||
|
invShift(s)
|
||||||
|
for i = 0, 15 do s[i] = INV[s[i]] end
|
||||||
|
addRound(s, words, 0)
|
||||||
|
local out = {}
|
||||||
|
for i = 0, 15 do out[i + 1] = string.char(s[i]) end
|
||||||
|
return table.concat(out)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function b64dec(s)
|
||||||
|
local map = {}
|
||||||
|
local alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
|
for i = 1, #alpha do map[alpha:sub(i, i)] = i - 1 end
|
||||||
|
s = s:gsub("%s", ""):gsub("=", "")
|
||||||
|
local out, buf, n = {}, 0, 0
|
||||||
|
for i = 1, #s do
|
||||||
|
local v = map[s:sub(i, i)]
|
||||||
|
if v then
|
||||||
|
buf = buf * 64 + v
|
||||||
|
n = n + 1
|
||||||
|
if n == 4 then
|
||||||
|
out[#out + 1] = string.char(
|
||||||
|
math.floor(buf / 65536) % 256,
|
||||||
|
math.floor(buf / 256) % 256,
|
||||||
|
buf % 256)
|
||||||
|
buf, n = 0, 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if n == 2 then
|
||||||
|
buf = buf * 64 * 64
|
||||||
|
out[#out + 1] = string.char(math.floor(buf / 65536) % 256)
|
||||||
|
elseif n == 3 then
|
||||||
|
buf = buf * 64
|
||||||
|
out[#out + 1] = string.char(math.floor(buf / 65536) % 256, math.floor(buf / 256) % 256)
|
||||||
|
end
|
||||||
|
return table.concat(out)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Crypto.open(bytesData, buvid3, id)
|
||||||
|
local even = {}
|
||||||
|
for i = 1, #buvid3, 2 do
|
||||||
|
even[#even + 1] = buvid3:sub(i, i)
|
||||||
|
end
|
||||||
|
local head = table.concat(even):sub(1, 18)
|
||||||
|
local tail = tostring(id)
|
||||||
|
tail = tail:sub(math.max(1, #tail - 2))
|
||||||
|
local key = (head .. "web" .. tail)
|
||||||
|
if #key < 24 then key = key .. string.rep("=", 24 - #key) end
|
||||||
|
key = key:sub(1, 24)
|
||||||
|
local words = expand192(key)
|
||||||
|
local iv = key:sub(1, 16)
|
||||||
|
local raw = b64dec(bytesData)
|
||||||
|
local prev = iv
|
||||||
|
local plain = {}
|
||||||
|
for i = 1, #raw, 16 do
|
||||||
|
local block = raw:sub(i, i + 15)
|
||||||
|
local dec = decryptBlock(block, words)
|
||||||
|
local chunk = {}
|
||||||
|
for b = 1, 16 do
|
||||||
|
chunk[b] = string.char(bxor(dec:byte(b), prev:byte(b)))
|
||||||
|
end
|
||||||
|
plain[#plain + 1] = table.concat(chunk)
|
||||||
|
prev = block
|
||||||
|
end
|
||||||
|
plain = table.concat(plain)
|
||||||
|
local pad = plain:byte(#plain)
|
||||||
|
if not pad or pad < 1 or pad > 16 then return nil end
|
||||||
|
return plain:sub(1, #plain - pad)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function u32hex(w)
|
||||||
|
w = band(w, 0xffffffff)
|
||||||
|
if w < 0 then w = w + 4294967296 end
|
||||||
|
return string.format("%08x", w)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function rol(x, n)
|
||||||
|
return bit.rol(band(x, 0xffffffff), n)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function u32n(x)
|
||||||
|
x = band(x, 0xffffffff)
|
||||||
|
if x < 0 then x = x + 4294967296 end
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
|
||||||
|
local function add32(a, b, c)
|
||||||
|
local s = u32n(a) + u32n(b)
|
||||||
|
if c then s = s + u32n(c) end
|
||||||
|
return band(s % 4294967296, 0xffffffff)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function processBlock(block, a, b, c, d)
|
||||||
|
for i = 0, 15 do
|
||||||
|
local o = i * 4
|
||||||
|
local w = bor(
|
||||||
|
block[o],
|
||||||
|
lshift(block[o + 1], 8),
|
||||||
|
lshift(block[o + 2], 16),
|
||||||
|
lshift(block[o + 3], 24))
|
||||||
|
a = add32(a, bor(band(b, c), band(bit.bnot(b), d)), w)
|
||||||
|
b = add32(b, bxor(c, a, d), w)
|
||||||
|
c = add32(c, bor(band(b, a), band(bit.bnot(b), d)), w)
|
||||||
|
d = add32(d, bxor(c, a, b), w)
|
||||||
|
a, b, c, d = rol(a, 5), rol(b, 7), rol(c, 3), rol(d, 11)
|
||||||
|
end
|
||||||
|
return a, b, c, d
|
||||||
|
end
|
||||||
|
|
||||||
|
local function md5v2(data)
|
||||||
|
local len = #data
|
||||||
|
local zeros = 64 - ((len + 9) % 64)
|
||||||
|
local n = len + 1 + zeros + 8
|
||||||
|
local buf = {}
|
||||||
|
for i = 0, n - 1 do buf[i] = 0 end
|
||||||
|
for i = 1, len do buf[i - 1] = data:byte(i) end
|
||||||
|
buf[len] = 0x80
|
||||||
|
local bits = len * 8
|
||||||
|
for i = 0, 7 do
|
||||||
|
buf[n - 8 + i] = bits % 256
|
||||||
|
bits = math.floor(bits / 256)
|
||||||
|
end
|
||||||
|
local a, b, c, d = 0xabcdef01, 0x12345678, 0xdeadbeef, 0xfeedcafe
|
||||||
|
for off = 0, n - 1, 64 do
|
||||||
|
local block = {}
|
||||||
|
for i = 0, 63 do block[i] = buf[off + i] end
|
||||||
|
a, b, c, d = processBlock(block, a, b, c, d)
|
||||||
|
end
|
||||||
|
local hex = u32hex(a) .. u32hex(b) .. u32hex(c) .. u32hex(d)
|
||||||
|
local out = {}
|
||||||
|
for i = 1, #hex do
|
||||||
|
local idx = hex:byte(i) % 62
|
||||||
|
out[i] = ALPHA62:sub(idx + 1, idx + 1)
|
||||||
|
end
|
||||||
|
return table.concat(out)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function xrc4(data)
|
||||||
|
local key = { 1, 3, 7 }
|
||||||
|
local S = {}
|
||||||
|
for i = 0, 255 do S[i] = i end
|
||||||
|
local j = 0
|
||||||
|
for i = 0, 255 do
|
||||||
|
j = band(j + S[i] + key[(i % 3) + 1], 255)
|
||||||
|
S[i], S[j] = S[j], S[i]
|
||||||
|
S[i] = bxor(S[i], i)
|
||||||
|
end
|
||||||
|
local out, ii, jj = {}, 0, 0
|
||||||
|
for n = 1, #data do
|
||||||
|
ii = band(ii + 1, 255)
|
||||||
|
jj = band(jj + S[ii], 255)
|
||||||
|
S[ii], S[jj] = S[jj], S[ii]
|
||||||
|
out[n] = string.char(bxor(data:byte(n), S[band(S[ii] + S[jj] + 1, 255)]))
|
||||||
|
end
|
||||||
|
return table.concat(out)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function b64v2(data, variant)
|
||||||
|
local table64 = B64[variant + 1]
|
||||||
|
local out = {}
|
||||||
|
local n = #data
|
||||||
|
for i = 0, n - 1, 3 do
|
||||||
|
local b1 = data:byte(i + 1) or 0
|
||||||
|
local b2 = data:byte(i + 2) or 0
|
||||||
|
local b3 = data:byte(i + 3) or 0
|
||||||
|
local v = b1 * 65536 + b2 * 256 + b3
|
||||||
|
for k = 0, 3 do
|
||||||
|
local shift = 18 - 6 * k
|
||||||
|
local idx = math.floor(v / (2 ^ shift)) % 64
|
||||||
|
out[#out + 1] = table64:sub(idx + 1, idx + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local rem = n % 3
|
||||||
|
if rem ~= 0 then
|
||||||
|
out[#out + 1] = string.rep("=", 3 - rem)
|
||||||
|
end
|
||||||
|
return table.concat(out)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function hexTs(ts)
|
||||||
|
local hex = {}
|
||||||
|
ts = math.floor(ts)
|
||||||
|
if ts == 0 then return "0" end
|
||||||
|
while ts > 0 do
|
||||||
|
local d = ts % 16
|
||||||
|
hex[#hex + 1] = string.sub("0123456789abcdef", d + 1, d + 1)
|
||||||
|
ts = math.floor(ts / 16)
|
||||||
|
end
|
||||||
|
for i = 1, math.floor(#hex / 2) do
|
||||||
|
hex[i], hex[#hex - i + 1] = hex[#hex - i + 1], hex[i]
|
||||||
|
end
|
||||||
|
return table.concat(hex)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function binTs(ts)
|
||||||
|
ts = math.floor(ts / 128)
|
||||||
|
if ts == 0 then return "0" end
|
||||||
|
local bits = {}
|
||||||
|
while ts > 0 do
|
||||||
|
bits[#bits + 1] = (ts % 2 == 1) and "1" or "0"
|
||||||
|
ts = math.floor(ts / 2)
|
||||||
|
end
|
||||||
|
for i = 1, math.floor(#bits / 2) do
|
||||||
|
bits[i], bits[#bits - i + 1] = bits[#bits - i + 1], bits[i]
|
||||||
|
end
|
||||||
|
return table.concat(bits)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Crypto.sign(query, body, ts)
|
||||||
|
local params = {}
|
||||||
|
for key, value in query:gmatch("([^&=]+)=([^&]*)") do
|
||||||
|
if key ~= "ultra_sign" then params[#params + 1] = { key, value } end
|
||||||
|
end
|
||||||
|
table.sort(params, function(a, b) return a[1] < b[1] end)
|
||||||
|
local acc = {}
|
||||||
|
for i = 1, #params do
|
||||||
|
acc[#acc + 1] = params[i][1] .. params[i][2]
|
||||||
|
end
|
||||||
|
acc = table.concat(acc)
|
||||||
|
ts = math.floor(ts)
|
||||||
|
local h1 = md5v2(acc .. hexTs(ts))
|
||||||
|
local h2 = md5v2(body .. binTs(ts))
|
||||||
|
local h3 = md5v2(b64v2(xrc4(body), ts % 2))
|
||||||
|
local t = ts % 4294967296
|
||||||
|
local function sh(n)
|
||||||
|
return math.floor(ts / (2 ^ n)) % 256
|
||||||
|
end
|
||||||
|
local function tb(n)
|
||||||
|
return math.floor(t / (2 ^ n)) % 256
|
||||||
|
end
|
||||||
|
local buf = {
|
||||||
|
1, sh(23), sh(31), sh(15), 7,
|
||||||
|
h1:byte(14), h1:byte(24), h2:byte(12), h2:byte(8), h3:byte(24), h3:byte(20),
|
||||||
|
tb(8), tb(24), tb(16),
|
||||||
|
7, 0, 5, 0, 4, 1, 5, 0, 2, 0, 1, 0, 6, 2, 0, 0,
|
||||||
|
}
|
||||||
|
local x = 0
|
||||||
|
for i = 1, 28 do x = bxor(x, buf[i]) end
|
||||||
|
buf[30] = x
|
||||||
|
local order = { 23, 8, 18, 22, 17, 29, 16, 13, 5, 4, 15, 28, 11, 7, 12, 20, 25, 26, 27, 21, 24, 19, 9, 3 }
|
||||||
|
local out = { buf[1], buf[2], 0, 0, 0 }
|
||||||
|
for i = 1, #order do out[#out + 1] = buf[order[i]] end
|
||||||
|
local function cat(bytes)
|
||||||
|
local s = {}
|
||||||
|
for i = 1, #bytes do s[i] = string.char(bytes[i]) end
|
||||||
|
return table.concat(s)
|
||||||
|
end
|
||||||
|
local head = cat(out)
|
||||||
|
local ha = md5v2(head .. string.char(h1:byte(8), h2:byte(30)))
|
||||||
|
local hb = md5v2(head .. string.char(h3:byte(32)))
|
||||||
|
local sign = { ha:sub(1, 24):byte(1, 24) }
|
||||||
|
local hb24 = { hb:sub(1, 24):byte(1, 24) }
|
||||||
|
for i = 1, 24 do sign[24 + i] = hb24[i] end
|
||||||
|
local hex = hexTs(ts)
|
||||||
|
local places = { 42, 38, 24, 30, 14, 20, 6, 48, 8, 12, 18 }
|
||||||
|
for pos = 1, #places do
|
||||||
|
local ch = hex:byte(pos)
|
||||||
|
if ch then sign[places[pos]] = ch end
|
||||||
|
end
|
||||||
|
local raw = {}
|
||||||
|
for i = 1, 48 do raw[i] = string.char(sign[i] or 0) end
|
||||||
|
return table.concat(raw)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Crypto.urlencode(s)
|
||||||
|
local out = {}
|
||||||
|
for i = 1, #s do
|
||||||
|
local c = s:byte(i)
|
||||||
|
local ch = string.char(c)
|
||||||
|
if ch:match("^[%w%-_%.!~%*'%(%)]$") then
|
||||||
|
out[#out + 1] = ch
|
||||||
|
else
|
||||||
|
out[#out + 1] = string.format("%%%02X", c)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return table.concat(out)
|
||||||
|
end
|
||||||
|
|
||||||
|
return Crypto
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
@@ -1,11 +1,16 @@
|
|||||||
return {
|
return {
|
||||||
["哔哩哔哩漫画"] = "Bilibili Manga",
|
["哔哩哔哩漫画"] = "Bilibili Manga",
|
||||||
["哔哩哔哩漫画书源"] = "Bilibili Manga Source",
|
["哔哩哔哩漫画书源"] = "Bilibili Manga Source",
|
||||||
["请输入 SESSDATA Cookie"] = "Enter SESSDATA Cookie",
|
|
||||||
["登录"] = "Login",
|
["登录"] = "Login",
|
||||||
["已登录"] = "Logged in",
|
["已登录"] = "Logged in",
|
||||||
["清除"] = "Clear",
|
["未登录 · 点此登录"] = "Not logged in · tap to log in",
|
||||||
["未登录"] = "Not logged in",
|
["哔哩漫画账号"] = "Bilibili Manga account",
|
||||||
|
["哔哩漫画登录 Cookie"] = "Bilibili Manga login cookie",
|
||||||
|
["在漫画官网登录后,复制请求标头里的完整 Cookie"] = "Log in on the manga site, then copy the full Cookie request header",
|
||||||
|
["确定退出哔哩漫画账号?"] = "Log out of Bilibili Manga?",
|
||||||
|
["哔哩账号标识缺失"] = "Bilibili account id is missing",
|
||||||
|
["登录校验失败,请确认已经登录哔哩哔哩"] = "Login check failed. Confirm you are logged in to Bilibili",
|
||||||
|
["没有读到 Cookie"] = "No cookie was found",
|
||||||
["搜索"] = "Search",
|
["搜索"] = "Search",
|
||||||
["首页"] = "Home",
|
["首页"] = "Home",
|
||||||
["分类"] = "Classify",
|
["分类"] = "Classify",
|
||||||
@@ -16,13 +21,17 @@ return {
|
|||||||
["网络不可用,请先连接 Wi-Fi"] = "Network unavailable, please connect to Wi-Fi first",
|
["网络不可用,请先连接 Wi-Fi"] = "Network unavailable, please connect to Wi-Fi first",
|
||||||
["请求失败"] = "Request failed",
|
["请求失败"] = "Request failed",
|
||||||
["响应无效"] = "Invalid response",
|
["响应无效"] = "Invalid response",
|
||||||
["登录已过期,请重新输入 Cookie"] = "Login expired, please re-enter Cookie",
|
["登录已过期,请重新登录"] = "Login expired. Please log in again",
|
||||||
["获取用户信息失败"] = "Failed to get user info",
|
["获取用户信息失败"] = "Failed to get user info",
|
||||||
["加载目录失败"] = "Failed to load table of contents",
|
["加载目录失败"] = "Failed to load table of contents",
|
||||||
["获取漫画详情失败"] = "Failed to get manga details",
|
["获取漫画详情失败"] = "Failed to get manga details",
|
||||||
["搜索失败"] = "Search failed",
|
["搜索失败"] = "Search failed",
|
||||||
["目录加载中…"] = "Loading table of contents...",
|
["目录加载中…"] = "Loading table of contents...",
|
||||||
["无搜索结果"] = "No search results",
|
["无搜索结果"] = "No search results",
|
||||||
|
["全部"] = "All",
|
||||||
|
["排序"] = "Sort",
|
||||||
|
["题材"] = "Genre",
|
||||||
|
["本章未购买"] = "This chapter has not been purchased",
|
||||||
["连载"] = "Ongoing",
|
["连载"] = "Ongoing",
|
||||||
["完结"] = "Completed",
|
["完结"] = "Completed",
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,16 @@
|
|||||||
return {
|
return {
|
||||||
["哔哩哔哩漫画"] = "哔哩哔哩漫画",
|
["哔哩哔哩漫画"] = "哔哩哔哩漫画",
|
||||||
["哔哩哔哩漫画书源"] = "哔哩哔哩漫画书源",
|
["哔哩哔哩漫画书源"] = "哔哩哔哩漫画书源",
|
||||||
["请输入 SESSDATA Cookie"] = "請輸入 SESSDATA Cookie",
|
|
||||||
["登录"] = "登入",
|
["登录"] = "登入",
|
||||||
["已登录"] = "已登入",
|
["已登录"] = "已登入",
|
||||||
["清除"] = "清除",
|
["未登录 · 点此登录"] = "未登入 · 點此登入",
|
||||||
["未登录"] = "未登入",
|
["哔哩漫画账号"] = "嗶哩漫畫帳號",
|
||||||
|
["哔哩漫画登录 Cookie"] = "嗶哩漫畫登入 Cookie",
|
||||||
|
["在漫画官网登录后,复制请求标头里的完整 Cookie"] = "在漫畫官網登入後,複製請求標頭裡的完整 Cookie",
|
||||||
|
["确定退出哔哩漫画账号?"] = "確定退出嗶哩漫畫帳號?",
|
||||||
|
["哔哩账号标识缺失"] = "嗶哩帳號識別碼缺失",
|
||||||
|
["登录校验失败,请确认已经登录哔哩哔哩"] = "登入驗證失敗,請確認已經登入嗶哩嗶哩",
|
||||||
|
["没有读到 Cookie"] = "沒有讀到 Cookie",
|
||||||
["搜索"] = "搜尋",
|
["搜索"] = "搜尋",
|
||||||
["首页"] = "首頁",
|
["首页"] = "首頁",
|
||||||
["分类"] = "分類",
|
["分类"] = "分類",
|
||||||
@@ -16,13 +21,17 @@ return {
|
|||||||
["网络不可用,请先连接 Wi-Fi"] = "網路不可用,請先連線 Wi-Fi",
|
["网络不可用,请先连接 Wi-Fi"] = "網路不可用,請先連線 Wi-Fi",
|
||||||
["请求失败"] = "請求失敗",
|
["请求失败"] = "請求失敗",
|
||||||
["响应无效"] = "回應無效",
|
["响应无效"] = "回應無效",
|
||||||
["登录已过期,请重新输入 Cookie"] = "登入已過期,請重新輸入 Cookie",
|
["登录已过期,请重新登录"] = "登入已過期,請重新登入",
|
||||||
["获取用户信息失败"] = "取得使用者資訊失敗",
|
["获取用户信息失败"] = "取得使用者資訊失敗",
|
||||||
["加载目录失败"] = "載入目錄失敗",
|
["加载目录失败"] = "載入目錄失敗",
|
||||||
["获取漫画详情失败"] = "取得漫畫詳情失敗",
|
["获取漫画详情失败"] = "取得漫畫詳情失敗",
|
||||||
["搜索失败"] = "搜尋失敗",
|
["搜索失败"] = "搜尋失敗",
|
||||||
["目录加载中…"] = "目錄載入中…",
|
["目录加载中…"] = "目錄載入中…",
|
||||||
["无搜索结果"] = "無搜尋結果",
|
["无搜索结果"] = "無搜尋結果",
|
||||||
|
["全部"] = "全部",
|
||||||
|
["排序"] = "排序",
|
||||||
|
["题材"] = "題材",
|
||||||
|
["本章未购买"] = "本章未購買",
|
||||||
["连载"] = "連載中",
|
["连载"] = "連載中",
|
||||||
["完结"] = "已完成",
|
["完结"] = "已完成",
|
||||||
}
|
}
|
||||||
@@ -3,4 +3,6 @@ if not package.path:find(PLUGIN_DIR .. "/?.lua", 1, true) then
|
|||||||
package.path = PLUGIN_DIR .. "/?.lua;" .. package.path
|
package.path = PLUGIN_DIR .. "/?.lua;" .. package.path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return function()
|
||||||
return require("moonsource_bilimanga._meta")
|
return require("moonsource_bilimanga._meta")
|
||||||
|
end
|
||||||
@@ -11,41 +11,60 @@ end
|
|||||||
-- Book 映射
|
-- 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)
|
function Mapper.book(info)
|
||||||
if not info then return nil end
|
if not info then return nil end
|
||||||
local cover = info.vcover or info.horizontal_cover or info.cover or ""
|
local comic_id = info.comic_id or info.season_id or info.id
|
||||||
local author = info.author
|
if comic_id == nil or comic_id == "" then return nil end
|
||||||
local author_str = ""
|
local cover = info.vertical_cover or info.vcover or info.horizontal_cover or info.square_cover or info.cover or ""
|
||||||
if author then
|
|
||||||
if type(author) == "string" then
|
|
||||||
author_str = author
|
|
||||||
elseif type(author) == "table" then
|
|
||||||
if author.name then author_str = author.name end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if author_str == "" then author_str = "未知作者" end
|
|
||||||
|
|
||||||
local styles = info.styles
|
local styles = info.styles
|
||||||
local category = ""
|
local category = ""
|
||||||
if styles and type(styles) == "table" and #styles > 0 then
|
if styles and type(styles) == "table" and #styles > 0 and type(styles[1]) == "string" then
|
||||||
category = table.concat(styles, " · ")
|
category = table.concat(styles, " · ")
|
||||||
end
|
end
|
||||||
|
|
||||||
local status_str = _("连载")
|
local status_str = _("连载")
|
||||||
if info.status == 2 then status_str = _("完结") end
|
if info.is_finish == 1 or info.is_finish == true or info.status == 2 then
|
||||||
|
status_str = _("完结")
|
||||||
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
source_id = SOURCE_ID,
|
source_id = SOURCE_ID,
|
||||||
stable_id = tostring(info.comic_id),
|
stable_id = tostring(comic_id),
|
||||||
title = clean(info.title),
|
title = clean(plainTitle(info)),
|
||||||
authors = author_str,
|
authors = authorOf(info),
|
||||||
cover = cover,
|
cover = cover,
|
||||||
intro = clean(info.desc),
|
intro = clean(info.desc ~= "" and info.desc or info.evaluate),
|
||||||
category = category,
|
category = category,
|
||||||
meta = {
|
meta = {
|
||||||
status = status_str,
|
status = status_str,
|
||||||
fans = info.fans or "",
|
fans = info.fans or "",
|
||||||
total_eps = info.ep_count or info.ord_count or 0,
|
total_eps = info.ep_count or info.total or info.ord_count or 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@@ -58,18 +77,29 @@ end
|
|||||||
-- Chapter 映射
|
-- Chapter 映射
|
||||||
-- ============================================================
|
-- ============================================================
|
||||||
|
|
||||||
|
-- 目录缓存格式。ep_list 是倒序(最新话在前),版本 1 起按 ord 升序;旧缓存一律重拉。
|
||||||
|
Mapper.TOC_VERSION = 1
|
||||||
|
|
||||||
function Mapper.chapters(comic)
|
function Mapper.chapters(comic)
|
||||||
if not comic then return {} end
|
if not comic then return {} end
|
||||||
local eps = comic.ep_list or {}
|
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 = {}
|
local chapters = {}
|
||||||
for i, ep in ipairs(eps) do
|
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] = {
|
chapters[#chapters + 1] = {
|
||||||
idx = i,
|
idx = i,
|
||||||
uid = tostring(ep.id),
|
uid = tostring(ep.id),
|
||||||
title = clean(ep.title or ep.short_title or ("第" .. i .. "话")),
|
title = title ~= "" and title or ("第" .. i .. "话"),
|
||||||
vip = ep.pay_type == 1 or (ep.is_pay == true and true) or false,
|
vip = ep.is_locked == true,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
if chapters[1] then chapters[1].toc_version = Mapper.TOC_VERSION end
|
||||||
return chapters
|
return chapters
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -103,9 +133,81 @@ end
|
|||||||
-- Search 映射
|
-- 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)
|
function Mapper.searchResults(wire)
|
||||||
if not wire then return {} end
|
local items = Mapper.rows(wire) or {}
|
||||||
local items = wire.list or {}
|
|
||||||
local books = {}
|
local books = {}
|
||||||
for i = 1, #items do
|
for i = 1, #items do
|
||||||
local book = Mapper.book(items[i])
|
local book = Mapper.book(items[i])
|
||||||
@@ -119,8 +221,7 @@ end
|
|||||||
-- ============================================================
|
-- ============================================================
|
||||||
|
|
||||||
function Mapper.favoriteItems(wire)
|
function Mapper.favoriteItems(wire)
|
||||||
if not wire then return {} end
|
return Mapper.rows(wire)
|
||||||
return wire.list or {}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ============================================================
|
-- ============================================================
|
||||||
@@ -128,10 +229,10 @@ end
|
|||||||
-- ============================================================
|
-- ============================================================
|
||||||
|
|
||||||
function Mapper.historyItem(wire, comic_id)
|
function Mapper.historyItem(wire, comic_id)
|
||||||
if not wire then return nil end
|
local list = Mapper.rows(wire)
|
||||||
local list = wire.list or {}
|
if not list then return nil end
|
||||||
for _, item in ipairs(list) do
|
for _, item in ipairs(list) do
|
||||||
if tostring(item.comic_id) == tostring(comic_id) then
|
if tostring(item.comic_id or item.season_id) == tostring(comic_id) then
|
||||||
return item
|
return item
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,82 +1,78 @@
|
|||||||
|
--[[--
|
||||||
|
哔哩漫画账号:浏览器授权与完整 Cookie。校验通过才保存。
|
||||||
|
|
||||||
|
漫画站复用哔哩哔哩主站会话。已购和大会员话由详情里的 is_locked 表示,
|
||||||
|
不在这里另做会员开关。
|
||||||
|
|
||||||
|
@module moonsource_bilimanga.setting
|
||||||
|
--]]
|
||||||
|
|
||||||
|
local CookieLogin = require("source.cookie_login")
|
||||||
local _ = require("moonsource_bilimanga.l10n")
|
local _ = require("moonsource_bilimanga.l10n")
|
||||||
|
|
||||||
local ID = "bilimanga"
|
local SOURCE_ID = "bilimanga"
|
||||||
|
local Setting = {}
|
||||||
|
|
||||||
local function entries(desktop)
|
local function loggedIn()
|
||||||
return { {
|
local cfg = require("utils.settings").getSource(SOURCE_ID)
|
||||||
group = "network",
|
return type(cfg.cookie) == "string" and cfg.cookie ~= ""
|
||||||
order = 100,
|
|
||||||
id = "login",
|
|
||||||
icon = "d_overwrite_feature",
|
|
||||||
title = _("哔哩哔哩漫画"),
|
|
||||||
page = {
|
|
||||||
title = _("哔哩哔哩漫画"),
|
|
||||||
sections = function()
|
|
||||||
local cfg = require("utils.settings").getSource(ID)
|
|
||||||
local has_cookie = cfg.cookie and cfg.cookie ~= ""
|
|
||||||
|
|
||||||
local function doLogin()
|
|
||||||
require("ui/modal").input({
|
|
||||||
title = _("请输入 SESSDATA Cookie"),
|
|
||||||
button = _("登录"),
|
|
||||||
text = cfg.cookie or "",
|
|
||||||
hint = "SESSDATA",
|
|
||||||
hint_is_field = true,
|
|
||||||
}, function(entry)
|
|
||||||
if not entry or entry == "" then return end
|
|
||||||
require("utils.settings").saveSource(ID, {
|
|
||||||
cookie = entry,
|
|
||||||
})
|
|
||||||
require("ui/uimanager"):popMenu()
|
|
||||||
local Toast = require("ui/themed").Toast
|
|
||||||
Toast:new({
|
|
||||||
text = _("已登录"),
|
|
||||||
timeout = 2,
|
|
||||||
}):show()
|
|
||||||
desktop:onEvent("book_source_refresh")
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function doClear()
|
local function commit(cookie, account, cb, current)
|
||||||
require("utils.settings").saveSource(ID, {
|
if type(account) ~= "table" or type(account.id) ~= "string" or not account.id:match("^%d+$") then
|
||||||
cookie = "",
|
cb(false, _("哔哩账号标识缺失"))
|
||||||
})
|
return
|
||||||
require("ui/uimanager"):popMenu()
|
end
|
||||||
local Toast = require("ui/themed").Toast
|
return CookieLogin.commitAccount(SOURCE_ID, cookie, account.id, cb, current)
|
||||||
Toast:new({
|
|
||||||
text = _("已清除 Cookie"),
|
|
||||||
timeout = 2,
|
|
||||||
}):show()
|
|
||||||
desktop:onEvent("book_source_refresh")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local rows = { {
|
---@type CookieLoginSpec
|
||||||
fn = function()
|
local SPEC = {
|
||||||
return require("desktop.pages.setting.tree").Row.action({
|
id = SOURCE_ID,
|
||||||
text = has_cookie and _("已登录") or _("未登录"),
|
title = _("哔哩漫画账号"),
|
||||||
callback = doLogin,
|
cookie_title = _("哔哩漫画登录 Cookie"),
|
||||||
})
|
cookie_hint = _("在漫画官网登录后,复制请求标头里的完整 Cookie"),
|
||||||
|
logout_text = _("确定退出哔哩漫画账号?"),
|
||||||
|
loggedIn = loggedIn,
|
||||||
|
checkAsync = function(cookie, cb)
|
||||||
|
return require("moonsource_bilimanga.client").loginCheckAsync(cookie, cb)
|
||||||
end,
|
end,
|
||||||
} }
|
commit = commit,
|
||||||
|
clear = function(cfg) cfg.cookie, cfg.account_id = nil, nil end,
|
||||||
|
}
|
||||||
|
|
||||||
if has_cookie then
|
function Setting.rowStatus()
|
||||||
rows[#rows + 1] = {
|
local on = CookieLogin.authorized(SPEC)
|
||||||
fn = function()
|
return on and _("已登录") or _("未登录 · 点此登录"), on
|
||||||
return require("desktop.pages.setting.tree").Row.action({
|
end
|
||||||
text = _("清除"),
|
|
||||||
callback = doClear,
|
function Setting.rowIcon()
|
||||||
})
|
return "smart_display"
|
||||||
|
end
|
||||||
|
|
||||||
|
Setting.browser = {
|
||||||
|
domain = "manga.bilibili.com",
|
||||||
|
scope = "cookie",
|
||||||
|
login = "https://manga.bilibili.com/account-center",
|
||||||
|
check = "https://manga.bilibili.com/account-center",
|
||||||
|
icon = "https://i0.hdslb.com/bfs/activity-plat/static/20250320/1e16455b3e0f77ddb2522c3ae2aba5d9/SgyyYc4rbJ.ico",
|
||||||
|
isAuthorized = function() return CookieLogin.authorized(SPEC) end,
|
||||||
|
apply = function(payload, cb)
|
||||||
|
return CookieLogin.save(SPEC, CookieLogin.fromPayload(payload, "bilibili.com"), cb)
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Setting.rows()
|
||||||
|
local rows = {
|
||||||
|
CookieLogin.browserRow(),
|
||||||
|
CookieLogin.pasteRow(SPEC),
|
||||||
|
}
|
||||||
|
if loggedIn() then rows[#rows + 1] = CookieLogin.logoutRow(SPEC) end
|
||||||
|
return rows
|
||||||
end
|
end
|
||||||
|
|
||||||
return { {
|
function Setting.login()
|
||||||
title = "",
|
CookieLogin.prompt(SPEC)
|
||||||
rows = rows,
|
|
||||||
} }
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
} }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return { entries = entries }
|
return Setting
|
||||||
@@ -5,7 +5,8 @@ local Chapter = require("moonsource_bilimanga.chapter")
|
|||||||
local Toc = require("source.toc")
|
local Toc = require("source.toc")
|
||||||
local _ = require("moonsource_bilimanga.l10n")
|
local _ = require("moonsource_bilimanga.l10n")
|
||||||
|
|
||||||
local BiliManga = {}
|
local BiliManga = setmetatable({}, { __index = SourceBase })
|
||||||
|
BiliManga.__index = BiliManga
|
||||||
local SOURCE_ID
|
local SOURCE_ID
|
||||||
|
|
||||||
function BiliManga.meta()
|
function BiliManga.meta()
|
||||||
@@ -15,10 +16,7 @@ end
|
|||||||
function BiliManga.new()
|
function BiliManga.new()
|
||||||
local meta = BiliManga.meta()
|
local meta = BiliManga.meta()
|
||||||
SOURCE_ID = meta.id
|
SOURCE_ID = meta.id
|
||||||
local source = setmetatable({ id = meta.id, name = meta.name, type = meta.type }, {
|
return setmetatable({ id = meta.id, name = meta.name, type = meta.type }, BiliManga)
|
||||||
__index = SourceBase,
|
|
||||||
})
|
|
||||||
return source
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function BiliManga:configured()
|
function BiliManga:configured()
|
||||||
@@ -45,7 +43,7 @@ function BiliManga:getDetailAsync(identity, cb)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
return Client.comicDetailLiteAsync(identity.stable_id, function(info, err)
|
return Client.comicDetailAsync(identity.stable_id, function(info, err)
|
||||||
if err then cb(nil, err); return end
|
if err then cb(nil, err); return end
|
||||||
local book = Mapper.book(info)
|
local book = Mapper.book(info)
|
||||||
require("book.store").rememberDetail(book)
|
require("book.store").rememberDetail(book)
|
||||||
@@ -53,11 +51,19 @@ function BiliManga:getDetailAsync(identity, cb)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- 当前格式的缓存目录;旧格式(倒序)当作没有。
|
||||||
|
---@param identity BookIdentity
|
||||||
|
---@return table[]|nil
|
||||||
|
local function cachedToc(identity)
|
||||||
|
local toc = Toc.read(identity.source_id or SOURCE_ID, identity.stable_id)
|
||||||
|
if toc and toc[1] and toc[1].toc_version == Mapper.TOC_VERSION then return toc end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
function BiliManga:loadTocAsync(identity, cb, force)
|
function BiliManga:loadTocAsync(identity, cb, force)
|
||||||
local tid = identity.source_id or SOURCE_ID
|
|
||||||
if not force then
|
if not force then
|
||||||
local cached = Toc.read(tid, identity.stable_id)
|
local cached = cachedToc(identity)
|
||||||
if cached and #cached > 0 then
|
if cached then
|
||||||
return SourceBase.defer(cb, cached)
|
return SourceBase.defer(cb, cached)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -80,20 +86,22 @@ function BiliManga:refreshTocAsync(identity, cb)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- 起始章:调用方指定的章 > 本地待推进度 > 第一章。
|
||||||
|
---@param identity BookIdentity
|
||||||
|
---@param opts table
|
||||||
|
---@param toc table[]
|
||||||
|
---@return integer
|
||||||
|
local function startIndex(identity, opts, toc)
|
||||||
|
local idx = opts.chapter_idx
|
||||||
|
if not idx then
|
||||||
|
local pending = require("book.progress_db").get(identity.source_id, identity.stable_id)
|
||||||
|
idx = pending and (pending.chapter_idx or math.floor((pending.fraction or 0) * #toc) + 1)
|
||||||
|
end
|
||||||
|
return math.max(1, math.min(#toc, idx or 1))
|
||||||
|
end
|
||||||
|
|
||||||
function BiliManga:openBookAsync(identity, opts, cb)
|
function BiliManga:openBookAsync(identity, opts, cb)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
SOURCE_ID = identity.source_id or SOURCE_ID
|
|
||||||
local cancelled = false
|
|
||||||
local active = nil
|
|
||||||
local dialog = nil
|
|
||||||
|
|
||||||
local function startIndex(toc)
|
|
||||||
if identity.chapter_idx and identity.chapter_idx > 0 then
|
|
||||||
return math.min(identity.chapter_idx, #toc)
|
|
||||||
end
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
|
|
||||||
local function finish(path, idx)
|
local function finish(path, idx)
|
||||||
local ok, err = require("book.store").touch(path, identity, {
|
local ok, err = require("book.store").touch(path, identity, {
|
||||||
chapter_idx = idx,
|
chapter_idx = idx,
|
||||||
@@ -101,105 +109,55 @@ function BiliManga:openBookAsync(identity, opts, cb)
|
|||||||
})
|
})
|
||||||
cb(ok and path or nil, err)
|
cb(ok and path or nil, err)
|
||||||
end
|
end
|
||||||
|
local toc = cachedToc(identity)
|
||||||
local function closeDialog()
|
if toc then
|
||||||
if dialog then
|
local idx = startIndex(identity, opts, toc)
|
||||||
pcall(dialog.dismiss)
|
|
||||||
dialog = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 检查缓存
|
|
||||||
local toc = Toc.read(SOURCE_ID, identity.stable_id)
|
|
||||||
if toc and #toc > 0 then
|
|
||||||
local idx = startIndex(toc)
|
|
||||||
local path = Chapter.existing(identity, toc[idx])
|
local path = Chapter.existing(identity, toc[idx])
|
||||||
if path then return SourceBase.defer(finish, path, idx) end
|
if path then return SourceBase.defer(finish, path, idx) end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 离线检查
|
|
||||||
if not require("ui/network/manager"):isConnected() then
|
if not require("ui/network/manager"):isConnected() then
|
||||||
return SourceBase.defer(cb, nil, _("网络不可用,请先连接 Wi-Fi"))
|
return SourceBase.defer(cb, nil, _("网络不可用,请先连接 Wi-Fi"))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 异步:拉目录 → 下载CBZ
|
local cancelled, active, dialog = false, nil, nil
|
||||||
|
local function closeDialog()
|
||||||
|
if dialog then dialog:close(); dialog = nil end
|
||||||
|
end
|
||||||
require("ui/uimanager"):nextTick(function()
|
require("ui/uimanager"):nextTick(function()
|
||||||
local ProgressDialog = require("ui/widgets/progressdialog")
|
if cancelled then return end
|
||||||
dialog = ProgressDialog:new{
|
dialog = require("ui.themes.widgets.progress_dialog"):new{
|
||||||
title = identity.book and identity.book.title or "",
|
title = identity.book and identity.book.title or identity.stable_id,
|
||||||
text = _("正在准备漫画…"),
|
dismissable = false,
|
||||||
progress = { min = 0, max = 1 },
|
|
||||||
}
|
}
|
||||||
dialog:show()
|
dialog:show()
|
||||||
|
dialog:set(_("正在准备漫画…"))
|
||||||
toc = Toc.read(SOURCE_ID, identity.stable_id)
|
local loaded = false
|
||||||
if not toc or #toc == 0 then
|
local toc_job = self:loadTocAsync(identity, function(fresh, err)
|
||||||
local retries = 0
|
loaded = true
|
||||||
local MAX_RETRIES = 3
|
active = nil
|
||||||
|
|
||||||
local function loadTocWithRetry()
|
|
||||||
if cancelled then return end
|
if cancelled then return end
|
||||||
retries = retries + 1
|
if not fresh then closeDialog(); cb(nil, err or _("加载目录失败")); return end
|
||||||
if retries > MAX_RETRIES then
|
local idx = startIndex(identity, opts, fresh)
|
||||||
|
local materialized = false
|
||||||
|
local chapter_job = Chapter.materializeAsync(identity, fresh[idx], function(done, total)
|
||||||
|
if dialog and total > 0 then dialog:update(_("正在准备漫画…"), done, total) end
|
||||||
|
end, function(path, materialize_err)
|
||||||
|
materialized = true
|
||||||
|
active = nil
|
||||||
closeDialog()
|
closeDialog()
|
||||||
cb(nil, _("加载目录失败"))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
self:refreshTocAsync(identity, function(toc_data, toc_err)
|
|
||||||
if cancelled then return end
|
if cancelled then return end
|
||||||
if not toc_data then
|
if not path then cb(nil, materialize_err or _("章节图片解析失败")); return end
|
||||||
require("ui/uimanager"):scheduleIn(2, loadTocWithRetry)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
toc = toc_data
|
|
||||||
local idx = startIndex(toc)
|
|
||||||
local chapter_job = Chapter.materializeAsync(identity, toc[idx],
|
|
||||||
function(done, total)
|
|
||||||
if cancelled then return end
|
|
||||||
dialog:update(_("正在准备漫画…"), done, total)
|
|
||||||
end,
|
|
||||||
function(path, materialize_err)
|
|
||||||
if cancelled then return end
|
|
||||||
closeDialog()
|
|
||||||
if not path then
|
|
||||||
cb(nil, materialize_err or _("章节图片解析失败"))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
finish(path, idx)
|
finish(path, idx)
|
||||||
end
|
|
||||||
)
|
|
||||||
active = chapter_job
|
|
||||||
end, true)
|
|
||||||
end
|
|
||||||
loadTocWithRetry()
|
|
||||||
else
|
|
||||||
local idx = startIndex(toc)
|
|
||||||
local chapter_job = Chapter.materializeAsync(identity, toc[idx],
|
|
||||||
function(done, total)
|
|
||||||
if cancelled then return end
|
|
||||||
dialog:update(_("正在准备漫画…"), done, total)
|
|
||||||
end,
|
|
||||||
function(path, materialize_err)
|
|
||||||
if cancelled then return end
|
|
||||||
closeDialog()
|
|
||||||
if not path then
|
|
||||||
cb(nil, materialize_err or _("章节图片解析失败"))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
finish(path, idx)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
active = chapter_job
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
if not materialized then active = chapter_job end
|
||||||
return {
|
end)
|
||||||
cancel = function()
|
if not loaded then active = toc_job end
|
||||||
|
end)
|
||||||
|
return { cancel = function()
|
||||||
cancelled = true
|
cancelled = true
|
||||||
if active then active.cancel() end
|
if active then active.cancel() end
|
||||||
closeDialog()
|
closeDialog()
|
||||||
end,
|
end }
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function BiliManga:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
|
function BiliManga:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
|
||||||
@@ -261,8 +219,8 @@ function BiliManga:getProgressAsync(identity, cb)
|
|||||||
cb(nil, nil, { empty = true })
|
cb(nil, nil, { empty = true })
|
||||||
end
|
end
|
||||||
|
|
||||||
local existing = Toc.read(identity.source_id or SOURCE_ID, identity.stable_id)
|
local existing = cachedToc(identity)
|
||||||
if existing and #existing > 0 then
|
if existing then
|
||||||
afterToc(existing)
|
afterToc(existing)
|
||||||
else
|
else
|
||||||
self:refreshTocAsync(identity, afterToc)
|
self:refreshTocAsync(identity, afterToc)
|
||||||
@@ -298,40 +256,103 @@ function BiliManga:putProgressAsync(identity, pos, cb)
|
|||||||
return { cancel = function() end }
|
return { cancel = function() end }
|
||||||
end
|
end
|
||||||
|
|
||||||
function BiliManga:syncBooksAsync(opts, cb)
|
BiliManga.shelf_add = true
|
||||||
local cfg = require("utils.settings").getSource("bilimanga")
|
|
||||||
if not cfg.cookie or cfg.cookie == "" then
|
--- 收藏夹即远端书架:Shelf.syncAsync 要的 shelfSyncAsync / addToShelfAsync / removeFromShelfAsync。
|
||||||
return SourceBase.defer(cb, { data = {} })
|
local Favorites = {}
|
||||||
|
Favorites.__index = Favorites
|
||||||
|
|
||||||
|
local FAVORITE_PAGE = 50
|
||||||
|
|
||||||
|
--- "2026-11-27 00:40:55"(北京时间)→ 时间戳;空串 / 格式不对为 0。
|
||||||
|
---@param text any
|
||||||
|
---@return integer
|
||||||
|
local function beijingTime(text)
|
||||||
|
local y, mo, d, h, mi, s = tostring(text or ""):match("^(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)$")
|
||||||
|
if not y then return 0 end
|
||||||
|
local as_local = os.time({ year = tonumber(y), month = tonumber(mo), day = tonumber(d),
|
||||||
|
hour = tonumber(h), min = tonumber(mi), sec = tonumber(s) })
|
||||||
|
local local_offset = os.time(os.date("*t", as_local)) - os.time(os.date("!*t", as_local))
|
||||||
|
return as_local + local_offset - 8 * 3600
|
||||||
end
|
end
|
||||||
|
|
||||||
Client.listFavoriteAsync(1, 100, function(wire, err)
|
--- 整个收藏夹(逐页拉,接口的 total 不可靠,按满页判断还有没有下一页)。
|
||||||
if err then cb(nil, err); return end
|
---@param cb fun(books: Book[]|nil, err: string|nil)
|
||||||
local items = Mapper.favoriteItems(wire)
|
function Favorites:shelfSyncAsync(cb)
|
||||||
local books = {}
|
local books, page = {}, 1
|
||||||
for i = 1, #items do
|
local function nextPage()
|
||||||
local book = Mapper.bookFromItem(items[i])
|
self.job = Client.listFavoriteAsync(page, FAVORITE_PAGE, function(wire, err)
|
||||||
if book then
|
self.job = nil
|
||||||
book.read_at = items[i].read_time or 0
|
if self.cancelled then return end
|
||||||
book.inserted_at = items[i].fav_time or 0
|
local items = wire and Mapper.favoriteItems(wire)
|
||||||
|
if not items then cb(nil, err or _("响应无效")); return end
|
||||||
|
for _idx, item in ipairs(items) do
|
||||||
|
local book = Mapper.bookFromItem(item)
|
||||||
|
if not book then cb(nil, _("响应无效")); return end
|
||||||
|
book.read_at = beijingTime(item.latest_read_time)
|
||||||
books[#books + 1] = book
|
books[#books + 1] = book
|
||||||
end
|
end
|
||||||
end
|
if #items < FAVORITE_PAGE then cb(books); return end
|
||||||
cb({ data = books })
|
page = page + 1
|
||||||
|
nextPage()
|
||||||
end)
|
end)
|
||||||
|
end
|
||||||
|
nextPage()
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
return { cancel = function() end }
|
function Favorites:addToShelfAsync(stable_id, cb)
|
||||||
|
self.job = Client.addFavoriteAsync(stable_id, cb)
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function Favorites:removeFromShelfAsync(stable_id, cb)
|
||||||
|
self.job = Client.deleteFavoriteAsync(stable_id, cb)
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function Favorites:cancel()
|
||||||
|
self.cancelled = true
|
||||||
|
if self.job then self.job.cancel() end
|
||||||
|
end
|
||||||
|
|
||||||
|
function BiliManga:syncBooksAsync(opts, cb)
|
||||||
|
if not Client.hasAccount() then return SourceBase.skipped(cb, "not_logged_in") end
|
||||||
|
local remote = setmetatable({ cancelled = false }, Favorites)
|
||||||
|
local job = require("source.shelf").syncAsync(self.id, remote, nil, opts, cb)
|
||||||
|
return { cancel = function()
|
||||||
|
remote:cancel()
|
||||||
|
job:cancel()
|
||||||
|
end }
|
||||||
end
|
end
|
||||||
|
|
||||||
function BiliManga:browseAsync(opts, cb)
|
function BiliManga:browseAsync(opts, cb)
|
||||||
if opts and opts.browse_type == "home" then
|
opts = opts or {}
|
||||||
Client.homeHotAsync(function(wire, err)
|
local limit = opts.limit or 60
|
||||||
if err then cb(nil, err); return end
|
local state = { cancelled = false, job = nil }
|
||||||
local books = Mapper.searchResults(wire)
|
state.job = Client.allLabelAsync(function(wire, err)
|
||||||
cb({ data = books })
|
if state.cancelled then return end
|
||||||
|
local labels = Mapper.labels(wire)
|
||||||
|
if err or not labels then cb(nil, err or _("响应无效")); return end
|
||||||
|
local order, style = Mapper.storeChoice(opts.node, labels)
|
||||||
|
local groups = Mapper.storeGroups(labels, order, style)
|
||||||
|
state.job = require("source.browse").pages(function(page, done)
|
||||||
|
return Client.classPageAsync({ order = order, style_id = style }, page, function(rows, page_err)
|
||||||
|
if state.cancelled then return end
|
||||||
|
if page_err then done(nil, page_err); return end
|
||||||
|
local books = Mapper.books(rows)
|
||||||
|
if not books then done(nil, _("响应无效")); return end
|
||||||
|
done(books, nil, #books == 15)
|
||||||
end)
|
end)
|
||||||
return { cancel = function() end }
|
end, limit, groups, function(result, reason)
|
||||||
end
|
state.job = nil
|
||||||
return SourceBase.defer(cb, { data = {} })
|
if not state.cancelled then cb(result, reason) end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
return { cancel = function()
|
||||||
|
state.cancelled = true
|
||||||
|
if state.job and state.job.cancel then state.job.cancel() end
|
||||||
|
end }
|
||||||
end
|
end
|
||||||
|
|
||||||
return BiliManga
|
return BiliManga
|
||||||
@@ -1,435 +0,0 @@
|
|||||||
--[[--
|
|
||||||
书源插件在线更新(Forgejo Release)。
|
|
||||||
|
|
||||||
本文件是 moonsource-template 仓库 common/update.lua 的副本,由 tools/sync.sh 同步到每个插件仓库的
|
|
||||||
<包名>/update.lua,请改模板仓库,不要手改副本。
|
|
||||||
|
|
||||||
每个插件在自己的仓库(moon-source/moonsource-<名字>)独立发版,只检查并更新它自己:
|
|
||||||
仓库名和目录名都由包名(require 时的模块名)推出,包体校验值取自同一 Release 的 SHA256SUMS。
|
|
||||||
|
|
||||||
@module moonsource.update
|
|
||||||
--]]
|
|
||||||
|
|
||||||
local PKG = (...):match("^(.-)%.update$")
|
|
||||||
local NAME = PKG:gsub("_", "-")
|
|
||||||
local DIR = NAME .. ".koplugin"
|
|
||||||
local REPO = "moon-source/" .. NAME
|
|
||||||
local SITE = "https://git.garfieldho.me"
|
|
||||||
local API = SITE .. "/api/v1/repos/" .. REPO .. "/releases/latest"
|
|
||||||
local CHECK_INTERVAL = 24 * 60 * 60
|
|
||||||
local MAX_ARCHIVE_BYTES = 50 * 1024 * 1024
|
|
||||||
local GetText = require("gettext")
|
|
||||||
local T = require("ffi/util").template
|
|
||||||
|
|
||||||
-- 源文是简体中文;这点文案不值得单开 l10n 目录,翻译内联在这里。
|
|
||||||
local CATALOG = {
|
|
||||||
en = {
|
|
||||||
["下载并安装"] = "Download and install",
|
|
||||||
["「%1」已是最新(%2)。重新下载并替换已安装的版本?"] = "%1 is up to date (%2). Download again and replace the installed version?",
|
|
||||||
["书源插件更新"] = "Source plugin update",
|
|
||||||
["书源插件更新失败:%1"] = "Source plugin update failed: %1",
|
|
||||||
["关"] = "Off",
|
|
||||||
["发现「%1」新版本 %2(当前 %3),更新后重启 KOReader。"] = "%1 %2 is available (current %3). KOReader restarts after the update.",
|
|
||||||
["已是最新版本(%1)"] = "Already up to date (%1)",
|
|
||||||
["开"] = "On",
|
|
||||||
["只更新本书源,其它书源各自独立更新"] = "Updates this source only; other sources update on their own",
|
|
||||||
["新版本缺少插件包,请稍后再试"] = "The new release is missing plugin packages. Try again later.",
|
|
||||||
["更新书源插件"] = "Updating source plugins",
|
|
||||||
["更新信息无效,请稍后再试"] = "Invalid update information. Try again later.",
|
|
||||||
["更新包校验失败,请稍后再试"] = "Update package checksum failed. Try again later.",
|
|
||||||
["更新包过大,已中止下载"] = "The update package is too large. Download aborted.",
|
|
||||||
["更新完成,正在重启 KOReader…"] = "Update complete. Restarting KOReader…",
|
|
||||||
["检查书源插件更新"] = "Check source plugin updates",
|
|
||||||
["检查更新"] = "Check for updates",
|
|
||||||
["检查更新失败:%1"] = "Update check failed: %1",
|
|
||||||
["正在下载…"] = "Downloading…",
|
|
||||||
["正在检查书源插件更新…"] = "Checking for source plugin updates…",
|
|
||||||
["正在检查或安装更新,请稍候"] = "An update check or install is in progress. Please wait.",
|
|
||||||
["稍后"] = "Later",
|
|
||||||
["自动检查书源插件更新"] = "Check source plugin updates automatically",
|
|
||||||
["%1 更新"] = "%1 updates",
|
|
||||||
["书源插件检查更新与自动更新"] = "Source plugin update checks and auto-update",
|
|
||||||
["书源插件"] = "Source plugins",
|
|
||||||
},
|
|
||||||
zh_TW = {
|
|
||||||
["下载并安装"] = "下載並安裝",
|
|
||||||
["「%1」已是最新(%2)。重新下载并替换已安装的版本?"] = "「%1」已是最新(%2)。重新下載並替換已安裝的版本?",
|
|
||||||
["书源插件更新"] = "書源外掛更新",
|
|
||||||
["书源插件更新失败:%1"] = "書源外掛更新失敗:%1",
|
|
||||||
["关"] = "關",
|
|
||||||
["发现「%1」新版本 %2(当前 %3),更新后重启 KOReader。"] = "發現「%1」新版本 %2(目前 %3),更新後重新啟動 KOReader。",
|
|
||||||
["已是最新版本(%1)"] = "已是最新版本(%1)",
|
|
||||||
["开"] = "開",
|
|
||||||
["只更新本书源,其它书源各自独立更新"] = "只更新本書源,其它書源各自獨立更新",
|
|
||||||
["新版本缺少插件包,请稍后再试"] = "新版本缺少外掛包,請稍後再試",
|
|
||||||
["更新书源插件"] = "更新書源外掛",
|
|
||||||
["更新信息无效,请稍后再试"] = "更新資訊無效,請稍後再試",
|
|
||||||
["更新包校验失败,请稍后再试"] = "更新包校驗失敗,請稍後再試",
|
|
||||||
["更新包过大,已中止下载"] = "更新包過大,已中止下載",
|
|
||||||
["更新完成,正在重启 KOReader…"] = "更新完成,正在重新啟動 KOReader…",
|
|
||||||
["检查书源插件更新"] = "檢查書源外掛更新",
|
|
||||||
["检查更新"] = "檢查更新",
|
|
||||||
["检查更新失败:%1"] = "檢查更新失敗:%1",
|
|
||||||
["正在下载…"] = "正在下載…",
|
|
||||||
["正在检查书源插件更新…"] = "正在檢查書源外掛更新…",
|
|
||||||
["正在检查或安装更新,请稍候"] = "正在檢查或安裝更新,請稍候",
|
|
||||||
["稍后"] = "稍後",
|
|
||||||
["自动检查书源插件更新"] = "自動檢查書源外掛更新",
|
|
||||||
["%1 更新"] = "%1 更新",
|
|
||||||
["书源插件检查更新与自动更新"] = "書源外掛檢查更新與自動更新",
|
|
||||||
["书源插件"] = "書源外掛",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
local function _(msgid)
|
|
||||||
local lang = tostring(GetText.current_lang or ""):gsub("%.[%w%-]+$", "")
|
|
||||||
local catalog
|
|
||||||
if lang:match("^zh_TW") or lang:match("^zh_HK") then
|
|
||||||
catalog = CATALOG.zh_TW
|
|
||||||
elseif not lang:match("^zh") then
|
|
||||||
catalog = CATALOG.en
|
|
||||||
end
|
|
||||||
return catalog and catalog[msgid] or GetText(msgid)
|
|
||||||
end
|
|
||||||
|
|
||||||
local Update = {}
|
|
||||||
|
|
||||||
-- 按包名分状态:各书源互不影响,各自只自动检查一次、同一版本只弹一次
|
|
||||||
local function shared()
|
|
||||||
local all = rawget(_G, "__moonsource_update")
|
|
||||||
if not all then
|
|
||||||
all = {}
|
|
||||||
rawset(_G, "__moonsource_update", all)
|
|
||||||
end
|
|
||||||
all[PKG] = all[PKG] or {}
|
|
||||||
return all[PKG]
|
|
||||||
end
|
|
||||||
|
|
||||||
local function pluginsDir()
|
|
||||||
local root = debug.getinfo(1, "S").source:match("^@(.*)/[^/]+/[^/]+$")
|
|
||||||
return root and root:match("^(.*)/[^/]+$")
|
|
||||||
end
|
|
||||||
|
|
||||||
---@return string
|
|
||||||
function Update.version()
|
|
||||||
return require(PKG .. ".version")
|
|
||||||
end
|
|
||||||
|
|
||||||
local function sourceId()
|
|
||||||
return require(PKG .. "._meta").id
|
|
||||||
end
|
|
||||||
|
|
||||||
--- 数字段依次比较;同数字时预发布比正式版旧;已装版本解析不出(开发版)一律视为更旧。
|
|
||||||
---@param candidate string
|
|
||||||
---@param installed string
|
|
||||||
---@return boolean
|
|
||||||
function Update.newer(candidate, installed)
|
|
||||||
local function parts(version)
|
|
||||||
local out = {}
|
|
||||||
for n in version:gmatch("%d+") do out[#out + 1] = tonumber(n) end
|
|
||||||
return out
|
|
||||||
end
|
|
||||||
local left, right = parts(candidate), parts(installed)
|
|
||||||
if #right == 0 or installed:find("dev", 1, true) then return true end
|
|
||||||
for i = 1, 3 do
|
|
||||||
local l, r = left[i] or 0, right[i] or 0
|
|
||||||
if l ~= r then return l > r end
|
|
||||||
end
|
|
||||||
return installed:find("%-") ~= nil and candidate:find("%-") == nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local function parseSums(body)
|
|
||||||
local sums = {}
|
|
||||||
for hash, name in tostring(body):gmatch("(%x+)%s+%*?(%S+)") do
|
|
||||||
if #hash == 64 then sums[name] = hash:lower() end
|
|
||||||
end
|
|
||||||
return sums
|
|
||||||
end
|
|
||||||
|
|
||||||
---@return table|nil release, string|nil err
|
|
||||||
local function parse(body)
|
|
||||||
local ok, data = pcall(require("json").decode, body)
|
|
||||||
if not ok or type(data) ~= "table" or data.draft or data.prerelease then
|
|
||||||
return nil, "invalid release response"
|
|
||||||
end
|
|
||||||
local tag = data.tag_name
|
|
||||||
local version = type(tag) == "string" and tag:match("^v?(%d+%.%d+%.%d+[%w%.%+%-]*)$")
|
|
||||||
if not version then return nil, "invalid release version" end
|
|
||||||
local base = SITE .. "/" .. REPO .. "/releases/download/" .. tag .. "/"
|
|
||||||
local assets, sums_url = {}, nil
|
|
||||||
for _idx, asset in ipairs(type(data.assets) == "table" and data.assets or {}) do
|
|
||||||
if type(asset) == "table" and type(asset.name) == "string" and asset.browser_download_url == base .. asset.name then
|
|
||||||
if asset.name == "SHA256SUMS" then
|
|
||||||
sums_url = asset.browser_download_url
|
|
||||||
else
|
|
||||||
assets[asset.name] = { url = asset.browser_download_url, size = tonumber(asset.size) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if not sums_url then return nil, "release has no plugin checksum" end
|
|
||||||
local notes = type(data.body) == "string" and require("utils.text").trim(data.body) or ""
|
|
||||||
return { version = version, assets = assets, sums_url = sums_url, notes = notes ~= "" and notes or nil }
|
|
||||||
end
|
|
||||||
|
|
||||||
local job
|
|
||||||
|
|
||||||
--- 发起一个异步请求并登记为在途;回调若同步触发,不会把已结束的请求挂回去占着「忙」。
|
|
||||||
---@param start fun(wrap: fun(cb: function): function): any
|
|
||||||
local function launch(start)
|
|
||||||
local settled = false
|
|
||||||
local function wrap(cb)
|
|
||||||
return function(...)
|
|
||||||
settled = true
|
|
||||||
job = nil
|
|
||||||
return cb(...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local handle = start(wrap)
|
|
||||||
if not settled then job = handle end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- 查询最新稳定版,并配好本插件的安装包 release.target = { dir, file, url, size, sha256 }。
|
|
||||||
---@param cb fun(release: table|nil, err: any)
|
|
||||||
function Update.check(cb)
|
|
||||||
if job then return cb(nil, "busy") end
|
|
||||||
local Request = require("http.request")
|
|
||||||
local timeout = require("http.timeout").net(1)
|
|
||||||
local function finish(release, sums_body)
|
|
||||||
local sums = parseSums(sums_body)
|
|
||||||
local file = DIR .. "-" .. release.version .. ".zip"
|
|
||||||
local asset = release.assets[file]
|
|
||||||
if not (asset and sums[file]) then return cb(nil, "release has no plugin archive") end
|
|
||||||
release.target = { dir = DIR, file = file, url = asset.url, size = asset.size, sha256 = sums[file] }
|
|
||||||
release.installed = Update.version()
|
|
||||||
release.available = Update.newer(release.version, release.installed)
|
|
||||||
local settings = require("utils.settings")
|
|
||||||
local cfg = settings.getSource(sourceId())
|
|
||||||
cfg.update_last_checked_at = os.time()
|
|
||||||
settings.saveSource(sourceId(), cfg)
|
|
||||||
cb(release)
|
|
||||||
end
|
|
||||||
launch(function(wrap)
|
|
||||||
return Request.get(API, { timeout = timeout, headers = { Accept = "application/json" } }, wrap(function(body, err)
|
|
||||||
if err then return cb(nil, err) end
|
|
||||||
local release, perr = parse(body)
|
|
||||||
if not release then return cb(nil, perr) end
|
|
||||||
launch(function(wrap_sums)
|
|
||||||
return Request.get(release.sums_url, { timeout = timeout }, wrap_sums(function(sums_body, sums_err)
|
|
||||||
if sums_err then return cb(nil, sums_err) end
|
|
||||||
finish(release, sums_body)
|
|
||||||
end))
|
|
||||||
end)
|
|
||||||
end))
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
--- 下载、校验并换掉已装的本插件目录。
|
|
||||||
---@param release table Update.check 的结果
|
|
||||||
---@param cb fun(ok: boolean, err: any)
|
|
||||||
---@param on_progress fun(bytes: integer|nil, target: table)|nil
|
|
||||||
function Update.install(release, cb, on_progress)
|
|
||||||
if job then return cb(false, "busy") end
|
|
||||||
local Request, Job = require("http.request"), require("workers.job")
|
|
||||||
local Paths, Install = require("utils.paths"), require("utils.plugin_install")
|
|
||||||
local target = release.target
|
|
||||||
Paths.ensureDir(Paths.root())
|
|
||||||
local archive = Paths.root() .. "/" .. target.dir .. ".update.zip"
|
|
||||||
os.remove(archive)
|
|
||||||
launch(function(wrap)
|
|
||||||
return Request.download({
|
|
||||||
url = target.url,
|
|
||||||
timeout = require("http.timeout").net(10),
|
|
||||||
allow_redirects = true,
|
|
||||||
max_bytes = MAX_ARCHIVE_BYTES,
|
|
||||||
on_progress = function(bytes)
|
|
||||||
if on_progress then on_progress(bytes, target) end
|
|
||||||
end,
|
|
||||||
}, archive, wrap(function(ok, err)
|
|
||||||
if not ok then
|
|
||||||
os.remove(archive)
|
|
||||||
return cb(false, err)
|
|
||||||
end
|
|
||||||
launch(function(wrap_job)
|
|
||||||
local function settle(failure)
|
|
||||||
return wrap_job(function()
|
|
||||||
os.remove(archive)
|
|
||||||
if failure then return cb(false, failure) end
|
|
||||||
cb(true)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
return Job.run(function()
|
|
||||||
local installed, detail = Install.run(archive, pluginsDir(), target.sha256, { name = target.dir })
|
|
||||||
if not installed then error(detail, 0) end
|
|
||||||
end, {
|
|
||||||
name = "moonsource.update",
|
|
||||||
kind = "heavy",
|
|
||||||
timeout = 120,
|
|
||||||
on_done = settle(),
|
|
||||||
on_failed = function(install_err) settle(install_err)() end,
|
|
||||||
})
|
|
||||||
end)
|
|
||||||
end))
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function errorText(err)
|
|
||||||
local text = tostring(err)
|
|
||||||
local known = {
|
|
||||||
{ "^busy$", _("正在检查或安装更新,请稍候") },
|
|
||||||
{ "^invalid release", _("更新信息无效,请稍后再试") },
|
|
||||||
{ "^release has no", _("新版本缺少插件包,请稍后再试") },
|
|
||||||
{ "update checksum mismatch", _("更新包校验失败,请稍后再试") },
|
|
||||||
{ "download too large", _("更新包过大,已中止下载") },
|
|
||||||
}
|
|
||||||
for _idx, entry in ipairs(known) do
|
|
||||||
if text:find(entry[1]) then return entry[2] end
|
|
||||||
end
|
|
||||||
return text:match("[^\n]*")
|
|
||||||
end
|
|
||||||
|
|
||||||
local function startInstall(release)
|
|
||||||
local ProgressDialog = require("ui.themes.widgets.progress_dialog")
|
|
||||||
local dialog = ProgressDialog:new{ title = _("更新书源插件"), text = _("正在下载…"), dismissable = false }
|
|
||||||
dialog:show()
|
|
||||||
Update.install(release, function(ok, err)
|
|
||||||
if ok then
|
|
||||||
dialog:set(_("更新完成,正在重启 KOReader…"), 100)
|
|
||||||
local UIManager = require("ui/uimanager")
|
|
||||||
UIManager:nextTick(function()
|
|
||||||
UIManager:broadcastEvent(require("ui/event"):new("Restart"))
|
|
||||||
end)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
dialog:close()
|
|
||||||
require("ui.themes.widgets.toast"):new{ text = T(_("书源插件更新失败:%1"), errorText(err)), timeout = 5 }:show()
|
|
||||||
end, function(bytes, target)
|
|
||||||
local fraction = target.size and target.size > 0 and bytes and math.min(1, bytes / target.size) or 0
|
|
||||||
dialog:set(_("正在下载…"), math.floor(fraction * 100))
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
--- 弹更新说明与安装确认。同版本只提示一次,force 跳过这层记忆。
|
|
||||||
---@param release table
|
|
||||||
---@param force boolean|nil
|
|
||||||
function Update.prompt(release, force)
|
|
||||||
local state = shared()
|
|
||||||
if not force and state.offered == release.version then return end
|
|
||||||
state.offered = release.version
|
|
||||||
local name = require(PKG .. "._meta").name
|
|
||||||
local header = T(_("发现「%1」新版本 %2(当前 %3),更新后重启 KOReader。"), name, release.version, release.installed)
|
|
||||||
if force and not release.available then
|
|
||||||
header = T(_("「%1」已是最新(%2)。重新下载并替换已安装的版本?"), name, release.version)
|
|
||||||
end
|
|
||||||
local dialog
|
|
||||||
dialog = require("ui.themes.widgets.text_viewer"):new{
|
|
||||||
title = _("书源插件更新"),
|
|
||||||
text = release.notes and (header .. "\n\n" .. release.notes) or header,
|
|
||||||
buttons = {
|
|
||||||
{ text = _("稍后"), callback = function() dialog:close() end },
|
|
||||||
{
|
|
||||||
text = _("下载并安装"),
|
|
||||||
variant = "primary",
|
|
||||||
callback = function()
|
|
||||||
dialog:close()
|
|
||||||
startInstall(release)
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
dialog:show()
|
|
||||||
end
|
|
||||||
|
|
||||||
--- 用户主动检查,始终给结果。
|
|
||||||
---@param force boolean|nil 为真时已是最新也提示重装
|
|
||||||
function Update.manualCheck(force)
|
|
||||||
local Toast = require("ui.themes.widgets.toast")
|
|
||||||
local loading = require("ui.themes.widgets.progress_dialog"):new{
|
|
||||||
title = _("检查更新"), text = _("正在检查书源插件更新…"), dismissable = false,
|
|
||||||
}
|
|
||||||
loading:show()
|
|
||||||
Update.check(function(release, err)
|
|
||||||
loading:close()
|
|
||||||
if err then
|
|
||||||
Toast:new{ text = T(_("检查更新失败:%1"), errorText(err)), timeout = 4 }:show()
|
|
||||||
elseif force or release.available then
|
|
||||||
shared().offered = nil
|
|
||||||
Update.prompt(release, force)
|
|
||||||
else
|
|
||||||
Toast:new{ text = T(_("已是最新版本(%1)"), release.installed), timeout = 3 }:show()
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function autoEnabled()
|
|
||||||
return require("utils.settings").getSource(sourceId()).update_auto_check ~= false
|
|
||||||
end
|
|
||||||
|
|
||||||
--- 按 24 小时间隔静默检查;只提示,不自动安装。
|
|
||||||
function Update.autoCheck()
|
|
||||||
local state = shared()
|
|
||||||
if state.checked or not autoEnabled() then return end
|
|
||||||
local last = tonumber(require("utils.settings").getSource(sourceId()).update_last_checked_at) or 0
|
|
||||||
if os.time() - last < CHECK_INTERVAL then return end
|
|
||||||
if not require("ui/network/manager"):isConnected() then return end
|
|
||||||
state.checked = true
|
|
||||||
Update.check(function(release)
|
|
||||||
if release and release.available and autoEnabled() then Update.prompt(release) end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
--- 桌面回到前台时自动检查。
|
|
||||||
function Update.register()
|
|
||||||
require("utils.extension").add("desktop.activated", function() Update.autoCheck() end)
|
|
||||||
end
|
|
||||||
|
|
||||||
--- 设置页「书库 → 更新」:每个书源各登记一项,只管自己。
|
|
||||||
---@param desktop table
|
|
||||||
---@return table[]
|
|
||||||
function Update.entries(desktop)
|
|
||||||
local name = require(PKG .. "._meta").name
|
|
||||||
local Row = require("ui.themes.widgets.row")
|
|
||||||
return {
|
|
||||||
{
|
|
||||||
group = "library", order = 40, id = "moonsource_update_" .. sourceId(), icon = "system_update",
|
|
||||||
title = T(_("%1 更新"), name),
|
|
||||||
subtitle = _("书源插件检查更新与自动更新"),
|
|
||||||
status = Update.version(),
|
|
||||||
page = {
|
|
||||||
title = T(_("%1 更新"), name),
|
|
||||||
sections = function()
|
|
||||||
return {{
|
|
||||||
title = _("书源插件"),
|
|
||||||
rows = {
|
|
||||||
function(iw)
|
|
||||||
return Row.widget({
|
|
||||||
width = iw, chevron = true, kind = "nav", icon = "system_update",
|
|
||||||
title = _("检查书源插件更新"),
|
|
||||||
subtitle = _("只更新本书源,其它书源各自独立更新"),
|
|
||||||
status = Update.version(),
|
|
||||||
callback = function() Update.manualCheck() end,
|
|
||||||
})
|
|
||||||
end,
|
|
||||||
function(iw)
|
|
||||||
local on = autoEnabled()
|
|
||||||
return Row.widget({
|
|
||||||
width = iw, chevron = false, kind = "toggle", icon = "autorenew",
|
|
||||||
title = _("自动检查书源插件更新"),
|
|
||||||
status = on and _("开") or _("关"), status_on = on,
|
|
||||||
callback = function()
|
|
||||||
local settings = require("utils.settings")
|
|
||||||
local cfg = settings.getSource(sourceId())
|
|
||||||
cfg.update_auto_check = not on
|
|
||||||
settings.saveSource(sourceId(), cfg)
|
|
||||||
desktop:updateView()
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
return Update
|
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
--[[--
|
||||||
|
哔哩漫画账号校验,以及已解锁话不被本机标成未购买。
|
||||||
|
|
||||||
|
@module tests.moonsource_bilimanga.account_spec
|
||||||
|
--]]
|
||||||
|
|
||||||
|
local Assert = require("support.assert")
|
||||||
|
local Stubs = require("support.stubs")
|
||||||
|
local Json = require("support.json_stub")
|
||||||
|
|
||||||
|
local Mapper = require("moonsource_bilimanga.mapper")
|
||||||
|
local chapters = Mapper.chapters({ ep_list = {
|
||||||
|
{ id = 1, title = "已解锁", is_locked = false, pay_type = 1, is_pay = true },
|
||||||
|
{ id = 2, title = "未解锁", is_locked = true, pay_type = 0 },
|
||||||
|
} })
|
||||||
|
Assert.eq(chapters[1].vip, false)
|
||||||
|
Assert.eq(chapters[2].vip, true)
|
||||||
|
|
||||||
|
local fetched = false
|
||||||
|
package.loaded["moonsource_bilimanga.client"] = {
|
||||||
|
imageIndexAsync = function() fetched = true end,
|
||||||
|
}
|
||||||
|
local Chapter = require("moonsource_bilimanga.chapter")
|
||||||
|
local reason
|
||||||
|
Chapter.materializeAsync(
|
||||||
|
{ source_id = "bilimanga", stable_id = "1" },
|
||||||
|
{ uid = "9", vip = true },
|
||||||
|
nil,
|
||||||
|
function(_path, err) reason = err end)
|
||||||
|
Stubs.flush()
|
||||||
|
Assert.eq(reason, "本章未购买")
|
||||||
|
Assert.is_false(fetched)
|
||||||
|
|
||||||
|
package.loaded["moonsource_bilimanga.client"] = nil
|
||||||
|
package.loaded["moonsource_bilimanga.chapter"] = nil
|
||||||
|
package.loaded["json"] = { encode = Json.encode, decode = Json.decode }
|
||||||
|
|
||||||
|
local sent
|
||||||
|
local reply
|
||||||
|
package.loaded["http.request"] = {
|
||||||
|
ok = function(code) return code and code >= 200 and code < 300 end,
|
||||||
|
request = function(opts, cb)
|
||||||
|
sent = opts
|
||||||
|
local job = { cancel = function() reply = nil end }
|
||||||
|
if reply then cb(reply.body, reply.err) end
|
||||||
|
return job
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
local Client = require("moonsource_bilimanga.client")
|
||||||
|
|
||||||
|
local function check(body)
|
||||||
|
reply = { body = { code = 200, body = body } }
|
||||||
|
local ok, err, account
|
||||||
|
Client.loginCheckAsync("SESSDATA=fixture", function(a, b, c)
|
||||||
|
ok, err, account = a, b, c
|
||||||
|
end)
|
||||||
|
return ok, err, account
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok, err = check(Json.encode({ code = -101, data = { isLogin = false } }))
|
||||||
|
Assert.eq(ok, false)
|
||||||
|
Assert.eq(err, "登录校验失败,请确认已经登录哔哩哔哩")
|
||||||
|
Assert.eq(sent.headers.Cookie, "SESSDATA=fixture")
|
||||||
|
Assert.eq(sent.url, "https://api.bilibili.com/x/web-interface/nav")
|
||||||
|
|
||||||
|
ok, err, account = check(Json.encode({ code = 0, data = { isLogin = true, mid = 42 } }))
|
||||||
|
Assert.eq(ok, true)
|
||||||
|
Assert.is_nil(err)
|
||||||
|
Assert.eq(account.id, "42")
|
||||||
|
|
||||||
|
local empty_ok, empty_err
|
||||||
|
Client.loginCheckAsync("", function(a, b) empty_ok, empty_err = a, b end)
|
||||||
|
Assert.eq(empty_ok, false)
|
||||||
|
Assert.eq(empty_err, "没有读到 Cookie")
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
--[[--
|
||||||
|
书架列表和书城分类的字段形状。
|
||||||
|
|
||||||
|
@module tests.moonsource_bilimanga.store_spec
|
||||||
|
--]]
|
||||||
|
|
||||||
|
local Assert = require("support.assert")
|
||||||
|
local Mapper = require("moonsource_bilimanga.mapper")
|
||||||
|
|
||||||
|
local book = Mapper.book({ season_id = 25717, title = "示例", vertical_cover = "http://cover" })
|
||||||
|
Assert.eq(book.stable_id, "25717")
|
||||||
|
Assert.eq(book.cover, "http://cover")
|
||||||
|
|
||||||
|
local shelf = Mapper.rows({ code = 0, data = {
|
||||||
|
{ comic_id = 1, title = "一" },
|
||||||
|
{ comic_id = 2, title = "二" },
|
||||||
|
} })
|
||||||
|
Assert.eq(#shelf, 2)
|
||||||
|
Assert.is_nil(Mapper.rows({ code = 0, msg = "坏了" }))
|
||||||
|
|
||||||
|
local labels = Mapper.labels({ data = {
|
||||||
|
styles = { { id = 999, name = "热血" } },
|
||||||
|
orders = { { id = 0, name = "人气推荐" }, { id = 1, name = "更新时间" } },
|
||||||
|
} })
|
||||||
|
local order, style = Mapper.storeChoice(nil, labels)
|
||||||
|
Assert.eq(order, 0)
|
||||||
|
Assert.eq(style, -1)
|
||||||
|
order, style = Mapper.storeChoice("1:999", labels)
|
||||||
|
Assert.eq(order, 1)
|
||||||
|
Assert.eq(style, 999)
|
||||||
|
order, style = Mapper.storeChoice("9:8", labels)
|
||||||
|
Assert.eq(order, 0)
|
||||||
|
Assert.eq(style, -1)
|
||||||
|
|
||||||
|
local groups = Mapper.storeGroups(labels, 0, 999)
|
||||||
|
Assert.eq(#groups, 2)
|
||||||
|
Assert.eq(groups[1].chips[2].id, "1:999")
|
||||||
|
Assert.eq(groups[2].chips[1].id, "0:-1")
|
||||||
|
Assert.eq(groups[2].chips[2].id, "0:999")
|
||||||
|
|
||||||
|
local books = Mapper.books({ { season_id = 29040, title = "分类" } })
|
||||||
|
Assert.eq(#books, 1)
|
||||||
|
Assert.eq(books[1].stable_id, "29040")
|
||||||
|
Assert.is_nil(Mapper.books({ { title = "没有身份" } }))
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
--[[-- 书源插件在线更新:版本比较、只配本插件的包、校验下载地址、设置页追加更新行。本文件由模板仓库同步,勿手改。 @module tests.update.update_spec --]]
|
|
||||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
|
||||||
|
|
||||||
package.preload["json"] = function() return require("support.json_stub") end
|
|
||||||
local Assert = require("support.assert")
|
|
||||||
local Json = require("support.json_stub")
|
|
||||||
|
|
||||||
local PKG = "moonsource_bilimanga"
|
|
||||||
local DIR = "moonsource-bilimanga.koplugin"
|
|
||||||
local REPO = "moon-source/" .. DIR:gsub("%.koplugin$", "")
|
|
||||||
|
|
||||||
local cfg = {}
|
|
||||||
package.preload["utils.settings"] = function()
|
|
||||||
return { getSource = function() return cfg end, saveSource = function() end }
|
|
||||||
end
|
|
||||||
package.preload["utils.text"] = function()
|
|
||||||
return { trim = function(s) return (s:gsub("^%s+", ""):gsub("%s+$", "")) end }
|
|
||||||
end
|
|
||||||
package.preload["http.timeout"] = function() return { net = function() return 5 end } end
|
|
||||||
|
|
||||||
local BASE = "https://git.garfieldho.me/" .. REPO .. "/releases/download/v1.2.3/"
|
|
||||||
local zip = DIR .. "-1.2.3.zip"
|
|
||||||
local other = "moonsource-other.koplugin-1.2.3.zip"
|
|
||||||
local release = {
|
|
||||||
tag_name = "v1.2.3", draft = false, prerelease = false, body = " 更新说明 ",
|
|
||||||
assets = {
|
|
||||||
{ name = "SHA256SUMS", size = 10, browser_download_url = BASE .. "SHA256SUMS" },
|
|
||||||
{ name = zip, size = 100, browser_download_url = BASE .. zip },
|
|
||||||
{ name = other, size = 200, browser_download_url = "https://evil.example/x.zip" },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
local sums = ("a"):rep(64) .. " " .. zip .. "\n" .. ("b"):rep(64) .. " " .. other .. "\n"
|
|
||||||
local requested = {}
|
|
||||||
package.preload["http.request"] = function()
|
|
||||||
return {
|
|
||||||
get = function(url, _opts, cb)
|
|
||||||
requested[#requested + 1] = url
|
|
||||||
if url:find("/releases/latest", 1, true) then cb(Json.encode(release)) else cb(sums) end
|
|
||||||
return { cancel = function() end }
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
local Update = require(PKG .. ".update")
|
|
||||||
|
|
||||||
-- 版本比较
|
|
||||||
Assert.is_true(Update.newer("0.6.1", "0.6.0"))
|
|
||||||
Assert.is_false(Update.newer("0.6.0", "0.6.0"))
|
|
||||||
Assert.is_false(Update.newer("0.5.9", "0.6.0"))
|
|
||||||
Assert.is_true(Update.newer("1.0.0", "1.0.0-rc.1"))
|
|
||||||
Assert.is_true(Update.newer("0.0.1", "0.0.0-dev"))
|
|
||||||
|
|
||||||
-- 只查自己仓库的 Release,只配自己的包
|
|
||||||
local got, err
|
|
||||||
Update.check(function(r, e) got, err = r, e end)
|
|
||||||
Assert.is_nil(err)
|
|
||||||
Assert.eq(requested[1], "https://git.garfieldho.me/api/v1/repos/" .. REPO .. "/releases/latest")
|
|
||||||
Assert.eq(got.version, "1.2.3")
|
|
||||||
Assert.eq(got.notes, "更新说明")
|
|
||||||
Assert.is_true(got.available)
|
|
||||||
Assert.eq(got.target.dir, DIR)
|
|
||||||
Assert.eq(got.target.sha256, ("a"):rep(64))
|
|
||||||
Assert.is_true(cfg.update_last_checked_at ~= nil)
|
|
||||||
|
|
||||||
-- 下载地址不在本仓库 Release 下的资产被拒
|
|
||||||
release.assets[2].browser_download_url = "https://evil.example/x.zip"
|
|
||||||
local rejected
|
|
||||||
Update.check(function(_r, e) rejected = e end)
|
|
||||||
Assert.matches(rejected, "no plugin archive")
|
|
||||||
release.assets[2].browser_download_url = BASE .. zip
|
|
||||||
|
|
||||||
-- 没有 SHA256SUMS 的 Release 无效
|
|
||||||
release.assets[1] = nil
|
|
||||||
local bad_err
|
|
||||||
Update.check(function(_r, e) bad_err = e end)
|
|
||||||
Assert.matches(bad_err, "no plugin checksum")
|
|
||||||
|
|
||||||
-- 设置页「书库 → 更新」每个书源登记自己的一项
|
|
||||||
package.preload["ui.themes.widgets.row"] = function() return { widget = function(spec) return spec end } end
|
|
||||||
local entries = Update.entries({ updateView = function() end })
|
|
||||||
Assert.len(entries, 1)
|
|
||||||
Assert.eq(entries[1].group, "library")
|
|
||||||
Assert.matches(entries[1].id, "^moonsource_update_")
|
|
||||||
Assert.len(entries[1].page.sections()[1].rows, 2)
|
|
||||||
Reference in new issue
Block a user