I'm trying to download some creative commons video using the YouTube Data API, but I'm quite new to this so I'm stuck. How to proceed forward ? I want to find "license": "creativeCommon" in the JSON file and print "this is creativeCommon" I am using python3
import json
import re
import urllib.request
from pytube import YouTube
api_key = API_KEY
video_id = "thD8Ad7pWps"
url = f"https://www.googleapis.com/youtube/v3/videos?part=status&id={video_id}&key={api_key}"
json_url = urllib.request.urlopen(url)
data = json.loads(json_url.read())
for item in data['items']:
value = item.get('items')
print (item.get('license'))
if value in ['creativeCommon']:
print ("this is creativeCommon")
else:
print ("this is not creativeCommon")
The api reply with
[{'kind': 'youtube#video', 'etag': '"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/ePPN2XwrH5k8OoNce4d83nQCMZk"', 'id': 'thD8Ad7pWps', 'status': {'uploadStatus': 'processed', 'privacyStatus': 'public', 'license': 'creativeCommon', 'embeddable': True, 'publicStatsViewable': True}}]
I expected this to return this is creativeCommon but it returned this is not creativeCommon.
The following is returned :
KeyError Traceback (most recent call last)
<ipython-input-3-0a69a7438b8f> in <module>
13
14 for item in data['items']:
---> 15 value = data[0]['status']['license']
16
17 if value in ['creativeCommon', 'CREATIVECOMMON']:
KeyError: 0
You can change below:
value = item.get('items')
to
value = item['status']['license']
This will give you the result: creativeCommon