I’m working on twitter video upload script in node and am running into the 131 error (which I haven’t seen solved yet).
Below is the function code I’m using, trying to follow the current documentation for chunk uploading.
Any help would be GREATLY appreciated
fs.stat(filePath, function(err, stats) {
var formData, normalAppendCallback, options;
console.log('STAT SIZE', stats.size);
formData = {
command: "INIT",
media_type: 'video/mp4',
total_bytes: stats.size
};
options = {
url: 'https://upload.twitter.com/1.1/media/upload.json',
oauth: oauthCredentials,
formData: formData
};
normalAppendCallback = function(media_id) {
return function(err, response, body) {
finished++;
if (finished === segment_index) {
// console.log(response)
console.log('CALL FINALIZE')
options.formData = {
command: 'FINALIZE',
media_id: media_id
};
request.post(options, function(err, response, body) {
console.log('FINALIZED',response.statusCode,body);
// console.log(response)
delete options.formData;
//Note: This is not working as expected yet.
options.qs = {
command: 'STATUS',
media_id: media_id
};
console.log('STATUS OPTIONS', options)
setTimeout(function() {
request.get(options, function(err, response, body) {
console.log('STATUS: ', response.statusCode, body);
});
}, 120000);
});
}
};
};
request.post(options, function(err, response, body) {
var media_id;
media_id = JSON.parse(body).media_id_string;
console.log(media_id)
fs.open(filePath, 'r', function(err, fd) {
var bytesRead, data;
while (offset < stats.size) {
console.log('tick')
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')
};
// console.log(options)
request.post(options, normalAppendCallback(media_id));
offset += bufferLength;
segment_index++
}
});
});
});