1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var fs = require("fs");
|
5 | var moment = require("moment");
|
6 | var serializable_1 = require("r2-lcp-js/dist/es5/src/serializable");
|
7 | var init_globals_1 = require("r2-opds-js/dist/es5/src/opds/init-globals");
|
8 | var opds2_1 = require("r2-opds-js/dist/es5/src/opds/opds2/opds2");
|
9 | var opds2_link_1 = require("r2-opds-js/dist/es5/src/opds/opds2/opds2-link");
|
10 | var opds2_metadata_1 = require("r2-opds-js/dist/es5/src/opds/opds2/opds2-metadata");
|
11 | var opds2_publication_1 = require("r2-opds-js/dist/es5/src/opds/opds2/opds2-publication");
|
12 | var init_globals_2 = require("r2-shared-js/dist/es5/src/init-globals");
|
13 | var metadata_1 = require("r2-shared-js/dist/es5/src/models/metadata");
|
14 | var publication_parser_1 = require("r2-shared-js/dist/es5/src/parser/publication-parser");
|
15 | var UrlUtils_1 = require("r2-utils-js/dist/es5/src/_utils/http/UrlUtils");
|
16 | init_globals_1.initGlobalConverters_OPDS();
|
17 | init_globals_2.initGlobalConverters_SHARED();
|
18 | init_globals_2.initGlobalConverters_GENERIC();
|
19 | console.log("process.cwd(): " + process.cwd());
|
20 | console.log("__dirname: " + __dirname);
|
21 | var args = process.argv.slice(2);
|
22 | if (!args.length) {
|
23 | console.log("FILEPATH ARGUMENTS ARE MISSING.");
|
24 | process.exit(1);
|
25 | }
|
26 | var opdsJsonFilePath = args[0];
|
27 | args = args.slice(1);
|
28 | if (fs.existsSync(opdsJsonFilePath)) {
|
29 | console.log("OPDS2 JSON file already exists.");
|
30 | process.exit(1);
|
31 | }
|
32 | (function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
33 | var feed, nPubs, _i, args_1, pathBase64, pathBase64Str, publication, err_1, publi, linkSelf, coverLink, linkCover, publicationMetadataJson, jsonObj, jsonStr;
|
34 | return tslib_1.__generator(this, function (_a) {
|
35 | switch (_a.label) {
|
36 | case 0:
|
37 | feed = new opds2_1.OPDSFeed();
|
38 | feed.Metadata = new opds2_metadata_1.OPDSMetadata();
|
39 | feed.Metadata.RDFType = "http://schema.org/DataFeed";
|
40 | feed.Metadata.Title = "Readium 2 OPDS 2.0 Feed";
|
41 | feed.Metadata.Modified = moment(Date.now()).toDate();
|
42 | feed.Publications = [];
|
43 | nPubs = 0;
|
44 | _i = 0, args_1 = args;
|
45 | _a.label = 1;
|
46 | case 1:
|
47 | if (!(_i < args_1.length)) return [3, 7];
|
48 | pathBase64 = args_1[_i];
|
49 | pathBase64Str = Buffer.from(decodeURIComponent(pathBase64), "base64").toString("utf8");
|
50 | if (UrlUtils_1.isHTTP(pathBase64Str)) {
|
51 | return [3, 6];
|
52 | }
|
53 | console.log("OPDS parsing: " + pathBase64Str);
|
54 | publication = void 0;
|
55 | _a.label = 2;
|
56 | case 2:
|
57 | _a.trys.push([2, 4, , 5]);
|
58 | return [4, publication_parser_1.PublicationParsePromise(pathBase64Str)];
|
59 | case 3:
|
60 | publication = _a.sent();
|
61 | return [3, 5];
|
62 | case 4:
|
63 | err_1 = _a.sent();
|
64 | console.log(err_1);
|
65 | return [3, 6];
|
66 | case 5:
|
67 | nPubs++;
|
68 | publi = new opds2_publication_1.OPDSPublication();
|
69 | publi.Links = [];
|
70 | linkSelf = new opds2_link_1.OPDSLink();
|
71 | linkSelf.Href = pathBase64 + "/manifest.json";
|
72 | linkSelf.TypeLink =
|
73 | (publication.Metadata && publication.Metadata.RDFType &&
|
74 | /http[s]?:\/\/schema\.org\/Audiobook$/.test(publication.Metadata.RDFType)) ?
|
75 | "application/audiobook+json" : ((publication.Metadata && publication.Metadata.RDFType &&
|
76 | (/http[s]?:\/\/schema\.org\/ComicStory$/.test(publication.Metadata.RDFType) ||
|
77 | /http[s]?:\/\/schema\.org\/VisualNarrative$/.test(publication.Metadata.RDFType))) ? "application/divina+json" :
|
78 | "application/webpub+json");
|
79 | linkSelf.AddRel("http://opds-spec.org/acquisition");
|
80 | publi.Links.push(linkSelf);
|
81 | feed.Publications.push(publi);
|
82 | publi.Images = [];
|
83 | coverLink = publication.GetCover();
|
84 | if (coverLink) {
|
85 | linkCover = new opds2_link_1.OPDSLink();
|
86 | linkCover.Href = pathBase64 + "/" + coverLink.Href;
|
87 | linkCover.TypeLink = coverLink.TypeLink;
|
88 | if (coverLink.Width && coverLink.Height) {
|
89 | linkCover.Width = coverLink.Width;
|
90 | linkCover.Height = coverLink.Height;
|
91 | }
|
92 | publi.Images.push(linkCover);
|
93 | }
|
94 | else {
|
95 | console.log("NO COVER IMAGE?");
|
96 | }
|
97 | if (publication.Metadata) {
|
98 | try {
|
99 | publicationMetadataJson = serializable_1.TaJsonSerialize(publication.Metadata);
|
100 | publi.Metadata = serializable_1.TaJsonDeserialize(publicationMetadataJson, metadata_1.Metadata);
|
101 | }
|
102 | catch (err) {
|
103 | console.log(err);
|
104 | return [3, 6];
|
105 | }
|
106 | }
|
107 | _a.label = 6;
|
108 | case 6:
|
109 | _i++;
|
110 | return [3, 1];
|
111 | case 7:
|
112 | feed.Metadata.NumberOfItems = nPubs;
|
113 | jsonObj = serializable_1.TaJsonSerialize(feed);
|
114 | jsonStr = global.JSON.stringify(jsonObj, null, "");
|
115 | fs.writeFileSync(opdsJsonFilePath, jsonStr, { encoding: "utf8" });
|
116 | console.log("DONE! :)");
|
117 | console.log(opdsJsonFilePath);
|
118 | return [2];
|
119 | }
|
120 | });
|
121 | }); })();
|
122 |
|
\ | No newline at end of file |