auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
const cheerio = require('cheerio');
/*
This logic taken from the https://github.com/zaccolley/letterboxd package. The package assumes it is doing
all the network stuff and I just needed the image parsing part.
*/
getImage = function(description) {
var $ = cheerio.load(description);
// find the film poster and grab it's src
var image = $('p img').attr('src');
// if the film has no image return no object
if (!image) {
return false;
}
const originalImageCropRegex = /-0-.*-crop/;
return {
tiny: image.replace(originalImageCropRegex, "-0-35-0-50-crop"),
small: image.replace(originalImageCropRegex, "-0-70-0-105-crop"),
medium: image.replace(originalImageCropRegex, "-0-150-0-225-crop"),
large: image.replace(originalImageCropRegex, "-0-230-0-345-crop"),
};
}
let imgdata = getImage(steps.trigger.event.description);
let text = `
I just watched ${steps.trigger.event['letterboxd:filmtitle']['#']} and rated it a ${steps.trigger.event["letterboxd:memberrating"]["#"]}. See my
review at ${steps.trigger.event.link}.
`;
console.log(imgdata, text);
return {
text, imgdata
};
URL of media to upload to Twitter
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
(params, auths) => {
}
const axios = require("axios")
// Data we need to make the Twitter API request
const oauthSignerUri = auths.twitter.oauth_signer_uri
const token = {
key: auths.twitter.oauth_access_token,
secret: auths.twitter.oauth_refresh_token,
}
const signConfig = {
token,
oauthSignerUri
}
// Download image, base64 encode it, then upload the media to Twitter
const imageResponse = await axios({
url: params.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 } = params
// First, tell Twitter the type of file you're uploading, how big it is, etc.
// https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-init
const mediaUploadInitRequest = {
method: 'POST',
data: '',
url: "https://upload.twitter.com/1.1/media/upload.json",
params: {
command: "INIT",
total_bytes,
media_type,
}
}
// Twitter returns a media_id_string that we use to upload the file,
// and to reference it in future steps
this.uploadMediaInitResponse = (await require("@pipedreamhq/platform").axios(this, mediaUploadInitRequest, signConfig))
this.mediaIdString = this.uploadMediaInitResponse.media_id_string
// Split the file into chunks, APPEND each chunk
const splitStringRe = new RegExp('.{1,' + 1000 + '}', 'g');
const chunks = base64EncodedFile.match(splitStringRe);
for (const [segment_index, media_data] of chunks.entries()) {
console.log(`Processing chunk ${segment_index}`)
// APPEND file content in chunks
// See https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-append
const mediaUploadAppendRequest = {
method: 'POST',
data: '',
url: "https://upload.twitter.com/1.1/media/upload.json",
params: {
command: "APPEND",
media_id: this.mediaIdString,
segment_index,
media_data,
}
}
await require("@pipedreamhq/platform").axios(this, mediaUploadAppendRequest, signConfig)
}
// Finally, tell Twitter we're done uploading
// https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-finalize
const mediaUploadFinalizeRequest = {
method: 'POST',
data: '',
url: "https://upload.twitter.com/1.1/media/upload.json",
params: {
command: "FINALIZE",
media_id: this.mediaIdString,
}
}
await require("@pipedreamhq/platform").axios(this, mediaUploadFinalizeRequest, signConfig)
async
(params, auths) => {
}
const axios = require('axios')
const {status, in_reply_to_status_id, auto_populate_reply_metadata, exclude_reply_user_ids, attachment_url, media_ids, possibly_sensitive, lat, long, place_id, display_coordinates, trim_user, enable_dmcommands, fail_dmcommands, card_uri} = params
const body = {
config: {
url: `https://api.twitter.com/1.1/statuses/update.json`,
method: 'POST',
params,
},
token: {
key: auths.twitter.oauth_access_token,
secret: auths.twitter.oauth_refresh_token,
},
}
const proxy = "https://api.pipedream.com/v1/oauth1/app_13GhY1"
const resp = await axios.post(proxy,body)
const {messages, data} = resp.data
for (const message of messages) {
console.log(message)
}
return data