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.

jsonfeedparser.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const stream_1 = require("stream");
  7. const clarinet_1 = __importDefault(require("clarinet"));
  8. /**
  9. * Gets rid of unnecessary keys in an object
  10. * and if the only keys in an object are `$t` or `$t` and `type`
  11. * it makes the whole object the value of `$t`
  12. * otherwise it renames `$t` to `text` for consistency.
  13. *
  14. * @param {Object}
  15. * @returns {Object}
  16. */
  17. const cleanObject = (obj) => {
  18. if (obj.$t !== undefined) {
  19. let keysLength = Object.keys(obj).length;
  20. if (keysLength === 1 || (keysLength === 2 && obj.type !== undefined)) {
  21. return obj.$t;
  22. }
  23. else {
  24. obj.text = obj.$t;
  25. delete obj.$t;
  26. }
  27. }
  28. return obj;
  29. };
  30. /**
  31. * Parses a JSON feed.
  32. *
  33. * @param {boolean} buffer If true, will buffer entire object.
  34. * @return {CStream}
  35. */
  36. class JSONFeedParser extends stream_1.Writable {
  37. constructor(buffer) {
  38. super();
  39. this._buffer = buffer;
  40. const parser = this.parser = clarinet_1.default.createStream();
  41. const stack = [];
  42. this._currObj = {};
  43. this._currKey = 'feed';
  44. let inArray = false;
  45. let feedJustFound = false;
  46. // Look feed object in case this is a json encoded atom feed.
  47. // Place these underlying keys onto the root object.
  48. const findfeed = (key) => {
  49. if (key === 'feed') {
  50. feedJustFound = true;
  51. parser.removeListener('openobject', findfeed);
  52. parser.removeListener('key', findfeed);
  53. }
  54. };
  55. const onvalue = (value) => {
  56. feedJustFound = false;
  57. this._currObj[this._currKey] = value;
  58. if (stack.length === 1) {
  59. parser.emit(this._currKey, value);
  60. if (!buffer) {
  61. delete this._currObj[this._currKey];
  62. }
  63. }
  64. if (inArray) {
  65. this._currKey++;
  66. }
  67. };
  68. const onopenobject = (key) => {
  69. if (feedJustFound) {
  70. feedJustFound = false;
  71. this._currKey = key;
  72. return;
  73. }
  74. let obj = this._currObj[this._currKey] = {};
  75. stack.push({
  76. obj: this._currObj,
  77. key: this._currKey,
  78. arr: inArray,
  79. });
  80. this._currObj = obj;
  81. this._currKey = key;
  82. inArray = false;
  83. };
  84. const onkey = (key) => { this._currKey = key; };
  85. const oncloseobject = () => {
  86. let parent = stack.pop();
  87. if (!parent) {
  88. return;
  89. }
  90. this._currObj = parent.obj;
  91. this._currKey = parent.key;
  92. inArray = parent.arr;
  93. // Clean object.
  94. const currObj = this._currObj;
  95. currObj[this._currKey] = cleanObject(currObj[this._currKey]);
  96. // Emit key in feed if curr is parent.
  97. if (stack.length === 1) {
  98. parser.emit(`${this._currKey}`, currObj[this._currKey]);
  99. if (!buffer) {
  100. delete currObj[this._currKey];
  101. }
  102. // Or parent is array.
  103. }
  104. else if (inArray) {
  105. const currArr = currObj;
  106. if (stack.length === 2) {
  107. let key = stack[1].key;
  108. let event = key === 'entry' || key === 'items' ?
  109. 'item' : stack[1].key;
  110. let data = currArr[this._currKey];
  111. parser.emit(event, data);
  112. if (!buffer) {
  113. currArr.splice(this._currKey, 1);
  114. }
  115. }
  116. if (stack.length > 2 || buffer) {
  117. this._currKey++;
  118. }
  119. }
  120. };
  121. const onopenarray = () => {
  122. feedJustFound = false;
  123. let obj = this._currObj[this._currKey] = [];
  124. stack.push({
  125. obj: this._currObj,
  126. key: this._currKey,
  127. arr: inArray,
  128. });
  129. this._currObj = obj;
  130. this._currKey = 0;
  131. inArray = true;
  132. };
  133. const onclosearray = () => {
  134. let parent = stack.pop();
  135. this._currObj = parent.obj;
  136. this._currKey = parent.key;
  137. inArray = parent.arr;
  138. if (stack.length === 1) {
  139. if (!buffer) {
  140. delete this._currObj[this._currKey];
  141. }
  142. }
  143. else if (inArray) {
  144. this._currKey++;
  145. }
  146. };
  147. parser.on('openobject', findfeed);
  148. parser.on('key', findfeed);
  149. parser.on('value', onvalue);
  150. parser.on('openobject', onopenobject);
  151. parser.on('key', onkey);
  152. parser.on('closeobject', oncloseobject);
  153. parser.on('openarray', onopenarray);
  154. parser.on('closearray', onclosearray);
  155. }
  156. _write(chunk, encoding, callback) {
  157. this.parser.write(chunk, encoding);
  158. callback(null);
  159. }
  160. _final(callback) {
  161. this.parser.end();
  162. callback(null);
  163. }
  164. done() {
  165. if (!this._buffer) {
  166. return;
  167. }
  168. let root = this._currObj[this._currKey];
  169. root.type = 'json';
  170. if (Array.isArray(root.entry)) {
  171. root.items = root.entry;
  172. }
  173. delete root.entry;
  174. return root;
  175. }
  176. }
  177. exports.default = JSONFeedParser;
  178. //# sourceMappingURL=jsonfeedparser.js.map