Help

Creating or Updating Multiple Records with API Script

Topic Labels: Scripting extentions
1765 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Berg_Holmgren
4 - Data Explorer
4 - Data Explorer

I found a script that fetches info from IMDB (Image, synopsis, etc.). My question is how do I alter this code to remove the need for user input. Here is how the script works as is, and I’ll insert the code below.
Titles are put into the script,
code

When it runs it asks for the user to click a button to pick which title to fetch.
Buttons

Then it shows you the data.
Shrek

And adds it to a record.
airtable

I want to eliminate the need for a button and have the script fetch all the titles within the list.

// Get an API key from OMDB here: http://www.omdbapi.com/apikey.aspx
const OMDB_API_KEY = 'xxxxxx';

// Shows all the button groups together and waits for a button click
// Returns the first button that was clicked
async function chooseFromButtonGroups(label, buttonGroups) {
    output.markdown(`## ${label}`);
    return await Promise.race(buttonGroups.map(([label, options]) =>
        input.buttonsAsync(label, options)
    ));
}

async function addTitle(title) {
    const info = await fetch(`https://www.omdbapi.com/?t=${title}&apikey=${OMDB_API_KEY}`);
    const { Title, Runtime, Plot, Poster } = await info.json();

    output.markdown(`
![${Title}](${Poster})
# ${Title}
${Plot}
Runtime: \`${Runtime}\`
    `);

    const table = base.getTable('To watch');
    await table.createRecordAsync({
        'Name': Title,
        'Notes': Plot,
        'Attachments': [{
            url: Poster,
            filename: `${Title}.png`,
        }]
    });
}
/I want to remove the need to choose a button.
const title = await chooseFromButtonGroups('What would you like to watch next?', [
    ['Originals', ['Paw Patrol: The Movie', 'Candyman']],
    ['Movies', ['Shrek 2', 'The Da Vinci Code', 'Bad Boys']],
    ['Series', ['Fraiser', 'Community', 'The Office', 'Veep']],
]);

output.clear();

await addTitle(title);

Thanks for any help!

1 Reply 1

Hi @Berg_Holmgren
await chooseFromButtonGroups is asking the user for input. You need to create an array of titles, then loop through them.

Also take a look at the wikipedia showcase script, it performs closer to what you are looking for. Example Script Showcase - Airtable Universe