Hi,
I'm trying to write a script that will extract a video link from a field, extract video details from that link, and then automatically input those details into specific fields in a table. However, the script is not functioning as intended. I would greatly appreciate it if someone could provide me with some guidance on how to fix this issue.
Script:
const AIRTABLE_API_KEY = 'Airtable API'; // Airtable API key
const YOUTUBE_API_KEY = 'YouTube Data API Key'; // YouTube Data API key
const baseId = 'Airtable Base ID'; // Airtable base ID
const tableName = 'Airtable Table name'; // Airtable table name
const recordId = 'Airtable record ID'; // Airtable record ID
// Get the value of the "Video" field from the Airtable record
fetch(url, {
headers: {
Authorization: `Bearer ${AIRTABLE_API_KEY}`
}
})
.then(response => response.json())
.then(record => {
const videoUrl = record.fields.Video;
// Extract the video ID from the video URL
const videoId = videoUrl.split('v=')[1];
// Get the details of the video from the YouTube Data API
fetch(youtubeUrl)
.then(response => response.json())
.then(data => {
const video = data.items[0];
// Extract the video details
const publishedAt = video.snippet.publishedAt;
const viewCount = video.statistics.viewCount;
const commentCount = video.statistics.commentCount;
const likeCount = video.statistics.likeCount;
const dislikeCount = video.statistics.dislikeCount;
// Update the specific fields in the Airtable record
const body = {
fields: {
PublishedAt: publishedAt,
ViewCount: viewCount,
CommentCount: commentCount,
LikeCount: likeCount,
DislikeCount: dislikeCount
}
};
fetch(updateUrl, {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${AIRTABLE_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
.then(response => response.json())
.then(record => {
// The record has been updated successfully
})
.catch(error => {
// There was an error updating the record
});
});
});