问题
It seems Youtube has gotten rid of /channel/XXXX urls on their page, its now /c/username? with username NOT really being a "username". For example
https://www.youtube.com/c/lukemiani
Running a lookup via
https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=lukemiani&key=...
returns no results.
I've got a bunch of non-technical users who've been trained to look for /channel/x or /user/x and input the correct thing into my app. Now that /channel is gone how do I (or they) translate /c/x to a channel id?
I'm looking for an API solution, not a view source and reverse engineer code solution.
回答1:
According to the official support staff, a given channel may have associated an URL of form:
https://www.youtube.com/c/CUSTOM_NAME
.
In such a case, the respective channel's customUrl property is CUSTOM_NAME
.
Now, your problem may be reformulated as follows:
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)?
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).
Here is the algorithm:
- Call the Search.list API endpoint with the following parameters:
q=CUSTOM_NAME
,type=channel
, andmaxResults=10
.
- Extract from the result set obtained the channel IDs (these IDs are located at items[].id.channelId);
- For each channel ID in the list obtained at step 2:
- Invoke Channels.list API endpoint for to obtain the channel's associated customUrl property (if any);
- If the obtained
customUrl
is equal withCUSTOM_NAME
, then stop the algorithm yielding the current channel ID; otherwise, continue executing the current loop;
- Stop the algorithm by yielding channel ID not found.
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.
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.
An implementation of the algorithm above in Python language, using Google's 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
where the function caseless_equal
used above is due to this SO answer.
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 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
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).
来源:https://stackoverflow.com/questions/63046669/obtaining-a-channel-id-from-a-youtube-com-c-xxxx-link