--[[-- 中文维基文库 HTTP。只打 api.php,带可识别的 User-Agent。 首页的 Portal 列表在这次进程里留一份。书目和正文不缓存成库。 @module moonsource_wikisource.client --]] local Mapper = require("moonsource_wikisource.mapper") local Request = require("http.request") local SourceBase = require("source.base") local _ = require("l10n").get("moonsource_wikisource") local BASE = "https://zh.wikisource.org/w/api.php" local UA = "MoonWikisource/0.1 (https://moon.ankio.net)" local HEADERS = { ["User-Agent"] = UA, Accept = "application/json", } local PAGE_SIZE = "50" ---@class WikisourceClient ---@field portals { id: string, title: string }[]|nil ---@field page { title: string, html: string }|nil local Client = {} Client.__index = Client Client.UA = UA ---@return WikisourceClient function Client.new() return setmetatable({}, Client) end ---@param s string ---@return string local function encode(s) return (s:gsub("[^%w%-_%.~]", function(c) return string.format("%%%02X", string.byte(c)) end)) end ---@param params table ---@return string local function url(params) local parts = { "format=json", "formatversion=2" } for key, value in pairs(params) do parts[#parts + 1] = encode(key) .. "=" .. encode(value) end return BASE .. "?" .. table.concat(parts, "&") end ---@param params table ---@param cb fun(data: table|nil, err: string|nil) ---@return { cancel: fun() }|nil function Client:getJson(params, cb) local pending = true local inner = Request.get(url(params), { headers = HEADERS, timeout = require("http.timeout").net(1), }, function(body, err) pending = false if not body then cb(nil, err or _("网络请求失败")); return end if body:sub(1, 1) ~= "{" then cb(nil, _("响应无效")); return end local ok, data = pcall(require("json").decode, body) if not ok or type(data) ~= "table" or type(data.error) == "table" then cb(nil, _("响应无效")) return end cb(data) end) if pending then return inner end end ---@param cb fun(portals: { id: string, title: string }[]|nil, err: string|nil) ---@return { cancel: fun() }|nil function Client:portalsAsync(cb) if self.portals then return SourceBase.defer(cb, self.portals) end return self:getJson({ action = "parse", page = "Wikisource:首页", prop = "wikitext", }, function(data, err) if not data then cb(nil, err); return end local text = type(data.parse) == "table" and data.parse.wikitext or nil local portals, map_err = Mapper.portals(text) if not portals then cb(nil, map_err); return end self.portals = portals cb(portals) end) end ---@param portal string ---@param cont string|nil ---@param cb fun(data: table|nil, err: string|nil) ---@return { cancel: fun() }|nil function Client:linksAsync(portal, cont, cb) if type(portal) ~= "string" or portal == "" then return SourceBase.defer(cb, nil, _("响应无效")) end local params = { action = "query", prop = "links", titles = portal, plnamespace = "0", pllimit = PAGE_SIZE, } if type(cont) == "string" and cont ~= "" then params.plcontinue = cont end return self:getJson(params, cb) end ---@param keyword string ---@param cb fun(data: table|nil, err: string|nil) ---@return { cancel: fun() }|nil function Client:searchAsync(keyword, cb) if type(keyword) ~= "string" or keyword == "" then return SourceBase.defer(cb, nil, _("响应无效")) end return self:getJson({ action = "query", list = "search", srsearch = keyword, srnamespace = "0", srlimit = "20", }, cb) end ---@param title string ---@param cb fun(page: { title: string, html: string }|nil, err: string|nil) ---@return { cancel: fun() }|nil function Client:parseAsync(title, cb) if type(title) ~= "string" or title == "" then return SourceBase.defer(cb, nil, _("响应无效")) end local cached = self.page if cached and cached.title == title then return SourceBase.defer(cb, cached) end return self:getJson({ action = "parse", page = title, prop = "text", disableeditsection = "1", disabletoc = "1", }, function(data, err) if not data then cb(nil, err); return end local parse = data.parse local html = type(parse) == "table" and parse.text or nil local name = type(parse) == "table" and parse.title or nil if type(html) ~= "string" or type(name) ~= "string" or name == "" then cb(nil, _("响应无效")) return end local page = { title = name, html = html } self.page = page cb(page) end) end return Client