Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter video upload function #966

Open
tamasvar opened this issue Dec 30, 2022 · 5 comments
Open

Twitter video upload function #966

tamasvar opened this issue Dec 30, 2022 · 5 comments

Comments

@tamasvar
Copy link

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
Copy link
Author

tamasvar commented Jan 2, 2023

'''  
                    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);
                    }); '''

@hardillb
Copy link
Member

hardillb commented Jan 2, 2023

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.

@tamasvar
Copy link
Author

tamasvar commented Jan 2, 2023

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

@hardillb
Copy link
Member

hardillb commented Jan 3, 2023

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.

@tamasvar
Copy link
Author

tamasvar commented Jan 4, 2023

#967

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants