auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
//const fetch = require('node-fetch@2');
import fetch from 'node-fetch';
const randomURL = 'https://gijoe.fandom.com/wiki/Special:RandomInCategory/A_Real_American_Hero_characters';
const apiURL = 'https://gijoe.fandom.com/api.php';
async function getRandomCharacter() {
return await fetch(randomURL + '?format=json', {redirect:'manual'})
.then(res => {
let header = res.headers.raw();
return header.location[0].split('/').pop();
});
}
async function getPageData(page) {
let resp = await fetch(apiURL + `?action=parse&page=${page}&format=json&prop=wikitext`);
let data = await resp.json();
console.log('data', data);
return {
title: data.parse.title,
wikitext: data.parse.wikitext['*']
};
}
/*
Looks for the infobox code and parses it into fields, also prepares image.
Tries to deal with the different forms of infobox
*/
function getInfoBox(str) {
let matches = str.match(/{{.*?[_ ]character[_ ]infobox[.\s\S]*?}}/);
if(!matches) {
//hopefully the only alternative
matches = str.match(/{{Character_infobox[.\s\S]*?}}/);
}
if(!matches) return;
if(matches[0]) {
let box = matches[0];
box = box.replace(/{{.*?[_ ]character[_ ]infobox[\s\S]/,'');
box = box.replace(/{{Character_infobox[\s\S]/,'');
box = box.replace(/}}/,'');
let parts = box.split('\n');
let result = {};
parts.forEach(p => {
if(p.indexOf("|") === 0) {
let [key,value] = p.split('=');
key = key.replace('|','');
if(key && value) result[key.trim()] = value;
}
});
/*
look for image as a key, and it may be: [[File:Marvel-Mainframe.jpg|270px]]'
*/
if(result.image) {
let [file] = result.image.split('|');
if(file.indexOf('File:') > 0 || file.indexOf('Image:') > 0) {
file = file.replace('[[File:','');
file = file.replace('[[Image:','');
file = file.replace(']]','');
result.image = file;
}
}
/*
possibly fix speciality:
"Sailor; [[Wikipedia:United States Navy SEALs|S.E.A.L.]]; Gunner's mate; Machinist
In this case, we'll just take the first part
*/
console.log(result);
if(!result.specialty) result.specialty = 'Unknown';
if(result.specialty.indexOf(';') > 0) {
result.specialty = result.specialty.split(';')[0];
}
return result;
}
}
async function getImageURL(f) {
let url = `https://gijoe.fandom.com/api.php?action=query&titles=Image:${f}&prop=imageinfo&iiprop=url&format=json`;
let result = await fetch(url);
let data = await result.json();
if(data && data.query && data.query.pages) {
//result is an object with one random key
let keys = Object.keys(data.query.pages);
let image = data.query.pages[keys[0]];
if(image && image.imageinfo) return image.imageinfo[0].url;
return '';
}
}
let char = await getRandomCharacter();
let page = await getPageData(char);
console.log(page.wikitext);
this.box = getInfoBox(page.wikitext);
//console.log(this.box);
if(!this.box) $end("No box?");
if(!this.box.image) $end("No image");
this.pic = await getImageURL(this.box.image);
if(this.pic === '') $end("No image");
this.url = `https://gijoe.fandom.com/wiki/${char}`;
auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
this.text = `
Name: ${steps.getjoe.box.name}
Speciality: ${steps.getjoe.box.specialty}
Link: ${steps.getjoe.url}
`;
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")
const chunk = require("lodash.chunk")
// 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,' + 10000 + '}', 'g');
const chunks = base64EncodedFile.match(splitStringRe);
// Collect all of our requests into an array
const requests = []
for (const [segment_index, media_data] of chunks.entries()) {
// 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,
}
}
requests.push(require("@pipedreamhq/platform").axios(this, mediaUploadAppendRequest, signConfig))
}
// Send N requests at a time
const requestChunks = chunk(requests, 10)
for (const [i, chunk] of requestChunks.entries()) {
console.log(`Processing chunk ${i}`)
await Promise.all(chunk)
}
// 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 oauthSignerUri = auths.twitter.oauth_signer_uri
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 config = {
url: `https://api.twitter.com/1.1/statuses/update.json`,
method: 'POST',
params,
}
const token = {
key: auths.twitter.oauth_access_token,
secret: auths.twitter.oauth_refresh_token,
}
const signConfig = {
token,
oauthSignerUri
}
return await require("@pipedreamhq/platform").axios(this, config, signConfig)