This repository has been archived by the owner on Oct 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·51 lines (42 loc) · 1.54 KB
/
index.js
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
#!/usr/bin/env node
const version = require("./package.json").version;
console.log(`Starting acsm-to-epub v${version}`);
const fs = require("fs");
const path = require("path");
const xml2js = require("xml2js");
const upload = require("./upload");
const knock = require("./knock");
const config = require("./config");
(async () => {
try {
await config.init();
upload.init();
await upload.testCredentials();
if (process.argv.length < 3 || path.extname(process.argv[2]) !== ".acsm") {
throw new Error("Usage: acsmtoepub <file.acsm>");
}
const originalFile = process.argv[2];
await knock.init(originalFile);
const cleanName = await getCleanName(originalFile);
const acsmPath = path.join(config.booksDir, `${cleanName}.acsm`);
await fs.promises.copyFile(originalFile, acsmPath);
console.log("Copied to", acsmPath);
const epubPath = await knock.convert(acsmPath);
console.log("Converted to", epubPath);
await upload.upload(`${cleanName}.epub`, epubPath);
console.log("uploaded file to Drive");
} catch (error) {
console.error(error);
process.exit(1);
}
})();
async function getCleanName(inputFile) {
console.log("Reading", inputFile);
const input = await fs.promises.readFile(inputFile, { encoding: "utf8" });
const xml = await xml2js.parseStringPromise(input);
const metadata = xml.fulfillmentToken.resourceItemInfo[0].metadata[0];
const title = metadata["dc:title"][0]._;
const author = metadata["dc:creator"][0]._;
const outputFile = `${title} - ${author}`;
return outputFile;
}