Reputation: 420
Code:
import requests
import json
key = 'key' #api key
url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&key='+ key +'&forUsername='
youtuber = input('What\'s the name of the youtuber: ') #get youtuber name
url += youtuber.lower() #youtuber name all lowercase
r = requests.get(url) #get data from youtuber channel webpage
try:
subs = json.loads(r.text)['items'][0]['statistics']['subscriberCount']
except:
print('Your youtuber doesn\'t exist ):')
exit()
print(youtuber,"has",subs,"subscribers! Woohoo!")
This is okay:
Input: Google
Output: Google has 9640000 subscribers! Woohoo!
This is not:
Input: Markiplier
Output: Markiplier has 84900 subscribers! Woohoo! (Actually, his main channel has 27.8M subscribers)
I run into the unfortunate issue of channels with the same name. How do I prevent this?
Upvotes: 0
Views: 353
Reputation: 6985
You have to acknowledge the following peculiarity of YouTube site: it permits (by design) that the very same name -- in your case Markiplier
-- to be a channel's custom URL and, at the same time, to be the (legacy) user name of another channel.
Please read the first few paragraphs of my answer to a very much related question. From there, you'll get a pretty good idea about the difference between the two (sometimes confusing) concepts: channel user name and channel custom URL.
Concretely, if you'll make use of my Python3 public script youtube-search.py
, you'll see that there are two different channels that exhibit the behavior I just described w.r.t. the name Markiplier
:
$ python3 youtube-search.py --user-name Markiplier
UCxubOASK0482qC5psq89MsQ
$ python3 youtube-search.py --custom-url Markiplier
UC7_YxT-KID8kRbqZo7MyscQ
Note that youtube-search.py
requires a valid API key to be passed to it as argument of the command line option --app-key
or, otherwise, passed on as the environment variable YOUTUBE_DATA_APP_KEY
. (Use the command line option --help
for brief helping info.)
A further Channels.list
API endpoint query on the following URL:
https://www.googleapis.com/youtube/v3/channels?id=UCxubOASK0482qC5psq89MsQ,UC7_YxT-KID8kRbqZo7MyscQ&part=id,snippet,statistics&fields=items(id,snippet(title,description,customUrl),statistics(subscriberCount))&maxResults=2&key=...
will confirm the difference you've experienced:
{
"items": [
{
"id": "UCxubOASK0482qC5psq89MsQ",
"snippet": {
"title": "markiplier",
"description": "I will no longer be updating this channel! All my new videos with be uploaded to markiplierGAME! Please re-subscribe on that channel to stay up-to-date on my videos!"
},
"statistics": {
"subscriberCount": "85000"
}
},
{
"id": "UC7_YxT-KID8kRbqZo7MyscQ",
"snippet": {
"title": "Markiplier",
"description": "Welcome to Markiplier! Here you'll find some hilarious gaming videos, original comedy sketches, animated parodies, and other bits of entertainment! If this sounds like your kind of channel then please Subscribe Today!\n\nTotal Charity Raised ▶ $3,000,000+",
"customUrl": "markiplier"
},
"statistics": {
"subscriberCount": "27800000"
}
}
]
}
Now, to make a summary of my points w.r.t. your question -- I run into the unfortunate issue of channels with the same name. How do I prevent this? --, I note the following:
Channel.list
endpoint queried with an appropriate forUsername
parameter.Upvotes: 2