This repository was archived by the owner on May 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinternals.js
92 lines (73 loc) · 1.82 KB
/
internals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Copyright 2018 Uptime Ventures, Ltd.
* All rights reserved.
*
* Usage of this source code is governed by a BSD-style
* license that can be found in LICENSE.md, at the root
* of this repository. Alternatively, visit
* https://spdx.org/licenses/BSD-3-Clause.html.
*
* @flow
*/
const crypto = require('crypto')
const rp = require('request-promise')
const { parseString } = require('xml2js')
const lget = require('lodash.get')
const transform = i => new Promise((resolve, reject) =>
parseString(i, (e, p) => e ? reject(e) : resolve(p))
)
const load = uri => rp({ uri, transform })
const select = (i, key) => {
const value = lget(i, key)
if (Array.isArray(value)) {
return value[0]
}
return value
}
const toDate = str => str ? new Date(str) : null
const digest = i =>
crypto
.createHash('md5')
.update(JSON.stringify(i))
.digest('hex')
const createChildren = (nodes, parent, createNode) => {
const children = []
nodes.forEach(n => {
const link = select(n, 'link')
children.push(link)
const node = {
id: link,
title: select(n, 'title'),
description: select(n, 'description'),
html: select(n, 'content:encoded'),
date: toDate(select(n, 'pubDate')),
link,
parent,
children: [],
}
node.internal = {
type: 'RSSEntry',
contentDigest: digest(node),
}
createNode(node)
})
return children
}
const createFeed = (feed, createNode) => {
const link = select(feed, 'link')
const children = createChildren(feed.item || [], link, createNode)
const node = {
id: link,
title: select(feed, 'title'),
description: select(feed, 'description'),
parent: null,
link,
children,
}
node.internal = { type: 'RSSFeed', contentDigest: digest(node) }
return node
}
module.exports = {
load,
createFeed,
}