[6a3a178] | 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 | * Get the feed object from the root of a DOM tree.
|
---|
| 8 | *
|
---|
| 9 | * @param doc - The DOM to to extract the feed from.
|
---|
| 10 | * @returns The feed.
|
---|
| 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 | * Parse an Atom feed.
|
---|
| 23 | *
|
---|
| 24 | * @param feedRoot The root of the feed.
|
---|
| 25 | * @returns The parsed feed.
|
---|
| 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 | * Parse a RSS feed.
|
---|
| 69 | *
|
---|
| 70 | * @param feedRoot The root of the feed.
|
---|
| 71 | * @returns The parsed feed.
|
---|
| 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 | * Get all media elements of a feed item.
|
---|
| 115 | *
|
---|
| 116 | * @param where Nodes to search in.
|
---|
| 117 | * @returns Media elements.
|
---|
| 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 | * Get one element by tag name.
|
---|
| 147 | *
|
---|
| 148 | * @param tagName Tag name to look for
|
---|
| 149 | * @param node Node to search in
|
---|
| 150 | * @returns The element or null
|
---|
| 151 | */
|
---|
| 152 | function getOneElement(tagName, node) {
|
---|
| 153 | return (0, legacy_1.getElementsByTagName)(tagName, node, true, 1)[0];
|
---|
| 154 | }
|
---|
| 155 | /**
|
---|
| 156 | * Get the text content of an element with a certain tag name.
|
---|
| 157 | *
|
---|
| 158 | * @param tagName Tag name to look for.
|
---|
| 159 | * @param where Node to search in.
|
---|
| 160 | * @param recurse Whether to recurse into child nodes.
|
---|
| 161 | * @returns The text content of the element.
|
---|
| 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 | * Adds a property to an object if it has a value.
|
---|
| 169 | *
|
---|
| 170 | * @param obj Object to be extended
|
---|
| 171 | * @param prop Property name
|
---|
| 172 | * @param tagName Tag name that contains the conditionally added property
|
---|
| 173 | * @param where Element to search for the property
|
---|
| 174 | * @param recurse Whether to recurse into child nodes.
|
---|
| 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 | * Checks if an element is a feed root node.
|
---|
| 184 | *
|
---|
| 185 | * @param value The name of the element to check.
|
---|
| 186 | * @returns Whether an element is a feed root node.
|
---|
| 187 | */
|
---|
| 188 | function isValidFeed(value) {
|
---|
| 189 | return value === "rss" || value === "feed" || value === "rdf:RDF";
|
---|
| 190 | }
|
---|