local Request = require("http.request") local JSON = require("json") local _ = require("moonsource_bilimanga.l10n") local BASE = "https://manga.bilibili.com" local NOV = "27" local function getCookie() local cfg = require("utils.settings").getSource("bilimanga") return cfg.cookie or "" end local function makeHeaders(extra) local 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", ["Content-Type"] = "application/json", ["Referer"] = BASE .. "/", } local cookie = getCookie() if cookie and cookie ~= "" then headers.Cookie = cookie end if extra then for k, v in pairs(extra) do headers[k] = v end end return headers end local function twirpAsync(service, method, body, cb) local url = BASE .. "/twirp/" .. service .. "." .. method .. "?nov=" .. NOV local state = { cancelled = false } local function finish(data, err) if state.cancelled then return end state.job = nil cb(data, err) end local job = Request.request({ url = url, method = "POST", body = JSON.encode(body or {}), headers = makeHeaders(), 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 local code = tonumber(res.code) if not Request.ok(code) then if code == 401 or code == 403 then local settings = require("utils.settings") settings.saveSource("bilimanga", { cookie = "" }) cb(nil, _("登录已过期,请重新输入 Cookie")) else finish(nil, _("请求失败")) end return end local ok, data = pcall(JSON.decode, res.body) if not ok or type(data) ~= "table" then finish(nil, _("响应无效")) return end finish(data) end) if not state.job then -- Request already completed synchronously (unlikely but possible) return { cancel = function() end } end state.job = job return { cancel = function() state.cancelled = true if state.job then state.job.cancel() end end, } end local function twirpAsyncSimple(service, method, body, cb) return twirpAsync(service, method, body, function(data, err) if err then cb(nil, err) else cb(data) end end) end -- ============================================================ -- Comic 服务 -- ============================================================ function Client.searchAsync(keyword, page, page_size, cb) return twirpAsync("comic.v1.Comic", "Search", { keyword = keyword, page = page or 1, page_size = page_size or 20, }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.comicDetailAsync(comic_id, cb) return twirpAsync("comic.v1.Comic", "ComicDetail", { comic_id = tonumber(comic_id), }, function(wire, err) if not wire then cb(nil, err); return end cb(wire.comic) end) end function Client.comicDetailLiteAsync(comic_id, cb) return twirpAsync("comic.v1.Comic", "ComicDetailLite", { comic_id = tonumber(comic_id), }, function(wire, err) if not wire then cb(nil, err); return end cb(wire.comic) end) end function Client.imageIndexAsync(ep_id, cb) return twirpAsync("comic.v1.Comic", "GetImageIndex", { ep_id = tonumber(ep_id), mode = 2, quality = 5, type = 1, }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.homeHotAsync(cb) return twirpAsync("comic.v1.Comic", "HomeHot", { tab = 0, age = 0, page = 1, page_size = 30, }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.classPageAsync(cb) return twirpAsync("comic.v1.Comic", "ClassPage", { page = 1, page_size = 20, }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.recommendAsync(comic_id, cb) return twirpAsync("comic.v1.Comic", "Recommend", { comic_id = tonumber(comic_id), }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end -- ============================================================ -- Bookshelf 服务 -- ============================================================ function Client.listFavoriteAsync(page_num, page_size, cb) return twirpAsync("bookshelf.v1.Bookshelf", "ListFavorite", { page_num = page_num or 1, page_size = page_size or 20, from = "web", source = "web", }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.listHistoryAsync(page_num, page_size, cb) return twirpAsync("bookshelf.v1.Bookshelf", "ListHistory", { page_num = page_num or 1, page_size = page_size or 20, type = 0, }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.addHistoryAsync(comic_id, ep_id, cb) return twirpAsync("bookshelf.v1.Bookshelf", "AddHistory", { comic_id = tonumber(comic_id), ep_id = tonumber(ep_id), type = 1, from = "web", source = "web", }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.addFavoriteAsync(comic_id, cb) return twirpAsync("bookshelf.v1.Bookshelf", "AddFavorite", { comic_id = tonumber(comic_id), type = 0, from = "web", source = "web", }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.deleteFavoriteAsync(comic_id, cb) return twirpAsync("bookshelf.v1.Bookshelf", "DeleteFavorite", { comic_id = tonumber(comic_id), type = 0, from = "web", source = "web", }, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end -- ============================================================ -- User 服务 -- ============================================================ function Client.getUserInfoAsync(cb) return twirpAsync("user.v1.User", "GetInitInfo", {}, function(wire, err) if not wire then cb(nil, err); return end cb(wire) end) end function Client.checkLoginAsync(cb) return Client.getUserInfoAsync(function(wire, err) if not wire then cb(nil, err); return end if wire.user_info then cb(wire.user_info) else cb(nil, _("获取用户信息失败")) end end) end -- ============================================================ -- 公共工具 -- ============================================================ function Client.hasAccount() local cfg = require("utils.settings").getSource("bilimanga") return cfg.cookie ~= nil and cfg.cookie ~= "" end function Client.setCookie(cookie) local cfg = require("utils.settings").getSource("bilimanga") cfg.cookie = cookie or "" require("utils.settings").saveSource("bilimanga", cfg) end function Client.clearCookie() Client.setCookie("") end -- 分页遍历 function Client.paginateAsync(fetchFn, cb) local all_items = {} local page = 1 local function fetchPage() fetchFn(page, function(result, err) if err then cb(nil, err) return end local items = result and result.list or result and result.items or {} if not items or #items == 0 then cb(all_items) return end for i = 1, #items do all_items[#all_items + 1] = items[i] end local has_more = result and result.has_more if not has_more or has_more == false then cb(all_items) return end page = page + 1 fetchPage() end) end fetchPage() end return Client