import { AnkiConnect } from '@/ankiconnect';
|
|
|
|
const DECK = 'Takoboto';
|
|
|
|
async function main() {
|
|
const anki = new AnkiConnect();
|
|
|
|
console.log(
|
|
await anki
|
|
.api('findCards', {
|
|
query: `deck:${DECK}`,
|
|
})
|
|
.then((cards) => anki.api('cardsInfo', { cards }))
|
|
.then((rs) => {
|
|
const modelToSubdecks = new Map<
|
|
string,
|
|
{
|
|
[subdeck: string]: number[];
|
|
}
|
|
>();
|
|
rs.map((r) => {
|
|
const vs = modelToSubdecks.get(r.modelName) || {};
|
|
// Get the subdeck names from Kanji
|
|
const subdeck = '???';
|
|
vs[subdeck] = vs[subdeck] || [];
|
|
vs[subdeck]!.push(r.cardId);
|
|
modelToSubdecks.set(r.modelName, vs);
|
|
});
|
|
|
|
return Promise.all(
|
|
Array.from(modelToSubdecks).map(([modelName, subdecks]) =>
|
|
anki.api('modelTemplates', { modelName }).then((templates) =>
|
|
Object.entries(subdecks).map(([subdeck, cardIds]) => ({
|
|
modelName,
|
|
templateNames: Object.keys(templates),
|
|
subdeck,
|
|
cardIdsSet: new Set(cardIds),
|
|
})),
|
|
),
|
|
),
|
|
);
|
|
})
|
|
.then((rs) => {
|
|
return Promise.all(
|
|
rs
|
|
.flat()
|
|
.flatMap((r) =>
|
|
r.templateNames.map((templateName) => ({
|
|
...r,
|
|
templateName,
|
|
})),
|
|
)
|
|
.map(({ templateName, subdeck, cardIdsSet }) =>
|
|
anki
|
|
.api('findCards', {
|
|
query: `deck:${DECK} card:${templateName}`,
|
|
})
|
|
.then((cards) =>
|
|
anki.api('changeDeck', {
|
|
cards: cards.filter((c) => cardIdsSet.has(c)),
|
|
deck: `${DECK}::${subdeck}::${templateName}`,
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
|
|
if (require.main === module) {
|
|
main();
|
|
}
|