node-red-nodes icon indicating copy to clipboard operation
node-red-nodes copied to clipboard

Twitter video upload function

Open tamasvar opened this issue 3 years ago • 5 comments

Which node are you reporting an issue on?

node-red-node-twitter

I would like to be able to upload larger files to Twitter. this already works in node.js The node.js code must be converted to node-red

node.js:

const Twitter = require("twitter")
const dotenv = require("dotenv")
const fs = require("fs")

dotenv.config()

const client = new Twitter({
  consumer_key: process.env.CONSUMER_KEY,
  consumer_secret: process.env.CONSUMER_SECRET,
  access_token_key: process.env.ACCESS_TOKEN_KEY,
  access_token_secret: process.env.ACCESS_TOKEN_SECRET
})

const pathToFile = "./media/homer.gif"
const mediaType = "image/gif"

const mediaData = fs.readFileSync(pathToFile)
const mediaSize = fs.statSync(pathToFile).size

initializeMediaUpload()
  .then(appendFileChunk)
  .then(finalizeUpload)
  .then(publishStatusUpdate)

function initializeMediaUpload() {
  return new Promise(function(resolve, reject) {
    client.post("media/upload", {
      command: "INIT",
      total_bytes: mediaSize,
      media_type: mediaType
    }, function(error, data, response) {
      if (error) {
        console.log(error)
        reject(error)
      } else {
        resolve(data.media_id_string)
      }
    })
  })
}

function appendFileChunk(mediaId) {
  return new Promise(function(resolve, reject) {
    client.post("media/upload", {
      command: "APPEND",
      media_id: mediaId,
      media: mediaData,
      segment_index: 0
    }, function(error, data, response) {
      if (error) {
        console.log(error)
        reject(error)
      } else {
        resolve(mediaId)
      }
    })
  })
}

function finalizeUpload(mediaId) {
  return new Promise(function(resolve, reject) {
    client.post("media/upload", {
      command: "FINALIZE",
      media_id: mediaId
    }, function(error, data, response) {
      if (error) {
        console.log(error)
        reject(error)
      } else {
        resolve(mediaId)
      }
    })
  })
}

function publishStatusUpdate(mediaId) {
  return new Promise(function(resolve, reject) {
    client.post("statuses/update", {
      status: "I tweeted from Node.js!",
      media_ids: mediaId
    }, function(error, data, response) {
      if (error) {
        console.log(error)
        reject(error)
      } else {
        console.log("Successfully uploaded media and tweeted!")
        resolve(data)
      }
    })
  })
}

tamasvar avatar Dec 30 '22 08:12 tamasvar

'''  
                    var mediaPromise;
                    if (msg.media && Buffer.isBuffer(msg.media)) {

                        mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json",{  
                            command: "INIT",
                            total_bytes: msg.mediaSize,
                            media_type: msg.mediaType
                        }).then(function(result) {
                            if (result.status === 200) {
                                return result.body.media_id_string;
                            } else {
                                throw new Error(result.body.errors[0]);
                            }
                        });


                        mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json",{  
                            command: "APPEND",
                            media_id: mediaId,
                            media: msg.media,
                            segment_index: 0
                        }).then(function(result) {
                            if (result.status === 200) {
                                return result.media_id;
                            } else {
                                throw new Error(result.body.errors[0]);
                            }
                        });
                        
                        mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json",{  
                            command: "FINALIZE",
                            media_id: mediaId
                        }).then(function(result) {
                            if (result.status === 200) {
                                return result.media_id;
                            } else {
                                throw new Error(result.body.errors[0]);
                            }
                        });
                 
                    } else {
                        mediaPromise = Promise.resolve();
                    }
                    mediaPromise.then(function(mediaId) {
                        var params = msg.params || {};
                        params.status = msg.payload;
                        if (mediaId) {
                            params.media_ids = mediaId;
                        }
                        node.twitterConfig.post("https://api.twitter.com/1.1/statuses/update.json",{},params).then(function(result) {
                            if (result.status === 200) {
                                node.status({});
                            } else {
                                node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
                                
                                if ('error' in result.body && typeof result.body.error === 'string') {
                                    node.error(result.body.error,msg);
                                } else {
                                    node.error(result.body.errors[0].message,msg);
                                }
                            }
                        }).catch(function(err) {
                            node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
                            node.error(err,msg);
                        })
                    }).catch(function(err) {
                        node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
                        node.error(err,msg);
                    }); '''

tamasvar avatar Jan 02 '23 11:01 tamasvar

If you want to submit code to the project, please do so as a pull request. It means we can review it in context of the existing code and we can check it out and test it.

Please let us know if you need a pointer to a guide on how to raise a pull request.

hardillb avatar Jan 02 '23 11:01 hardillb

I know that the code doesn't work yet, that's why I wrote it here in case someone could fix it so that it works

tamasvar avatar Jan 02 '23 19:01 tamasvar

It will still be better presented as a (draft) pull request, so that people can test and examine the code in context. As it is we don't know where this would fit into the existing code.

hardillb avatar Jan 03 '23 11:01 hardillb

https://github.com/node-red/node-red-nodes/pull/967

tamasvar avatar Jan 04 '23 18:01 tamasvar