Skip to content

Instantly share code, notes, and snippets.

@nocodesupplyco
Created January 11, 2023 21:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Get YouTube Thumbnails w/ Airtable Script
//allow user to select the table, view, image field and image size fields that should be checked
const config = input.config({
title: "Get YouTube Thumbnail",
description: "Upload the YouTube thumbnail to the image field.",
items: [
input.config.table("tableSelect", {
label: "Table",
}),
input.config.view("viewSelect", {
label: "View",
parentTable: "tableSelect",
}),
input.config.field("linkSelect", {
label: "Link Field",
description: "The URL field of the website to scrape",
parentTable: "tableSelect",
}),
input.config.field("imageSelect", {
label: "Screenshot Field",
description: "The attachment field where the screenshot will be stored.",
parentTable: "tableSelect",
}),
input.config.field("dateSelect", {
label: "Screenshot Date Field",
description: "The date field that will update with the date/time when the screenshot was taken.",
parentTable: "tableSelect",
}),
input.config.field("errorSelect", {
label: "Screenshot Error Field",
description: "The checkbox field that gets checked if the API returns an error.",
parentTable: "tableSelect",
}),
],
});
//set config choices to variables
const configTable = config.tableSelect.name;
const configView = config.viewSelect.name;
const configLink = config["linkSelect"].name;
const configImage = config["imageSelect"].name;
const configDate = config["dateSelect"].name;
const configError = config["errorSelect"].name;
//set table and view
const table = base.getTable(configTable);
const view = table.getView(configView);
//get all image fields
const link = await view.selectRecordsAsync({ fields: [configLink] });
//loop through each image field to get size
for (let record of link.records) {
//Get the URL from the link field
let linkValue = record.getCellValue(configLink);
// This function will get the video ID
function get_video_id(url) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return match && match[7].length == 11 ? match[7] : false;
}
//pass the linkValue through the get_video_id function to get the video ID
let videoId = get_video_id(linkValue);
let thumbnailUrl = "https://img.youtube.com/vi/" + videoId + "/maxresdefault.jpg";
//update records with response data
await table.updateRecordAsync(record, {
[configImage]: [{ url: thumbnailUrl }],
[configDate]: new Date(),
});
console.log("Uploaded Thumbnail:" + linkValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment