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 true if it's true.
import urllib.request as urllib2
import json
response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key=MY_KEY')
data = list(json.load(response))
{
"kind": "youtube#videoListResponse",
"etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/3jdRB-NXSAfUQj7e_FmBbivkK1o\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/NUd32t1_moLGAwVuu-ZujlkaiWM\"",
"id": "gwLej8heN5c",
"status": {
"uploadStatus": "processed",
"privacyStatus": "public",
"license": "creativeCommon",
"embeddable": true,
"publicStatsViewable": true
}
}
]
}
@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.