Files

62 lines
2.5 KiB
Lua

--[[--
微信读书插件接入月读通用挂点:书架大快照导入页(web.routes)、订阅页公众号来源(desktop.account_feeds)、
给本地书借段评(reader.paragraph_lender)。由 moon_source.lua 在加载时 require 一次;所有重模块都在回调里才加载。
@module moonsource_weread.extras
--]]
local Ext = require("utils.extension")
local L = require("l10n").get("moonsource_weread")
local PREFIX = "moonsource_weread."
local here = debug.getinfo(1, "S").source:match("^@(.+)/[^/]+$") or "."
local function keepAwake()
require("utils.awake").poke()
end
-- 导入要几分钟,设备自动休眠会停掉远程管理:每个请求算一次操作,导入结束请求停了自然恢复休眠计时。
Ext.add("web.routes", {
name = "wechat_import",
dir = here .. "/web",
assets = { "wechat_import.html", "wechat_import.js" },
l10n = PREFIX .. "l10n.web",
download = function(action)
keepAwake()
local ShelfImport = require(PREFIX .. "shelf_import")
if action ~= "file" or not ShelfImport.size() then return nil end
return { ctype = "application/json; charset=utf-8", filename = "wechat_shelf.json", path = ShelfImport.path() }
end,
handle = function(method, action, params, cb)
keepAwake()
local ShelfImport = require(PREFIX .. "shelf_import")
if method == "GET" and action == "status" then
cb(200, { size = ShelfImport.size() })
elseif method == "POST" and action == "batch" then
local ids, err = ShelfImport.ingest(params)
if ids then cb(200, { ids = ids }) else cb(500, { error = err }) end
elseif method == "POST" and action == "commit" then
local hidden, err = ShelfImport.commit(params.ids or {}, tonumber(params.synckey))
if hidden then cb(200, { hidden = hidden }) else cb(500, { error = err }) end
elseif method == "POST" and action == "discard" then
ShelfImport.discard()
cb(200, {})
else
cb(404, { error = "unknown action" })
end
end,
})
Ext.add("desktop.account_feeds", {
label = L("微信读书公众号"),
busy = L("正在读取微信读书书架…"),
login_hint = L("请先登录微信读书"),
empty = L("微信读书书架里没有公众号"),
mp = function() return require(PREFIX .. "mp") end,
client = function() return require(PREFIX .. "client") end,
})
Ext.add("reader.paragraph_lender", { id = "weread", label = L("微信读书") })
return {}