Hello,
I’ve got a piece of node.js code created from the snippets I found in other threads which is supposed to upload pictures to Twitter to be used in the next step to create the tweet with media. It works for small-size images, but for the bigger ones (mine will be approx 4mbs png files) I am getting timeout errors.
I’d appreciate any help with that - Twitter accepts bigger files than those I want to upload and it is a smooth and fast process in Twitter clients that must be using the API too.
The code I put into the step to upload an image:
import axios from 'axios';
import { axios as platformAxios } from '@pipedream/platform';
export default defineComponent({
props: {
twitter: {
type: 'app',
app: 'twitter'
},
url: {
type: 'string',
label: "Photo URL",
description: 'Full URL to the image to download then upload to a Tweet',
},
media_type: {
type: 'string',
label: "Media Type",
description: 'The MIME type of the media being uploaded. See https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-init'
}
},
async run({ props, $set }) {
const oauthSignerUri = this.twitter.$auth.oauth_signer_uri;
const token = {
key: this.twitter.$auth.oauth_access_token,
secret: this.twitter.$auth.oauth_refresh_token,
};
const signConfig = {
token,
oauthSignerUri
};
const imageResponse = await axios({
url: this.url,
method: "GET",
responseType: "arraybuffer"
});
const file = Buffer.from(imageResponse.data, 'binary');
const total_bytes = file.length;
const base64EncodedFile = file.toString('base64');
const media_type = this.media_type;
const mediaUploadInitRequest = {
method: 'POST',
data: '',
url: "https://upload.twitter.com/1.1/media/upload.json",
params: {
command: "INIT",
total_bytes,
media_type,
}
};
const uploadMediaInitResponse = await platformAxios(props, mediaUploadInitRequest, signConfig);
console.log(uploadMediaInitResponse);
const mediaIdString = uploadMediaInitResponse.media_id_string;
const splitStringRe = new RegExp('.{1,' + 5000 + '}', 'g');
const chunks = base64EncodedFile.match(splitStringRe);
for (const [segment_index, media_data] of chunks.entries()) {
console.log(`Processing chunk ${segment_index}`);
const mediaUploadAppendRequest = {
method: 'POST',
data: '',
url: "https://upload.twitter.com/1.1/media/upload.json",
params: {
command: "APPEND",
media_id: mediaIdString,
segment_index,
media_data,
}
};
await platformAxios(props, mediaUploadAppendRequest, signConfig);
}
const mediaUploadFinalizeRequest = {
method: 'POST',
data: '',
url: "https://upload.twitter.com/1.1/media/upload.json",
params: {
command: "FINALIZE",
media_id: mediaIdString,
}
};
await platformAxios(props, mediaUploadFinalizeRequest, signConfig);
},
});