mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
feat: glossary cron
This commit is contained in:
1 parent
365c68aa4d
commit
c5e6f5e2eb
16 files changed
+367
-57
No files matched your search
@@ -0,0 +1,86 @@
|
||||
import { Avatar, encodePBBuffer, fetchGlossary, Gallery } from '@blitzkit/core';
|
||||
import locales from '@blitzkit/i18n/locales.json';
|
||||
import { extname } from 'path';
|
||||
import ProgressBar from 'progress';
|
||||
import { commitAssets } from '../core/github/commitAssets';
|
||||
import { FileChange } from '../core/github/commitMultipleFiles';
|
||||
|
||||
export async function glossary() {
|
||||
console.log('Building glossary...');
|
||||
|
||||
const avatars: Record<string, { avatar: Avatar; url: string }> = {};
|
||||
|
||||
for (const supported of locales.supported) {
|
||||
console.log(`Fetching glossary for ${supported.locale}`);
|
||||
|
||||
const glossary = await fetchGlossary(supported.blitz ?? supported.locale);
|
||||
|
||||
console.log(`Found ${Object.keys(glossary).length} things`);
|
||||
|
||||
for (const key in glossary) {
|
||||
const glossaryEntry = glossary[key];
|
||||
|
||||
if (
|
||||
!key.startsWith('avatar') ||
|
||||
key.endsWith('_part') ||
|
||||
glossaryEntry.image_url === null
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key in avatars) {
|
||||
avatars[key].avatar.name.locales[supported.locale] =
|
||||
glossaryEntry.title;
|
||||
} else {
|
||||
avatars[key] = {
|
||||
url: glossaryEntry.image_url,
|
||||
avatar: {
|
||||
id: key,
|
||||
name: { locales: { [supported.locale]: glossaryEntry.title } },
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const changes: FileChange[] = [];
|
||||
const gallery: Gallery = { avatars: [] };
|
||||
const totalCount = Object.keys(avatars).length;
|
||||
|
||||
const bar = new ProgressBar(
|
||||
`Fetching avatars of ${totalCount} things :bar`,
|
||||
totalCount,
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
Object.entries(avatars).map(async ([key, { url, avatar }]) => {
|
||||
if (!(locales.default in avatars[key].avatar.name.locales)) {
|
||||
console.warn(`Avatar ${key} has no ${locales.default} name`);
|
||||
bar.tick();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
gallery.avatars.push(avatar);
|
||||
|
||||
const fileExtension = extname(url);
|
||||
const content = await fetch(url).then((response) =>
|
||||
response.arrayBuffer(),
|
||||
);
|
||||
|
||||
changes.push({
|
||||
path: `gallery/avatars/${key}${fileExtension}`,
|
||||
content: Buffer.from(content),
|
||||
});
|
||||
|
||||
bar.tick();
|
||||
}),
|
||||
);
|
||||
|
||||
changes.push({
|
||||
path: 'definitions/gallery.pb',
|
||||
content: encodePBBuffer(Gallery, gallery),
|
||||
});
|
||||
|
||||
await commitAssets('glossary', changes);
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import { definitions } from './definitions';
|
||||
import { equipmentIcons } from './equipmentIcons';
|
||||
import { flags } from './flags';
|
||||
import { gameModeBanners } from './gameModeBanners';
|
||||
import { glossary } from './glossary';
|
||||
import { migration } from './migration';
|
||||
import { moduleIcons } from './moduleIcons';
|
||||
import { popularTanks } from './popularTanks';
|
||||
@@ -47,6 +48,7 @@ const methods = [
|
||||
tankIcons,
|
||||
blitzkitTankIcons,
|
||||
popularTanks,
|
||||
glossary,
|
||||
];
|
||||
|
||||
for (const method of methods) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { assertSecret } from '@blitzkit/core';
|
||||
import { chunk } from 'lodash-es';
|
||||
import { commitMultipleFiles, FileChange } from './commitMultipleFiles';
|
||||
|
||||
export async function commitAssets(message: string, changes: FileChange[]) {
|
||||
@@ -6,10 +7,19 @@ export async function commitAssets(message: string, changes: FileChange[]) {
|
||||
|
||||
if (changes.length === 0) return;
|
||||
|
||||
await commitMultipleFiles(
|
||||
assertSecret(import.meta.env.PUBLIC_ASSET_REPO),
|
||||
assertSecret(import.meta.env.PUBLIC_ASSET_BRANCH),
|
||||
`${message} - ${new Date().toDateString()}`,
|
||||
changes,
|
||||
);
|
||||
const chunks = chunk(changes, 16);
|
||||
let chunkIndex = 0;
|
||||
|
||||
for (const chunk of chunks) {
|
||||
console.log(`Committing chunk ${chunkIndex + 1}/${chunks.length}`);
|
||||
|
||||
await commitMultipleFiles(
|
||||
assertSecret(import.meta.env.PUBLIC_ASSET_REPO),
|
||||
assertSecret(import.meta.env.PUBLIC_ASSET_BRANCH),
|
||||
`${message} - ${new Date().toDateString()} (${chunkIndex + 1}/${chunks.length})`,
|
||||
chunk,
|
||||
);
|
||||
|
||||
chunkIndex++;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user