icon_reloading script update (#927)

- skip list as string list
 - better root dir, ts-node compatibility
 - parsing plugins.json instead of parsing folders
 - check if google returns notavailable icon
 - better log
This commit is contained in:
Justin COLLON
2024-05-11 05:49:43 +02:00
committed by GitHub
parent c50eccf867
commit f3b0c676d9
11 changed files with 92 additions and 57 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 414 B

After

Width:  |  Height:  |  Size: 289 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 590 B

After

Width:  |  Height:  |  Size: 609 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

+2 -1
View File
@@ -13,7 +13,8 @@
"host-linux": "chmod +x ./host.sh && ./host.sh",
"host-windows": "powershell ./host.ps1",
"prettier": "prettier --write \"./**/*.{js,ts}\"",
"prepare": "husky install"
"prepare": "husky install",
"icons": "npm run clearMultisrc && npm run generate && npm run json && ts-node ./scripts/icon_reloading.ts"
},
"author": "LNReader",
"license": "MIT",
+90 -56
View File
@@ -4,78 +4,112 @@ import { Plugin } from '@typings/plugin';
import { languages } from '@libs/languages';
import * as path from 'path';
const delay = (ms: number) => new Promise(res => setTimeout(res, ms));
const root = path.dirname(__dirname);
const root = path.join(__dirname, '..');
const size = 96;
const skip: { [key: string]: boolean } = {
const skip: string[] = [
//custom icons
'ReN': true,
'sektenovel': true,
'FWK.US': true,
'ReN',
'sektenovel',
'FWK.US',
'daonovel',
'dragontea',
'foxaholic',
'kiniga',
'sonicmtl',
'translatinotaku',
'wuxiaworld.site',
'moonlightnovel',
'mtl-novel',
'mysticalmerries',
'lightnovelpubvip',
'novelTL',
//low quality
'BLN': true,
'novelhall': true,
'NO.net': true,
'BLN',
'novelhall',
'NO.net',
'novelbookid',
'olaoe',
//broken icons
'guavaread': true,
'LightNovelUpdates': true,
'turkcelightnovels': true,
'novelki.pl': true,
'lunarletters': true,
//dead plugins?
'freenovel.me': true,
'daonovel': true,
'mtl-novel': true,
'novelroom': true,
'zinnovel': true,
'novelr18': true,
'novelstic': true,
'readwebnovels': true,
};
'guavaread',
'LightNovelUpdates',
'turkcelightnovels',
'novelki.pl',
'lunarletters',
'novelsparadise',
'earlynovel',
'wbnovel',
'novhell',
'RLIB',
];
const used = new Set();
used.add(path.join(root, '..', 'icons', 'coverNotAvailable.webp'));
used.add(path.join(root, 'icons', 'coverNotAvailable.webp'));
(async () => {
console.log('Loading plugins.json ⌛');
const plugin_path = path.join(root, '.dist', 'plugins.json');
if (!fs.existsSync(plugin_path)) {
console.log('❌', plugin_path, 'not found (run "json_plugins.ts" first)');
return;
}
const plugins = JSON.parse(fs.readFileSync(plugin_path, 'utf-8'));
console.log('\nDownloading icons ⌛');
for (let language in languages) {
const langPath = path.join(root, 'plugins', language.toLowerCase());
if (!fs.existsSync(langPath)) continue;
const plugins = fs.readdirSync(langPath);
for (let i = 0; i < plugins.length; i++) {
const instance: Plugin.PluginBase = require(
`../plugins/${language.toLowerCase()}/${plugins[i].split('.')[0]}`,
).default;
let language;
for (let plugin in plugins) {
const { id, name, site, iconUrl, lang } = plugins[plugin];
if (language !== lang) {
language = lang;
console.log(
` ${language} `
.padStart(Math.floor((language.length + 32) / 2), '=')
.padEnd(30, '='),
);
}
let icon;
if (iconUrl) icon = 'icons/' + iconUrl.split('icons/')[1];
try {
if (icon) used.add(path.join(root, icon));
const { id, name, site, icon } = instance;
try {
if (icon) used.add(path.join(root, '..', 'icons', icon));
if (!skip.includes(id) && icon && site) {
const image = await fetch(
`https://www.google.com/s2/favicons?domain=${site}&sz=${size}&type=png`,
)
.then(res => res.arrayBuffer())
.then(res => Buffer.from(res));
if (!skip[id] && icon && site) {
const image = await fetch(
`https://www.google.com/s2/favicons?domain=${site}&sz=${size}&type=png`,
)
.then(res => res.arrayBuffer())
.then(res => Buffer.from(res));
const notAvailableImage = fs.readFileSync(
path.join(root, 'scripts', 'notavailable.png'),
);
const fullPath = path.join(root, '..', 'icons', icon);
const dir = fullPath.match(/^.*[\\\/]/)?.[0] || fullPath;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(fullPath, image);
console.log(name, '✅');
} else {
console.log(name, '❌');
if (Buffer.compare(image, notAvailableImage) === 0) {
console.log(
' ',
name.padEnd(26),
`(${id})`.padEnd(20),
'Is site down?',
'\r❌',
);
continue;
}
} catch (err) {
console.log(name, '❌', err);
fs.writeFileSync(icon, image);
console.log(' ', name.padEnd(26), `(${id})`, '\r✅');
} else {
console.log(' ', `Skipping ${name}`.padEnd(26), `(${id})`, '\r🔄');
}
await delay(2500);
} catch (err) {
console.log(
' ',
name.padEnd(26),
`(${id})`.padEnd(20),
err instanceof Error ? err.constructor.name : typeof err,
'\r❌',
);
console.log(err);
await new Promise(resolve => setTimeout(resolve, 2500));
}
}
console.log('\nDeleting unused icons ⌛');
@@ -88,7 +122,7 @@ used.add(path.join(root, '..', 'icons', 'coverNotAvailable.webp'));
}, []);
}
fileList(path.join(root, '..', 'icons')).forEach(path => {
fileList(path.join(root, 'icons')).forEach(path => {
if (!used.has(path)) {
console.log('🗑️', path);
fs.rmSync(path, { force: true });
Binary file not shown.

After

Width:  |  Height:  |  Size: 726 B