Last active
January 6, 2021 08:52
-
-
Save korakot/07e76d7bab7e0f0e2d36aeb8a9a46861 to your computer and use it in GitHub Desktop.
Get youtube video duration from its id, using YouTube Data API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests import get | |
!pip install isodate -q | |
import isodate | |
key = 'AZxxxx' # from https://console.cloud.google.com/apis/credentials | |
def get_durations(id_list): | |
url = 'https://www.googleapis.com/youtube/v3/videos' | |
kw = { | |
'id': ','.join(id_list), | |
'key': key, | |
'part': 'contentDetails', | |
} | |
r = get(url, kw) | |
dur_list = [] | |
for it in r.json()['items']: | |
dur = it['contentDetails']['duration'] | |
dur = isodate.parse_duration(dur) | |
dur = str(dur).lstrip('0:') | |
dur_list.append(dur) | |
return dur_list # max 50 items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment