prototype subdeck from Kanji

This commit is contained in:
polv committed 2022-04-27 14:04:52 +07:00
1 parent f427a8e148
commit 13f2bb421c
4 files changed
+80 -16

No files matched your search

+52 -15
View File
@@ -1,4 +1,4 @@
import { AnkiConnect } from '@/ankiconnect';
import { AnkiConnect, IAnkiConnectActions } from '@/ankiconnect';
import { makeKanjiLevels } from './get-kanji-level';
@@ -8,7 +8,10 @@ const KANJI_FIELD = 'Japanese';
async function main() {
const anki = new AnkiConnect();
console.log(
const deckConfig = await anki.api('getDeckConfig', {
deck: DECK,
});
await anki
.api('findCards', {
query: `deck:${DECK}`,
@@ -62,7 +65,11 @@ async function main() {
),
);
})
.then((rs) => {
.then(async (rs) => {
const remaining: {
[templateName: string]: Set<number>;
} = {};
const cardsByTemplateName = {
data: new Map<string, number[]>(),
async get(templateName: string) {
@@ -71,32 +78,62 @@ async function main() {
v = await anki.api('findCards', {
query: `deck:${DECK} card:${templateName}`,
});
remaining[templateName] = new Set(v);
}
this.data.set(templateName, v);
return v;
},
};
return Promise.all(
rs
const toChangeDeck: IAnkiConnectActions['changeDeck']['params'][] = [];
for (const { templateName, subdeck, cardIdsSet } of rs
.flat()
.flatMap((r) =>
r.templateNames.map((templateName) => ({
...r,
templateName,
})),
)
.map(({ templateName, subdeck, cardIdsSet }) =>
cardsByTemplateName.get(templateName).then((cards) =>
anki.api('changeDeck', {
cards: cards.filter((c) => cardIdsSet.has(c)),
)) {
const cards = await cardsByTemplateName.get(templateName);
toChangeDeck.push({
cards: cards.filter((c) => {
const r = remaining[templateName];
if (r) {
r.delete(c);
}
return cardIdsSet.has(c);
}),
deck: `${DECK}::${subdeck}::${templateName}`,
}),
),
),
);
}),
});
}
Object.entries(remaining).map(([templateName, cards]) => {
if (cards.size) {
toChangeDeck.push({
cards: [...cards],
deck: `${DECK}::独習::${templateName}`,
});
}
});
if (toChangeDeck.length) {
const batchSize = 20;
for (let i = 0; i < toChangeDeck.length; i += batchSize) {
await Promise.all(
toChangeDeck
.slice(i, i + batchSize)
.map((params) => anki.api('changeDeck', params)),
);
}
await anki.api('setDeckConfigId', {
decks: toChangeDeck.map((r) => r.deck),
configId: deckConfig.id,
});
}
});
}
if (require.main === module) {
+4
View File
@@ -42,3 +42,7 @@ export interface IAnkiTemplate {
bqfmt: string;
bamt: string;
}
export interface IAnkiDeckConfig {
id: number;
}
+19 -1
View File
@@ -1,6 +1,7 @@
import axios, { AxiosInstance } from 'axios';
import { IAnkiCard, IAnkiQuery } from './anki';
import { IAnkiCard, IAnkiDeckConfig, IAnkiQuery } from './anki';
import { logger } from './logger';
export interface IAnkiConnectActions
extends Record<
@@ -67,6 +68,19 @@ export interface IAnkiConnectActions
};
result: null;
};
getDeckConfig: {
params: {
deck: string;
};
result: IAnkiDeckConfig;
};
setDeckConfigId: {
params: {
decks: string[];
configId: number;
};
result: boolean;
};
// Media Actions
storeMediaFile: {
@@ -443,6 +457,8 @@ export class AnkiConnect {
params: IAnkiConnectActions[A]['params'],
version = this.version,
): Promise<IAnkiConnectActions[A]['result']> {
logger(`AnkiConnect: calling ${action}`, params);
const { data: response } = await this.$axios.post<{
result: any;
error: string | null;
@@ -461,6 +477,8 @@ export class AnkiConnect {
throw response.error;
}
logger(`AnkiConnect: finished ${action}`, params);
return response.result;
}
+5
View File
@@ -0,0 +1,5 @@
export function logger(...args: any[]) {
if (process.env['DEBUG']) {
console.debug(...args);
}
}