How to get videos duration using youtube api playlistItems

Question:

I wanna get all videos from playlist with it’s url. So, it works, but I have a problem, that youtube removed a way to get videos duration using playlistItems().list().
It used to be in ‘contentDetails’, but now there are only ‘videoId’ and ‘videoPublishedAt’.
We can request every single video using videos().list(), but I’m afraid of the speed of this way…
May be, there is a way to avoid using request to every video to get it’s duration?

Sorry for my English 🙂
You may write in any language you want.

There is a python code that I’m using for getting videos from playlist:

youtube = build("youtube", "v3", developerKey=API_KEY)

def get_videos(url):

    # ........

    collected_data = []
    next_page_token = None

    while True:
        request = youtube.playlistItems().list(
            part='snippet,contentDetails',
            playlistId=playlist_id,
            maxResults=50,
            pageToken=next_page_token
        )
        response = request.execute()
        collected_data.append(response)
        next_page_token = response.get('nextPageToken')
        if not next_page_token:
            break

    return collected_data[0]
Asked By: Obektev

||

Answers:

Using YouTube Data API v3 Videos: list endpoint it only divides the speed of your algorithm by two by making another request to this endpoint. Indeed it seems that you aren’t aware that this endpoint supports up to 50 provided ids within each request.

Answered By: Benjamin Loison
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.