-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
139 lines (116 loc) · 3.4 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
var uuid = require('uuid')
var sublevel = require('level-sublevel')
var inherits = require('util').inherits
var EventEmitter = require('events').EventEmitter
var downloader = require('./downloader')
var hooks = require('hooks')
var through = require('through')
var fs = require('fs')
inherits(Giffer, EventEmitter)
function Giffer(args) {
// Make hooks possible
for (var k in hooks) {
this[k] = hooks[k]
}
this.hook('download', this.download)
this.hook('handleGif', this.handleGif)
this.hook('saveMetaData', this.saveMetaData)
this.hook('emitGif', this.emitGif)
this.timeToRestart = args.timeToRestart || 300000 // in ms
this.adapters = []
var db = sublevel(args.db) // value encoding must be json
this.urlDb = db.sublevel('url')
var seqDb = this.seqDb = db.sublevel('seq')
// create time based index
this.urlDb.pre(function(ch, add) {
if (ch.type === 'put') {
add({
key: '' + ch.value.time,
value: ch.key,
type: 'put',
prefix: seqDb
})
} else if (ch.type === 'del') {
this.urlDb.get(ch.key, function(err, obj) {
if (err) return console.error(err)
if (obj && obj.filename) fs.unlink(this.outDir + '/' + obj.filename, noop)
this.seqDb.del(obj.time, noop)
}.bind(this))
}
}.bind(this))
args.adapters.forEach(this.adapters.push.bind(this.adapters))
this.outDir = args.outputDir
this._timeouts = []
}
Giffer.prototype.start = function() {
this.adapters.forEach(function(adapter) {
if (!adapter.start) return
adapter.start()
adapter.on('gif', this.handleGif.bind(this))
adapter.on('stop', function() {
var timeout = setTimeout(adapter.start.bind(adapter), this.timeToRestart)
this._timeouts.push(timeout)
}.bind(this))
}.bind(this))
}
Giffer.prototype.stop = function() {
// stop adapters
this.adapters.forEach(function(adapter) {
if (!adapter.stop) return
adapter.stop()
})
this._timeouts.forEach(function(timeout) {
clearTimout(timeout)
})
}
Giffer.prototype.plugin = function(plugin, opts) {
plugin(this, opts)
}
Giffer.prototype.createSeqReadStream = function(opts) {
var self = this
var tr = through(function(v) {
this.pause()
self.urlDb.get(v.value, function(err, value) {
if (err) return this.emit('error', err)
this.emit('data', {
key: v.key,
filename: value.filename,
metadata: value.meta
})
this.resume()
}.bind(this))
})
process.nextTick(function() {
this.seqDb.createReadStream(opts).pipe(tr)
}.bind(this))
return tr
}
Giffer.prototype.handleGif = function(url, metadata) {
if (!metadata || !metadata.origin) return
this.urlDb.get(url, function(err, value) {
if (!err && value) return
var id = uuid.v1()
this.download(id, url, metadata)
}.bind(this))
}
Giffer.prototype.download = function(id, url, metadata) {
downloader.download(url, this.outDir + '/' + id + '.gif', function() {
this.saveMetaData(url, id, metadata)
}.bind(this))
}
Giffer.prototype.saveMetaData = function(url, id, metadata) {
this.urlDb.put(url, {
filename: id + '.gif',
dir: this.outDir,
time: Date.now(),
meta: metadata
}, function(err) {
if (err) throw err
this.emitGif(id + '.gif', metadata)
}.bind(this))
}
Giffer.prototype.emitGif = function(filename, metadata) {
this.emit('gif', filename, metadata)
}
module.exports = Giffer
function noop() {}