✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。 Co-authored-by: Copilot <[email protected]>
This commit is contained in:
commit
bea9eee670
24 files changed
+2483
No files matched your search
@@ -0,0 +1,19 @@
|
||||
# 网易云阅读
|
||||
|
||||
| 能力 | 是否支持 | 备注 |
|
||||
| --- | --- | --- |
|
||||
| 手机号密码登录 | 支持 | 支持同一会话的图形验证码 |
|
||||
| 微信扫码登录 | 支持 | 官方扫码确认后兑换网易会话 |
|
||||
| 浏览器登录与 Cookie 导入 | 支持 | 电脑站登录后导入或粘贴 |
|
||||
| 书架拉取 | 支持 | 读取官网书架 |
|
||||
| 书架新增与移出 | 支持 | 本地先生效,远端确认后清脏/撕墓碑;失败重试 |
|
||||
| 搜索与分类书城 | 支持 | 男频、女频、出版;动态分类、排序、资费及原创连载状态 |
|
||||
| 详情与分卷目录 | 支持 | 保留章节顺序 |
|
||||
| 免费与已购正文 | 支持 | 未购明确拒绝,不绕过权限 |
|
||||
| 章节与全本缓存 | 支持 | 已缓存内容可断网续章和关书重开 |
|
||||
| 阅读进度同步 | 支持 | 章节及章内比例;上传后回读确认 |
|
||||
| 笔记与统计同步 | 不支持 | 仅本地保存 |
|
||||
| 整书书评 | 支持 | 阅读器和章末评论入口均按整本书归属 |
|
||||
| 章评与段评 | 不支持 | 当前电脑/移动 Web 未找到可验证接口,不代表 App 不支持 |
|
||||
| 评论回复与写操作 | 不支持 | 未接入 |
|
||||
| 账号切换 | 不支持直接隔离 | 须先关书、退出并清理旧本地数据;未同步记录会丢失 |
|
||||
@@ -0,0 +1,5 @@
|
||||
local _ = require("gettext")
|
||||
return {
|
||||
fullname = _("月读书源:网易云阅读"),
|
||||
description = _("给月读增加「网易云阅读」书源。需要同时安装月读,与平台官方无关。"),
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
--[[--
|
||||
KOReader 只认带 main.lua 的插件目录;书源本身不需要任何插件钩子,月读读取旁边的 moon_source.lua。
|
||||
--]]
|
||||
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||
|
||||
return WidgetContainer:extend{
|
||||
name = "moonsource_netease",
|
||||
is_doc_only = false,
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
--[[--
|
||||
月读第三方书源描述:网易云阅读。契约见月读 examples/moonsource-demo.koplugin/moon_source.lua。
|
||||
--]]
|
||||
|
||||
local function lazy(module)
|
||||
return setmetatable({}, { __index = function(_, key) return require(module)[key] end })
|
||||
end
|
||||
|
||||
return {
|
||||
api = 1,
|
||||
provides = { source = true },
|
||||
meta = function() return require("moonsource_netease.source").meta() end,
|
||||
new = function() return require("moonsource_netease.source").new() end,
|
||||
setting = lazy("moonsource_netease.setting"),
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
--[[--
|
||||
书源元信息:id / 名称 / 类型,加上启用页展示用的说明与能力清单。`init.lua` 的 `meta()` 直接返回本表。
|
||||
|
||||
网易云阅读网页搜索、分类书库和免费章节不需要登录。
|
||||
电脑端支持手机号密码、微信扫码以及电脑浏览器 Cookie 登录。
|
||||
登录后同步官网书架,本地增删联网后上行。
|
||||
|
||||
@module moonsource_netease._meta
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_netease")
|
||||
|
||||
return {
|
||||
id = "ext.netease",
|
||||
name = _("网易云阅读"),
|
||||
type = "chapter",
|
||||
description = _("网易云阅读在线阅读"),
|
||||
abilities = { _("搜索与发现"), _("书架同步"), _("进度同步"), _("书评"), _("VIP 章节") },
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
--[[--
|
||||
电脑端登录会话。Cookie 归本次登录窗口所有,校验成功才替换持久会话。
|
||||
密码响应是 HTML 中的 mobileCallback,不是手机版 JSON。
|
||||
|
||||
@module moonsource_netease.auth
|
||||
--]]
|
||||
|
||||
local Request = require("http.request")
|
||||
local Text = require("utils.text")
|
||||
local Mapper = require("moonsource_netease.mapper")
|
||||
local _ = require("l10n").get("moonsource_netease")
|
||||
|
||||
local ORIGIN = "https://yuedu.163.com"
|
||||
local CALLBACK = ORIGIN .. "/weiboCallback.do?site=7&from=login&fromHttps=true"
|
||||
|
||||
---@class NeteaseLogin
|
||||
---@field cookies table<string, string>
|
||||
---@field job HttpJob|nil
|
||||
---@field serial integer
|
||||
---@field prepared boolean|nil
|
||||
local Login = {}
|
||||
Login.__index = Login
|
||||
|
||||
function Login.new()
|
||||
return setmetatable({ cookies = {}, serial = 0 }, Login)
|
||||
end
|
||||
|
||||
function Login:cancel()
|
||||
self.serial = self.serial + 1
|
||||
if self.job then self.job:cancel(); self.job = nil end
|
||||
end
|
||||
|
||||
function Login:cookie()
|
||||
local names, parts = {}, {}
|
||||
for name in pairs(self.cookies) do names[#names + 1] = name end
|
||||
table.sort(names)
|
||||
for _idx, name in ipairs(names) do parts[#parts + 1] = name .. "=" .. self.cookies[name] end
|
||||
return table.concat(parts, "; ")
|
||||
end
|
||||
|
||||
--- 同步回调不能把后续请求的取消句柄覆盖掉;微信请求不携带网易 Cookie。
|
||||
function Login:request(method, url, fields, cb)
|
||||
self.serial = self.serial + 1
|
||||
local serial, pending = self.serial, true
|
||||
local options = { timeout = require("http.timeout").net(1.5) }
|
||||
if url:sub(1, #ORIGIN + 1) == ORIGIN .. "/" then
|
||||
options.headers = { Cookie = self:cookie(), Referer = ORIGIN .. "/loginForm.do" }
|
||||
end
|
||||
local function done(raw, err, res)
|
||||
pending = false
|
||||
if serial ~= self.serial then return end
|
||||
self.job = nil
|
||||
if options.headers then
|
||||
for _idx, line in ipairs(Request.headerAll(res, "Set-Cookie")) do
|
||||
local name, value = line:match("^%s*([^=;%s]+)=([^;]*)")
|
||||
if name then
|
||||
self.cookies[name] = value ~= "" and value ~= '""' and value or nil
|
||||
end
|
||||
end
|
||||
end
|
||||
cb(raw, err, res)
|
||||
end
|
||||
local job
|
||||
if method == "POST" then
|
||||
job = Request.post(url, Text.formEncode(fields), options, done)
|
||||
else
|
||||
job = Request.get(url, options, done)
|
||||
end
|
||||
if pending and serial == self.serial then self.job = job end
|
||||
end
|
||||
|
||||
function Login:confirm(cb)
|
||||
self:request("GET", ORIGIN .. "/myinfor.do", nil, function(raw, err)
|
||||
if not Mapper.authorized(raw) then
|
||||
cb(nil, err and tostring(err) or _("网易云阅读登录校验失败,请在浏览器重新登录"))
|
||||
return
|
||||
end
|
||||
local settings = require("utils.settings")
|
||||
local cfg = settings.getSource("ext.netease")
|
||||
cfg.cookie, cfg.logged_in, cfg.csrf = self:cookie(), true, nil
|
||||
settings.saveSource("ext.netease")
|
||||
require("source.auth").ok("ext.netease")
|
||||
cb(true)
|
||||
end)
|
||||
end
|
||||
|
||||
function Login:prepare(cb)
|
||||
if self.prepared then cb(true); return end
|
||||
self:request("GET", ORIGIN .. "/loginForm.do?targetUrl=" .. Text.urlEncode(ORIGIN .. "/myinfor.do"),
|
||||
nil, function(raw, err)
|
||||
if not raw or not raw:find("login%-container") then
|
||||
cb(nil, err and tostring(err) or _("登录失败"))
|
||||
return
|
||||
end
|
||||
self.prepared = true
|
||||
cb(true)
|
||||
end)
|
||||
end
|
||||
|
||||
function Login:passwordAsync(phone, password, code, cb)
|
||||
self:prepare(function(ok, err)
|
||||
if not ok then cb(nil, err); return end
|
||||
self:request("POST", ORIGIN .. "/cellPhoneLogin.do", {
|
||||
operation = "login", cellPhone = phone, password = password,
|
||||
remember_me = "on", checkCode = code or "",
|
||||
}, function(raw, reason)
|
||||
if not raw then cb(nil, reason and tostring(reason) or _("登录失败")); return end
|
||||
local result = raw:match("mobileCallback%s*%(%s*['\"]([%w]+)['\"]%s*%)")
|
||||
if result == "success" then self:confirm(cb); return end
|
||||
local messages = {
|
||||
notRegistered = _("网易云阅读手机号未注册"),
|
||||
notRegisteredNeedCode = _("网易云阅读手机号未注册"),
|
||||
authenticationFailed = _("网易云阅读密码错误"),
|
||||
authFailNeedCode = _("网易云阅读密码错误"),
|
||||
errorCaptcha = _("图形验证码错误"),
|
||||
}
|
||||
cb(nil, messages[result] or _("登录失败"),
|
||||
result == "notRegisteredNeedCode" or result == "authFailNeedCode" or result == "errorCaptcha")
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
function Login:captchaAsync(cb)
|
||||
self:prepare(function(ok, err)
|
||||
if not ok then cb(nil, err); return end
|
||||
self:request("GET", ORIGIN .. "/captcha.do?d=" .. os.time() * 1000, nil, function(raw, reason)
|
||||
if not raw then cb(nil, reason and tostring(reason) or _("图形验证码加载失败")); return end
|
||||
local _ext, mime = require("utils.image").ext(raw)
|
||||
if not mime then cb(nil, _("图形验证码加载失败")); return end
|
||||
cb(raw)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
function Login:qrCreateAsync(cb)
|
||||
self:request("GET", "https://open.weixin.qq.com/connect/qrconnect?" .. Text.formEncode({
|
||||
appid = "wxc662b4239bb6644e", redirect_uri = CALLBACK, response_type = "code",
|
||||
scope = "snsapi_login", state = "",
|
||||
}), nil, function(raw, err)
|
||||
local uuid = raw and raw:match("/connect/qrcode/([%w_%-]+)")
|
||||
if not uuid then cb(nil, err and tostring(err) or _("获取二维码失败")); return end
|
||||
cb(uuid)
|
||||
end)
|
||||
end
|
||||
|
||||
function Login:qrPollAsync(uuid, last, cb)
|
||||
self:request("GET", "https://long.open.weixin.qq.com/connect/l/qrconnect?"
|
||||
.. Text.formEncode({ uuid = uuid, last = last, f = "json" }), nil, function(raw, err)
|
||||
if not raw then cb(nil, nil, err and tostring(err) or _("请求失败")); return end
|
||||
local ok, wire = pcall(require("json").decode, raw)
|
||||
local status = ok and type(wire) == "table" and tonumber(wire.wx_errcode)
|
||||
or tonumber(raw:match("wx_errcode%s*=%s*(%d+)"))
|
||||
local code = ok and type(wire) == "table" and wire.wx_code
|
||||
or raw:match("wx_code%s*=%s*['\"]([^'\"]+)['\"]")
|
||||
if not status or (status == 405 and (type(code) ~= "string" or code == "")) then
|
||||
cb(nil, nil, _("响应无效"))
|
||||
return
|
||||
end
|
||||
cb(status, code)
|
||||
end)
|
||||
end
|
||||
|
||||
function Login:exchangeAsync(code, cb)
|
||||
local function follow(url, remaining)
|
||||
self:request("GET", url, nil, function(raw, err, res)
|
||||
local status = res and tonumber(res.code)
|
||||
if status and status >= 300 and status < 400 then
|
||||
local location = Request.header(res, "Location")
|
||||
if location and location:sub(1, 1) == "/" and location:sub(1, 2) ~= "//" then
|
||||
location = ORIGIN .. location
|
||||
end
|
||||
if remaining == 0 or type(location) ~= "string"
|
||||
or location:sub(1, #ORIGIN + 1) ~= ORIGIN .. "/" then
|
||||
cb(nil, _("网易云阅读登录校验失败,请在浏览器重新登录"))
|
||||
return
|
||||
end
|
||||
follow(location, remaining - 1)
|
||||
return
|
||||
end
|
||||
if not raw then cb(nil, err and tostring(err) or _("登录失败")); return end
|
||||
self:confirm(cb)
|
||||
end)
|
||||
end
|
||||
follow(CALLBACK .. "&code=" .. Text.urlEncode(code) .. "&state=", 5)
|
||||
end
|
||||
|
||||
return Login
|
||||
@@ -0,0 +1,230 @@
|
||||
--[[--
|
||||
网易云阅读 Web 接口。账号、书架、目录、正文和进度共用电脑端 Cookie。
|
||||
公开搜索和分类使用官方移动网页 JSON,不向该域名发送电脑端 Cookie。
|
||||
|
||||
@module moonsource_netease.client
|
||||
--]]
|
||||
|
||||
local JSON = require("json")
|
||||
local Mapper = require("moonsource_netease.mapper")
|
||||
local Request = require("http.request")
|
||||
local Text = require("utils.text")
|
||||
local _ = require("l10n").get("moonsource_netease")
|
||||
|
||||
local Client = {}
|
||||
local ORIGIN = "https://yuedu.163.com"
|
||||
local SHELF_ORIGIN = "https://guofeng.yuedu.163.com"
|
||||
local PUBLIC = "https://yuedu.3g.163.com"
|
||||
local SOURCE_ID = "ext.netease"
|
||||
|
||||
function Client.loggedIn()
|
||||
local cfg = require("utils.settings").getSource(SOURCE_ID)
|
||||
return cfg.logged_in == true and type(cfg.cookie) == "string" and cfg.cookie ~= ""
|
||||
and not require("source.auth").expired(SOURCE_ID)
|
||||
end
|
||||
|
||||
--- account 是发请求时的已保存 Cookie,回包后与当前保存值比对判断是否换号。
|
||||
local function opts(origin)
|
||||
local cookie = require("utils.settings").getSource(SOURCE_ID).cookie
|
||||
return {
|
||||
timeout = require("http.timeout").net(1),
|
||||
account = cookie,
|
||||
jar = require("http.cookie_jar").session(SOURCE_ID, cookie),
|
||||
headers = { Referer = (origin or ORIGIN) .. "/" },
|
||||
}
|
||||
end
|
||||
|
||||
local function getJson(url, options, cb)
|
||||
return Request.get(url, options, function(raw, err)
|
||||
if err or not raw then cb(nil, err and tostring(err) or _("请求失败")); return end
|
||||
local ok, wire = pcall(JSON.decode, raw)
|
||||
if not ok or type(wire) ~= "table" then cb(nil, _("响应无效")); return end
|
||||
cb(wire)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 只校验,不改当前会话;匿名账号页会重定向至 loginForm.do。
|
||||
function Client.loginCheckAsync(cookie, cb)
|
||||
return Request.get(ORIGIN .. "/myinfor.do", {
|
||||
headers = { Cookie = cookie }, timeout = require("http.timeout").net(1),
|
||||
}, function(raw, err, res)
|
||||
if err or not Mapper.authorized(raw) then
|
||||
local location = res and Request.header(res, "Location")
|
||||
local status = res and tonumber(res.code)
|
||||
local expired = (status and status >= 300 and status < 400
|
||||
and location and location:find("/loginForm.do", 1, true) ~= nil)
|
||||
or (not err and raw and raw:find("login%-container") ~= nil)
|
||||
cb(false, not expired and err and tostring(err)
|
||||
or _("网易云阅读登录校验失败,请在浏览器重新登录"), expired == true)
|
||||
return
|
||||
end
|
||||
cb(true)
|
||||
end)
|
||||
end
|
||||
|
||||
function Client:shelfSyncAsync(cb)
|
||||
if not Client.loggedIn() then return require("source.base").defer(cb, nil, _("未登录网易云阅读")) end
|
||||
local cancelled, job = false, nil
|
||||
local books, seen = {}, {}
|
||||
local options = opts(SHELF_ORIGIN)
|
||||
options.cache_ttl = 0
|
||||
local function nextPage(page)
|
||||
local pending = true
|
||||
local current = Request.get(SHELF_ORIGIN .. "/shelf.do?operation=query_current&style=grid&page=" .. page,
|
||||
options, function(raw, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not raw then cb(nil, err and tostring(err) or _("请求失败")); return end
|
||||
local rows, next_page, reason = Mapper.shelfPage(raw, page)
|
||||
if not rows then cb(nil, reason); return end
|
||||
for _idx, item in ipairs(rows) do
|
||||
if not seen[item.stable_id] then
|
||||
seen[item.stable_id] = true
|
||||
books[#books + 1] = item
|
||||
end
|
||||
end
|
||||
if next_page then nextPage(next_page); return end
|
||||
cb(books)
|
||||
end)
|
||||
if pending then job = current end
|
||||
end
|
||||
nextPage(1)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job:cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
local function changeShelf(url, fields, cb)
|
||||
if not Client.loggedIn() then return require("source.base").defer(cb, nil, _("未登录网易云阅读")) end
|
||||
local options = opts(SHELF_ORIGIN)
|
||||
options.cache_ttl = 0
|
||||
return Request.post(SHELF_ORIGIN .. url, fields and Text.formEncode(fields) or "", options, function(raw, err)
|
||||
if err then cb(nil, tostring(err)); return end
|
||||
local ok, wire = pcall(JSON.decode, raw)
|
||||
if not ok or type(wire) ~= "table" then cb(nil, _("响应无效")); return end
|
||||
local code = tonumber(wire.resultCode)
|
||||
if code == -999 then
|
||||
local reason = _("网易云阅读登录已失效")
|
||||
if options.account == require("utils.settings").getSource(SOURCE_ID).cookie then
|
||||
reason = require("source.auth").expire(SOURCE_ID, reason)
|
||||
end
|
||||
cb(nil, reason)
|
||||
return
|
||||
end
|
||||
if code ~= 0 then
|
||||
require("utils.log").warn("book netease shelf mutation failed", code)
|
||||
cb(nil, code == nil and _("响应无效") or _("同步失败"))
|
||||
return
|
||||
end
|
||||
cb(wire)
|
||||
end)
|
||||
end
|
||||
|
||||
function Client:addToShelfAsync(stable_id, cb)
|
||||
return changeShelf("/subscribe.do", { operation = "addSub", id = stable_id }, cb)
|
||||
end
|
||||
|
||||
function Client:removeFromShelfAsync(stable_id, cb)
|
||||
return changeShelf("/shelf.do?operation=unsub&source_uuids=" .. Text.urlEncode(stable_id), nil, cb)
|
||||
end
|
||||
|
||||
function Client.searchAsync(keyword, cb)
|
||||
return getJson(PUBLIC .. "/search/book/data.json?key=" .. Text.urlEncode(keyword), {}, function(wire, err)
|
||||
if not wire then cb(nil, err); return end
|
||||
cb(Mapper.searchBooks(wire))
|
||||
end)
|
||||
end
|
||||
|
||||
function Client.categoriesAsync(cb)
|
||||
return Request.get(PUBLIC .. "/category", { cache_ttl = 3600 }, function(raw, err)
|
||||
if err or not raw then cb(nil, err and tostring(err) or _("请求失败")); return end
|
||||
cb(Mapper.categories(raw))
|
||||
end)
|
||||
end
|
||||
|
||||
---@param options { channel: string, category: string, sort: string, pay: string, state: string }
|
||||
---@param page integer
|
||||
function Client.catalogAsync(options, page, cb)
|
||||
local publish = options.channel == "publish"
|
||||
local fields = {
|
||||
id = options.category == "all" and "" or options.category,
|
||||
sort = options.sort == "hot" and (publish and 1 or 2) or (publish and 2 or 1),
|
||||
pay = options.pay, page = page,
|
||||
}
|
||||
if not publish then fields.gender, fields.state = options.channel, options.state end
|
||||
local url = PUBLIC .. (publish and "/category/publish/data.json?" or "/category/original/data.json?")
|
||||
.. Text.formEncode(fields)
|
||||
return getJson(url, {}, function(wire, err)
|
||||
if not wire then cb(nil, err); return end
|
||||
cb(Mapper.catalog(wire))
|
||||
end)
|
||||
end
|
||||
|
||||
function Client.infoAsync(stable_id, cb)
|
||||
return getJson(ORIGIN .. "/getBook.do?id=" .. Text.urlEncode(stable_id), opts(), function(wire, err)
|
||||
if not wire then cb(nil, err); return end
|
||||
if not Mapper.detail(wire) then cb(nil, _("响应无效")); return end
|
||||
cb(wire)
|
||||
end)
|
||||
end
|
||||
|
||||
---@param chapter { uid?: string, leaf?: boolean, big_content_id?: string }
|
||||
function Client.contentAsync(stable_id, chapter, cb)
|
||||
local url = ORIGIN .. (chapter.leaf == false and "/getChapterContent.do?" or "/getArticleContent.do?")
|
||||
.. Text.formEncode({
|
||||
sourceUuid = stable_id, articleUuid = chapter.uid, bigContentId = chapter.big_content_id,
|
||||
})
|
||||
return getJson(url, opts(), function(wire, err)
|
||||
if not wire then cb(nil, err); return end
|
||||
if tonumber(wire.resultCode) == 1 then cb(nil, _("本章未购买")); return end
|
||||
if tonumber(wire.resultCode) ~= 0 then cb(nil, _("请求失败")); return end
|
||||
cb(Mapper.content(wire))
|
||||
end)
|
||||
end
|
||||
|
||||
function Client.commentsAsync(stable_id, cb)
|
||||
local url = ORIGIN .. "/snsComment.do?operation=get&type=2&page=1&id=" .. Text.urlEncode(stable_id)
|
||||
return getJson(url, opts(), function(wire, err)
|
||||
if not wire then cb(nil, err); return end
|
||||
local items, pages = Mapper.comments(wire)
|
||||
if not items then cb(nil, _("网易云阅读书评加载失败")); return end
|
||||
cb(items, nil, pages)
|
||||
end)
|
||||
end
|
||||
|
||||
function Client.putProgressAsync(stable_id, toc, idx, fraction, cb)
|
||||
if not Client.loggedIn() then return require("source.base").defer(cb, nil, _("未登录网易云阅读")) end
|
||||
local fields = Mapper.readlog(stable_id, toc, idx, fraction, os.time() * 1000)
|
||||
if not fields then return require("source.base").defer(cb, nil, _("缺少网易云阅读章节位置")) end
|
||||
-- 官网匿名写进度也返回 1;先确认会话,避免把未同步的本地进度标成成功。
|
||||
local cancelled, job, pending = false, nil, true
|
||||
local options = opts()
|
||||
local initial = Client.loginCheckAsync(options.account, function(valid, reason, expired)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not valid then
|
||||
if expired and options.account == require("utils.settings").getSource(SOURCE_ID).cookie then
|
||||
reason = require("source.auth").expire(SOURCE_ID, _("网易云阅读登录已失效"))
|
||||
end
|
||||
cb(nil, reason)
|
||||
return
|
||||
end
|
||||
job = Request.post(ORIGIN .. "/addUserReadLog.do", Text.formEncode(fields), options, function(raw, err)
|
||||
if cancelled then return end
|
||||
if err or tonumber(raw) ~= 1 then
|
||||
cb(nil, err and tostring(err) or _("网易云阅读进度保存失败"))
|
||||
return
|
||||
end
|
||||
require("source.auth").ok(SOURCE_ID)
|
||||
cb(true)
|
||||
end)
|
||||
end)
|
||||
if pending then job = initial end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job:cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
return Client
|
||||
@@ -0,0 +1,48 @@
|
||||
return {
|
||||
["网易云阅读"] = "NetEase Cloud Reading",
|
||||
["网易云阅读在线阅读"] = "NetEase Cloud Reading online books",
|
||||
["网易云阅读目录为空"] = "NetEase Cloud Reading chapter list is empty",
|
||||
["网易云阅读书评加载失败"] = "Could not load NetEase Cloud Reading reviews",
|
||||
["未登录网易云阅读"] = "Not signed in to NetEase Cloud Reading",
|
||||
["网易云阅读书架读取失败,请在官网确认登录"] = "Could not read the NetEase bookshelf. Check your sign-in on the official website",
|
||||
["网易云阅读登录已失效"] = "NetEase Cloud Reading sign-in has expired",
|
||||
["缺少网易云阅读章节位置"] = "Missing the NetEase Cloud Reading chapter position",
|
||||
["网易云阅读进度保存失败"] = "Could not save the NetEase Cloud Reading position",
|
||||
["网易云阅读账号"] = "NetEase Cloud Reading account",
|
||||
["网易云阅读手机号未注册"] = "This phone number is not registered with NetEase Cloud Reading",
|
||||
["网易云阅读密码错误"] = "Wrong NetEase Cloud Reading password",
|
||||
["图形验证码错误"] = "Wrong image code",
|
||||
["请输入图形码"] = "Enter the characters in the image",
|
||||
["图形验证码加载失败"] = "Could not load the image code",
|
||||
["确定退出网易云阅读账号?"] = "Sign out of NetEase Cloud Reading?",
|
||||
["网易云阅读图形验证码"] = "NetEase Cloud Reading image code",
|
||||
["正文为空"] = "Chapter text is empty",
|
||||
["用微信扫一扫"] = "Scan with WeChat",
|
||||
["打开微信,扫屏幕上的二维码"] = "Open WeChat and scan the QR code",
|
||||
["本章未购买"] = "This chapter has not been purchased",
|
||||
["出版"] = "Published",
|
||||
["价格"] = "Price",
|
||||
["免费"] = "Free",
|
||||
["付费"] = "Paid",
|
||||
["连载中"] = "Ongoing",
|
||||
["已完结"] = "Completed",
|
||||
["微信扫码登录"] = "WeChat QR login",
|
||||
["正在登录…"] = "Signing in…",
|
||||
["正在获取二维码…"] = "Fetching QR code…",
|
||||
["章节目录保存失败"] = "Failed to save chapter list",
|
||||
["已在微信中拒绝登录"] = "Login declined in WeChat",
|
||||
["手机号"] = "Phone number",
|
||||
["请填写正确的手机号"] = "Enter a valid phone number",
|
||||
["按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie"] = "Press F12, open Network, refresh, and copy the entire Cookie request header",
|
||||
["女频"] = "Female",
|
||||
["男频"] = "Male",
|
||||
["网易云阅读登录 Cookie"] = "NetEase Cloud Reading sign-in cookie",
|
||||
["网易云阅读登录校验失败,请在浏览器重新登录"] = "NetEase Cloud Reading sign-in verification failed. Please sign in again in your browser",
|
||||
["VIP 章节"] = "VIP chapters",
|
||||
["书架同步"] = "Shelf sync",
|
||||
["搜索与发现"] = "Search and discover",
|
||||
["进度同步"] = "Progress sync",
|
||||
["手机号密码登录"] = "Sign in with phone and password",
|
||||
["请填写密码"] = "Enter your password",
|
||||
["刷新"] = "Refresh",
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
return {
|
||||
["网易云阅读"] = "網易雲閱讀",
|
||||
["网易云阅读在线阅读"] = "網易雲閱讀線上閱讀",
|
||||
["网易云阅读目录为空"] = "網易雲閱讀目錄為空",
|
||||
["网易云阅读书评加载失败"] = "網易雲閱讀書評載入失敗",
|
||||
["未登录网易云阅读"] = "未登入網易雲閱讀",
|
||||
["网易云阅读书架读取失败,请在官网确认登录"] = "無法讀取網易雲閱讀書架,請在官網確認登入狀態",
|
||||
["网易云阅读登录已失效"] = "網易雲閱讀登入已失效",
|
||||
["缺少网易云阅读章节位置"] = "缺少網易雲閱讀章節位置",
|
||||
["网易云阅读进度保存失败"] = "網易雲閱讀進度保存失敗",
|
||||
["网易云阅读账号"] = "網易雲閱讀帳號",
|
||||
["网易云阅读手机号未注册"] = "這個手機號還沒註冊網易雲閱讀",
|
||||
["网易云阅读密码错误"] = "網易雲閱讀密碼錯誤",
|
||||
["图形验证码错误"] = "圖形驗證碼錯誤",
|
||||
["请输入图形码"] = "請輸入圖形碼",
|
||||
["图形验证码加载失败"] = "圖形驗證碼載入失敗",
|
||||
["确定退出网易云阅读账号?"] = "確定退出網易雲閱讀帳號?",
|
||||
["网易云阅读图形验证码"] = "網易雲閱讀圖形驗證碼",
|
||||
["正文为空"] = "正文為空",
|
||||
["用微信扫一扫"] = "用微信掃一掃",
|
||||
["打开微信,扫屏幕上的二维码"] = "打開微信,掃螢幕上的二維碼",
|
||||
["本章未购买"] = "本章未購買",
|
||||
["出版"] = "出版",
|
||||
["价格"] = "價格",
|
||||
["免费"] = "免費",
|
||||
["付费"] = "付費",
|
||||
["连载中"] = "連載中",
|
||||
["已完结"] = "已完結",
|
||||
["微信扫码登录"] = "微信掃碼登入",
|
||||
["正在登录…"] = "正在登入…",
|
||||
["正在获取二维码…"] = "正在取得二維碼…",
|
||||
["章节目录保存失败"] = "章節目錄儲存失敗",
|
||||
["已在微信中拒绝登录"] = "已在微信中拒絕登入",
|
||||
["手机号"] = "手機號碼",
|
||||
["请填写正确的手机号"] = "請填寫正確的手機號碼",
|
||||
["按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie"] = "按鍵盤 F12,點「網路」,重新整理網頁,複製請求標頭裡的整段 Cookie",
|
||||
["女频"] = "女頻",
|
||||
["男频"] = "男頻",
|
||||
["网易云阅读登录 Cookie"] = "網易雲閱讀登入 Cookie",
|
||||
["网易云阅读登录校验失败,请在浏览器重新登录"] = "網易雲閱讀登入驗證失敗,請在瀏覽器重新登入",
|
||||
["VIP 章节"] = "VIP 章節",
|
||||
["书架同步"] = "書架同步",
|
||||
["搜索与发现"] = "搜尋與發現",
|
||||
["进度同步"] = "進度同步",
|
||||
["手机号密码登录"] = "手機號碼密碼登入",
|
||||
["请填写密码"] = "請填寫密碼",
|
||||
["刷新"] = "重新整理",
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
return function()
|
||||
local copy = {}
|
||||
for k, v in pairs(require("moonsource_netease._meta")) do copy[k] = v end
|
||||
return copy
|
||||
end
|
||||
@@ -0,0 +1,305 @@
|
||||
--[[--
|
||||
网易云阅读响应到书籍、目录和正文。
|
||||
|
||||
电脑端目录来自 getBook.do,分卷也保留为章节,避免改变已有章节序号。
|
||||
正文的 x 字段指定 XOR / AES 解密顺序;没有 x 时为标准 Base64。
|
||||
|
||||
@module moonsource_netease.mapper
|
||||
--]]
|
||||
|
||||
local Text = require("utils.text")
|
||||
local JSON = require("json")
|
||||
local _ = require("l10n").get("moonsource_netease")
|
||||
|
||||
---@class NeteaseChapter : BookChapter
|
||||
---@field big_content_id string|nil
|
||||
---@field leaf boolean|nil
|
||||
local Mapper = {}
|
||||
|
||||
Mapper.TOC_VERSION = 2
|
||||
local SOURCE_ID = "ext.netease"
|
||||
|
||||
function Mapper.authorized(html)
|
||||
return type(html) == "string" and html:find("<title[^>]*>个人资料[^<]*</title>") ~= nil
|
||||
and html:find('id=["\']editNickName["\']') ~= nil
|
||||
end
|
||||
|
||||
---@param value any
|
||||
---@return string
|
||||
local function clean(value)
|
||||
if type(value) ~= "string" then return "" end
|
||||
return Text.trim(value)
|
||||
end
|
||||
|
||||
---@param value any
|
||||
---@return string|nil
|
||||
local function picture(value)
|
||||
if type(value) ~= "string" or value == "" then return nil end
|
||||
if value:sub(1, 2) == "//" then return "https:" .. value end
|
||||
return value
|
||||
end
|
||||
|
||||
---@param row table
|
||||
---@return string|nil
|
||||
local function stableId(row)
|
||||
if row.sourceUuid ~= nil and tostring(row.sourceUuid) ~= "" then return tostring(row.sourceUuid) end
|
||||
if type(row.sourceUrl) == "string" then
|
||||
local id = row.sourceUrl:match("/source/([^/?#]+)")
|
||||
if id and id ~= "" then return id end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param row table
|
||||
---@return Book|nil
|
||||
local function book(row)
|
||||
local id = stableId(row)
|
||||
local title = clean(row.title or row.bookName)
|
||||
if not id or title == "" then return nil end
|
||||
return {
|
||||
source_id = SOURCE_ID,
|
||||
stable_id = id,
|
||||
title = title,
|
||||
authors = clean(row.author or row.authorName),
|
||||
intro = clean(row.description or row.intro),
|
||||
cover = picture(row.iconUrl or row.coverImage or row.cover),
|
||||
}
|
||||
end
|
||||
|
||||
---@param rows table|nil
|
||||
---@return Book[]
|
||||
function Mapper.books(rows)
|
||||
local out = {}
|
||||
if type(rows) ~= "table" then return out end
|
||||
for _idx, row in ipairs(rows) do
|
||||
if type(row) == "table" then
|
||||
local item = book(row)
|
||||
if item then out[#out + 1] = item end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
---@param wire table|nil
|
||||
---@return Book[]|nil
|
||||
---@return string|nil
|
||||
function Mapper.searchBooks(wire)
|
||||
if type(wire) ~= "table" or tonumber(wire.code) ~= 0 or type(wire.data) ~= "table" then
|
||||
return nil, type(wire) == "table" and wire.error or nil
|
||||
end
|
||||
return Mapper.books(wire.data.books)
|
||||
end
|
||||
|
||||
--- 分类取官网实际目录,不在客户端另存一份容易过时的分类编码。
|
||||
function Mapper.categories(html)
|
||||
local categories = { male = {}, female = {}, publish = {} }
|
||||
local seen = {}
|
||||
for href, label in html:gmatch('<a[^>]-href=["\']([^"\']+)["\'][^>]*>(.-)</a>') do
|
||||
href = Text.xmlDecode(href)
|
||||
local id = href:match("[?&]id=(%d+)")
|
||||
local channel = href:match("/category/original%.do%?gender=(%a+)")
|
||||
if href:find("/category/publish.do?", 1, true) then channel = "publish" end
|
||||
if id and categories[channel] and not seen[channel .. ":" .. id] then
|
||||
seen[channel .. ":" .. id] = true
|
||||
local rows = categories[channel]
|
||||
rows[#rows + 1] = { id = id, title = Text.stripHtml(label) }
|
||||
end
|
||||
end
|
||||
for _channel, rows in pairs(categories) do
|
||||
if #rows == 0 then return nil, _("响应无效") end
|
||||
end
|
||||
return categories
|
||||
end
|
||||
|
||||
function Mapper.catalog(wire)
|
||||
local data = type(wire) == "table" and wire.data
|
||||
if type(wire) ~= "table" or tonumber(wire.code) ~= 0 or type(data) ~= "table" or type(data.books) ~= "table"
|
||||
or type(data.pageQuery) ~= "table" or type(data.pageQuery.more) ~= "boolean" then
|
||||
return nil, _("响应无效")
|
||||
end
|
||||
local books = Mapper.books(data.books)
|
||||
if #books ~= #data.books then return nil, _("响应无效") end
|
||||
return books, nil, data.pageQuery.more
|
||||
end
|
||||
|
||||
--- 官网书架是服务端 HTML;每行的分享数据携带结构化书籍身份,不能当搜索 JSON 解析。
|
||||
---@param html string
|
||||
---@param page integer
|
||||
---@return Book[]|nil
|
||||
---@return integer|nil next_page
|
||||
---@return string|nil err
|
||||
function Mapper.shelfPage(html, page)
|
||||
if not html:find("<title[^>]*>[^<]*我的书架") then
|
||||
return nil, nil, _("网易云阅读书架读取失败,请在官网确认登录")
|
||||
end
|
||||
local books, seen = {}, {}
|
||||
for row in html:gmatch('<div[^>]-class=["\']m%-bookshow["\'][^>]*>(.-)</li>') do
|
||||
if not row:find('data%-local=["\']true["\']') then
|
||||
local encoded = row:match('data%-contentinfo="([^"]*)"') or row:match("data%-contentinfo='([^']*)'")
|
||||
local ok, info = pcall(JSON.decode, Text.xmlDecode(encoded or ""))
|
||||
if not ok or type(info) ~= "table" then return nil, nil, _("响应无效") end
|
||||
local item = book({
|
||||
sourceUuid = info.sourceUuid, title = info.sourceTitle,
|
||||
iconUrl = info.imageUrl, description = info.imageDescription,
|
||||
author = Text.stripHtml(row:match("<p[^>]*>(.-)</p>") or ""),
|
||||
})
|
||||
if not item then return nil, nil, _("响应无效") end
|
||||
if not seen[item.stable_id] then
|
||||
seen[item.stable_id] = true
|
||||
books[#books + 1] = item
|
||||
end
|
||||
end
|
||||
end
|
||||
local next_page
|
||||
for encoded in html:gmatch('href=["\']([^"\']+)["\']') do
|
||||
local link = Text.xmlDecode(encoded)
|
||||
local target = tonumber(link:match("[?&]page=(%d+)"))
|
||||
if link:find("/shelf.do?", 1, true) and link:find("operation=query_current", 1, true)
|
||||
and target and target > page and (not next_page or target < next_page) then
|
||||
next_page = target
|
||||
end
|
||||
end
|
||||
return books, next_page
|
||||
end
|
||||
|
||||
---@param wire table|nil
|
||||
---@return Book|nil
|
||||
function Mapper.detail(wire)
|
||||
if type(wire) ~= "table" or type(wire.portions) ~= "table" then return nil end
|
||||
return book({
|
||||
sourceUuid = wire.id, title = wire.title, author = wire.author,
|
||||
description = wire.shareDescription, coverImage = wire.coverImg,
|
||||
})
|
||||
end
|
||||
|
||||
---@param wire table|nil
|
||||
---@return NeteaseChapter[]|nil
|
||||
function Mapper.chapters(wire)
|
||||
local rows = type(wire) == "table" and wire.portions or nil
|
||||
if type(rows) ~= "table" then return nil end
|
||||
local out = {}
|
||||
for _idx, row in ipairs(rows) do
|
||||
if type(row) == "table" and row.id ~= nil and tostring(row.id) ~= "" then
|
||||
out[#out + 1] = {
|
||||
idx = #out + 1,
|
||||
uid = tostring(row.id),
|
||||
title = clean(row.title),
|
||||
sec_id = tostring(row.id),
|
||||
big_content_id = row.bigContentId,
|
||||
leaf = row.leafNode,
|
||||
word_count = tonumber(row.charSize),
|
||||
vip = tonumber(row.needPay) == 1 and row.isPaid ~= true,
|
||||
}
|
||||
end
|
||||
end
|
||||
if out[1] then out[1].toc_version = Mapper.TOC_VERSION end
|
||||
return out
|
||||
end
|
||||
|
||||
--- 电脑端以章节 UUID 定位;匿名响应带默认章节,但没有阅读时间。
|
||||
---@param wire table|nil
|
||||
---@return integer|nil
|
||||
function Mapper.progressIndex(wire)
|
||||
if type(wire) ~= "table" or (tonumber(wire.curDate) or 0) <= 0 then return nil end
|
||||
for idx, row in ipairs(wire.portions or {}) do
|
||||
if row.id == wire.curArticleUuid then return idx end
|
||||
end
|
||||
end
|
||||
|
||||
--- 电脑端章内百分比是 0..100,不是比例 0..1。
|
||||
---@param stable_id string
|
||||
---@param toc BookChapter[]|nil
|
||||
---@param idx number|nil
|
||||
---@param fraction number|nil
|
||||
---@param now_ms number
|
||||
---@return table|nil
|
||||
function Mapper.readlog(stable_id, toc, idx, fraction, now_ms)
|
||||
if type(toc) ~= "table" or type(idx) ~= "number" then return nil end
|
||||
idx = math.floor(idx)
|
||||
local chapter = toc[idx]
|
||||
if type(chapter) ~= "table" or type(chapter.uid) ~= "string" or chapter.uid == "" then
|
||||
return nil
|
||||
end
|
||||
local page = tonumber(fraction) or 0
|
||||
if page < 0 then page = 0 end
|
||||
if page > 1 then page = 1 end
|
||||
return {
|
||||
sourceUuid = stable_id,
|
||||
articleUuid = chapter.uid,
|
||||
curPagePercent = page * 100,
|
||||
date = now_ms,
|
||||
}
|
||||
end
|
||||
|
||||
--- 书评。网页模板用的字段:username、text、posttime、grade、likes、replied.username。
|
||||
--- 第二返回值是总页数,没有分页信息就是 nil。
|
||||
---@param wire table|nil
|
||||
---@return { author: string, text: string, time: string|nil, grade: number|nil, likes: number|nil, reply_to: string|nil }[]|nil
|
||||
---@return integer|nil
|
||||
function Mapper.comments(wire)
|
||||
if type(wire) ~= "table" or tonumber(wire.resultCode) ~= 0 or type(wire.data) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
local out = {}
|
||||
for _idx, row in ipairs(wire.data) do
|
||||
if type(row) == "table" and type(row.text) == "string" then
|
||||
local item = {
|
||||
author = type(row.username) == "string" and row.username or "",
|
||||
text = row.text,
|
||||
time = type(row.posttime) == "string" and row.posttime or nil,
|
||||
grade = tonumber(row.stars),
|
||||
likes = tonumber(row.likes),
|
||||
}
|
||||
if type(row.replied) == "table" and type(row.replied.username) == "string" then
|
||||
item.reply_to = row.replied.username
|
||||
end
|
||||
out[#out + 1] = item
|
||||
end
|
||||
end
|
||||
return out, tonumber(wire.totalPage)
|
||||
end
|
||||
|
||||
--- XOR 作用于 JavaScript UTF-16 码元,而不是 UTF-8 字节。
|
||||
local function xor(text, key)
|
||||
local bxor = require("bit").bxor
|
||||
local Glyph = require("utils.glyph")
|
||||
return (text:gsub(Text.UTF8_CHAR, function(ch)
|
||||
local cp = Glyph.codepoint(ch)
|
||||
if cp <= 0xFFFF then return Text.utf8char(bxor(cp, key)) end
|
||||
cp = cp - 0x10000
|
||||
local high = bxor(0xD800 + math.floor(cp / 0x400), key)
|
||||
local low = bxor(0xDC00 + cp % 0x400, key)
|
||||
return Text.utf8char(0x10000 + (high - 0xD800) * 0x400 + low - 0xDC00)
|
||||
end))
|
||||
end
|
||||
|
||||
--- 正文解码失败必须报错,不得把密文写进章节缓存。
|
||||
---@param wire table|nil
|
||||
---@return string|nil
|
||||
function Mapper.content(wire)
|
||||
local cipher = type(wire) == "table" and wire.content or nil
|
||||
if type(cipher) ~= "string" or cipher == "" then return nil, _("正文为空") end
|
||||
local html = Text.base64Decode(cipher)
|
||||
if type(wire.x) == "string" and wire.x ~= "" then
|
||||
local ok, steps = pcall(JSON.decode, Text.base64Decode(wire.x))
|
||||
if not ok or type(steps) ~= "table" or #steps < 1 or #steps > 2 then
|
||||
return nil, _("正文解析失败")
|
||||
end
|
||||
for _idx, step in ipairs(steps) do
|
||||
if type(step) ~= "table" then return nil, _("正文解析失败") end
|
||||
local decoded, value
|
||||
if step.m == 1 and tonumber(step.k) then
|
||||
decoded, value = pcall(xor, html, tonumber(step.k))
|
||||
elseif step.m == 2 and type(step.k) == "string" and html:match("^%x+$") and #html % 32 == 0 then
|
||||
local Aes = require("crypto.aes")
|
||||
decoded, value = pcall(Aes.ecb_decrypt, Aes.from_hex(html), step.k)
|
||||
end
|
||||
if not decoded then return nil, _("正文解析失败") end
|
||||
html = value
|
||||
end
|
||||
end
|
||||
if html == "" then return nil, _("正文为空") end
|
||||
return html
|
||||
end
|
||||
|
||||
return Mapper
|
||||
@@ -0,0 +1,92 @@
|
||||
--[[--
|
||||
网易云阅读微信开放平台扫码,确认后由网易回调换取电脑端 Cookie。
|
||||
|
||||
@module moonsource_netease.qrlogin
|
||||
--]]
|
||||
|
||||
local Text = require("utils.text")
|
||||
local _ = require("l10n").get("moonsource_netease")
|
||||
|
||||
local QRLogin = {}
|
||||
QRLogin.__index = QRLogin
|
||||
|
||||
function QRLogin:new(on_user)
|
||||
return setmetatable({ on_user = on_user, generation = 0 }, QRLogin)
|
||||
end
|
||||
|
||||
function QRLogin:cancel()
|
||||
self.generation = self.generation + 1
|
||||
if self.session then self.session:cancel() end
|
||||
if self.poll then require("ui/uimanager"):unschedule(self.poll); self.poll = nil end
|
||||
if self.waiting then self.waiting:close(); self.waiting = nil end
|
||||
if self.dialog then
|
||||
local dialog = self.dialog
|
||||
self.dialog = nil
|
||||
dialog:close()
|
||||
end
|
||||
end
|
||||
|
||||
function QRLogin:retry(text)
|
||||
self:cancel()
|
||||
require("ui.themes.widgets.confirm"):new{
|
||||
text = text, ok_text = _("重新获取"), ok_callback = function() self:start() end,
|
||||
}:show()
|
||||
end
|
||||
|
||||
function QRLogin:pollAsync(gen, uuid, last)
|
||||
if gen ~= self.generation then return end
|
||||
if os.time() >= self.deadline then self:retry(_("二维码已过期")); return end
|
||||
self.session:qrPollAsync(uuid, last, function(status, code, err)
|
||||
if gen ~= self.generation then return end
|
||||
if status == 405 then
|
||||
self.session:exchangeAsync(code, function(ok, reason)
|
||||
if gen ~= self.generation then return end
|
||||
if not ok then self:retry(reason or _("登录失败")); return end
|
||||
self:cancel()
|
||||
self.on_user()
|
||||
end)
|
||||
return
|
||||
end
|
||||
if status ~= 404 and status ~= 408 then
|
||||
self:retry(err or (status == 403 and _("已在微信中拒绝登录") or _("二维码已过期")))
|
||||
return
|
||||
end
|
||||
if status == 404 and last ~= 404 then
|
||||
require("ui.themes.widgets.toast"):new{ text = _("已扫码,请在手机上确认") }:show()
|
||||
end
|
||||
self.poll = function()
|
||||
self.poll = nil
|
||||
self:pollAsync(gen, uuid, status)
|
||||
end
|
||||
require("ui/uimanager"):scheduleIn(1, self.poll)
|
||||
end)
|
||||
end
|
||||
|
||||
function QRLogin:start()
|
||||
self:cancel()
|
||||
local gen = self.generation
|
||||
self.session = require("moonsource_netease.auth").new()
|
||||
self.deadline = os.time() + 300
|
||||
self.waiting = require("ui.themes.widgets.progress_dialog"):new{
|
||||
title = _("微信扫码登录"), text = _("正在获取二维码…"),
|
||||
on_cancel = function() self:cancel() end,
|
||||
}
|
||||
self.waiting:show()
|
||||
self.session:qrCreateAsync(function(uuid, err)
|
||||
if gen ~= self.generation then return end
|
||||
self.waiting:close()
|
||||
self.waiting = nil
|
||||
if not uuid then self:retry(err or _("获取二维码失败")); return end
|
||||
self.dialog = require("ui.themes.widgets.qr"):new{
|
||||
title = _("微信扫码登录"), caption = _("用微信扫一扫"),
|
||||
text = "https://open.weixin.qq.com/connect/confirm?uuid=" .. Text.urlEncode(uuid),
|
||||
close_callback = function()
|
||||
if gen == self.generation then self:cancel() end
|
||||
end,
|
||||
}
|
||||
self.dialog:show()
|
||||
self:pollAsync(gen, uuid)
|
||||
end)
|
||||
end
|
||||
|
||||
return QRLogin
|
||||
@@ -0,0 +1,156 @@
|
||||
--[[--
|
||||
网易云阅读电脑端账号:手机号密码、微信扫码、电脑浏览器 Cookie。
|
||||
|
||||
@module moonsource_netease.setting
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_netease")
|
||||
local CookieLogin = require("source.cookie_login")
|
||||
|
||||
local SOURCE_ID = "ext.netease"
|
||||
local Setting = {}
|
||||
local LOGIN_URL = "https://yuedu.163.com/loginForm.do?targetUrl=https%3A%2F%2Fyuedu.163.com%2Fmyinfor.do"
|
||||
|
||||
local function loggedIn()
|
||||
return require("moonsource_netease.client").loggedIn()
|
||||
end
|
||||
|
||||
local SPEC = {
|
||||
id = SOURCE_ID,
|
||||
title = _("网易云阅读账号"),
|
||||
cookie_title = _("网易云阅读登录 Cookie"),
|
||||
cookie_hint = _("按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie"),
|
||||
logout_text = _("确定退出网易云阅读账号?"),
|
||||
loggedIn = loggedIn,
|
||||
checkAsync = function(cookie, cb)
|
||||
return require("moonsource_netease.client").loginCheckAsync(cookie, cb)
|
||||
end,
|
||||
store = function(cfg, cookie)
|
||||
cfg.cookie, cfg.logged_in, cfg.csrf = cookie, true, nil
|
||||
end,
|
||||
clear = function(cfg)
|
||||
cfg.cookie, cfg.csrf, cfg.logged_in = nil, nil, nil
|
||||
end,
|
||||
}
|
||||
|
||||
Setting.browser = {
|
||||
domain = "yuedu.163.com",
|
||||
scope = "cookie",
|
||||
login = LOGIN_URL,
|
||||
check = "https://yuedu.163.com/myinfor.do",
|
||||
icon = "https://yuedust.yuedu.126.net/favicon.ico",
|
||||
isAuthorized = function() return CookieLogin.authorized(SPEC) end,
|
||||
apply = function(payload, cb)
|
||||
CookieLogin.save(SPEC, CookieLogin.fromPayload(payload, "163.com"), cb)
|
||||
end,
|
||||
}
|
||||
|
||||
local function connected()
|
||||
if require("ui/network/manager"):isConnected() then return true end
|
||||
require("ui.themes.widgets.toast"):new{ text = _("网络不可用,请先连接 Wi-Fi"), timeout = 3 }:show()
|
||||
return false
|
||||
end
|
||||
|
||||
local function onLogin()
|
||||
require("source.registry").reload()
|
||||
require("ui.themes.widgets.toast"):new{ text = _("登录成功"), timeout = 2 }:show()
|
||||
end
|
||||
|
||||
local function showLogin()
|
||||
local Toast = require("ui.themes.widgets.toast")
|
||||
local session = require("moonsource_netease.auth").new()
|
||||
local closed, busy, verification, waiting = false, false, nil, nil
|
||||
local dialog
|
||||
local function submit(phone, password, code)
|
||||
if closed or busy or not connected() then return end
|
||||
busy = true
|
||||
waiting = require("ui.themes.widgets.progress_dialog"):new{
|
||||
title = _("网易云阅读账号"), text = _("正在登录…"),
|
||||
on_cancel = function()
|
||||
session:cancel()
|
||||
busy = false
|
||||
waiting:close()
|
||||
waiting = nil
|
||||
end,
|
||||
}
|
||||
waiting:show()
|
||||
session:passwordAsync(phone, password, code, function(ok, err, needs_code)
|
||||
busy = false
|
||||
waiting:close()
|
||||
waiting = nil
|
||||
if closed then return end
|
||||
if ok then dialog:close(); onLogin(); return end
|
||||
Toast:new{ text = err or _("登录失败"), timeout = 3 }:show()
|
||||
if needs_code then
|
||||
verification = require("moonsource_netease.verify").new(session, function(value)
|
||||
verification = nil
|
||||
if value then submit(phone, password, value) end
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end
|
||||
dialog = require("ui.themes.widgets.input"):new{
|
||||
title = _("网易云阅读账号"),
|
||||
keep_open = true,
|
||||
fields = {
|
||||
{ label = _("手机号"), text = "", input_type = "number" },
|
||||
{ label = _("密码"), text = "", text_type = "password" },
|
||||
},
|
||||
ok_text = _("登录"),
|
||||
close_callback = function()
|
||||
closed = true
|
||||
if verification then verification:cancel() end
|
||||
session:cancel()
|
||||
if waiting then waiting:close(); waiting = nil end
|
||||
end,
|
||||
callback = function(phone, password)
|
||||
phone = require("utils.text").trim(phone)
|
||||
if #phone ~= 11 or not phone:match("^%d+$") then
|
||||
Toast:new{ text = _("请填写正确的手机号"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
if password == "" then
|
||||
Toast:new{ text = _("请填写密码"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
submit(phone, password)
|
||||
end,
|
||||
}
|
||||
dialog:show()
|
||||
end
|
||||
|
||||
function Setting.rowStatus()
|
||||
local on = loggedIn()
|
||||
return on and _("已登录") or _("未登录 · 点此登录"), on
|
||||
end
|
||||
|
||||
function Setting.rows()
|
||||
local Row = require("ui.themes.widgets.row")
|
||||
local rows = {
|
||||
function(iw)
|
||||
local status, status_on = Setting.rowStatus()
|
||||
return Row.widget({
|
||||
width = iw, chevron = true, kind = "nav", icon = "account_circle",
|
||||
title = _("网易云阅读账号"), subtitle = _("手机号密码登录"),
|
||||
status = status, status_on = status_on, callback = showLogin,
|
||||
})
|
||||
end,
|
||||
function(iw)
|
||||
return Row.widget({
|
||||
width = iw, chevron = true, kind = "nav", icon = "qr_code",
|
||||
title = _("微信扫码登录"), subtitle = _("打开微信,扫屏幕上的二维码"),
|
||||
callback = function()
|
||||
if connected() then require("moonsource_netease.qrlogin"):new(onLogin):start() end
|
||||
end,
|
||||
})
|
||||
end,
|
||||
CookieLogin.browserRow(),
|
||||
CookieLogin.pasteRow(SPEC),
|
||||
}
|
||||
if loggedIn() then rows[#rows + 1] = CookieLogin.logoutRow(SPEC) end
|
||||
return rows
|
||||
end
|
||||
|
||||
Setting.login = showLogin
|
||||
|
||||
return Setting
|
||||
@@ -0,0 +1,257 @@
|
||||
--[[--
|
||||
网易云阅读数据源。
|
||||
|
||||
搜索、榜单和免费章节不需要登录。未购买章节给一页提示。
|
||||
手机号密码或微信扫码登录后,电脑端 Cookie 用于书架与阅读。
|
||||
书架本地优先增删,联网后经公共书架同步推到官网。
|
||||
|
||||
@module moonsource_netease
|
||||
--]]
|
||||
|
||||
local Browse = require("source.browse")
|
||||
local Client = require("moonsource_netease.client")
|
||||
local Mapper = require("moonsource_netease.mapper")
|
||||
local SourceBase = require("source.base")
|
||||
local Text = require("utils.text")
|
||||
local Toc = require("source.toc")
|
||||
local _ = require("l10n").get("moonsource_netease")
|
||||
|
||||
local Netease = {}
|
||||
|
||||
---@return BookSourceMeta
|
||||
function Netease.meta()
|
||||
return require("moonsource_netease.loadmeta")()
|
||||
end
|
||||
|
||||
---@class NeteaseSource : SourceBase
|
||||
local Source = setmetatable({ shelf_add = true }, { __index = SourceBase })
|
||||
Source.__index = Source
|
||||
|
||||
---@return NeteaseSource
|
||||
function Netease.new()
|
||||
local meta = Netease.meta()
|
||||
return setmetatable({ id = meta.id, name = meta.name, type = meta.type }, Source)
|
||||
end
|
||||
|
||||
--- 未登录仍可搜索和读免费章节。进度上行只在登录后发生,避免每次唤醒都重试。
|
||||
---@return boolean
|
||||
function Source:configured()
|
||||
return Client.loggedIn()
|
||||
end
|
||||
|
||||
function Source:syncBooksAsync(opts, cb)
|
||||
if not self:configured() then return SourceBase.skipped(cb, "netease not logged in") end
|
||||
return require("source.shelf").syncAsync(self.id, Client, nil, opts, cb)
|
||||
end
|
||||
|
||||
---@param keyword string
|
||||
---@param cb fun(res: { data: Book[] }|nil, err: string|nil)
|
||||
function Source:searchAsync(keyword, cb)
|
||||
return Client.searchAsync(keyword, function(books, err)
|
||||
if not books then cb(nil, err or _("请求失败")); return end
|
||||
cb({ data = books })
|
||||
end)
|
||||
end
|
||||
|
||||
---@param opts { node?: string, limit?: integer }|nil
|
||||
---@param cb fun(res: { data: Book[], groups: StoreChipGroup[] }|nil, err: string|nil)
|
||||
function Source:browseAsync(opts, cb)
|
||||
local node = opts and opts.node or ""
|
||||
local channel, category, sort, pay, state = node:match(
|
||||
"^(%a+):(%w+):(%a+):([012]):([012])$")
|
||||
channel = channel or node
|
||||
if channel ~= "male" and channel ~= "female" and channel ~= "publish" then channel = "male" end
|
||||
if sort ~= "new" then sort = "hot" end
|
||||
category, pay, state = category or "all", pay or "0", state or "0"
|
||||
if channel == "publish" then state = "0" end
|
||||
local cancelled, job, pending = false, nil, true
|
||||
local initial = Client.categoriesAsync(function(categories, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not categories then cb(nil, err); return end
|
||||
local valid = category == "all"
|
||||
for _idx, row in ipairs(categories[channel]) do
|
||||
if row.id == category then valid = true end
|
||||
end
|
||||
if not valid then category = "all" end
|
||||
local values = { channel, category, sort, pay, state }
|
||||
local function chips(title, rows, position)
|
||||
local list = {}
|
||||
for _idx, row in ipairs(rows) do
|
||||
local node = { unpack(values) }
|
||||
node[position] = row.id
|
||||
if position == 1 then node[2], node[5] = "all", "0" end
|
||||
list[#list + 1] = { id = table.concat(node, ":"), title = row.title }
|
||||
end
|
||||
local active = { unpack(values) }
|
||||
if position == 1 then active[2], active[5] = "all", "0" end
|
||||
local group = Browse.group(list, table.concat(active, ":"))
|
||||
group.title = title
|
||||
return group
|
||||
end
|
||||
local cats = { { id = "all", title = _("全部") } }
|
||||
for _idx, row in ipairs(categories[channel]) do cats[#cats + 1] = row end
|
||||
local groups = {
|
||||
chips(nil, {
|
||||
{ id = "male", title = _("男频") }, { id = "female", title = _("女频") },
|
||||
{ id = "publish", title = _("出版") },
|
||||
}, 1),
|
||||
chips(_("分类"), cats, 2),
|
||||
chips(_("排序"), { { id = "hot", title = _("热门") }, { id = "new", title = _("最新") } }, 3),
|
||||
chips(_("价格"), {
|
||||
{ id = "0", title = _("全部") }, { id = "1", title = _("免费") }, { id = "2", title = _("付费") },
|
||||
}, 4),
|
||||
}
|
||||
if channel ~= "publish" then
|
||||
groups[#groups + 1] = chips(_("状态"), {
|
||||
{ id = "0", title = _("全部") }, { id = "1", title = _("连载中") }, { id = "2", title = _("已完结") },
|
||||
}, 5)
|
||||
end
|
||||
job = Browse.pages(function(page, done)
|
||||
return Client.catalogAsync({
|
||||
channel = channel, category = category, sort = sort, pay = pay, state = state,
|
||||
}, page, done)
|
||||
end, opts and opts.limit or 60, groups, function(result, reason)
|
||||
if not cancelled then cb(result, reason) end
|
||||
end)
|
||||
end)
|
||||
if pending then job = initial end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job:cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
function Source:getDetailAsync(identity, cb)
|
||||
return Client.infoAsync(identity.stable_id, function(wire, err)
|
||||
local detail = wire and Mapper.detail(wire)
|
||||
if detail then cb(require("book.store").rememberDetail(detail)); return end
|
||||
local row = identity.book or require("book.books_db").get(self.id, identity.stable_id)
|
||||
if row then cb(row); return end
|
||||
cb(nil, err or _("请求失败"))
|
||||
end)
|
||||
end
|
||||
|
||||
---@param self NeteaseSource
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
|
||||
local function fetchTocAsync(self, identity, cb)
|
||||
return Client.infoAsync(identity.stable_id, function(wire, err)
|
||||
if not wire then cb(nil, err); return end
|
||||
local toc = Mapper.chapters(wire)
|
||||
if not toc or #toc == 0 then cb(nil, _("网易云阅读目录为空")); return end
|
||||
if not Toc.put(self.id, identity.stable_id, toc) then cb(nil, _("章节目录保存失败")); return end
|
||||
cb(toc)
|
||||
end)
|
||||
end
|
||||
|
||||
function Source:loadTocAsync(identity, cb, force)
|
||||
if not force then
|
||||
local cached = Toc.read(self.id, identity.stable_id)
|
||||
if cached and #cached > 0 and (cached[1].toc_version == Mapper.TOC_VERSION
|
||||
or not require("ui/network/manager"):isConnected()) then
|
||||
return SourceBase.defer(cb, cached)
|
||||
end
|
||||
end
|
||||
return fetchTocAsync(self, identity, cb)
|
||||
end
|
||||
|
||||
Source.refreshTocAsync = fetchTocAsync
|
||||
|
||||
---@param open boolean
|
||||
---@return ChapterFetchContent
|
||||
local function fetcher(open)
|
||||
return function(identity, chapter, done)
|
||||
local function finish(html, err)
|
||||
if html then
|
||||
if chapter.vip then Toc.markPaid("ext.netease", identity.stable_id, chapter.uid, true) end
|
||||
done({ title = chapter.title, html = html })
|
||||
return
|
||||
end
|
||||
if not open or err ~= _("本章未购买") then done(nil, err); return end
|
||||
local notice = string.format(_("这是 VIP 章节,你还没有购买。\n请先在%s购买本章,再回来阅读。"), _("网易云阅读"))
|
||||
done({ title = chapter.title, html = Text.chapterHtml(chapter.title, notice), temporary = true })
|
||||
end
|
||||
if chapter.vip and not Client.loggedIn() then return finish(nil, _("本章未购买")) end
|
||||
return Client.contentAsync(identity.stable_id, chapter, finish)
|
||||
end
|
||||
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 = fetcher(true),
|
||||
}, cb)
|
||||
end
|
||||
|
||||
function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
|
||||
local Chapter = require("source.chapter")
|
||||
return Chapter.prefetchAsync(identity, identity.book, toc, from_idx, count, {
|
||||
fetchContent = fetcher(false),
|
||||
}, cb)
|
||||
end
|
||||
|
||||
function Source:cacheAllChaptersAsync(identity, on_progress, cb, range)
|
||||
local Chapter = require("source.chapter")
|
||||
return Chapter.cacheAllAsync(self, identity, fetcher(false), on_progress, cb, nil, range, nil)
|
||||
end
|
||||
|
||||
function Source:getProgressAsync(identity, cb)
|
||||
local cancelled, job = false, nil
|
||||
job = Client.infoAsync(identity.stable_id, function(wire, err)
|
||||
if cancelled then return end
|
||||
if not wire then cb(nil, err); return end
|
||||
local toc = Mapper.chapters(wire)
|
||||
local idx = Mapper.progressIndex(wire)
|
||||
if not toc or not idx or not toc[idx] then cb(nil, nil, { empty = true }); return end
|
||||
if not Toc.put(self.id, identity.stable_id, toc) then cb(nil, _("章节目录保存失败")); return end
|
||||
local chapter_fraction = math.max(0, math.min(1, (tonumber(wire.curPagePercent) or 0) / 100))
|
||||
cb({
|
||||
chapter_idx = idx,
|
||||
chapter_title = toc[idx].title,
|
||||
chapter_fraction = chapter_fraction,
|
||||
fraction = (idx - 1 + chapter_fraction) / #toc,
|
||||
extra = { chapter_uid = toc[idx].uid, chapter_idx = idx, chapter_fraction = chapter_fraction },
|
||||
})
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job and job.cancel then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
function Source:bookReviewsAsync(identity, cb)
|
||||
return Client.commentsAsync(identity.stable_id, function(items, err, pages)
|
||||
if not items then cb(nil, err); return end
|
||||
cb({ items = items, pages = pages })
|
||||
end)
|
||||
end
|
||||
|
||||
function Source:putProgressAsync(identity, pos, cb)
|
||||
if not self:configured() then return SourceBase.defer(cb, nil, _("未登录网易云阅读")) end
|
||||
local cancelled, job, pending = false, nil, true
|
||||
local initial = self:loadTocAsync(identity, function(toc, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not toc then cb(nil, err); return end
|
||||
local idx = tonumber(pos.chapter_idx) or tonumber(identity.chapter_idx)
|
||||
if idx then idx = math.floor(idx) end
|
||||
local chapter = idx and toc[idx]
|
||||
if not chapter then
|
||||
cb(nil, _("缺少网易云阅读章节位置"))
|
||||
return
|
||||
end
|
||||
job = Client.putProgressAsync(identity.stable_id, toc, idx, pos.chapter_fraction, function(ok, perr)
|
||||
cb(ok and true or nil, perr)
|
||||
end)
|
||||
end)
|
||||
if pending then job = initial end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job and job.cancel then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
return Netease
|
||||
@@ -0,0 +1,72 @@
|
||||
--[[--
|
||||
电脑端密码登录的图形验证码,与登录窗口共用匿名会话。
|
||||
|
||||
@module moonsource_netease.verify
|
||||
--]]
|
||||
|
||||
local Text = require("utils.text")
|
||||
local _ = require("l10n").get("moonsource_netease")
|
||||
|
||||
local Verification = {}
|
||||
Verification.__index = Verification
|
||||
|
||||
function Verification:cancel()
|
||||
if self.done then return end
|
||||
self.done = true
|
||||
self.session:cancel()
|
||||
if self.waiting then self.waiting:close(); self.waiting = nil end
|
||||
if self.dialog then self.dialog:close(); self.dialog = nil end
|
||||
end
|
||||
|
||||
function Verification:finish(code)
|
||||
if self.done then return end
|
||||
self:cancel()
|
||||
self.callback(code)
|
||||
end
|
||||
|
||||
function Verification:load()
|
||||
if self.done then return end
|
||||
if self.dialog then self.dialog:close(); self.dialog = nil end
|
||||
self.waiting = require("ui.themes.widgets.progress_dialog"):new{
|
||||
title = _("网易云阅读图形验证码"), text = _("正在加载…"),
|
||||
on_cancel = function() self:finish() end,
|
||||
}
|
||||
self.waiting:show()
|
||||
self.session:captchaAsync(function(raw, err)
|
||||
if self.done then return end
|
||||
self.waiting:close()
|
||||
self.waiting = nil
|
||||
local bitmap = raw and require("ui/renderimage"):renderImageData(raw, #raw, false)
|
||||
if not bitmap then
|
||||
require("ui.themes.widgets.toast"):new{ text = err or _("图形验证码加载失败") }:show()
|
||||
self:finish()
|
||||
return
|
||||
end
|
||||
local Theme = require("ui.themes")
|
||||
self.dialog = require("ui.themes.widgets.input"):new{
|
||||
title = _("网易云阅读图形验证码"),
|
||||
header_widget = require("ui/widget/imagewidget"):new{
|
||||
image = bitmap, width = Theme.px(180), height = Theme.px(90), scale_factor = 0, alpha = true,
|
||||
},
|
||||
fields = { { label = _("请输入图形码"), text = "" } },
|
||||
ok_text = _("登录"),
|
||||
cancel_callback = function() self:finish() end,
|
||||
actions = { { text = _("刷新"), callback = function() self:load() end } },
|
||||
validate = function(code)
|
||||
if Text.trim(code) ~= "" then return not self.done end
|
||||
require("ui.themes.widgets.toast"):new{ text = _("请输入图形码") }:show()
|
||||
return false
|
||||
end,
|
||||
callback = function(code) self:finish(Text.trim(code)) end,
|
||||
}
|
||||
self.dialog:show()
|
||||
end)
|
||||
end
|
||||
|
||||
function Verification.new(session, cb)
|
||||
local self = setmetatable({ session = session, callback = cb }, Verification)
|
||||
self:load()
|
||||
return self
|
||||
end
|
||||
|
||||
return Verification
|
||||
Reference in new issue
Block a user