Help

Re: I asked ChatGPT to create a script for YouTube video data extraction [Need Guidance]

1747 1
cancel
Showing results for 
Search instead for 
Did you mean: 
revol
4 - Data Explorer
4 - Data Explorer
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
const url = `https://api.airtable.com/v0/${baseId}/${tableName}/${recordId}`;

 

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
    const youtubeUrl = `https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=${videoId}&key=${YOUTUBE_API_KEY}`;

 

    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 updateUrl = `https://api.airtable.com/v0/${baseId}/${tableName}/${recordId}`;

 

        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
          });
      });
  });
2 Replies 2

I am using GitHub Copilot (based on one of the OpenAI models) on regular basis. It does some amazing code autocomplete, saving me often plenty of key strokes. Occasionally I will even use a whole function from the suggestion.

When it comes to whole functioning blocks of code - I have attempted a couple of times using both Copilot and ChatGPT and the answer is that, they are sometimes correct, sometimes not. If they are not correct, trying to debug the error might take more time, than just writing the whole thing from scratch.

There might be so many things wrong with it. First point is that  it is using fetch method to update airtable instead of using the native Airtable script, second is they are also made synchronously. Not sure if this Google Youtube endpoint will work just on API key without Oauth connection. This  should also be checked.

 

Thanks for taking the time to write a detailed response, Greg. I'll be sure to review all the points you mentioned.