更新时间:2023-02-21 07:58:23
根据官方支持人员,一个给定的频道可能关联了一个 URL 形式:
According to the official support staff, a given channel may have associated an URL of form:
https://www.***.com/c/CUSTOM_NAME
.
在这种情况下,相应频道的customUrl
属性是 CUSTOM_NAME
.
In such a case, the respective channel's customUrl
property is CUSTOM_NAME
.
现在,您的问题可以重新表述如下:
Now, your problem may be reformulated as follows:
给定一个 CUSTOM_NAME
,上面的 URL 指向一个现有的频道,是否有程序能够生成——通过使用 *** 数据 API——- 该频道的 ID,以便相应的程序符合 DTOS(即该程序通过不抓取从相应自定义 URL 获得的 HTML 文本来工作)?
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 *** 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)?
对上述问题的简短回答是不,没有.(请查看我的回答和所附评论我最近对一个类似问题的回答).
The short answer to the above question is no, there's none. (Please have a look at my answer and the attached comments I gave recently to a similar question).
更长的答案如下:是的,可以想象一种算法可以解决问题,但只能部分解决(因为不能保证它总是给出积极的结果).
The longer answer would be the following: yes, it can be imagined an algorithm that solves the problem, but only partially (since there's no guarantee that it'll always give positive results).
这是算法:
Search.list
具有以下参数的 API 端点:q=CUSTOM_NAME
,type=channel
和maxResults=10
.Channels.list
用于获取频道关联的 API 端点 customUrl
财产(如果有);customUrl
与CUSTOM_NAME
相等,则停止算法生成当前频道ID;否则,继续执行当前循环;由于 Search.list
端点提供的结果集的模糊性,不能排除实际存在自定义 URL(即指向上述表单的 URL)的可能性现有频道),该算法无法为其生成关联频道的 ID.
Due to the fuzzy nature of the result sets provided by the Search.list
endpoint, one cannot exclude the possibility that there could actually exist custom URLs (i.e. URLs of the form above that are pointing to existing channels) for which this algorithm is not able to yield the ID of the associated channel.
最后一点:Channels.list
端点接受其 id
参数以逗号分隔的频道 ID 列表.因此,可以很容易地修改上面的算法,而不是 N
次对 Channels.list
端点的调用 (N )只有一个.
A final note: the Channels.list
endpoint accepts its id
parameter to be a comma-separated list of channel IDs. Therefore, one may easily modify the algorithm above such that instead of N
invocations (N <= 10
) of Channels.list
endpoint to have only one.
以上算法在 Python 语言中的实现,使用 Google 的 Python API 客户端库:
An implementation of the algorithm above in Python language, using Google's APIs Client Library for Python:
def find_channel_by_custom_url(
***, custom_url, max_results = 10):
resp = ***.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'] == '***#channel'
ch.append(item['id']['channelId'])
if not len(ch):
return None
resp = ***.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.
where the function caseless_equal
used above is due to this SO answer.
我在此处发布了一个简单的 Python3 脚本,其中包含 find_channel_by_custom_url
上面的代码变成一个独立的程序.应用于此脚本的自定义 URL 会产生预期结果:
I posted here a simple Python3 script that encompasses the function find_channel_by_custom_url
above into a standalone program. Your custom URL applied to this script yields the expected result:
$ python3 ***-search.py \
--custom-url lukemiani \
--app-key ...
UC3c8H4Tlnm5M6pXsVMGnmNg
$ python3 ***-search.py \
--user-name lukemiani \
--app-key ...
***-search.py: error: user name "lukemiani": no associated channel found
请注意,您必须将您的应用程序密钥作为参数传递给此脚本,作为命令行选项 --app-key
(使用 --help
获取简要帮助信息).
Note that you have to pass to this script your application key as argument to the command line option --app-key
(use --help
for brief help info).