Skip to main content

Check the license in the Youtube video through API [StackOverflow answer]

This is my answer to the trouble shooting question about getting the license information from the Youtube API: https://stackoverflow.com/questions/56678956/how-to-check-for-creativecommons-videos-by-using-youtube-data-api-i-have-the/56714010#56714010.

@goodvibration answer was correct, as long as you keep into account that the json.load uses the read function, which means that once the data is read, it can not be read again. In this case it would return empty byte string.

This code works by printing True at the end.

response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key=MY_KEY') for item in json.load(response)['items']: print(item['status']['license'] == 'creativeCommon')

Also, in your original example and in your example of error you used list(json.load(response)) when saving to data. This would mean, that you don't get the entire json, but just the keys. So in your case, I would suggest to not change the response to list. But since you did not use the data variable later in example, it does not really change the results. But it could be important, if you have more information you want to check/save.

Also, in this case, because the json.load() uses the read function, which can not be used multiple times, you need to save the entire json and then read from it. The code would then be:

response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key=MY_KEY') data = json.load(response) for item in data['items']: print(item['status']['license'] == 'creativeCommon')

You can see this if you try and read the response that you get. You can put the following code to test it:

response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key=MY_KEY') print(response.read()) print(response.read())

In this case the results would be:

b'{\n "kind": "youtube#videoListResponse",\n "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/3jdRB-NXSAfUQj7e_FmBbivkK1o\"",\n "pageInfo": {\n "totalResults": 1,\n "resultsPerPage": 1\n },\n "items": [\n {\n "kind": "youtube#video",\n "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/NUd32t1_moLGAwVuu-ZujlkaiWM\"",\n "id": "gwLej8heN5c",\n "status": {\n "uploadStatus": "processed",\n "privacyStatus": "public",\n "license": "creativeCommon",\n "embeddable": true,\n "publicStatsViewable": true\n }\n }\n ]\n}\n' b''

In the first case, the result is a json in byte-string form, while the second one is an empty byte-string. So when you would try to use multiple json.load() on the same response element, you would get the JSONDecodeError (same as in your comment), since the second time there is no json to parse.