-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
186 lines (148 loc) · 4.32 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const FeedParser = require('feedparser')
const FeedGen = require('feed')
const toStream = require('string-to-stream')
const async = require('async')
const request = require('request')
const uuid = require('uuid')
const pump = require('pump')
const collect = require('collect-stream')
const moment = require('moment')
const DEFAULT_OPTS = {scrapLink: true}
const METADATA_FILE = 'metadata.json'
const SCRAP_DIR = 'scrapped'
function Feed (archive, opts) {
if (!(this instanceof Feed)) return new Feed(archive, opts)
opts = Object.assign({}, DEFAULT_OPTS, opts)
this.scrapLink = opts.scrapLink
this.archive = archive
this.archive.ready(() => {
this.key = this.archive.key
this.discoveryKey = this.archive.discoveryKey
})
}
Feed.prototype.ready = function (cb) {
this.archive.ready(cb)
}
Feed.prototype.update = function (feedStream, cb) {
if (!this.archive.metadata.writable) return cb(new Error("can't update archive you don't own"))
var self = this
var feedparser = new FeedParser()
feedStream.pipe(feedparser)
var tasks = []
feedparser.on('error', e => cb(e))
feedparser.on('meta', meta => {
this.meta = meta
tasks.push((cb) => {
var ws = self.archive.createWriteStream(METADATA_FILE)
toStream(JSON.stringify(meta)).pipe(ws).on('finish', cb)
})
})
feedparser.on('readable', function () {
var readable = this
var item
while ((item = readable.read())) {
tasks.push(_save(item))
}
})
feedparser.on('end', function () {
async.series(tasks, (err) => {
if (err) return cb(err)
cb(null, self)
})
})
function _save (item) {
return (cb) => {
self.save(item, cb)
}
}
}
Feed.prototype.setMeta = function (meta, cb) {
var self = this
self.meta = meta
var ws = self.archive.createWriteStream(METADATA_FILE)
pump(toStream(JSON.stringify(meta)), ws, cb)
}
Feed.prototype.list = function (cb) {
this.archive.readdir('/', function (err, list) {
if (err) return cb(err)
var results = []
list.forEach(name => {
if (name !== METADATA_FILE && name !== SCRAP_DIR) results.push(name)
})
cb(null, results)
})
}
Feed.prototype.export = function (count, cb) {
var self = this
this.list((err, files) => {
if (err) return cb(err)
if (files.length > count) {
files = files.slice(0, 10)
}
var tasks = []
files.forEach(e => { tasks.push(this._load(e)) })
async.series(tasks, (err, results) => {
if (err) return cb(err)
buildXML(results, cb)
})
})
function buildXML (entries, cb) {
var meta = self.meta
var feed = new FeedGen(Object.assign({}, meta, {feed_url: meta.xmlUrl, site_url: meta.link}))
entries.forEach(e => {
var x = JSON.parse(e)
x.date = moment(x.date).toDate()
feed.addItem(x)
})
cb(null, feed.render('rss-2.0'))
}
}
Feed.prototype.save = function (item, scrappedData, cb) {
if (cb === undefined && typeof scrappedData === 'function') {
cb = scrappedData
scrappedData = undefined
}
if (!item.guid) item.guid = uuid.v1()
if (!item.date) item.date = new Date()
var self = this
self.list((err, files) => {
if (err) return cb(err)
if (files.find(name => name === item.guid)) return cb() // ignore duplicated entry
var to = self.archive.createWriteStream(item.guid)
toStream(JSON.stringify(item)).pipe(to).on('finish', done)
})
function done () {
if (scrappedData) return _saveScrapped(item, scrappedData, cb)
if (self.scrapLink) return _scrap(item, cb)
return cb()
}
function _scrap (item, cb) {
var url = item.url || item.link
request(url, (err, resp, body) => {
if (err) return cb(err)
if (resp.statusCode !== 200) { return cb(new Error('invalid status code')) }
_saveScrapped(item, body, cb)
})
}
function _saveScrapped (item, data, cb) {
pump(toStream(data), self.archive.createWriteStream(scrappedPath(item.guid)), cb)
}
}
Feed.prototype.get = function (id, cb) {
this._load(id)((err, item) => {
if (err) return cb(err)
cb(null, item)
})
}
Feed.prototype.getScrapped = function (id, cb) {
this.get(scrappedPath(id), cb)
}
Feed.prototype._load = function (entry, opts) {
return (cb) => {
collect(this.archive.createReadStream(entry), cb)
}
}
module.exports = Feed
function scrappedPath (id) {
return `${SCRAP_DIR}/${id}`
}