visit
In this article you’ll learn how tо:
4. To create a new project: click “New Project”. Otherwise, skip to step 7.
6. Choose your new project
7. Click to the API & Services
8. Click “ENABLE APIS AND SERVICES”
Highlight and copy everything in the URL after “channel/” as follows: //www.youtube.com/channel/UCYvmuw-JtVrTZQ-7Y4kd63Q
import pandas as pd
import requests
import json
api_key = 'YOUR_API_KEY'
channel_id = 'UCYvmuw-JtVrTZQ-7Y4kd63Q'
def get_channel_stat() -> dict:
url = f'//www.googleapis.com/youtube/v3/channels?part=statistics&id={channel_id}&key={api_key}'
r = requests.get(url)
data = r.json()
return data['items'][0]['statistics']
stats = get_channel_stat()
pd.DataFrame([stats])
While page statistics have been collected, it is important to clarify that these results are approximations: API YouTube does not provide accurate statistics.
“GET request //www.googleapis.com/youtube/v3/videos”
part=snippet,id, this parameter specifies which part of json that will be returned.
def get_pl_data_per_page(npt: str=None, limit: int=None) -> dict:
url = f"//www.googleapis.com/youtube/v3/playlists?key={api_key}&channelId={channel_id}&part=snippet,id&order=date"
if limit is not None:
url = f"{url}&maxResults={limit}"
if npt is not None:
url = f"{url}&&pageToken={npt}"
r = requests.get(url)
return r.json()
def get_playlists_data(limit: int=50):
res = []
nextPageToken = None
while True:
data = get_pl_data_per_page(nextPageToken)
nextPageToken = data.get("nextPageToken", None)
res.append(data)
if nextPageToken is None: break
return res
data = get_playlists_data()
df = pd.DataFrame(data)
p_ids = set(df.explode('items')["items"].apply(pd.Series)["id"].to_list())
p_ids
Now that we have the list of playlists, let's get all the videos from a playlist.
limit = 50
plid = 'PLz68cPKdtIOzuoKHzMcQoO7WaQHYm5J3Y'
url = f'//www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults={limit}\
&playlistId={plid}&key={api_key}'
json_url = requests.get(url)
data = json.loads(json_url.text)
to_append = []
for item in data['items']:
playlistId = item['snippet']['playlistId']
videoOwnerChannelTitle = item['snippet']['videoOwnerChannelTitle']
position = item['snippet']['position']
videoId = item['snippet']['resourceId']['videoId']
to_append.append([playlistId, videoOwnerChannelTitle, position, videoId])
cols = ['playlist_id', 'videoOwnerChannelTitle', 'position', 'videoId']
pd.DataFrame(to_append, columns=cols)
In the next article, getting statistics for each video in a channel will be analyzed.