I’m using the YouTube Data API to search for channels that meet a specific subscriber count range (e.g., between 100,000 and 500,000 subscribers). However, the API consumes a lot of quota units because it searches through multiple channels, including many that don’t fit my criteria, before returning a few suitable ones.
For example, I make a search request using youtube.search().list
to find channels based on a keyword. After retrieving the results, I manually check the subscriber count of each channel using the youtube.channels().list
method, which further consumes quota. If I search 10 channels and only 3 fit the subscriber count range, I’ve used 10 units, 7 of which were wasted on channels outside my target range.
Here’s a minimal reproducible example:
python
import requests
API_KEY = 'YOUR_API_KEY'
def search_youtube(query, max_results=10):
url = 'https://www.googleapis.com/youtube/v3/search'
params = {
'part': 'snippet',
'q': query,
'type': 'channel',
'maxResults': max_results,
'key': API_KEY
}
response = requests.get(url, params=params)
return response.json()
def get_channel_details(channel_id):
url = 'https://www.googleapis.com/youtube/v3/channels'
params = {
'part': 'statistics',
'id': channel_id,
'key': API_KEY
}
response = requests.get(url, params=params)
return response.json()
# Sample query and channel processing
query = 'Minecraft'
search_results = search_youtube(query)
for item in search_results.get('items', []):
channel_id = item['id']['channelId']
channel_details = get_channel_details(channel_id)
subscriber_count = int(channel_details['items'][0]['statistics']['subscriberCount'])
if 100000 <= subscriber_count <= 500000:
print(f"Channel: {item['snippet']['title']}, Subscribers: {subscriber_count}")
Here’s the link to my full code on Pastebin.
Question:
Is there a more efficient way to filter channels by subscriber count from the start, or to minimize API quota usage? Can I adjust my search parameters or use a different method to reduce the number of unnecessary API calls?
Any suggestions or best practices would be greatly appreciated!
Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Based on your question I think this will help you get the highest subscribed channel for your query MINECRAFT
.
https://www.googleapis.com/youtube/v3/search?q=minecraft&type=video&order=viewCount&part=snippet
will give you a YouTube channel that has Minecraft video on it with the highest viewCount. the highest view count on a video means the channel has at least 1m+ subscribers. I don’t know the other method but I used this in one of my projects for the highest subscribed channel mark with videos. Hope this will help.
here is the response of this endpoint https://www.googleapis.com/youtube/v3/search?q=minecraft&type=video&order=viewCount&part=snippet
contains channelId with the videoId
that has the highest viewCount
. Also, you can read this documentation carefully here
{
"nextPageToken": "..",
"kind": "youtube#searchListResponse",
"items": [
{
"snippet": {
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/pCBP4M08ndE/default.jpg",
"width": 120,
"height": 90
},
"high": {
"url": "https://i.ytimg.com/vi/pCBP4M08ndE/hqdefault.jpg",
"width": 480,
"height": 360
},
"medium": {
"url": "https://i.ytimg.com/vi/pCBP4M08ndE/mqdefault.jpg",
"width": 320,
"height": 180
}
},
"title": "Cave Spider Roller Coaster - Animation vs. Minecraft Shorts Ep. 14",
"channelId": "UCbKWv2x9t6u8yZoB3KcPtnw",
"publishTime": "2019-09-21T11:00:11Z",
"publishedAt": "2019-09-21T11:00:11Z",
"liveBroadcastContent": "none",
"channelTitle": "Alan Becker",
"description": "It's like the Roller Coaster Episode except with spiders. Watch Episode 15 Here: https://youtu.be/X7Mpp35EAqI Full Animation vs."
},
"kind": "youtube#searchResult",
"etag": "....",
"id": {
"kind": "youtube#video",
"videoId": "pCBP4M08ndE"
}
},
{
"snippet": {
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/nwC_BbTrOqA/default.jpg",
"width": 120,
"height": 90
},
"high": {
"url": "https://i.ytimg.com/vi/nwC_BbTrOqA/hqdefault.jpg",
"width": 480,
"height": 360
},
"medium": {
"url": "https://i.ytimg.com/vi/nwC_BbTrOqA/mqdefault.jpg",
"width": 320,
"height": 180
}
},
"title": "BEES FIGHT - Alex and Steve Life (Minecraft Animation)",
"channelId": "UCawEP-InoKYKutsgq_vqIXA",
"publishTime": "2020-08-02T13:00:12Z",
"publishedAt": "2020-08-02T13:00:12Z",
"liveBroadcastContent": "none",
"channelTitle": "Squared Media",
"description": "Alex and Steve discover a village inhabited by Villagers and Bees living in harmony. A Witch raids the village with their mob army ..."
},
"kind": "youtube#searchResult",
"etag": ".....",
"id": {
"kind": "youtube#video",
"videoId": "nwC_BbTrOqA"
}
},
{
"snippet": {
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/lVgjUWFSR6w/default.jpg",
"width": 120,
"height": 90
},
"high": {
"url": "https://i.ytimg.com/vi/lVgjUWFSR6w/hqdefault.jpg",
"width": 480,
"height": 360
},
"medium": {
"url": "https://i.ytimg.com/vi/lVgjUWFSR6w/mqdefault.jpg",
"width": 320,
"height": 180
}
},
"title": "Minecraft NOSTALGIA 🥺 #shorts",
"channelId": "UCapgXl5RTXwm9d5DO21jh3Q",
"publishTime": "2022-03-25T13:48:35Z",
"publishedAt": "2022-03-25T13:48:35Z",
"liveBroadcastContent": "none",
"channelTitle": "GEVids",
"description": "This is Minecraft's most nostalgic and OG worlds. These don't include Minecraft 1.18 seeds, but rather worlds from ..."
},
"kind": "youtube#searchResult",
"etag": "....",
"id": {
"kind": "youtube#video",
"videoId": "lVgjUWFSR6w"
}
},
{
"snippet": {
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/gDtQ-To3iSU/default.jpg",
"width": 120,
"height": 90
},
"high": {
"url": "https://i.ytimg.com/vi/gDtQ-To3iSU/hqdefault.jpg",
"width": 480,
"height": 360
},
"medium": {
"url": "https://i.ytimg.com/vi/gDtQ-To3iSU/mqdefault.jpg",
"width": 320,
"height": 180
}
},
"title": "Minecraft RTX: What if ~44 SURPRISE #Shorts",
"channelId": "UC2gJotBXDcyOY-Ny6yDxtZg",
"publishTime": "2023-07-26T19:07:49Z",
"publishedAt": "2023-07-26T19:07:49Z",
"liveBroadcastContent": "none",
"channelTitle": "Teddy P",
"description": "Minecraft is a popular video game where players explore a blocky, procedurally-generated 3D world and gather resources to craft ..."
},
"kind": "youtube#searchResult",
"etag": "....",
"id": {
"kind": "youtube#video",
"videoId": "gDtQ-To3iSU"
}
},
{
"snippet": {
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/KZKIiiSfn_0/default.jpg",
"width": 120,
"height": 90
},
"high": {
"url": "https://i.ytimg.com/vi/KZKIiiSfn_0/hqdefault.jpg",
"width": 480,
"height": 360
},
"medium": {
"url": "https://i.ytimg.com/vi/KZKIiiSfn_0/mqdefault.jpg",
"width": 320,
"height": 180
}
},
"title": "MINECRAFT SKELETON RAP REMIX | "I've Got A Bone" | Oxygen Beats Dan Bull Animated Music Video",
"channelId": "UC1hkAIJnb2CSmm7SPJaPR-A",
"publishTime": "2020-04-03T20:01:44Z",
"publishedAt": "2020-04-03T20:01:44Z",
"liveBroadcastContent": "none",
"channelTitle": "Dan Bull",
"description": "New animated music video featuring the Allay. Go and watch now! ▻ https://youtu.be/MjtlV-r37Gg REMIX! Creeper Rap."
},
"kind": "youtube#searchResult",
"etag": "....",
"id": {
"kind": "youtube#video",
"videoId": "KZKIiiSfn_0"
}
}
],
"regionCode": "ZZ",
"etag": "lUYbeeZcU0qLv4YSrvA_NMa6s6g",
"pageInfo": {
"resultsPerPage": 5,
"totalResults": 1000000
}
}
x1337Loser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1