node-red / node-red/node-red-nodes
Twitter video upload function
Open
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 1.1k
- Forks
- 612
- Avg merge
- 13h 57m
- Merged PRs (30d)
- 3
Description
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)
}
})
})
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the node-red-node-twitter implementation and compare its current upload path with the supplied Node.js INIT, APPEND, and FINALIZE example. The work is complete when the Twitter node can upload larger files and publish the resulting media successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nodejs
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100