按订阅者数量高效过滤 YouTube API 搜索结果

问题描述 投票:0回答:1

我正在使用 YouTube 数据 API 搜索满足特定订阅者数量范围(例如 100,000 到 500,000 名订阅者)的频道。但是,该 API 消耗了大量配额单位,因为它会搜索多个渠道,包括许多不符合我的条件的渠道,然后返回一些合适的渠道。

例如,我使用

youtube.search().list
发出搜索请求,以根据关键字查找频道。检索结果后,我使用
youtube.channels().list
方法手动检查每个频道的订阅人数,这进一步消耗了配额。如果我搜索 10 个频道,只有 3 个符合订阅者计数范围,那么我已经使用了 10 个单位,其中 7 个被浪费在我的目标范围之外的频道上。

这是一个最小的可重现示例:

蟒蛇

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}")

这是我在 Pastebin 上的完整代码的链接。

问题: 是否有更有效的方法从一开始就按订阅者数量过滤频道,或者最大限度地减少 API 配额使用?我可以调整搜索参数或使用不同的方法来减少不必要的 API 调用次数吗?

任何建议或最佳实践将不胜感激!

python google-api youtube-api youtube-data-api quota
1个回答
0
投票

根据您的问题,我认为这将帮助您获得查询的最高订阅频道

MINECRAFT

https://www.googleapis.com/youtube/v3/search?q=minecraft&type=video&order=viewCount&part=snippet
将为您提供一个 YouTube 频道,其中包含观看次数最高的 Minecraft 视频。视频观看次数最高意味着该频道拥有至少 100 万以上订阅者。我不知道另一种方法,但我在我的一个项目中使用了这种方法来获得视频的最高订阅频道标记。希望这会有所帮助。

这里是此端点

https://www.googleapis.com/youtube/v3/search?q=minecraft&type=video&order=viewCount&part=snippet
的响应,其中包含具有最高
videoId
viewCount
的通道 ID。另外,您可以仔细阅读此文档这里

{
  "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 | &quot;I&#39;ve Got A Bone&quot; | 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
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.