Skip to content

Commit afbed88

Browse files
committed
Twitter-video-upload-ver01
I tried to write the code but it doesn't work. I would like to be able to upload a video or a larger gif file with this module.
1 parent 9bae313 commit afbed88

File tree

1 file changed

+61
-39
lines changed

1 file changed

+61
-39
lines changed

social/twitter/27-twitter.js

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -611,52 +611,74 @@ module.exports = function(RED) {
611611
node.warn(RED._("twitter.errors.truncated"));
612612
}
613613
var mediaPromise;
614-
if (msg.media && Buffer.isBuffer(msg.media)) {
615-
// var mediaType = fileType(msg.media);
616-
// if (mediaType === null) {
617-
// node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
618-
// node.error("msg.media is not a valid media object",msg);
619-
// return;
620-
// }
621-
mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json",null,null,{
622-
media: msg.media
623-
}).then(function(result) {
624-
if (result.status === 200) {
625-
return result.body.media_id_string;
626-
} else {
627-
throw new Error(result.body.errors[0]);
628-
}
629-
});
614+
if (msg.media && Buffer.isBuffer(msg.media)) {
630615

631-
} else {
632-
mediaPromise = Promise.resolve();
633-
}
634-
mediaPromise.then(function(mediaId) {
635-
var params = msg.params || {};
636-
params.status = msg.payload;
637-
if (mediaId) {
638-
params.media_ids = mediaId;
616+
mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json",{
617+
command: "INIT",
618+
total_bytes: msg.mediaSize,
619+
media_type: msg.mediaType
620+
}).then(function(result) {
621+
if (result.status === 200) {
622+
return result.body.media_id_string;
623+
} else {
624+
throw new Error(result.body.errors[0]);
639625
}
640-
node.twitterConfig.post("https://api.twitter.com/1.1/statuses/update.json",{},params).then(function(result) {
641-
if (result.status === 200) {
642-
node.status({});
626+
});
627+
628+
629+
mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json",{
630+
command: "APPEND",
631+
media_id: mediaId,
632+
media: msg.media,
633+
segment_index: 0
634+
}).then(function(result) {
635+
if (result.status === 200) {
636+
return result.media_id;
637+
} else {
638+
throw new Error(result.body.errors[0]);
639+
}
640+
});
641+
642+
mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json",{
643+
command: "FINALIZE",
644+
media_id: mediaId
645+
}).then(function(result) {
646+
if (result.status === 200) {
647+
return result.media_id;
648+
} else {
649+
throw new Error(result.body.errors[0]);
650+
}
651+
});
652+
653+
} else {
654+
mediaPromise = Promise.resolve();
655+
}
656+
mediaPromise.then(function(mediaId) {
657+
var params = msg.params || {};
658+
params.status = msg.payload;
659+
if (mediaId) {
660+
params.media_ids = mediaId;
661+
}
662+
node.twitterConfig.post("https://api.twitter.com/1.1/statuses/update.json",{},params).then(function(result) {
663+
if (result.status === 200) {
664+
node.status({});
665+
} else {
666+
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
667+
668+
if ('error' in result.body && typeof result.body.error === 'string') {
669+
node.error(result.body.error,msg);
643670
} else {
644-
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
645-
646-
if ('error' in result.body && typeof result.body.error === 'string') {
647-
node.error(result.body.error,msg);
648-
} else {
649-
node.error(result.body.errors[0].message,msg);
650-
}
671+
node.error(result.body.errors[0].message,msg);
651672
}
652-
}).catch(function(err) {
653-
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
654-
node.error(err,msg);
655-
})
673+
}
656674
}).catch(function(err) {
657675
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
658676
node.error(err,msg);
659-
});
677+
})
678+
}).catch(function(err) {
679+
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
680+
node.error(err,msg);
681+
});
660682
// if (msg.payload.length > 280) {
661683
// msg.payload = msg.payload.slice(0,279);
662684
// node.warn(RED._("twitter.errors.truncated"));

0 commit comments

Comments
 (0)