|
|
@ -2,14 +2,18 @@ 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']!) { |
|
|
|
constructor(public apiKey = process.env['WANIKANI_API_KEY']) { |
|
|
|
this.$axios = axios.create({ |
|
|
|
baseURL: 'https://api.wanikani.com/v2/', |
|
|
|
headers: { |
|
|
|
Authorization: `Bearer ${process.env['WANIKANI_API_KEY']}`, |
|
|
|
Authorization: apiKey ? `Bearer ${apiKey}` : '', |
|
|
|
}, |
|
|
|
}); |
|
|
|
} |
|
|
@ -54,13 +58,108 @@ export class WaniKani { |
|
|
|
}>(nextURL) |
|
|
|
.then((r) => r.data); |
|
|
|
data.push(...r.data); |
|
|
|
console.info(r.pages.next_url); |
|
|
|
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( |
|
|
|
opts.mode?.online |
|
|
|
? { |
|
|
|
id: n.noteId, |
|
|
|
fields: { |
|
|
|
[fields.audio]: soundTag.make(audio.url), |
|
|
|
}, |
|
|
|
} |
|
|
|
: { |
|
|
|
id: n.noteId, |
|
|
|
fields: {}, |
|
|
|
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, |
|
|
|
}, |
|
|
|
})), |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
type WaniKaniDate = string; |
|
|
|