Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

google api - YouTube Data API: Exclude livestreams from Search.list result set

Today I have an (I think) simple question. I want to exclude YouTube live streams from my YouTube Data API Search.list result sets. How can I do that? I can't find a function to do that in the docs from the API.

That's what I tried:

https://www.googleapis.com/youtube/v3/search?channelId=UCZMsvbAhhRblVGXmEXW8TSA&part=snippet,id&order=viewCount&maxResults=1&regionCode=DE&eventType=completed&type=video&publishedAfter=2021-02-05T00:00:00Z&key={KEY}

But that includes live streams, I want to remove them from the search. The live streams has always LIVE in the video title, maybe that helps. I tried also to use q, but I get always 0 search results.

question from:https://stackoverflow.com/questions/66068899/youtube-data-api-exclude-livestreams-from-search-list-result-set

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The short answer is the following: you'll have to filter out manually the videos that are live streams from each result set obtained from the Search.list API endpoint.

The longer answer follows below:

A given video is a live stream if and only if that video has attached the property liveStreamingDetails.

I'd warmly recommend to go through a very recent answer of mine that details a Python solution to precisely this issue, the function get_non_livestream_videos:

def get_non_livestream_videos(youtube, video_ids):
    assert len(video_ids) <= 50
    if not video_ids: return []

    response = youtube.videos().list(
        fields = 'items(id,liveStreamingDetails)',
        part = 'id,liveStreamingDetails',
        maxResults = len(video_ids),
        id = ','.join(video_ids),
    ).execute()

    items = response.get('items', [])
    assert len(items) <= len(video_ids)

    not_live = lambda video: 
        not video.get('liveStreamingDetails')
    video_id = lambda video: video['id']

    return map(video_id, filter(not_live, items))

If you have a list of video IDs video_ids then this function calls the Videos.list API endpoint for to determine the existence of the property liveStreamingDetails for each of the respective videos. Any video that has such a property gets filtered out from the resulting list of video IDs.

Note that above I used the fields request parameter for to get from the APIs only the info that's actually needed.

Also note that a precondition of using get_non_livestream_videos is that its list argument video_ids to be of at most 50 elements.

This is not an actual restriction when using this function, because it's supposed to be used on a list of video IDs that's obtained from a paged API result set. (Search.list returns paginated result sets of at most 50 items.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...