Skip to content
This repository has been archived by the owner on Feb 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #35 from princesoni1989/master
Browse files Browse the repository at this point in the history
Added support to upload native video to twitter.
  • Loading branch information
reneraab committed May 3, 2016
2 parents d02c400 + fba8a11 commit 61fa0bc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,10 @@ For Streams you must use _getStream_ which has two instead of just one callback:
To upload media to Twitter, call `twitter.uploadMedia(params, accessToken, accessTokenSecret, callback)` with params containing the following:
* _media_: Either the raw binary content of the image, the binary base64 encoded (see isBase64 below) or the path to the file containing the image.
* _isBase64_: Set to true, if media contains base64 encoded data

For a example result see https://dev.twitter.com/rest/reference/post/media/upload. You can pass multiple media_ids to the statuses/update endpoint by seperating them with commas (e.g. "[id1],[id2],[id3],[id4]").

## How to upload Video ##
To upload video to Twitter, call `twitter.uploadVideo(params, accessToken, accessTokenSecret, callback)` with params containing the following:
* _media_: Path to the file containing the video.

You can pass media_id to the statuses/update endpoint and video will be uploaded to twitter. Please note that video should be less than 15mb or 30 sec in length.
84 changes: 84 additions & 0 deletions twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,90 @@ Twitter.prototype.uploadMedia = function(params, accessToken, accessTokenSecret,
}
};

/**
* upload video to twitter
* @param params
* @param accessToken
* @param accessTokenSecret
* @param callback
*/
Twitter.prototype.uploadVideo = function (params, accessToken, accessTokenSecret, callback) {
var bufferLength = 1000000;
var theBuffer = new Buffer(bufferLength);
var offset = 0;
var segment_index = 0;
var finished = 0;
var oauthObj = {
consumer_key: this.consumerKey,
consumer_secret: this.consumerSecret,
token: accessToken,
token_secret: accessTokenSecret
};

fs.stat(params.media, function (err, stats) {
var formData, finalizeVideo, options;
formData = {
command: "INIT",
media_type: 'video/mp4',
total_bytes: stats.size
};
options = {
url: uploadBaseUrl + "media/upload.json",
oauth: oauthObj,
formData: formData
};

finalizeVideo = function (media_id) {
return function (err, response, body) {

finished++;
if (finished === segment_index) {

options.formData = {
command: 'FINALIZE',
media_id: media_id
};
request.post(options, function (err, response, body) {
if (err) {
return cb(err, body);
} else {
try {
return callback(null, JSON.parse(body));
} catch (e) {
return callback(e, body);
}
}
});
}
};
};
request.post(options, function (err, response, body) {
var media_id;
media_id = JSON.parse(body).media_id_string;
fs.open(params.media, 'r', function (err, fd) {
var bytesRead, data;

while (offset < stats.size) {

bytesRead = fs.readSync(fd, theBuffer, 0, bufferLength, null);
data = bytesRead < bufferLength ? theBuffer.slice(0, bytesRead) : theBuffer;
options.formData = {
command: "APPEND",
media_id: media_id,
segment_index: segment_index,
media_data: data.toString('base64')
};
request.post(options, finalizeVideo(media_id));
offset += bufferLength;
segment_index++
}
});
});
});
};



// Search
Twitter.prototype.search = function(params, accessToken, accessTokenSecret, callback) {
this.oa.get(baseUrl + "search/tweets.json?" + querystring.stringify(params), accessToken, accessTokenSecret, function(error, data, response) {
Expand Down

0 comments on commit 61fa0bc

Please sign in to comment.