FeedHandler.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __importDefault = (this && this.__importDefault) || function (mod) {
  16. return (mod && mod.__esModule) ? mod : { "default": mod };
  17. };
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
  22. result["default"] = mod;
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. var domhandler_1 = __importDefault(require("domhandler"));
  27. var DomUtils = __importStar(require("domutils"));
  28. var Parser_1 = require("./Parser");
  29. //TODO: Consume data as it is coming in
  30. var FeedHandler = /** @class */ (function (_super) {
  31. __extends(FeedHandler, _super);
  32. /**
  33. *
  34. * @param callback
  35. * @param options
  36. */
  37. function FeedHandler(callback, options) {
  38. var _this = this;
  39. if (typeof callback === "object" && callback !== null) {
  40. callback = undefined;
  41. options = callback;
  42. }
  43. _this = _super.call(this, callback, options) || this;
  44. return _this;
  45. }
  46. FeedHandler.prototype.onend = function () {
  47. var feed = {};
  48. var feedRoot = getOneElement(isValidFeed, this.dom);
  49. if (feedRoot) {
  50. if (feedRoot.name === "feed") {
  51. var childs = feedRoot.children;
  52. feed.type = "atom";
  53. addConditionally(feed, "id", "id", childs);
  54. addConditionally(feed, "title", "title", childs);
  55. var href = getAttribute("href", getOneElement("link", childs));
  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. feed.items = getElements("entry", childs).map(function (item) {
  66. var entry = {};
  67. var children = item.children;
  68. addConditionally(entry, "id", "id", children);
  69. addConditionally(entry, "title", "title", children);
  70. var href = getAttribute("href", getOneElement("link", children));
  71. if (href) {
  72. entry.link = href;
  73. }
  74. var description = fetch("summary", children) ||
  75. fetch("content", children);
  76. if (description) {
  77. entry.description = description;
  78. }
  79. var pubDate = fetch("updated", children);
  80. if (pubDate) {
  81. entry.pubDate = new Date(pubDate);
  82. }
  83. return entry;
  84. });
  85. }
  86. else {
  87. var childs = getOneElement("channel", feedRoot.children)
  88. .children;
  89. feed.type = feedRoot.name.substr(0, 3);
  90. feed.id = "";
  91. addConditionally(feed, "title", "title", childs);
  92. addConditionally(feed, "link", "link", childs);
  93. addConditionally(feed, "description", "description", childs);
  94. var updated = fetch("lastBuildDate", childs);
  95. if (updated) {
  96. feed.updated = new Date(updated);
  97. }
  98. addConditionally(feed, "author", "managingEditor", childs, true);
  99. feed.items = getElements("item", feedRoot.children).map(function (item) {
  100. var entry = {};
  101. var children = item.children;
  102. addConditionally(entry, "id", "guid", children);
  103. addConditionally(entry, "title", "title", children);
  104. addConditionally(entry, "link", "link", children);
  105. addConditionally(entry, "description", "description", children);
  106. var pubDate = fetch("pubDate", children);
  107. if (pubDate)
  108. entry.pubDate = new Date(pubDate);
  109. return entry;
  110. });
  111. }
  112. }
  113. this.feed = feed;
  114. this.handleCallback(feedRoot ? null : Error("couldn't find root of feed"));
  115. };
  116. return FeedHandler;
  117. }(domhandler_1.default));
  118. exports.FeedHandler = FeedHandler;
  119. function getElements(what, where) {
  120. return DomUtils.getElementsByTagName(what, where, true);
  121. }
  122. function getOneElement(what, where) {
  123. return DomUtils.getElementsByTagName(what, where, true, 1)[0];
  124. }
  125. function fetch(what, where, recurse) {
  126. if (recurse === void 0) { recurse = false; }
  127. return DomUtils.getText(DomUtils.getElementsByTagName(what, where, recurse, 1)).trim();
  128. }
  129. function getAttribute(name, elem) {
  130. if (!elem) {
  131. return null;
  132. }
  133. var attribs = elem.attribs;
  134. return attribs[name];
  135. }
  136. function addConditionally(obj, prop, what, where, recurse) {
  137. if (recurse === void 0) { recurse = false; }
  138. var tmp = fetch(what, where, recurse);
  139. // @ts-ignore
  140. if (tmp)
  141. obj[prop] = tmp;
  142. }
  143. function isValidFeed(value) {
  144. return value === "rss" || value === "feed" || value === "rdf:RDF";
  145. }
  146. var defaultOptions = { xmlMode: true };
  147. /**
  148. * Parse a feed.
  149. *
  150. * @param feed The feed that should be parsed, as a string.
  151. * @param options Optionally, options for parsing. When using this option, you probably want to set `xmlMode` to `true`.
  152. */
  153. function parseFeed(feed, options) {
  154. if (options === void 0) { options = defaultOptions; }
  155. var handler = new FeedHandler(options);
  156. new Parser_1.Parser(handler, options).end(feed);
  157. return handler.feed;
  158. }
  159. exports.parseFeed = parseFeed;