Refactor: Using English key (#728)

* fix: use english key instead of native

* fix: ukrainian

* fix: web test with new plugins map
This commit is contained in:
nyagami
2024-01-27 09:26:19 +07:00
committed by GitHub
parent 62fc988019
commit 6ff206eab3
5 changed files with 265 additions and 264 deletions

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

+5 -5
View File
@@ -2,14 +2,14 @@ export const languages = {
Arabic: "العربية",
Chinese: "中文, 汉语, 漢語",
English: "English",
French: "français, française",
French: "Français",
Indonesian: "Bahasa Indonesia",
Japanese: "日本語",
Korean: "한국어, 조선말",
Korean: "조선말, 한국어",
Portuguese: "Português",
Russian: "русский язык",
Spanish: "español, castellano",
Russian: "Русский",
Spanish: "Español",
Turkish: "Türkçe",
Ukraine: "Україна",
Ukrainian: "Українська",
Vietnamese: "Tiếng Việt",
} as const;
@@ -1,255 +1,256 @@
import { Plugin } from "@typings/plugin";
import { fetchApi, fetchFile } from "@libs/fetch";
import { NovelStatus } from "@libs/novelStatus";
import dayjs from "dayjs";
class Smakolykytl implements Plugin.PluginBase {
id = "smakolykytl";
name = "Смаколики";
site = "https://smakolykytl.site/";
version = "1.0.0";
icon = "src/ua/smakolykytl/icon.png";
async popularNovels(
pageNo: number,
{ showLatestNovels, filters }: Plugin.PopularNovelsOptions,
): Promise<Plugin.NovelItem[]> {
const url = showLatestNovels
? "https://api.smakolykytl.site/api/user/updates"
: "https://api.smakolykytl.site/api/user/projects";
const result = await fetchApi(url);
const json = (await result.json()) as response;
const novels: Plugin.NovelItem[] = [];
(json?.projects || json?.updates)?.forEach((novel) =>
novels.push({
name: novel.title,
cover: novel.image.url,
url: this.site + "titles/" + novel.id,
}),
);
return novels;
}
async parseNovelAndChapters(novelUrl: string): Promise<Plugin.SourceNovel> {
const id = novelUrl.split("/").pop();
const result = await fetchApi(
"https://api.smakolykytl.site/api/user/projects/" + id,
);
const book = (await result.json()) as response;
const novel: Plugin.SourceNovel = {
url: novelUrl,
name: book?.project?.title,
cover: book?.project?.image?.url,
summary: book?.project?.description,
author: book?.project?.author,
status: book?.project?.status_translate.includes("Триває")
? NovelStatus.Ongoing
: NovelStatus.Completed,
};
const tags = [book?.project?.genres, book?.project?.tags]
.flat()
.map((tags) => tags?.title)
.filter((tags) => tags);
if (tags.length) {
novel.genres = tags.join(", ");
}
const chapters: Plugin.ChapterItem[] = [];
const res = await fetchApi(
"https://api.smakolykytl.site/api/user/projects/" + id + "/books",
);
const data = (await res.json()) as response;
data?.books?.forEach(
(volume) =>
volume?.chapters?.map((chapter) =>
chapters.push({
name: volume.title + " " + chapter.title,
releaseTime: dayjs(chapter.modifiedAt).format("LLL"),
url: this.site + "read/" + chapter.id,
}),
),
);
novel.chapters = chapters;
return novel;
}
async parseChapter(chapterUrl: string): Promise<string> {
const id = chapterUrl.split("/").pop();
const result = await fetchApi(
"https://api.smakolykytl.site/api/user/chapters/" + id,
);
const json = (await result.json()) as response;
const chapterRaw: HTML[] = JSON.parse(json?.chapter?.content || "[]");
const chapterText = jsonToHtml(chapterRaw);
return chapterText;
}
async searchNovels(
searchTerm: string,
//pageNo: number | undefined = 1,
): Promise<Plugin.NovelItem[]> {
const result = await fetchApi(
"https://api.smakolykytl.site/api/user/projects",
);
const json = (await result.json()) as response;
const novels: Plugin.NovelItem[] = [];
json?.projects
?.filter(
(novel) =>
novel.title.includes(searchTerm) || String(novel.id) === searchTerm,
)
?.forEach((novel) =>
novels.push({
name: novel.title,
cover: novel.image.url,
url: this.site + "titles/" + novel.id,
}),
);
return novels;
}
fetchImage = fetchFile;
}
export default new Smakolykytl();
function jsonToHtml(json: HTML[], html: string = "") {
json.forEach((element) => {
switch (element.type) {
case "hardBreak":
html += "<br>";
break;
case "horizontalRule":
html += "<hr>";
break;
case "image":
if (element.attrs) {
const attrs = Object.entries(element.attrs)
.filter((attr) => attr?.[1])
.map((attr) => `${attr[0]}="${attr[1]}"`);
html += "<img " + attrs.join("; ") + ">";
}
break;
case "paragraph":
html +=
"<p>" +
(element.content ? jsonToHtml(element.content) : "<br>") +
"</p>";
break;
case "text":
html += element.text;
break;
default:
html += JSON.stringify(element, null, "\t"); //maybe I missed something.
break;
}
});
return html;
}
interface response {
projects?: TopLevelProject[];
updates?: Update[];
project?: TopLevelProject;
books?: BookElement[];
chapter?: TopLevelChapter;
}
interface BookElement {
id: number;
rank: number;
title: string;
chapters: PurpleChapter[];
}
interface PurpleChapter {
id: number;
title: string;
rank: string;
modifiedAt: Date;
}
interface TopLevelChapter {
id: number;
title: string;
rank: string;
content: string;
modifiedAt: Date;
book: ChapterBook;
}
interface ChapterBook {
id: number;
rank: number;
title: string;
chapters: FluffyChapter[];
project: BookProject;
}
interface FluffyChapter {
id: number;
rank: string;
}
interface BookProject {
id: number;
}
interface TopLevelProject {
id: number;
title: string;
description: string;
author: string;
translator: string;
modifiedAt: Date;
alternatives: string;
release: string;
nation: string;
status: string;
status_translate: string;
image: Image;
tags?: Genre[];
genres?: Genre[];
}
interface Genre {
id: number;
title: string;
}
interface Image {
id: number;
url: string;
name: string;
}
interface Update {
id: number;
title: string;
author: string;
translator: string;
modifiedAt: Date;
image: Image;
}
interface HTML {
type: string;
content?: HTML[];
attrs?: Attrs;
text?: string;
}
interface Attrs {
src: string;
alt: string | null;
title: string | null;
}
import { Plugin } from "@typings/plugin";
import { fetchApi, fetchFile } from "@libs/fetch";
import { NovelStatus } from "@libs/novelStatus";
import dayjs from "dayjs";
class Smakolykytl implements Plugin.PluginBase {
id = "smakolykytl";
name = "Смаколики";
site = "https://smakolykytl.site/";
version = "1.0.0";
icon = "src/uk/smakolykytl/icon.png";
async popularNovels(
pageNo: number,
{ showLatestNovels, filters }: Plugin.PopularNovelsOptions,
): Promise<Plugin.NovelItem[]> {
const url = showLatestNovels
? "https://api.smakolykytl.site/api/user/updates"
: "https://api.smakolykytl.site/api/user/projects";
const result = await fetchApi(url);
const json = (await result.json()) as response;
const novels: Plugin.NovelItem[] = [];
(json?.projects || json?.updates)?.forEach((novel) =>
novels.push({
name: novel.title,
cover: novel.image.url,
url: this.site + "titles/" + novel.id,
}),
);
return novels;
}
async parseNovelAndChapters(novelUrl: string): Promise<Plugin.SourceNovel> {
const id = novelUrl.split("/").pop();
const result = await fetchApi(
"https://api.smakolykytl.site/api/user/projects/" + id,
);
const book = (await result.json()) as response;
const novel: Plugin.SourceNovel = {
url: novelUrl,
name: book?.project?.title,
cover: book?.project?.image?.url,
summary: book?.project?.description,
author: book?.project?.author,
status: book?.project?.status_translate.includes("Триває")
? NovelStatus.Ongoing
: NovelStatus.Completed,
};
const tags = [book?.project?.genres, book?.project?.tags]
.flat()
.map((tags) => tags?.title)
.filter((tags) => tags);
if (tags.length) {
novel.genres = tags.join(", ");
}
const chapters: Plugin.ChapterItem[] = [];
const res = await fetchApi(
"https://api.smakolykytl.site/api/user/projects/" + id + "/books",
);
const data = (await res.json()) as response;
data?.books?.forEach(
(volume) =>
volume?.chapters?.map((chapter) =>
chapters.push({
name: volume.title + " " + chapter.title,
releaseTime: dayjs(chapter.modifiedAt).format("LLL"),
url: this.site + "read/" + chapter.id,
}),
),
);
novel.chapters = chapters;
return novel;
}
async parseChapter(chapterUrl: string): Promise<string> {
const id = chapterUrl.split("/").pop();
const result = await fetchApi(
"https://api.smakolykytl.site/api/user/chapters/" + id,
);
const json = (await result.json()) as response;
const chapterRaw: HTML[] = JSON.parse(json?.chapter?.content || "[]");
const chapterText = jsonToHtml(chapterRaw);
return chapterText;
}
async searchNovels(
searchTerm: string,
//pageNo: number | undefined = 1,
): Promise<Plugin.NovelItem[]> {
const result = await fetchApi(
"https://api.smakolykytl.site/api/user/projects",
);
const json = (await result.json()) as response;
const novels: Plugin.NovelItem[] = [];
json?.projects
?.filter(
(novel) =>
novel.title.includes(searchTerm) || String(novel.id) === searchTerm,
)
?.forEach((novel) =>
novels.push({
name: novel.title,
cover: novel.image.url,
url: this.site + "titles/" + novel.id,
}),
);
return novels;
}
fetchImage = fetchFile;
}
export default new Smakolykytl();
function jsonToHtml(json: HTML[], html: string = "") {
json.forEach((element) => {
switch (element.type) {
case "hardBreak":
html += "<br>";
break;
case "horizontalRule":
html += "<hr>";
break;
case "image":
if (element.attrs) {
const attrs = Object.entries(element.attrs)
.filter((attr) => attr?.[1])
.map((attr) => `${attr[0]}="${attr[1]}"`);
html += "<img " + attrs.join("; ") + ">";
}
break;
case "paragraph":
html +=
"<p>" +
(element.content ? jsonToHtml(element.content) : "<br>") +
"</p>";
break;
case "text":
html += element.text;
break;
default:
html += JSON.stringify(element, null, "\t"); //maybe I missed something.
break;
}
});
return html;
}
interface response {
projects?: TopLevelProject[];
updates?: Update[];
project?: TopLevelProject;
books?: BookElement[];
chapter?: TopLevelChapter;
}
interface BookElement {
id: number;
rank: number;
title: string;
chapters: PurpleChapter[];
}
interface PurpleChapter {
id: number;
title: string;
rank: string;
modifiedAt: Date;
}
interface TopLevelChapter {
id: number;
title: string;
rank: string;
content: string;
modifiedAt: Date;
book: ChapterBook;
}
interface ChapterBook {
id: number;
rank: number;
title: string;
chapters: FluffyChapter[];
project: BookProject;
}
interface FluffyChapter {
id: number;
rank: string;
}
interface BookProject {
id: number;
}
interface TopLevelProject {
id: number;
title: string;
description: string;
author: string;
translator: string;
modifiedAt: Date;
alternatives: string;
release: string;
nation: string;
status: string;
status_translate: string;
image: Image;
tags?: Genre[];
genres?: Genre[];
}
interface Genre {
id: number;
title: string;
}
interface Image {
id: number;
url: string;
name: string;
}
interface Update {
id: number;
title: string;
author: string;
translator: string;
modifiedAt: Date;
image: Image;
}
interface HTML {
type: string;
content?: HTML[];
attrs?: Attrs;
text?: string;
}
interface Attrs {
src: string;
alt: string | null;
title: string | null;
}
+2 -2
View File
@@ -42,7 +42,7 @@ for (let language in languages) {
const langPath = path.join(root, "plugins", language.toLowerCase());
if (!fs.existsSync(langPath)) continue;
const plugins = fs.readdirSync(langPath);
json[languageNative] = [];
json[language] = [];
plugins.forEach((plugin) => {
if (plugin.startsWith(".")) return;
const instance:
@@ -74,7 +74,7 @@ for (let language in languages) {
pluginSet.add(id);
}
json[languageNative].push(info);
json[language].push(info);
console.log(name, "✅");
});
}
+2 -2
View File
@@ -21,7 +21,7 @@ export const all_plugins = (): PluginList => {
);
if (!fs.existsSync(langPath)) continue;
const plugins = fs.readdirSync(langPath);
res[languageNative] = [];
res[languageEnglish] = [];
plugins.forEach((plugin) => {
if (plugin.startsWith(".")) return;
const requirePath = `@plugins/${languageEnglish.toLowerCase()}/${
@@ -37,7 +37,7 @@ export const all_plugins = (): PluginList => {
requirePath,
icon,
}; // lang: language with native name
res[info.lang].push(info);
res[languageEnglish].push(info);
});
}
return res;