In this section, we present how to connect YouTube to MindsDB.

YouTube is a popular online video-sharing platform and social media website where users can upload, view, share, and interact with videos created by individuals and organizations from around the world.

Prerequisites

Before proceeding, ensure the following prerequisites are met:

  1. Install MindsDB on your system or obtain access to cloud options.
  2. To use YouTube with MindsDB, install the required dependencies following this instruction.

Connection

There are two ways you can connect YouTube to MindsDB:

  1. Limited permissions: This option provides MindsDB with read-only access to YouTube, including viewing comments data.
  2. Elevated permissions: This optiona provides MindsDB with full access to YouTube, including viewing comments data and posting replies to comments.

Option 1: Limited permissions

Establish a connection to YouTube from MindsDB by executing the below SQL command and following the Google authorization link provided as output:

CREATE DATABASE mindsdb_youtube
WITH ENGINE = 'youtube',
PARAMETERS = {
  "youtube_api_token": "<your-youtube-api-key-token>"
};

Alternatively, you can connect YouTube to MindsDB via the form.

To do that, click on the Add button, choose New Datasource, search for YouTube, and follow the instructions in the form. After providing the connection name and the YouTube API token, click on the Test Connection button. Once the connection is established, click on the Save and Continue button.

Required connection parameters include the following:

  • youtube_api_token: It is a Google API key used for authentication. Check out this guide on how to create the API key to access YouTube data.

Option 2: Elevated permissions

Establish a connection to YouTube from MindsDB by executing the below SQL command and following the Google authorization link provided as output:

CREATE DATABASE mindsdb_youtube_oauth
WITH ENGINE = 'youtube',
PARAMETERS = {
  "credentials_file": "path-to-credentials-json-file"
  -- alternatively, use the credentials_url parameter
};

Alternatively, you can connect YouTube to MindsDB via the form.

To do that, click on the Add button, choose New Datasource, search for YouTube, and follow the instructions in the form. After providing the connection name and the credentials file or URL, click on the Test Connection button and complete the authorization process in the pop-up window. Once the connection is established, click on the Save and Continue button.

Required connection parameters include one of the following:

  • credentials_file: It is a path to a file generated from the Google Cloud Console, as described below.
  • credentials_url: It is a URL to a file generated from the Google Cloud Console, as described below.

Usage

Use the established connection to query the comments table.

You can query for one video’s comments:

SELECT * 
FROM mindsdb_youtube.comments
WHERE video_id = "raWFGQ20OfA";

Or for one channels’s comments:

SELECT * 
FROM mindsdb_youtube.comments
WHERE channel_id="UC-...";

You can include ordering and limiting the output data:

SELECT * FROM mindsdb_youtube.comments
WHERE video_id = "raWFGQ20OfA"
ORDER BY display_name ASC
LIMIT 5;

Use the established connection to query the channels table.

SELECT * FROM mindsdb_youtube.channels
WHERE channel_id="UC-...";

Here, the channel_id column is mandatory in the WHERE clause.

Use the established connection to query the videos table.

SELECT * FROM mindsdb_youtube.videos
WHERE video_id="id";

Here, the video_id column is mandatory in the WHERE clause.

With the connection option 2, you can insert replies to comments:

INSERT INTO mindsdb_youtube_oauth.comments (comment_id, reply)
VALUES ("comment_id", "reply message");