add vocabulary to subjects
This commit is contained in:
1 parent
a2e6e66bcf
commit
92c1de3d1f
2 files changed
+90
-43
No files matched your search
@@ -9,14 +9,16 @@ interface ILevelMap {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function makeWaniKaniKanjiLevels(opts: { cache?: boolean } = {}) {
|
async function makeWaniKaniKanjiLevels(
|
||||||
|
opts: { cache?: boolean; force?: boolean } = {},
|
||||||
|
) {
|
||||||
const FILENAME = 'cache/wanikani-kanji.yaml';
|
const FILENAME = 'cache/wanikani-kanji.yaml';
|
||||||
if (opts.cache && existsSync(FILENAME)) {
|
if (opts.cache && existsSync(FILENAME)) {
|
||||||
return yaml.load(readFileSync(FILENAME, 'utf-8')) as ILevelMap;
|
return yaml.load(readFileSync(FILENAME, 'utf-8')) as ILevelMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
const wkKanji = await new WaniKani()
|
const wkKanji = await new WaniKani()
|
||||||
.subjects()
|
.subjects({ force: !!opts.force })
|
||||||
.then((vs) => vs.filter((v) => v.object === 'kanji') as IKanji[]);
|
.then((vs) => vs.filter((v) => v.object === 'kanji') as IKanji[]);
|
||||||
|
|
||||||
const levelMap: ILevelMap = {};
|
const levelMap: ILevelMap = {};
|
||||||
|
|||||||
+86
-41
@@ -1,4 +1,4 @@
|
|||||||
import fs from 'fs';
|
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
||||||
|
|
||||||
import axios, { AxiosInstance } from 'axios';
|
import axios, { AxiosInstance } from 'axios';
|
||||||
|
|
||||||
@@ -20,18 +20,20 @@ export class WaniKani {
|
|||||||
force?: boolean;
|
force?: boolean;
|
||||||
} = {}) {
|
} = {}) {
|
||||||
const subjects = {
|
const subjects = {
|
||||||
data: [] as (IKanji | IRadical)[],
|
data: [] as ISubject[],
|
||||||
filename: 'cache/wanikani.json',
|
filename: 'cache/wanikani.json',
|
||||||
load() {
|
load() {
|
||||||
this.data = JSON.parse(fs.readFileSync(this.filename, 'utf-8'));
|
this.data = existsSync(this.filename)
|
||||||
|
? JSON.parse(readFileSync(this.filename, 'utf-8'))
|
||||||
|
: [];
|
||||||
return this.data;
|
return this.data;
|
||||||
},
|
},
|
||||||
dump(d: (IKanji | IRadical)[]) {
|
dump(d: ISubject[]) {
|
||||||
this.data = d;
|
this.data = d;
|
||||||
this.finalize();
|
this.finalize();
|
||||||
},
|
},
|
||||||
finalize() {
|
finalize() {
|
||||||
fs.writeFileSync(this.filename, JSON.stringify(this.data));
|
writeFileSync(this.filename, JSON.stringify(this.data));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -41,14 +43,14 @@ export class WaniKani {
|
|||||||
}
|
}
|
||||||
data = [];
|
data = [];
|
||||||
|
|
||||||
let nextURL = '/subjects?types=radical,kanji';
|
let nextURL = '/subjects';
|
||||||
while (nextURL) {
|
while (nextURL) {
|
||||||
const r = await this.$axios
|
const r = await this.$axios
|
||||||
.get<{
|
.get<{
|
||||||
pages: {
|
pages: {
|
||||||
next_url?: string;
|
next_url?: string;
|
||||||
};
|
};
|
||||||
data: (IKanji | IRadical)[];
|
data: ISubject[];
|
||||||
}>(nextURL)
|
}>(nextURL)
|
||||||
.then((r) => r.data);
|
.then((r) => r.data);
|
||||||
data.push(...r.data);
|
data.push(...r.data);
|
||||||
@@ -61,40 +63,42 @@ export class WaniKani {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IKanji {
|
type WaniKaniDate = string;
|
||||||
id: number;
|
type Integer = number;
|
||||||
/** Actual type */
|
type Level = Integer;
|
||||||
object: 'kanji';
|
type SubjectID = Integer;
|
||||||
url: string;
|
|
||||||
data_updated_at: string;
|
interface IMeaning {
|
||||||
data: {
|
meaning: string;
|
||||||
level: number;
|
primary: boolean;
|
||||||
document_url: string;
|
|
||||||
characters: string;
|
|
||||||
meanings: {
|
|
||||||
meaning: string;
|
|
||||||
primary: boolean;
|
|
||||||
}[];
|
|
||||||
readings: {
|
|
||||||
reading: string;
|
|
||||||
primary: boolean;
|
|
||||||
type: 'kunyomi' | 'onyomi';
|
|
||||||
}[];
|
|
||||||
component_subject_ids: number[];
|
|
||||||
visually_similar_subject_ids: number[];
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IRadical {
|
interface ISubjectBase<
|
||||||
id: number;
|
T extends 'radical' | 'kanji' | 'vocabulary',
|
||||||
/** Actual type */
|
Data extends {},
|
||||||
object: 'radical';
|
> {
|
||||||
|
id: SubjectID;
|
||||||
|
object: T;
|
||||||
url: string;
|
url: string;
|
||||||
data_updated_at: string;
|
data_updated_at: WaniKaniDate;
|
||||||
data: {
|
data: {
|
||||||
level: number;
|
auxillary_meanings: IMeaning[];
|
||||||
|
characters: string;
|
||||||
|
created_at: WaniKaniDate;
|
||||||
document_url: string;
|
document_url: string;
|
||||||
characters: string | null;
|
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: {
|
character_images: {
|
||||||
url: string;
|
url: string;
|
||||||
metadata: {
|
metadata: {
|
||||||
@@ -103,10 +107,51 @@ export interface IRadical {
|
|||||||
};
|
};
|
||||||
content_type: string;
|
content_type: string;
|
||||||
}[];
|
}[];
|
||||||
meanings: {
|
amalgamation_subject_ids: SubjectID[];
|
||||||
meaning: string;
|
}
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type IKanji = ISubjectBase<
|
||||||
|
'kanji',
|
||||||
|
{
|
||||||
|
readings: {
|
||||||
|
reading: string;
|
||||||
primary: boolean;
|
primary: boolean;
|
||||||
|
type: 'kunyomi' | 'onyomi';
|
||||||
}[];
|
}[];
|
||||||
amalgamation_subject_ids: number[];
|
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;
|
||||||
Reference in new issue
Block a user