gpt4 book ai didi

youtube-api - 从 youtube.com/c/xxxx 链接获取 channel ID?

转载 作者:行者123 更新时间:2023-12-05 09:08:47 27 4
gpt4 key购买 nike

Youtube 似乎已经去掉了他们页面上的/channel/XXXX url,现在是/c/username?用户名并不是真正的“用户名”。例如

https://www.youtube.com/c/lukemiani

通过

运行查找
https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=lukemiani&key=...

不返回任何结果。

我有一群非技术用户,他们经过培训可以查找/channel/x 或/user/x 并将正确的内容输入到我的应用程序中。现在/channel 已经不存在了,我(或他们)如何将/c/x 转换为 channel ID?

我正在寻找 API 解决方案,而不是查看源代码和逆向工程代码解决方案。

最佳答案

根据official support staff ,给定 channel 可能关联了以下形式的 URL:

https://www.youtube.com/c/CUSTOM_NAME .

在这种情况下,相应 channel 的 customUrl 属性是 CUSTOM_NAME .

现在,您的问题可以重新表述如下:

Given a CUSTOM_NAME for which the URL above points to an existing channel, is there a procedure that is able to produce -- by making use of YouTube Data API -- that channel's ID, such that the respective procedure to be DTOS-compliant (i.e. the procedure works by not scraping the HTML text obtained from the respective custom URL)?

对上述问题的简短回答是不,没有。 (请查看我最近针对类似问题给出的 my answer and the attached comments)。

较长的答案如下:是的,可以想象一个解决问题的算法,但只是部分解决(因为不能保证它会总是给出积极的结果)。

算法如下:

  1. 调用 Search.list 具有以下参数的 API 端点:
    • q=CUSTOM_NAME ,
    • type=channel , 和
    • maxResults=10 .
  2. 从结果集中提取 channel ID(这些 ID 位于 items[].id.channelId );
  3. 对于第 2 步获得的列表中的每个 channel ID:
    1. 调用 Channels.list 获取 channel 关联的API端点 customUrl 属性(property)(如果有的话);
    2. 如果得到customUrl等于CUSTOM_NAME ,然后停止生成当前 channel ID 的算法;否则,继续执行当前循环;
  4. 通过产生找不到 channel ID 来停止算法。

由于 Search.list 提供的结果集的模糊性质端点,不能排除这样一种可能性,即实际上可能存在自定义 URL(即上述形式的指向现有 channel 的 URL),对此算法无法生成相关 channel 的 ID。

最后一点:Channels.list端点接受其 id 参数是以逗号分隔的 channel ID 列表。因此,可以很容易地修改上面的算法,而不是 NN <= 10 的调用( Channels.list )端点只有一个。


上述算法在 Python 语言中的实现,使用 Google 的 APIs Client Library for Python:

def find_channel_by_custom_url(
youtube, custom_url, max_results = 10):
resp = youtube.search().list(
q = custom_url,
part = 'id',
type = 'channel',
fields = 'items(id(kind,channelId))',
maxResults = max_results
).execute()
assert len(resp['items']) <= max_results

ch = []
for item in resp['items']:
assert item['id']['kind'] == 'youtube#channel'
ch.append(item['id']['channelId'])

if not len(ch):
return None

resp = youtube.channels().list(
id = ','.join(ch),
part = 'id,snippet',
fields = 'items(id,snippet(customUrl))',
maxResults = len(ch)
).execute()
assert len(resp['items']) <= len(ch)

for item in resp['items']:
url = item['snippet'].get('customUrl')
if url is not None and \
caseless_equal(url, custom_url):
assert item['id'] is not None
return item['id']

return None

其中函数 caseless_equal上面使用的是由于 this SO answer .

我发布了 here包含函数 find_channel_by_custom_url 的简单 Python3 脚本以上为独立程序。应用于此脚本的自定义 URL 会产生预期的结果:

$ python3 youtube-search.py \
--custom-url lukemiani \
--app-key ...
UC3c8H4Tlnm5M6pXsVMGnmNg

$ python3 youtube-search.py \
--user-name lukemiani \
--app-key ...
youtube-search.py: error: user name "lukemiani": no associated channel found

请注意,您必须将您的应用程序 key 作为命令行选项 --app-key 的参数传递给此脚本(使用 --help 获取简短的帮助信息)。

关于youtube-api - 从 youtube.com/c/xxxx 链接获取 channel ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63046669/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com