Files
eh-downloader/config.ts
2023-05-21 20:19:43 +08:00

80 lines
2.2 KiB
TypeScript

import { JsonValue, parse } from "std/jsonc/mod.ts";
export class Config {
_data;
constructor(data: JsonValue) {
this._data = <{ [x: string]: unknown }> <unknown> Object.assign(
{},
data,
);
}
_return_string(key: string) {
const v = this._data[key];
if (v === undefined || typeof v === "string") {
return v;
}
throw new Error(`Config ${key} value ${v} is not a string`);
}
_return_number(key: string) {
const v = this._data[key];
if (v === undefined) return undefined;
if (typeof v === "number") {
return v;
}
throw new Error(`Config ${key} value ${v} is not a number`);
}
_return_bool(key: string) {
const v = this._data[key];
if (v === undefined) {
return v;
}
if (typeof v === "boolean") {
return v;
} else if (typeof v === "string") {
if (v === "true") {
return true;
} else if (v === "false") {
return false;
}
} else if (typeof v === "number") {
return v != 0;
}
throw new Error(`Config ${key} value ${v} is not a boolean`);
}
get cookies() {
return this._return_string("cookies");
}
get db_path() {
return this._return_string("db_path");
}
get ua() {
return this._return_string("ua");
}
get ex() {
return this._return_bool("ex") || false;
}
get base() {
return this._return_string("base") || "./downloads";
}
get max_task_count() {
return this._return_number("max_task_count") || 1;
}
get mpv() {
return this._return_bool("mpv") || false;
}
get max_retry_count() {
return this._return_number("max_retry_count") || 3;
}
get max_download_img_count() {
return this._return_number("max_download_img_count") || 3;
}
get download_original_img() {
return this._return_bool("download_original_img") || false;
}
}
export async function load_settings(path: string) {
const s = (new TextDecoder()).decode(await Deno.readFile(path));
return new Config(parse(s));
}