create basis for sort-anki
This commit is contained in:
1 parent
a144c7fd74
commit
49cd39297a
2 files changed
+100
-17
No files matched your search
+61
-16
@@ -1,24 +1,69 @@
|
|||||||
import { AnkiConnect } from '@/ankiconnect';
|
import { AnkiConnect } from '@/ankiconnect';
|
||||||
|
|
||||||
|
const DECK = 'Takoboto';
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const anki = new AnkiConnect();
|
const anki = new AnkiConnect();
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
await anki.multi<['findNotes', 'getTags']>({
|
await anki
|
||||||
actions: [
|
.api('findCards', {
|
||||||
{
|
query: `deck:${DECK}`,
|
||||||
action: 'findNotes',
|
})
|
||||||
params: {
|
.then((cards) => anki.api('cardsInfo', { cards }))
|
||||||
query: 'added:1',
|
.then((rs) => {
|
||||||
},
|
const modelToSubdecks = new Map<
|
||||||
},
|
string,
|
||||||
{ action: 'getTags' },
|
{
|
||||||
],
|
[subdeck: string]: number[];
|
||||||
}),
|
}
|
||||||
);
|
>();
|
||||||
console.log(
|
rs.map((r) => {
|
||||||
await anki.api('findNotes', {
|
const vs = modelToSubdecks.get(r.modelName) || {};
|
||||||
query: 'deck:Takoboto',
|
// 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}`,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+39
-1
@@ -10,6 +10,44 @@ export interface IAnkiConnectActions
|
|||||||
result: unknown;
|
result: unknown;
|
||||||
}
|
}
|
||||||
> {
|
> {
|
||||||
|
// Card Actions
|
||||||
|
cardsInfo: {
|
||||||
|
params: {
|
||||||
|
cards: number[];
|
||||||
|
};
|
||||||
|
result: {
|
||||||
|
answer: string;
|
||||||
|
question: string;
|
||||||
|
deckName: string;
|
||||||
|
modelName: string;
|
||||||
|
fieldOrder: number;
|
||||||
|
fields: {
|
||||||
|
[fieldName: string]: {
|
||||||
|
value: string;
|
||||||
|
order: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
css: string;
|
||||||
|
cardId: number;
|
||||||
|
interval: number;
|
||||||
|
note: number;
|
||||||
|
ord: number;
|
||||||
|
type: number;
|
||||||
|
queue: number;
|
||||||
|
due: number;
|
||||||
|
reps: number;
|
||||||
|
lapses: number;
|
||||||
|
left: number;
|
||||||
|
mod: number;
|
||||||
|
}[];
|
||||||
|
};
|
||||||
|
findCards: {
|
||||||
|
params: {
|
||||||
|
query: IAnkiQuery;
|
||||||
|
};
|
||||||
|
result: number[];
|
||||||
|
};
|
||||||
|
|
||||||
// Deck Actions
|
// Deck Actions
|
||||||
deckNames: {
|
deckNames: {
|
||||||
result: string[];
|
result: string[];
|
||||||
@@ -427,7 +465,7 @@ export class AnkiConnect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strongly-typed usage is via `[string, tuple]`:
|
* Strongly-typed usage is via `<[action1, action2, ...]>`:
|
||||||
*
|
*
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* await new AnkiConnect().multi<['findNotes', 'getTags']>({
|
* await new AnkiConnect().multi<['findNotes', 'getTags']>({
|
||||||
|
|||||||
Reference in new issue
Block a user