I want to make a program that shows a channel's subscriber count. My code works fine and gives me the correct subscriber count when the id of the channel has not been customized, but when the id has been customized, the json result does not contain an "items" tag.
Non-customized id:
{
"kind": "youtube#channelListResponse",
"etag": "18WUxifrtT73p-fgWEjP_ReiVQs",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#channel",
"etag": "r7AcY54fWu7t_3lHv3W5KQjgns0",
"id": "UC8butISFwT-Wl7EV0hUK0BQ",
"statistics": {
"viewCount": "268031518",
"subscriberCount": "4660000",
"hiddenSubscriberCount": false,
"videoCount": "1236"
}
}
]
}
Customized id:
{
"kind": "youtube#channelListResponse",
"etag": "RuuXzTIr0OoDqI4S0RU6n4FqKEM",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
}
}
My code:
import urllib.request
import json
def grab_id(id):
data = urllib.request.urlopen(f"https://www.googleapis.com/youtube/v3/channels?part=statistics&id={id}&key=mykey").read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
subs = int(subs)
return subs
print(grab_id("id"))
Thanks!