-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgen_pack.ts
155 lines (150 loc) · 5.09 KB
/
gen_pack.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
import type { File, Folder, Metadata } from "./serialize/serialize-types.ts";
import { fsToFolder } from "./serialize/fs.ts";
import { extractImagesFromAudio } from "./generate/extract_images_from_audio.ts";
import { genMissingItems } from "./generate/gen_missing_items.ts";
import { convertAudioOfFolder } from "./utils/convert_audio.ts";
import { folderToPack } from "./serialize/converter.ts";
import { serializePack } from "./serialize/serializer.ts";
import { getAssetsPaths } from "./serialize/assets.ts";
import { createPackZip } from "./utils/zip.ts";
import { downloadRss } from "./generate/rss_parser.ts";
import { exists } from "@std/fs";
import { bgRed } from "@std/fmt/colors";
import { basename, join } from "@std/path";
import {
checkRunPermission,
convertToImageItem,
fileImageItemRegEx,
folderImageItemRegEx,
getNightModeAudioItem,
isFolder,
} from "./utils/utils.ts";
import { getLang, initI18n } from "./utils/i18n.ts";
import { convertImageOfFolder } from "./utils/convert_image.ts";
import $ from "@david/dax";
import type { StudioPackGenerator } from "./studio_pack_generator.ts";
async function genThumbnail(
folder: Folder,
storyPath: string,
thumbnailFromFirstItem: boolean,
) {
await checkRunPermission();
const thumbnailPath = join(storyPath, "thumbnail.png");
if (!(await exists(thumbnailPath))) {
let itemFile: File | null = null;
let thumbnailItemPath = storyPath;
if (thumbnailFromFirstItem) {
let files = folder.files;
let insideFolder = files.find(isFolder);
while (insideFolder) {
files = insideFolder.files;
thumbnailItemPath = join(thumbnailItemPath, insideFolder.name);
insideFolder = files.find(isFolder);
}
itemFile = files.find((f) => fileImageItemRegEx.test(f.name)) as File;
}
if (!itemFile) {
thumbnailItemPath = storyPath;
itemFile = folder.files.find((f) =>
folderImageItemRegEx.test(f.name)
) as File;
}
if (itemFile) {
await convertToImageItem(
join(thumbnailItemPath, itemFile.name),
thumbnailPath,
);
}
}
}
export async function generatePack(opt: StudioPackGenerator) {
if (
opt.nightMode &&
(opt.autoNextStoryTransition || opt.selectNextStoryAtEnd)
) {
console.log(
bgRed(
"The night mode is incompatible with auto-next-story-transition or select-next-story-at-end options",
),
);
Deno.exit(4);
}
const start = Date.now();
console.log("generatePack", { opt });
const lang = opt.lang || (await getLang());
await initI18n(lang);
let pathsToHandle = [opt.storyPath];
if (opt.storyPath.startsWith("http")) {
pathsToHandle = await downloadRss(opt.storyPath, ".", opt);
console.log(`downloaded in ${opt.storyPath}`);
}
for (const storyPath of pathsToHandle) {
if (!opt.skipNotRss) {
let folder: Folder = await fsToFolder(storyPath, false);
if (!opt.skipExtractImageFromMp3) {
await extractImagesFromAudio(storyPath, folder);
folder = await fsToFolder(storyPath, false);
}
if (!opt.skipImageItemGen) {
await genThumbnail(folder, storyPath, opt.thumbnailFromFirstItem);
}
if (!opt.skipImageItemGen || !opt.skipAudioItemGen) {
await genMissingItems(folder, lang, true, storyPath, opt);
folder = await fsToFolder(storyPath, false);
}
if (!opt.skipAudioConvert) {
await convertAudioOfFolder(
storyPath,
folder,
!!opt.addDelay,
opt.seekStory,
);
}
if (!opt.skipImageConvert) {
await convertImageOfFolder(storyPath, folder);
}
if (!opt.skipZipGeneration) {
folder = await fsToFolder(storyPath, true);
const metadata: Metadata = await getMetadata(storyPath, opt);
const pack = await folderToPack(folder, metadata);
const nightModeAudioItemName = getNightModeAudioItem(folder);
const serializedPack = await serializePack(pack, opt, {
autoNextStoryTransition: opt.autoNextStoryTransition,
nightModeAudioItemName,
});
const assets = getAssetsPaths(serializedPack, folder);
const date = new Date()
.toISOString()
.substring(0, 19)
.replace("T", "--")
.replaceAll(":", "-");
const zipPath = opt.outputFolder
? join(opt.outputFolder, `${basename(storyPath)}--${date}.zip`)
: `${storyPath}--${date}.zip`;
await createPackZip(zipPath, storyPath, serializedPack, assets);
console.log(
`Done (${
(Date.now() - start) / 1000
} sec) : ${storyPath} → ${zipPath}`,
);
}
}
await $.path(`${storyPath}/0-config.json`).writeText(
JSON.stringify(opt, null, " "),
);
}
}
export async function getMetadata(
storyPath: string,
opt: StudioPackGenerator,
): Promise<Metadata> {
const metadataPath = `${storyPath}/metadata.json`;
if (await exists(metadataPath)) {
const metadataJson = await Deno.readTextFile(metadataPath);
return JSON.parse(metadataJson);
} else {
return {
nightMode: !!opt.nightMode,
};
}
}