forked from bcomnes/twitter-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (95 loc) · 2.5 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
var request = require('request')
var fs = require('fs')
var extend = require('util-extend')
var duplexify = require('duplexify')
var through = require('through2')
// Caution!
// // https://dev.twitter.com/overview/api/twitter-ids-json-and-snowflake
var ENDPOINT = 'https://upload.twitter.com/1.1/media/upload.json'
function twitterVideo (oauth, total_bytes) {
var media_id
var dup = duplexify()
var oauthReq = {
consumer_key: oauth.consumer_key,
consumer_secret: oauth.consumer_secret,
token: oauth.access_token,
token_secret: oauth.access_token_secret
}
var baseReq = {
url: ENDPOINT,
oauth: oauthReq,
json: true
}
dup.once('pipe', initialRequest)
function initialRequest () {
var initReq = extend({
formData: {
media_type: 'video/mp4',
command: 'INIT',
total_bytes: total_bytes || 15000000
}
}, baseReq)
request.post(initReq, initCb)
}
function initCb (err, res, body) {
if (err) return dup.destroy(err)
media_id = body.media_id_string
dup.setWritable(appendStream(media_id, baseReq, finalRequest))
}
function finalRequest (cb) {
var finalReq = extend({
formData: {
command: 'FINALIZE',
media_id: media_id
}
}, baseReq)
request.post(finalReq, function (err, res, body) {
if (err) return cb(err)
dup.emit('media_id', media_id)
return cb()
})
}
return dup
}
function counter () {
var i = 0
return function aCounter () {
return i++
}
}
function appendStream (media_id, baseReq, flush) {
var c = counter()
var append = through(write, flush)
var appendReq = extend({ formData: {
command: 'APPEND',
media_id: media_id
}
}, baseReq)
function write (chunk, enc, next) {
appendReq.formData.segment_index = c()
appendReq.formData.media = chunk
request.post(appendReq, function append (err, res, body) {
next(err)
})
}
return append
}
function fromFile (path, oauth, cb) {
fs.stat(path, function (err, stats) {
if (err) return cb(err)
var fileSize = stats.size
var fileStream = fs.createReadStream(path)
var videoStream = twitterVideo(oauth, fileSize)
videoStream.on('error', function streamError (err) {
return cb(err)
})
videoStream.on('finish', function finalize () {
videoStream.on('media_id', function (media_id) {
return cb(null, media_id)
})
})
fileStream.pipe(videoStream)
})
}
twitterVideo.fromFile = fromFile
module.exports = twitterVideo