-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
50 lines (42 loc) · 1.33 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
var hypercore = require('hypercore')
var Chirp = require('chirp-stream')
var discovery = require('hyperdiscovery')
var assert = require('assert')
module.exports = archiverStream
function archiverStream (dir, opts, cb) {
if (typeof opts === 'function') cb = opts
assert.ok(dir, 'hypertweet: dir/storage required')
assert.ok(cb, 'hypertweet: callback required')
if (!opts) opts = {}
var storage = dir
var streamUrl = opts.streamUrl || 'https://userstream.twitter.com/1.1/user.json'
var twitter = Chirp({
consumer: {
public: process.env.TWITTER_CONSUMER_KEY,
secret: process.env.TWITTER_CONSUMER_SECRET
},
token: {
public: process.env.TWITTER_ACCESS_TOKEN_KEY,
secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
}
})
var tweetstream = twitter.stream(streamUrl, opts.streamOpts)
var userFeed = hypercore(storage, {valueEncoding: 'json'})
userFeed.ready(function (err) {
if (err) return cb(err)
discovery(userFeed)
cb(null, userFeed)
})
tweetstream.on('json', function (data) {
if (!data.user) return // ignore non-tweet stuff
userFeed.append(data, function (err) {
if (err) return console.error(err)
})
})
tweetstream.on('end', function () {
console.error('tweetstream end')
})
tweetstream.on('error', function (err) {
console.error(err)
})
}