我正在尝试将媒体上传到Twitter。我运行这段代码,显然append,init和finalize函数正在运行。但是,当我运行状态功能时,那里会永远收到状态待定错误,并且无法升级媒体。我不知道媒体附加或finalizze函数中存在错误,导致状态更新检查永远挂起。
var request = require('request');
var fs = require('fs');
var twit = require("twit")
var config = require("./config.js")
var t = new twit(config)
var MEDIA_ENDPOINT_URL = 'https://upload.twitter.com/1.1/media/upload.json'
var POST_TWEET_URL = 'https://api.twitter.com/1.1/statuses/update.json'
/**
* Video Tweet constructor
**/
var VideoTweet = function (data) {
var self = this;
self.file_path = data.file_path;
self.tweet_text = data.tweet_text;
self.total_bytes = undefined;
self.media_id = undefined;
self.processing_info = undefined;
// retreives file info and inits upload on complete
fs.stat(self.file_path, function (error, stats) {
self.total_bytes = stats.size
self.upload_init();
});
};
/**
* Inits media upload
*/
VideoTweet.prototype.upload_init = function () {
console.log('INIT');
var self = this;
t.post('media/upload',
{
'command': 'INIT',
'media_type': 'video/mp4',
'total_bytes': self.total_bytes,
'media_category': 'tweetvideo'
},
// inits media upload
function (error, response, body) {
// store media ID for later reference
self.media_id = response.media_id_string;
// start appening media segments
self.upload_append();
})
}
/**
* Uploads/appends video file segments
*/
VideoTweet.prototype.upload_append = function () {
var buffer_length = 5000000;
var buffer = Buffer.alloc(buffer_length);
var bytes_sent = 0;
var self = this;
// open and read video file
fs.open(self.file_path, 'r', function(error, file_data) {
var bytes_read, data,
segment_index = 0,
segments_completed = 0;
// upload video file in chunks
while (bytes_sent < self.total_bytes) {
console.log('APPEND');
bytes_read = fs.readSync(file_data, buffer, 0, buffer_length, null);
data = bytes_read < buffer_length ? buffer.slice(0, bytes_read) : buffer;
t.post('media/upload',
{
command: 'APPEND',
media_id: self.media_id,
segment_index: segment_index,
media_data: data.toString('base64')
},
function () {
segments_completed = segments_completed + 1;
console.log('segment_completed');
if (segments_completed == segment_index) {
console.log('Upload chunks complete');
self.upload_finalize();
}
});
bytes_sent = bytes_sent + buffer_length;
segment_index = segment_index + 1;
}
});
}
/**
* Finalizes media segments uploaded
*/
VideoTweet.prototype.upload_finalize = function () {
console.log('FINALIZE');
var self = this;
t.post('media/upload',
{
'command': 'FINALIZE',
'media_id': self.media_id
},
function(error, response, body) {
self.check_status(response.processing_info);
});
}
/**
* Checks status of uploaded media
*/
VideoTweet.prototype.check_status = function (processing_info) {
var self = this;
// if response does not contain any processing_info, then video is ready
if (!processing_info) {
self.tweet();
return;
}
console.log('STATUS');
t.get('media/upload',
{
'command': 'STATUS',
'media_id': self.media_id
},
// check processing status
function(error, response, body) {
console.log('Media processing status is ' + processing_info.state);
if (processing_info.state == 'succeeded') {
self.tweet();
return
}
else if (processing_info.state == 'failed') {
return;
}
// check status again after specified duration
var timeout_length = processing_info.check_after_secs ? processing_info.check_after_secs * 1000 : 0;
console.log('Checking after ' + timeout_length + ' milliseconds');
setTimeout(function () {
self.check_status(processing_info)
}, timeout_length);
});
}
/**
* Tweets text with attached media
*/
VideoTweet.prototype.tweet = function () {
var self = this;
t.post('statuses/update',
{
'status': self.tweet_text,
'media_ids': self.media_id
})
// publish Tweet
}
/**
* Instantiates a VideoTweet
*/
videoTweet = new VideoTweet({
file_path: '/home/rafael/Documentos/botdaduda/6879447192097213697.mp4',
tweet_text: 'I just uploaded a video with the @TwitterAPI and #nodejs.'
});