Last active
February 13, 2024 18:42
-
-
Save nocodesupplyco/37d3730ab5b3ec60e153f6d256c35058 to your computer and use it in GitHub Desktop.
Get YouTube Thumbnails w/ Airtable Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define settings fields and descriptions | |
const config = input.config({ | |
title: "Get YouTube Thumbnail", | |
description: "Download the max res thumbnail image from YouTube videos for each record in a specific view. Script created by [Base Scripts](https://nocodesupply.co/basescripts) — use at your own discretion.", | |
items: [ | |
input.config.table("tableSelect", { | |
label: "Table", | |
description: "Select the table with videos to get thumbnails for.", | |
}), | |
input.config.view("viewSelect", { | |
label: "View", | |
description: "Select the view with videos to get thumbnails for.", | |
parentTable: "tableSelect", | |
}), | |
input.config.field("linkSelect", { | |
label: "YouTube Link Field", | |
description: "Select the field containing the YouTube video link.", | |
parentTable: "tableSelect", | |
}), | |
input.config.field("imageSelect", { | |
label: "Image Field", | |
description: "Select the attachment field where the thumbnail should be stored.", | |
parentTable: "tableSelect", | |
}), | |
input.config.field("errorSelect", { | |
label: "Image Error Field", | |
description: "Select a checkbox field that can be checked if the script returns an error of any kind.", | |
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 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); | |
console.log(linkValue); | |
// This function will get the video ID | |
function get_video_id(url) { | |
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(live\/)|(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); | |
console.log(videoId); | |
let thumbnailUrl = "https://img.youtube.com/vi/" + videoId + "/maxresdefault.jpg"; | |
// Update records with response data | |
await table.updateRecordAsync(record, { | |
[configImage]: [{ url: thumbnailUrl }], | |
}); | |
console.log("Uploaded Thumbnail:" + linkValue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment