Question:
How do I get data from a YouTube channel to make a semi-automatically updating website?
You can use the YouTube API (I’m not sure it’s free), then you need a backend to update the website. Example backend (Python):
from requests import ...
# Code to fetch from youTube API
# Flask app setup code
@app.route('/')
def index():
return render_template("index.html", youtube_data=youtube_data)
Then you can use youtube_data
for YouTube data.
Using the YouTube API does not incur any monetary cost for the entity calling the API.
Google already provides a Python client for all of its APIs, including YouTube, which handles authentication, forming and making the API request as well as some datatype translation (i.e. JSON to dictionary, etc.)
You could use webscraping but that seems… worst?
I mean using the API grants you reliability, stability and other things. You can use BeaultifulSoup but be warned: web scraping might violate YouTube’s terms of service and could get you banned. Moreover, since YouTube can change its website’s structure without notice, your scraper can break without warning.
It’s typically more efficient, reliable, and in many cases easier to use the API. If you’re building a tool or service that you want to be reliable, the API is the way to go.
@WindLother Would I implement the YouTube API into a basic JavaScript file or would I have to use flask?
They have cases for both Python and Javascript so it revolves about what you have more expertise.
Just check their github.
@WindLother Is it possible that you could explain, using the YouTube API, how it is possible to scan a YouTube channel and retrieve all of there videos with stats such as what is most popular and what is newest?
Oh sure!
According to YouTube DOCS you must have:
Setup
There are a few setup steps you need to complete before you can use this library:
- If you don’t already have a Google account, sign up.
- If you have never created a Google APIs Console project, read the Managing Projects page and create a project in the Google API Console.
- Install the library.
After that you can build a simple code to retrieve all video IDs from a channel:
import googleapiclient.discovery
import googleapiclient.errors
api_version = "v3"
api_service_name = "youtube"
API_KEY = "YOUR_YOUTUBE_API_KEY"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey=API_KEY
)
channel_id = "CHANNEL_ID_HERE"
# Get the uploads playlist id from the channel
response = youtube.channels().list(part="contentDetails", id=channel_id).execute()
uploads_playlist_id = response["items"][0]["contentDetails"]["relatedPlaylists"][
"uploads"
]
# Get all video IDs from the uploads playlist
video_ids = []
next_page_token = None
while True:
response = (
youtube.playlistItems()
.list(
playlistId=uploads_playlist_id,
part="contentDetails",
maxResults=50,
pageToken=next_page_token,
)
.execute()
)
video_ids += [item["contentDetails"]["videoId"] for item in response["items"]]
next_page_token = response.get("nextPageToken")
if not next_page_token:
break
Or, if you want, the video stats too:
video_data = []
max_count = 50 # Remember that YouTube API allows a maximum of 50 items per request for video details
for i in range(0, len(video_ids), max_count):
response = (
youtube.videos()
.list(id=",".join(video_ids[i : i + max_count]), part="snippet,statistics")
.execute()
)
video_data += response["items"]
They have a good documentation if you have any doubts too.
(I build the code in python since it’s the language I’m most familiar with but the javascript kinda follow the same pattern).
If possible, could you rewrite the code in JavaScript? Sorry for the trouble!
Sure! @UMARismyname if you find anything that needs to be changed feel free!
const API_KEY = 'YOUR_YOUTUBE_API_KEY';
const CHANNEL_ID = 'CHANNEL_ID_HERE';
gapi.client.setApiKey(API_KEY);
gapi.client.load('youtube', 'v3', () => {
console.log('YouTube API loaded.');
});
async function fetchAllVideosFromChannel() {
const uploadsPlaylistId = await getUploadsPlaylistId(CHANNEL_ID);
const videoIds = await getAllVideoIdsFromPlaylist(uploadsPlaylistId);
const videoData = await getVideoDetails(videoIds);
// Sorting and processing of videoData goes here
}
async function getUploadsPlaylistId(channelId) {
const response = await gapi.client.youtube.channels.list({
part: 'contentDetails',
id: channelId
});
return response.result.items[0].contentDetails.relatedPlaylists.uploads;
}
async function getAllVideoIdsFromPlaylist(playlistId) {
let videoIds = [];
let nextPageToken;
do {
const response = await gapi.client.youtube.playlistItems.list({
playlistId: playlistId,
part: 'contentDetails',
maxResults: 50,
pageToken: nextPageToken
});
videoIds = videoIds.concat(response.result.items.map(item => item.contentDetails.videoId));
nextPageToken = response.result.nextPageToken;
} while (nextPageToken);
return videoIds;
}
async function getVideoDetails(videoIds) {
const response = await gapi.client.youtube.videos.list({
id: videoIds.join(','),
part: 'snippet,statistics'
});
return response.result.items;
}
What exactly will this return?
Each async function tells what you will get.
Thank you so much for the help!
@WindLother I am getting a console error that “gapi” is not defined. How should I go about solving this?
define gapi
:
const gapi = require('gapi')
That’s what I had previously done. I just implemented it once more and it says that “require” is not defined.
oh, are you doing this for a web frontend? gapi
is node
-only.
Or, if you’re just using ES modules, you’d do
import gapi from "gapi"
wait, are you using javascript or node?
I am using JavaScript