1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getFeed = void 0;
|
4 | var stringify_1 = require("./stringify");
|
5 | var legacy_1 = require("./legacy");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function getFeed(doc) {
|
13 | var feedRoot = getOneElement(isValidFeed, doc);
|
14 | return !feedRoot
|
15 | ? null
|
16 | : feedRoot.name === "feed"
|
17 | ? getAtomFeed(feedRoot)
|
18 | : getRssFeed(feedRoot);
|
19 | }
|
20 | exports.getFeed = getFeed;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function getAtomFeed(feedRoot) {
|
28 | var _a;
|
29 | var childs = feedRoot.children;
|
30 | var feed = {
|
31 | type: "atom",
|
32 | items: (0, legacy_1.getElementsByTagName)("entry", childs).map(function (item) {
|
33 | var _a;
|
34 | var children = item.children;
|
35 | var entry = { media: getMediaElements(children) };
|
36 | addConditionally(entry, "id", "id", children);
|
37 | addConditionally(entry, "title", "title", children);
|
38 | var href = (_a = getOneElement("link", children)) === null || _a === void 0 ? void 0 : _a.attribs.href;
|
39 | if (href) {
|
40 | entry.link = href;
|
41 | }
|
42 | var description = fetch("summary", children) || fetch("content", children);
|
43 | if (description) {
|
44 | entry.description = description;
|
45 | }
|
46 | var pubDate = fetch("updated", children);
|
47 | if (pubDate) {
|
48 | entry.pubDate = new Date(pubDate);
|
49 | }
|
50 | return entry;
|
51 | }),
|
52 | };
|
53 | addConditionally(feed, "id", "id", childs);
|
54 | addConditionally(feed, "title", "title", childs);
|
55 | var href = (_a = getOneElement("link", childs)) === null || _a === void 0 ? void 0 : _a.attribs.href;
|
56 | if (href) {
|
57 | feed.link = href;
|
58 | }
|
59 | addConditionally(feed, "description", "subtitle", childs);
|
60 | var updated = fetch("updated", childs);
|
61 | if (updated) {
|
62 | feed.updated = new Date(updated);
|
63 | }
|
64 | addConditionally(feed, "author", "email", childs, true);
|
65 | return feed;
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function getRssFeed(feedRoot) {
|
74 | var _a, _b;
|
75 | var childs = (_b = (_a = getOneElement("channel", feedRoot.children)) === null || _a === void 0 ? void 0 : _a.children) !== null && _b !== void 0 ? _b : [];
|
76 | var feed = {
|
77 | type: feedRoot.name.substr(0, 3),
|
78 | id: "",
|
79 | items: (0, legacy_1.getElementsByTagName)("item", feedRoot.children).map(function (item) {
|
80 | var children = item.children;
|
81 | var entry = { media: getMediaElements(children) };
|
82 | addConditionally(entry, "id", "guid", children);
|
83 | addConditionally(entry, "title", "title", children);
|
84 | addConditionally(entry, "link", "link", children);
|
85 | addConditionally(entry, "description", "description", children);
|
86 | var pubDate = fetch("pubDate", children);
|
87 | if (pubDate)
|
88 | entry.pubDate = new Date(pubDate);
|
89 | return entry;
|
90 | }),
|
91 | };
|
92 | addConditionally(feed, "title", "title", childs);
|
93 | addConditionally(feed, "link", "link", childs);
|
94 | addConditionally(feed, "description", "description", childs);
|
95 | var updated = fetch("lastBuildDate", childs);
|
96 | if (updated) {
|
97 | feed.updated = new Date(updated);
|
98 | }
|
99 | addConditionally(feed, "author", "managingEditor", childs, true);
|
100 | return feed;
|
101 | }
|
102 | var MEDIA_KEYS_STRING = ["url", "type", "lang"];
|
103 | var MEDIA_KEYS_INT = [
|
104 | "fileSize",
|
105 | "bitrate",
|
106 | "framerate",
|
107 | "samplingrate",
|
108 | "channels",
|
109 | "duration",
|
110 | "height",
|
111 | "width",
|
112 | ];
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function getMediaElements(where) {
|
120 | return (0, legacy_1.getElementsByTagName)("media:content", where).map(function (elem) {
|
121 | var attribs = elem.attribs;
|
122 | var media = {
|
123 | medium: attribs.medium,
|
124 | isDefault: !!attribs.isDefault,
|
125 | };
|
126 | for (var _i = 0, MEDIA_KEYS_STRING_1 = MEDIA_KEYS_STRING; _i < MEDIA_KEYS_STRING_1.length; _i++) {
|
127 | var attrib = MEDIA_KEYS_STRING_1[_i];
|
128 | if (attribs[attrib]) {
|
129 | media[attrib] = attribs[attrib];
|
130 | }
|
131 | }
|
132 | for (var _a = 0, MEDIA_KEYS_INT_1 = MEDIA_KEYS_INT; _a < MEDIA_KEYS_INT_1.length; _a++) {
|
133 | var attrib = MEDIA_KEYS_INT_1[_a];
|
134 | if (attribs[attrib]) {
|
135 | media[attrib] = parseInt(attribs[attrib], 10);
|
136 | }
|
137 | }
|
138 | if (attribs.expression) {
|
139 | media.expression =
|
140 | attribs.expression;
|
141 | }
|
142 | return media;
|
143 | });
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function getOneElement(tagName, node) {
|
153 | return (0, legacy_1.getElementsByTagName)(tagName, node, true, 1)[0];
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | function fetch(tagName, where, recurse) {
|
164 | if (recurse === void 0) { recurse = false; }
|
165 | return (0, stringify_1.textContent)((0, legacy_1.getElementsByTagName)(tagName, where, recurse, 1)).trim();
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | function addConditionally(obj, prop, tagName, where, recurse) {
|
177 | if (recurse === void 0) { recurse = false; }
|
178 | var val = fetch(tagName, where, recurse);
|
179 | if (val)
|
180 | obj[prop] = val;
|
181 | }
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | function isValidFeed(value) {
|
189 | return value === "rss" || value === "feed" || value === "rdf:RDF";
|
190 | }
|