Browse Source

add wanikani sentence to Anki

main
parent
commit
e178881b37
2 changed files with 99 additions and 10 deletions
  1. +13
    -3
      scripts/populate-from-wanikani.ts
  2. +86
    -7
      src/wanikani.ts

+ 13
- 3
scripts/populate-from-wanikani.ts View File

@ -1,9 +1,19 @@
import { WaniKani } from '@/wanikani';
if (require.main === module) {
new WaniKani().populateSound(
// new WaniKani().populateSound(
// 'note:jp.takoboto',
// { ja: 'Japanese', audio: 'JapaneseAudio' },
// { mode: { online: true } },
// );
new WaniKani().populateSentence(
'note:jp.takoboto',
{ ja: 'Japanese', audio: 'JapaneseAudio' },
{ mode: { online: true } },
{
vocabJa: 'Japanese',
sentenceJa: 'Sentence',
sentenceAudio: 'SentenceAudio',
sentenceEn: 'SentenceMeaning',
},
{ overwrite: true },
);
}

+ 86
- 7
src/wanikani.ts View File

@ -122,17 +122,15 @@ export class WaniKani {
ja.replace(/\[.+?\]/g, '').replace(/ /g, ''),
);
if (audio) {
notesToUpdate.push(
opts.mode?.online
notesToUpdate.push({
id: n.noteId,
...(opts.mode?.online
? {
id: n.noteId,
fields: {
[fields.audio]: soundTag.make(audio.url),
},
}
: {
id: n.noteId,
fields: {},
audio: [
{
url: audio.url,
@ -140,8 +138,8 @@ export class WaniKani {
fields: [fields.audio],
},
],
},
);
}),
});
}
}
}
@ -160,6 +158,87 @@ export class WaniKani {
}
});
}
async populateSentence(
query: string,
fields: {
vocabJa: string;
sentenceJa: string;
sentenceAudio?: string;
sentenceEn: string;
},
opts: {
overwrite?: boolean;
} = {},
) {
const subjects = await this.subjects();
const vocabularies = subjects.filter(
(s) => s.object === 'vocabulary',
) as IVocabulary[];
const sentenceMap = new Map<
string,
{
ja: string;
en: string;
}
>();
vocabularies.map((v) => {
if (sentenceMap.has(v.data.characters)) return;
const sent = v.data.context_sentences[0];
if (!sent || !sent.ja.trim()) return;
sentenceMap.set(v.data.characters, sent);
});
if (!sentenceMap.size) return;
query += ` -${fields.vocabJa}:`;
if (fields.sentenceAudio) {
query += ` -${fields.sentenceAudio}:`;
}
if (!opts.overwrite) {
query += ` -${fields.sentenceJa}: -${fields.sentenceEn}:`;
}
const anki = new AnkiConnect();
anki
.api('findNotes', {
query,
})
.then((notes) => anki.api('notesInfo', { notes }))
.then(async (notes) => {
const notesToUpdate: IAnkiConnectActions['updateNoteFields']['params']['note'][] =
[];
for (const n of notes) {
const { value: ja } = n.fields[fields.vocabJa] || {};
if (ja) {
const sent = sentenceMap.get(
ja.replace(/\[.+?\]/g, '').replace(/ /g, ''),
);
if (sent) {
notesToUpdate.push({
id: n.noteId,
fields: {
[fields.sentenceJa]: sent.ja,
[fields.sentenceEn]: sent.en,
},
});
}
}
}
if (!notesToUpdate.length) return;
await anki.multi<'updateNoteFields'[]>({
actions: notesToUpdate.map((note) => ({
action: 'updateNoteFields',
params: {
note,
},
})),
});
});
}
}
type WaniKaniDate = string;

Loading…
Cancel
Save