93 lines
3.0 KiB
Lua
93 lines
3.0 KiB
Lua
--[[--
|
|
LinguoLand 数据映射。将 API 响应转换为月读书源标准数据结构。
|
|
|
|
@module moonsource_linguoland.mapper
|
|
--]]
|
|
|
|
local Mapper = {}
|
|
local _ = require("l10n").get("moonsource_linguoland")
|
|
|
|
--- 只要字符串。JSON null 在没洗过的回包里是函数,不能进书名和简介。
|
|
local function str(value)
|
|
if type(value) == "string" and value ~= "" then return value end
|
|
end
|
|
|
|
--- 书架条目 → Book[]
|
|
---@param entries table[]
|
|
---@return table[]
|
|
function Mapper.shelfBooks(entries)
|
|
local books = {}
|
|
for _idx, entry in ipairs(entries) do
|
|
local catalog = entry.catalog
|
|
if catalog then
|
|
local storage_ref = str(catalog.storageRef) or ""
|
|
local book = {
|
|
source_id = "linguoland",
|
|
stable_id = tostring(catalog.id),
|
|
title = str(entry.titleOverride) or str(catalog.title) or _("未知书名"),
|
|
authors = str(entry.authorOverride) or str(catalog.author) or _("未知作者"),
|
|
cover = str(catalog.coverUrl) or "",
|
|
intro = str(catalog.description) or "",
|
|
category = str(catalog.level) or "",
|
|
meta = {
|
|
language = str(catalog.language),
|
|
source = {
|
|
shelf_entry_id = entry.id,
|
|
storage_ref = storage_ref,
|
|
origin = str(entry.origin),
|
|
format = str(catalog.format),
|
|
},
|
|
},
|
|
}
|
|
if storage_ref ~= "" then book._epub_url = storage_ref end
|
|
books[#books + 1] = book
|
|
end
|
|
end
|
|
return books
|
|
end
|
|
|
|
--- 书城目录条目 → Book[]
|
|
---@param items table[]
|
|
---@return table[]
|
|
function Mapper.catalogBooks(items)
|
|
local books = {}
|
|
for _idx, item in ipairs(items) do
|
|
local storage_ref = str(item.storageRef) or ""
|
|
local book = {
|
|
source_id = "linguoland",
|
|
stable_id = tostring(item.id),
|
|
title = str(item.title) or _("未知书名"),
|
|
authors = str(item.author) or _("未知作者"),
|
|
cover = str(item.coverUrl) or "",
|
|
intro = str(item.description) or "",
|
|
category = str(item.level) or "",
|
|
meta = {
|
|
language = str(item.language),
|
|
source = {
|
|
storage_ref = storage_ref,
|
|
format = str(item.format),
|
|
},
|
|
},
|
|
}
|
|
if storage_ref ~= "" then book._epub_url = storage_ref end
|
|
books[#books + 1] = book
|
|
end
|
|
return books
|
|
end
|
|
|
|
--- 合集列表 → BrowseChipGroup[]
|
|
---@param collections table[]
|
|
---@return table[]
|
|
function Mapper.collectionsToGroups(collections)
|
|
local groups = {}
|
|
for _idx, col in ipairs(collections) do
|
|
groups[#groups + 1] = {
|
|
id = tostring(col.id),
|
|
title = col.title or col.titleI18n and col.titleI18n["zh-CN"] or "",
|
|
}
|
|
end
|
|
return groups
|
|
end
|
|
|
|
return Mapper
|