-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
189 lines (183 loc) · 5.71 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { parseArgs as parse } from "@std/cli/parse-args";
import { load_settings } from "./config.ts";
import { check_file_permissions } from "./permissons.ts";
import { AlreadyClosedError, TaskManager } from "./task_manager.ts";
import { ParsedUrl, parseUrl, UrlType } from "./url.ts";
import { sure_dir, try_remove_sync } from "./utils.ts";
import { EhDb } from "./db.ts";
import { load_eht_file, update_database_tag } from "./eh_translation.ts";
import { get_abort_signal } from "./signal_handler.ts";
function show_help() {
console.log("Usage: main.ts [options] Command");
console.log("Commonds:");
console.log(" d/download <URL> Download a gallery");
console.log(" r/run Run tasks in database");
console.log(" optimize Optimize the database");
console.log(" utt/update_tag_translation");
console.log(" ez/export_zip <GID> Export a gallery as zip");
console.log(
" umsd/update_meili_search_data Sync all gallery metadata to meiliserach server.",
);
console.log(" fgp/fix_gallery_page Fix incorrect gallery page.");
console.log("Options:");
console.log(" -h, --help Show this help");
console.log(" -c, --config <PATH> Specify config file path.");
console.log(" -a, --add-only Just add task to task list.");
console.log(" -b, --better-optimize Use better way to optimize.");
}
enum CMD {
Unknown,
Download,
Run,
Optimize,
UpdateTagTranslation,
ExportZip,
UpdateMeiliSearchData,
FixGalleryPage,
}
const args = parse(Deno.args, {
alias: {
config: ["c"],
help: ["h"],
add_only: ["a", "add-only"],
better_optimize: ["b", "better-optimize"],
},
boolean: ["help", "add_only", "better_optimize"],
string: ["config"],
default: { config: "./config.json" },
negatable: ["add_only"],
});
if (!args._.length || args.help) {
show_help();
Deno.exit(0);
}
const rcmd = args._[0];
let cmd = CMD.Unknown;
if (rcmd == "d" || rcmd == "download") cmd = CMD.Download;
if (rcmd == "r" || rcmd == "run") cmd = CMD.Run;
if (rcmd == "optimize") cmd = CMD.Optimize;
if (rcmd == "utt" || rcmd == "update_tag_translation") {
cmd = CMD.UpdateTagTranslation;
}
if (rcmd == "ez" || rcmd == "export_zip") cmd = CMD.ExportZip;
if (rcmd == "umsd" || rcmd == "update_meili_search_data") {
cmd = CMD.UpdateMeiliSearchData;
}
if (rcmd == "fgp" || rcmd == "fix_gallery_page") cmd = CMD.FixGalleryPage;
if (cmd == CMD.Unknown) {
throw Error(`Unknown command: ${rcmd}`);
}
const settings = await load_settings(args.config);
if (!check_file_permissions(settings.base)) {
throw Error("Can not aceess download loaction.");
}
async function download() {
const manager = new TaskManager(settings);
await manager.init();
try {
const urls: ParsedUrl[] = [];
for (const i of args._.slice(1)) {
const r = parseUrl(i.toString());
if (r) urls.push(r);
}
for (const u of urls) {
if (u.type == UrlType.Gallery || u.type == UrlType.MPV) {
await manager.add_download_task(u.gid, u.token);
}
}
if (args.add_only) {
return;
}
await manager.run();
} finally {
if (!manager.aborted) manager.close();
}
}
async function run() {
const manager = new TaskManager(settings);
await manager.init();
try {
await manager.run();
} finally {
if (!manager.aborted) manager.close();
}
}
async function optimize() {
const db = new EhDb(settings.db_path || settings.base);
await db.init();
if (args.better_optimize) db.better_optimize();
db.optimize();
db.close();
}
async function update_tag_translation() {
const db = new EhDb(settings.db_path || settings.base);
await db.init();
const signal = get_abort_signal();
try {
const f = await load_eht_file(
args._.length > 1 ? args._[1].toString() : undefined,
signal,
);
await update_database_tag(db, f, signal);
} catch (e) {
if (!signal.aborted) throw e;
} finally {
try_remove_sync("utt.lock");
db.close();
}
}
async function export_zip() {
const manager = new TaskManager(settings);
await manager.init();
try {
for (const gid of args._.slice(1)) {
if (typeof gid === "number") {
await manager.add_export_zip_task(gid);
}
}
await manager.run();
} finally {
if (!manager.aborted) manager.close();
}
}
async function update_meili_search_data() {
const manager = new TaskManager(settings);
await manager.init();
try {
await manager.add_update_meili_search_data_task();
await manager.run();
} finally {
if (!manager.aborted) manager.close();
}
}
async function fix_gallery_page() {
const manager = new TaskManager(settings);
await manager.init();
try {
await manager.add_fix_gallery_page_task();
await manager.run();
} finally {
if (!manager.aborted) manager.close();
}
}
async function main() {
await sure_dir(settings.base);
if (cmd == CMD.Download) {
await download();
} else if (cmd == CMD.Run) {
await run();
} else if (cmd == CMD.Optimize) {
await optimize();
} else if (cmd == CMD.UpdateTagTranslation) {
await update_tag_translation();
} else if (cmd == CMD.ExportZip) {
await export_zip();
} else if (cmd == CMD.UpdateMeiliSearchData) {
await update_meili_search_data();
} else if (cmd == CMD.FixGalleryPage) {
await fix_gallery_page();
}
}
main().catch((e) => {
if (!(e instanceof AlreadyClosedError)) throw e;
});