Home > Software design >  YouTube API lists offline Livestreams as video
YouTube API lists offline Livestreams as video

Time:09-27

I need a list of all videos from a YouTube channel using the YouTube Data API. I used the Upload-Playlist to get this list. But I discovered, that this list contains offline livestreams as well. Does anyone know how to detect, if the type of the item is a video or livestream? In the JSON from the request is a type argument, but this says e.g.: 'resourceId': {'kind': 'youtube#video', 'videoId': 'Rul7Z2pkH1w'}.

I'm working with Python and here is my code:

import pandas as pd
import requests

channel = 'UC192SEGl9dAng9GwredMSww'
api_key = '...'
# build dataframe
df = pd.DataFrame(columns=['channel_id',
                           'video_id',
                           'video_title',
                           'published_date',
                           'type'])

playlist_id = channel[:1]   'U'   channel[2:]

url = 'https://youtube.googleapis.com/youtube/v3/playlistItems?part=id&part=snippet&part=status&playlistId='   playlist_id   '&maxResults=50&key='   api_key
response = requests.get(url=url).json()

# save the channel_id and video_id in a dataframe
for i in response['items']:
    channel_id = i['snippet']['channelId']
    video_id = i['snippet']['resourceId']['videoId']
    published_date = i['snippet']['publishedAt']
    video_title = i['snippet']['title']
    vid_type = i['snippet']['resourceId']['kind']

    df = pd.concat([df, pd.DataFrame([{
        'channel_id': channel_id,
        'video_id': video_id,
        'video_title': video_title,
        'published_date': published_date,
        'type': vid_type
    }])], ignore_index=True)

    url2 = 'https://youtube.googleapis.com/youtube/v3/videos?part=snippet&part=id&part=statistics&part=localizations&part=status&part=contentDetails&part=liveStreamingDetails&part=topicDetails&id='   video_id   '&maxResults=50&key='   api_key
    response2 = requests.get(url=url).json()

    print(response2)

Thank you in advance!

CodePudding user response:

In your code - especially from the response2 variable, get the value of snippet.liveBroadcastContent path and see if the value is: none.

Quote from the documentation:

snippet.liveBroadcastContent string

Indicates if the video is an upcoming/active live broadcast. Or it's "none" if the video is not an upcoming/active live broadcast.

Valid values for this property are:

  • live
  • none
  • upcoming

CodePudding user response:

In addition to @Marco Aurelio Fernandez Reyes answer:

By using YouTube Data API v3 Videos: list endpoint with liveStreamingDetails you can distinguish videos from livestreams.

For the video id Rul7Z2pkH1w you shared, that is a livestream waiting to start you'll get:

"liveStreamingDetails": {
    "scheduledStartTime": "2018-07-01T18:27:49Z",
    "activeLiveChatId": "Cg0KC1J1bDdaMnBrSDF3KicKGFVDMTkyU0VHbDlkQW5nOUd3cmVkTVN3dxILUnVsN1oycGtIMXc"
}

For a video such as mWdFMNQBcjs, you wouldn't get any JSON entry liveStreamingDetails.

For an ended livestream such as TCBbXgBIC1I, you'll get:

"liveStreamingDetails": {
    "actualStartTime": "2022-08-05T11:45:13Z",
    "actualEndTime": "2022-08-05T11:45:54Z"
}
  • Related