import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
|
|
import axios, { AxiosInstance } from 'axios';
|
|
|
|
import { soundTag } from './anki';
|
|
import { AnkiConnect, IAnkiConnectActions } from './ankiconnect';
|
|
import { logger } from './logger';
|
|
|
|
export class WaniKani {
|
|
$axios: AxiosInstance;
|
|
|
|
constructor(public apiKey = process.env['WANIKANI_API_KEY']) {
|
|
this.$axios = axios.create({
|
|
baseURL: 'https://api.wanikani.com/v2/',
|
|
headers: {
|
|
Authorization: apiKey ? `Bearer ${apiKey}` : '',
|
|
},
|
|
});
|
|
}
|
|
|
|
async subjects({
|
|
force,
|
|
}: {
|
|
force?: boolean;
|
|
} = {}) {
|
|
const subjects = {
|
|
data: [] as ISubject[],
|
|
filename: 'cache/wanikani.json',
|
|
load() {
|
|
this.data = existsSync(this.filename)
|
|
? JSON.parse(readFileSync(this.filename, 'utf-8'))
|
|
: [];
|
|
return this.data;
|
|
},
|
|
dump(d: ISubject[]) {
|
|
this.data = d;
|
|
this.finalize();
|
|
},
|
|
finalize() {
|
|
writeFileSync(this.filename, JSON.stringify(this.data));
|
|
},
|
|
};
|
|
|
|
let data = subjects.load();
|
|
if (data.length && !force) {
|
|
return data;
|
|
}
|
|
data = [];
|
|
|
|
let nextURL = '/subjects';
|
|
while (nextURL) {
|
|
const r = await this.$axios
|
|
.get<{
|
|
pages: {
|
|
next_url?: string;
|
|
};
|
|
data: ISubject[];
|
|
}>(nextURL)
|
|
.then((r) => r.data);
|
|
data.push(...r.data);
|
|
logger('WaniKani API:', r.pages.next_url);
|
|
nextURL = r.pages.next_url || '';
|
|
}
|
|
|
|
subjects.dump(data);
|
|
return data;
|
|
}
|
|
|
|
async populateSound(
|
|
query: string,
|
|
fields: {
|
|
ja: string;
|
|
audio: string;
|
|
},
|
|
opts: {
|
|
mode?: {
|
|
online?: boolean;
|
|
};
|
|
} = {},
|
|
) {
|
|
const subjects = await this.subjects();
|
|
|
|
const vocabularies = subjects.filter(
|
|
(s) => s.object === 'vocabulary',
|
|
) as IVocabulary[];
|
|
const audioMap = new Map<
|
|
string,
|
|
{
|
|
url: string;
|
|
filename: string;
|
|
}
|
|
>();
|
|
vocabularies.map((v) => {
|
|
if (audioMap.has(v.data.characters)) return;
|
|
const audio = v.data.pronunciation_audios[0];
|
|
if (!audio) return;
|
|
audioMap.set(v.data.characters, {
|
|
url: audio.url,
|
|
filename: `wanikani_${v.data.characters}_${audio.metadata.source_id}${
|
|
audio.content_type === 'audio/ogg' ? '.ogg' : '.mp3'
|
|
}`,
|
|
});
|
|
});
|
|
if (!audioMap.size) return;
|
|
|
|
query += ` -${fields.ja}: ${fields.audio}:`;
|
|
|
|
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.ja] || {};
|
|
if (ja) {
|
|
const audio = audioMap.get(
|
|
ja.replace(/\[.+?\]/g, '').replace(/ /g, ''),
|
|
);
|
|
if (audio) {
|
|
notesToUpdate.push({
|
|
id: n.noteId,
|
|
...(opts.mode?.online
|
|
? {
|
|
fields: {
|
|
[fields.audio]: soundTag.make(audio.url),
|
|
},
|
|
}
|
|
: {
|
|
audio: [
|
|
{
|
|
url: audio.url,
|
|
filename: audio.filename,
|
|
fields: [fields.audio],
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!notesToUpdate.length) return;
|
|
|
|
while (notesToUpdate.length) {
|
|
await anki.multi<'updateNoteFields'[]>({
|
|
actions: notesToUpdate.splice(0, 100).map((note) => ({
|
|
action: 'updateNoteFields',
|
|
params: {
|
|
note,
|
|
},
|
|
})),
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
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;
|
|
type Integer = number;
|
|
type Level = Integer;
|
|
type SubjectID = Integer;
|
|
|
|
interface IMeaning {
|
|
meaning: string;
|
|
primary: boolean;
|
|
}
|
|
|
|
interface ISubjectBase<
|
|
T extends 'radical' | 'kanji' | 'vocabulary',
|
|
Data extends {},
|
|
> {
|
|
id: SubjectID;
|
|
object: T;
|
|
url: string;
|
|
data_updated_at: WaniKaniDate;
|
|
data: {
|
|
auxillary_meanings: IMeaning[];
|
|
characters: string;
|
|
created_at: WaniKaniDate;
|
|
document_url: string;
|
|
hidden_at: WaniKaniDate | null;
|
|
lesson_position: Integer;
|
|
level: Level;
|
|
meaning_mnemonic: string;
|
|
meanings: IMeaning[];
|
|
slug: string;
|
|
spaced_repetition_system_id: Integer;
|
|
} & Data;
|
|
}
|
|
|
|
export type IRadical = ISubjectBase<
|
|
'radical',
|
|
{
|
|
character_images: ({
|
|
url: string;
|
|
} & (
|
|
| {
|
|
metadata: {
|
|
inline_styles?: boolean;
|
|
dimensions?: string;
|
|
};
|
|
content_type: 'image/svg+xml';
|
|
}
|
|
| {
|
|
metadata: {
|
|
color: string;
|
|
dimensions: string;
|
|
style_name: string;
|
|
};
|
|
content_type: 'image/png';
|
|
}
|
|
))[];
|
|
amalgamation_subject_ids: SubjectID[];
|
|
}
|
|
>;
|
|
|
|
export type IKanji = ISubjectBase<
|
|
'kanji',
|
|
{
|
|
readings: {
|
|
reading: string;
|
|
primary: boolean;
|
|
accepted_answer: boolean;
|
|
type: 'kunyomi' | 'onyomi' | 'nanori';
|
|
}[];
|
|
component_subject_ids: SubjectID[];
|
|
visually_similar_subject_ids: SubjectID[];
|
|
}
|
|
>;
|
|
|
|
export type IVocabulary = ISubjectBase<
|
|
'vocabulary',
|
|
{
|
|
component_subject_ids: SubjectID[];
|
|
context_sentences: {
|
|
en: string;
|
|
ja: string;
|
|
}[];
|
|
part_of_speech: string[];
|
|
pronunciation_audios: {
|
|
url: string;
|
|
metadata: {
|
|
gender: 'male' | 'female';
|
|
source_id: Integer;
|
|
pronunciation: string;
|
|
voice_actor_id: Integer;
|
|
voice_actor_name: string;
|
|
voice_description: string;
|
|
};
|
|
content_type: 'audio/mpeg' | 'audio/ogg';
|
|
}[];
|
|
readings: {
|
|
accepted_answer: boolean;
|
|
primary: boolean;
|
|
reading: string;
|
|
}[];
|
|
reading_mnemonic: string;
|
|
}
|
|
>;
|
|
|
|
export type ISubject = IRadical | IKanji | IVocabulary;
|