🚚 (split): 从月读拆出独立仓库,沿用原书源 id
Release / release (push) Successful in 10s

This commit is contained in:
Garfield committed 2026-11-27 08:08:02 +08:00
commit aae155c43f
23 files changed
+1912

No files matched your search

@@ -0,0 +1,15 @@
--[[--
爱看书元信息。
@module moonsource_18ta._meta
--]]
local _ = require("l10n").get("moonsource_18ta")
return {
id = "18ta",
name = _("爱看书"),
type = "chapter",
description = _("爱看书分类书库与按章在线阅读"),
abilities = { _("分类书库"), _("完本"), _("在线阅读"), _("整本缓存") },
}
@@ -0,0 +1,118 @@
--[[--
18ta 匿名 HTTP 客户端(只走 http.request)。
站点 WAF 对首个请求回 403 并下发会话 Cookie(Set-Cookie),带上 Cookie 后放行;
Cookie 随每个响应刷新。站内搜索维护中,不接;只有分类书库、书籍详情、章节正文。
@module moonsource_18ta.client
--]]
local Request = require("http.request")
local Mapper = require("moonsource_18ta.mapper")
local _ = require("l10n").get("moonsource_18ta")
local BASE = "https://www.18ta.com"
local TIMEOUT = require("http.timeout").net(1)
local UA = "Mozilla/5.0"
---@class T18taClient
---@field jar CookieJar 会话 Cookie(WAF 下发,随响应刷新)
local Client = {}
Client.__index = Client
---@return T18taClient
function Client.new()
return setmetatable({ jar = require("http.cookie_jar").new() }, Client)
end
--- GET(WAF 兜底:403 且拿到 Cookie 时重发一次)。
---@param path string 站内路径(/ 开头)或完整 URL
---@param cb fun(body: string|nil, err: string|nil, res: table|nil)
---@return { cancel: fun() }
function Client:get(path, cb)
path = path:gsub("^https://www%.18ta%.com/", "/")
if not path:match("^/[^/]") then
return require("source.base").defer(cb, nil, _("响应无效"))
end
local url = BASE .. path
local job, cancelled, finished = nil, false, false
local function finish(body, err, res)
if cancelled or finished then return end
finished = true
cb(body, err, res)
end
local function request(retry)
local pending, seq = true, self.jar.seq
local inner = Request.get(url, {
headers = { ["User-Agent"] = UA }, jar = self.jar, timeout = TIMEOUT,
}, function(body, err, res)
pending = false
if cancelled or finished then return end
job = nil
-- 回调前 Request 已把 Set-Cookie 收进罐:seq 变了就是拿到了新 Cookie。
if not body and res and res.code == 403 and self.jar.seq ~= seq and not retry then
request(true)
return
end
finish(body, err, res)
end)
if pending then job = inner end
end
request(false)
return {
cancel = function()
cancelled = true
if job then job.cancel() end
end,
}
end
--- 分类书库一页:/sort/<cat>/<page>.html
---@param node string
---@param page integer
---@param cb fun(body: string|nil, err: string|nil)
---@return { cancel: fun() }
function Client:listAsync(node, page, cb)
return self:get(Mapper.storePage(node, page), cb)
end
--- 书籍详情页(含目录):/akshtml<id>.html
---@param book_id string
---@param cb fun(body: string|nil, err: string|nil)
---@return { cancel: fun() }
function Client:bookAsync(book_id, cb)
return self:get("/akshtml" .. book_id .. ".html", cb)
end
--- 一章可能拆成多页;只在同章所有页成功后交付正文。path 取自目录(/aks/<group>/<id>/<ch>.html)或完整 URL。
---@param path string
---@param cb fun(html: string|nil, err: string|nil)
---@return { cancel: fun() }
function Client:contentAsync(path, cb)
local handle, parts = {}, {}
local function fetch(current)
local pending = true
local inner = self:get(current, function(html, err)
pending = false
if handle.cancelled then return end
handle.inner = nil
if not html then cb(nil, err); return end
local body = Mapper.chapterContent(html)
if not body then cb(nil, _("阅读章节内容为空")); return end
local next_path, next_err = Mapper.chapterNext(html, current)
if next_err then cb(nil, next_err); return end
parts[#parts + 1] = body
if next_path then fetch(next_path); return end
cb(table.concat(parts, "\n"))
end)
if pending then handle.inner = inner end
end
fetch(path)
function handle.cancel()
handle.cancelled = true
if handle.inner then handle.inner.cancel() end
end
return handle
end
return Client
@@ -0,0 +1,11 @@
return {
["在线阅读"] = "Online reading",
["整本缓存"] = "Full-book caching",
["分类书库"] = "Category library",
["完本"] = "Completed",
["章节目录保存失败"] = "Failed to save chapter list",
["阅读章节内容为空"] = "Chapter content is empty",
["爱看书"] = "Aikanshu",
["爱看书分类书库与按章在线阅读"] = "Aikanshu catalog and online chapter reading",
["无法解析书籍信息"] = "Unable to parse book information",
}
@@ -0,0 +1,11 @@
return {
["在线阅读"] = "線上閱讀",
["整本缓存"] = "整本快取",
["分类书库"] = "分類書庫",
["完本"] = "完結",
["章节目录保存失败"] = "章節目錄儲存失敗",
["阅读章节内容为空"] = "閱讀章節內容為空",
["爱看书"] = "愛看書",
["爱看书分类书库与按章在线阅读"] = "愛看書分類書庫與按章線上閱讀",
["无法解析书籍信息"] = "無法解析書籍資訊",
}
@@ -0,0 +1,5 @@
return function()
local copy = {}
for k, v in pairs(require("moonsource_18ta._meta")) do copy[k] = v end
return copy
end
@@ -0,0 +1,186 @@
--[[--
18ta 页面解析:纯函数,无网络。
书目卡片是 /sort/<cat>/<page>.html 的 `<li>…</li>` 块;书页用 og:novel meta、.intro 简介与 og:image 封面;
目录在 <ul id="ul_all_chapters">(章序升序;页首「最新章节」块是另一个 <ul>,不在其内)。
章节正文在 <article id="article"> 内,纯 <p> 段落,无图无脚本。
@module moonsource_18ta.mapper
--]]
local Text = require("utils.text")
local _ = require("l10n").get("moonsource_18ta")
local BASE = "https://www.18ta.com"
local Mapper = {}
Mapper.CONTENT_MARK = "moon:18ta:complete"
---@param node string|nil
---@return string|nil
function Mapper.storeNode(node)
if type(node) ~= "string" then return nil end
local path = node:gsub("^/quanben/", "/")
if path == "/sort/" or path:match("^/sort/%d+/1%.html$") then return node end
end
---@param node string
---@param page integer
---@return string
function Mapper.storePage(node, page)
if node:sub(-1) == "/" then return node .. page .. ".html" end
return (node:gsub("/1%.html$", "/" .. page .. ".html"))
end
--- 分类来自官网筛选区;完本首页的分页链接漏掉 quanben,仍按选中频道构造请求。
---@param html string
---@param node string
---@return StoreChipGroup[]|nil
function Mapper.storeGroups(html, node)
local filters = html:match('<div id="after_menu">(.-)</div>%s*</div>')
if not filters then return nil end
local completed = node:find("^/quanben/") ~= nil
local category = node:gsub("^/quanben/", "/")
local chips, seen = {}, {}
for href, title in filters:gmatch('<a[^>]-href="([^"]+)"[^>]*>(.-)</a>') do
local path = href:gsub("^/quanben/", "/")
if Mapper.storeNode(path) and not seen[path] then
seen[path] = true
chips[#chips + 1] = {
id = completed and "/quanben" .. path or path,
title = _(Text.trim(Text.stripHtml(title))),
}
end
end
if #chips == 0 or not seen[category] then return nil end
return {
{ title = _("分类"), chips = chips, active = node },
{ title = _("状态"), chips = {
{ id = category, title = _("全部") },
{ id = "/quanben" .. category, title = _("完本") },
}, active = node },
}
end
---@param html string
---@param node string
---@param page integer
---@return boolean
function Mapper.storeMore(html, node, page)
local pager = html:match('<div[^>]-id="pagelink"[^>]*>(.-)</div>')
if not pager then return false end
local next_path = Mapper.storePage(node, page + 1):gsub("^/quanben/", "/")
for href in pager:gmatch('href="([^"]+)"') do
if href:gsub("^/quanben/", "/") == next_path then return true end
end
return false
end
--- 分类书库一页。
---@param html string
---@return Book[]|nil books
function Mapper.list(html)
if not html:find('<ul class="flex">', 1, true) then return nil end
local books = {}
for card in html:gmatch('<div class="img_span">.-</li>') do
local id = card:match('href="/akshtml(%d+)%.html"')
if id then
local title = card:match("<h2>(.-)</h2>")
if not title then return nil end
local author = card:match('fa%-user%-circle%-o">([^<]*)</i>')
local intro = card:match('<p class="indent">(.-)</p>')
local cover = card:match('data%-original="([^"]+)"')
local cat, status = card:match("<span>(.-)%s*/%s*(.-)</span>")
books[#books + 1] = {
source_id = "18ta",
stable_id = id,
title = Text.trim(Text.xmlDecode(title)),
authors = author and Text.trim(Text.stripHtml(author)) or "",
category = cat and Text.trim(cat) or nil,
intro = intro and Text.trim(Text.stripHtml(intro)) or "",
cover = cover and Text.absoluteUrl(BASE, cover) or nil,
meta = { status = status and Text.trim(status) or nil },
}
else return nil end
end
return books
end
--- 书籍详情页。source_id / stable_id 由调用方带上。
---@param html string
---@return table|nil
function Mapper.book(html)
local function og(prop)
local v = html:match('<meta property="og:novel:' .. prop .. '" content="([^"]*)"')
if not v or v == "" or v == "None" then return nil end
return Text.xmlDecode(v)
end
local title = og("book_name")
if not title then return nil end
local intro = html:match('<div class="intro">(.-)</div>')
local cover = html:match('<meta property="og:image" content="([^"]*)"')
return {
title = title,
authors = og("author") or "",
category = og("category"),
intro = intro and Text.trim(Text.stripHtml(intro)) or "",
cover = cover and Text.absoluteUrl(BASE, cover) or nil,
meta = { status = og("status") },
}
end
--- 目录(章序升序)。
---@param html string
---@return table[] toc
function Mapper.toc(html)
local ul = html:match('<ul id="ul_all_chapters">(.-)</ul>')
if not ul then return {} end
local toc = {}
for href, text in ul:gmatch('<li><a href="([^"]+)" title="[^"]*">([^<]*)</a></li>') do
toc[#toc + 1] = {
idx = #toc + 1,
uid = href,
title = Text.trim(Text.xmlDecode(text)),
-- 客户端同时接受站内路径与旧绝对地址;不用为每章再造一条带相同长前缀的字符串。
origin = href,
}
end
return toc
end
--- 章节正文片段(<article> 内部)。
---@param html string
---@return string|nil
function Mapper.chapterContent(html)
local body = html:match('<article[^>]-id="article"[^>]*>(.-)</article>')
if not body then return nil end
body = body:gsub("<script[^>]*>.-</script>", ""):gsub("<style[^>]*>.-</style>", "")
if not body:find("<p", 1, true) or Text.trim(Text.stripHtml(body)) == "" then return nil end
return body
end
---@param html string
---@param current string
---@return string|nil next_path
---@return string|nil err
function Mapper.chapterNext(html, current)
local next_tag = html:match('<a[^>]-id="next_url"[^>]*>')
local href = next_tag and next_tag:match('href="([^"]+)"')
current = current:gsub("^https://www%.18ta%.com/", "/")
local stem, suffix = current:match("^(/aks/%d+/%d+/%d+)([^/]*)%.html$")
if not stem or not href then return nil, _("响应无效") end
local page = suffix == "" and 1 or tonumber(suffix:match("^_(%d+)$"))
if not page then return nil, _("响应无效") end
if href == stem .. "_" .. (page + 1) .. ".html" then return href end
local book_dir = stem:match("^(/aks/%d+/%d+/)")
local book_id = stem:match("^/aks/%d+/(%d+)/")
if href == "/akshtml" .. book_id .. ".html"
or (href:match("^/aks/%d+/%d+/%d+%.html$") and href:sub(1, #book_dir) == book_dir
and href ~= stem .. ".html") then
return nil
end
return nil, _("响应无效")
end
return Mapper
@@ -0,0 +1,173 @@
--[[--
18ta 书源:分类浏览 + 按章在线阅读。
站点搜索维护中,足迹只存浏览器本地;接分类与完本、详情、目录、分页正文与整本缓存。
@module koplugin.book.moonsource_18ta.source
--]]
local Browse = require("source.browse")
local Client = require("moonsource_18ta.client")
local Mapper = require("moonsource_18ta.mapper")
local SourceBase = require("source.base")
local Toc = require("source.toc")
local _ = require("l10n").get("moonsource_18ta")
local T18ta = {}
-- 接口按 IP 限流,逐章请求之间留间隔。
local FETCH_INTERVAL = 1
---@return BookSourceMeta
function T18ta.meta()
return require("moonsource_18ta.loadmeta")()
end
---@class T18taSource : SourceBase
---@field client T18taClient
local Source = setmetatable({}, { __index = SourceBase })
Source.__index = Source
---@return T18taSource
function T18ta.new()
local meta = T18ta.meta()
return setmetatable({ id = meta.id, name = meta.name, type = meta.type, client = Client.new() }, Source)
end
function Source:configured()
return true
end
function Source:capabilities()
return { scrape = false, edit = false, stats_pull = false, shelf_cache = false, shelf_push = false }
end
function Source:browseAsync(opts, cb)
opts = opts or {}
local node = opts.node
-- 兼容旧版保存的数字分类;新节点直接使用官网路径。
if node and node:match("^%d+$") then node = "/sort/" .. node .. "/1.html" end
node = Mapper.storeNode(node) or "/sort/"
local groups
return Browse.pages(function(page, page_cb)
return self.client:listAsync(node, page, function(body, err)
if not body then page_cb(nil, err) return end
local books = Mapper.list(body)
local filters = Mapper.storeGroups(body, node)
if not books or not filters then page_cb(nil, _("响应无效")); return end
groups = filters
page_cb(books, nil, Mapper.storeMore(body, node, page))
end)
end, opts.limit or 60, {}, function(result, err)
if result then result.groups = groups end
cb(result, err)
end)
end
function Source:getDetailAsync(identity, cb)
return self.client:bookAsync(identity.stable_id, function(html, err)
if not html then cb(nil, err) return end
local row = Mapper.book(html)
if not row then
cb(nil, _("无法解析书籍信息"))
return
end
local book = {
source_id = self.id,
stable_id = identity.stable_id,
title = row.title,
authors = row.authors,
category = row.category,
intro = row.intro,
cover = row.cover,
meta = row.meta,
}
cb(require("book.store").rememberDetail(book))
end)
end
function Source:loadTocAsync(identity, cb, force)
if force then return self:refreshTocAsync(identity, cb) end
local cancelled, active = false, nil
active = Toc.readAsync(self.id, identity.stable_id, function(cached, err)
if cancelled then return end
if cached and #cached > 0 then cb(cached); return end
if err then cb(nil, err); return end
active = self:refreshTocAsync(identity, cb)
end)
return { cancel = function()
cancelled = true
if active then active.cancel() end
end }
end
function Source:refreshTocAsync(identity, cb)
local cancelled, active, pending = false, nil, true
local request = self.client:bookAsync(identity.stable_id, function(html, err)
pending = false
if cancelled then return end
if not html then cb(nil, err) return end
active = Toc.parseAsync(self.id, identity.stable_id, function()
local toc = Mapper.toc(html)
if #toc == 0 then return nil, _("章节列表为空") end
return toc
end, cb)
end)
if pending then active = request end
return { cancel = function()
cancelled = true
if active then active.cancel() end
end }
end
---@return ChapterFetchContent
local function contentFetcher(self)
return function(_ref, ch, done)
return self.client:contentAsync(ch.origin, function(html, err)
if not html then done(nil, err) return end
done({ title = ch.title, html = html, mark = Mapper.CONTENT_MARK })
end)
end
end
--- 离线保留旧缓存;在线重取未标记的旧版单页正文,失败不覆盖它。
---@param path string
---@return boolean
local function reuse(path)
if not require("ui/network/manager"):isConnected() then return true end
local file = assert(io.open(path, "rb"))
local head, err = file:read(64)
file:close()
assert(head, err)
return head:find(Mapper.CONTENT_MARK, 1, true) ~= nil
end
function Source:openBookAsync(identity, opts, cb)
local Chapter = require("source.chapter")
local open = opts and opts.chapter_idx and Chapter.openAsync or Chapter.openWithUi
return open(self, identity, identity.book, opts, {
loadToc = function(ref, done) return self:loadTocAsync(ref, done) end,
fetchContent = contentFetcher(self),
reuse = reuse,
}, cb)
end
function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
return require("source.chapter").prefetchAsync(identity, identity.book, toc, from_idx, count, {
fetchContent = contentFetcher(self),
interval_seconds = FETCH_INTERVAL,
reuse = reuse,
}, cb)
end
--- 缓存整本章节正文;已落盘章节由公共实现自动跳过。
---@param identity BookIdentity
---@param on_progress fun(done: integer, total: integer)|nil
---@param cb fun(ok: boolean, cached: integer, err: string|nil, total: integer, failed: integer)
---@return { cancel: fun() }
function Source:cacheAllChaptersAsync(identity, on_progress, cb, range)
return require("source.chapter").cacheAllAsync(self, identity, contentFetcher(self), on_progress, cb,
FETCH_INTERVAL, range, reuse)
end
return T18ta
@@ -0,0 +1,4 @@
-- 版本号由 tools/package.sh 打包时把占位符换成发布版本;源码运行显示 0.0.0-dev。
local VERSION = "@VERSION@"
if VERSION:find("^@") then return "0.0.0-dev" end
return VERSION