Extra contents beyond WaniKani
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

483 lines
11 KiB

import { existsSync, readFileSync, writeFileSync } from 'fs';
import axios, { AxiosInstance } from 'axios';
import { soundTag } from './anki';
import { AnkiConnect, IAnkiConnectActions } from './ankiconnect';
import { ILevelLabel } from './level';
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}` : '',
},
});
}
static getLevelLabel(level: number): ILevelLabel {
if (level < 11) {
return {
range: '01-10',
ja: '快',
en: 'PLEASANT',
};
} else if (level < 21) {
return {
range: '11-20',
ja: '苦',
en: 'PAINFUL',
};
} else if (level < 31) {
return {
range: '21-30',
ja: '死',
en: 'DEATH',
};
} else if (level < 41) {
return {
range: '31-40',
ja: '地獄',
en: 'HELL',
};
} else if (level < 51) {
return {
range: '41-50',
ja: '天国',
en: 'PARADISE',
};
} else if (level < 61) {
return {
range: '51-60',
ja: '現実',
en: 'REALITY',
};
}
throw new Error(`invalid level: ${level}`);
}
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,
},
})),
});
});
}
async addTags(
query: string,
fields: {
ja: string;
},
) {
const subjects = await this.subjects();
const vocabularies = subjects.filter(
(s) => s.object === 'vocabulary',
) as IVocabulary[];
const vocabMap = new Map<
string,
{
level: number;
label: ILevelLabel;
}
>();
vocabularies.map((v) => {
if (vocabMap.has(v.data.characters)) return;
vocabMap.set(v.data.characters, {
level: v.data.level,
label: WaniKani.getLevelLabel(v.data.level),
});
});
if (!vocabMap.size) return;
query += ` -${fields.ja}:`;
const anki = new AnkiConnect();
anki
.api('findNotes', {
query,
})
.then((notes) => anki.api('notesInfo', { notes }))
.then(async (notes) => {
const notesToUpdate: {
id: SubjectID;
tags: string[];
}[] = [];
for (const n of notes) {
const { value: ja } = n.fields[fields.ja] || {};
if (ja) {
const vocab = vocabMap.get(
ja.replace(/\[.+?\]/g, '').replace(/ /g, ''),
);
if (vocab) {
notesToUpdate.push({
id: n.noteId,
tags: [
'wanikani',
`level-${vocab.level}`,
`wanikani-level-${vocab.level}`,
`wanikani-level-${vocab.label.range}-${vocab.label.ja}`,
`wanikani-level-${
vocab.label.range
}-${vocab.label.en.toLocaleLowerCase()}`,
],
});
}
}
}
if (!notesToUpdate.length) return;
const addTagsMap = new Map<
string,
{
notes: SubjectID[];
tags: string;
}
>();
notesToUpdate.map(({ id, tags }) => {
const identifier = JSON.stringify(tags);
const v = addTagsMap.get(identifier) || {
notes: [],
tags: tags.join(' '),
};
v.notes.push(id);
addTagsMap.set(identifier, v);
});
await anki.multi<'addTags'[]>({
actions: Array.from(addTagsMap.values()).map((params) => ({
action: 'addTags',
params,
})),
});
});
}
}
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;