Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FeedHandler.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var DomHandler = require("domhandler");
  2. var DomUtils = require("domutils");
  3. //TODO: make this a streamable handler
  4. function FeedHandler(callback, options) {
  5. this.init(callback, options);
  6. }
  7. require("inherits")(FeedHandler, DomHandler);
  8. FeedHandler.prototype.init = DomHandler;
  9. function getElements(what, where) {
  10. return DomUtils.getElementsByTagName(what, where, true);
  11. }
  12. function getOneElement(what, where) {
  13. return DomUtils.getElementsByTagName(what, where, true, 1)[0];
  14. }
  15. function fetch(what, where, recurse) {
  16. return DomUtils.getText(
  17. DomUtils.getElementsByTagName(what, where, recurse, 1)
  18. ).trim();
  19. }
  20. function addConditionally(obj, prop, what, where, recurse) {
  21. var tmp = fetch(what, where, recurse);
  22. if (tmp) obj[prop] = tmp;
  23. }
  24. var isValidFeed = function(value) {
  25. return value === "rss" || value === "feed" || value === "rdf:RDF";
  26. };
  27. FeedHandler.prototype.onend = function() {
  28. var feed = {},
  29. feedRoot = getOneElement(isValidFeed, this.dom),
  30. tmp,
  31. childs;
  32. if (feedRoot) {
  33. if (feedRoot.name === "feed") {
  34. childs = feedRoot.children;
  35. feed.type = "atom";
  36. addConditionally(feed, "id", "id", childs);
  37. addConditionally(feed, "title", "title", childs);
  38. if (
  39. (tmp = getOneElement("link", childs)) &&
  40. (tmp = tmp.attribs) &&
  41. (tmp = tmp.href)
  42. )
  43. feed.link = tmp;
  44. addConditionally(feed, "description", "subtitle", childs);
  45. if ((tmp = fetch("updated", childs))) feed.updated = new Date(tmp);
  46. addConditionally(feed, "author", "email", childs, true);
  47. feed.items = getElements("entry", childs).map(function(item) {
  48. var entry = {},
  49. tmp;
  50. item = item.children;
  51. addConditionally(entry, "id", "id", item);
  52. addConditionally(entry, "title", "title", item);
  53. if (
  54. (tmp = getOneElement("link", item)) &&
  55. (tmp = tmp.attribs) &&
  56. (tmp = tmp.href)
  57. )
  58. entry.link = tmp;
  59. if ((tmp = fetch("summary", item) || fetch("content", item)))
  60. entry.description = tmp;
  61. if ((tmp = fetch("updated", item)))
  62. entry.pubDate = new Date(tmp);
  63. return entry;
  64. });
  65. } else {
  66. childs = getOneElement("channel", feedRoot.children).children;
  67. feed.type = feedRoot.name.substr(0, 3);
  68. feed.id = "";
  69. addConditionally(feed, "title", "title", childs);
  70. addConditionally(feed, "link", "link", childs);
  71. addConditionally(feed, "description", "description", childs);
  72. if ((tmp = fetch("lastBuildDate", childs)))
  73. feed.updated = new Date(tmp);
  74. addConditionally(feed, "author", "managingEditor", childs, true);
  75. feed.items = getElements("item", feedRoot.children).map(function(
  76. item
  77. ) {
  78. var entry = {},
  79. tmp;
  80. item = item.children;
  81. addConditionally(entry, "id", "guid", item);
  82. addConditionally(entry, "title", "title", item);
  83. addConditionally(entry, "link", "link", item);
  84. addConditionally(entry, "description", "description", item);
  85. if ((tmp = fetch("pubDate", item)))
  86. entry.pubDate = new Date(tmp);
  87. return entry;
  88. });
  89. }
  90. }
  91. this.dom = feed;
  92. DomHandler.prototype._handleCallback.call(
  93. this,
  94. feedRoot ? null : Error("couldn't find root of feed")
  95. );
  96. };
  97. module.exports = FeedHandler;