php - 按 channel 名称获取 channel 中最受欢迎的视频

标签 php youtube youtube-api

我正在尝试使用YouTube 3.0 API列出 channel 中最受欢迎(或最新)的视频。

我已经实现了这一点,只是不确定这样做是否正确,因此需要建议。

首先,我按名称找到 channel ID(例如TEDxTalks代替{channel_name}):

https://www.googleapis.com/youtube/v3/search?part=snippet&type=channel&q={channel_name}&key={API_KEY}

然后,我得到此 channel ID的视频,按viewCount(或date)排序,可以完美列出视频:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channel_id}&order=viewCount&type=video&key={API_KEY}

第一个请求的可靠性如何?如果名称与 channel 网址段名或用户名(例如tedxtalks)完全匹配,它将始终返回相应的 channel 吗?还是有更可靠的方法来按名称获取 channel ID?

谢谢。

最佳答案

Search API不适用于完全匹配查询。并搜索所有 channel 标题(如果存在,则在URL中找到用户名), channel 名称(xxx添加1个视频)甚至 channel ID!

不幸的是,要回答您的问题,这是不可靠的,没有其他更可靠的方法。

一个简单的示例,尝试查询用户名“abc”。

https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=abc&type=channel&key= {YOUR_API_KEY}

Search API响应将返回ABCNetwork,ABCNews,udruzenjeabc ...等。前50个项目中没有channelTitle(username)“abc”。

即使并非所有 channel 都有用户名。如果您获得了特定 channel 的用户名,则应使用Channel API进行完全匹配查询,

https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=abc&maxResults=5&key= {YOUR_API_KEY}

您可能想知道YouTube API为什么不提供对“ channel 名称”(xxx加1个视频)的确切搜索?这是因为 channel 名称不是唯一的,因为个人名称不是唯一的。

https://developers.google.com/youtube/v3/guides/working_with_channel_ids所示:

If you are using v3 and want to retrieve the channel ID that corresponds to the currently authorized user, you can call the channels.list(part="id", mine=true) method. This is equivalent to asking for the channel profile of the default user in v2.

If you ever do find yourself with an arbitrary legacy YouTube username that you need to translate into a channel ID using v3 of the API, you can make a channels.list(part="id", forUsername="username") call to the API.

If you only know a display name and are looking to find the corresponding channel, the search.list(part="snippet", type="channel", q="display name") method will come in handy. You should be prepared to deal with the possibility of the call returning more than one item in the response, since display names are not unique.



最后,我不知道YouTube为什么不强制为所有新用户生成唯一的可读用户名(例如Facebook如何在重复的用户名末尾添加随机数,例如zuck.5和zuck.21)。

引用文献:
  • https://developers.google.com/youtube/v3/docs/search/list
  • https://developers.google.com/youtube/v3/docs/channels/list#try-it
  • https://support.google.com/youtube/answer/2657968?hl=en
  • https://developers.google.com/youtube/v3/guides/working_with_channel_ids
  • 关于php - 按 channel 名称获取 channel 中最受欢迎的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24942684/

    相关文章:

    uiwebview - iOS 上的 Chromecast 嵌入式 youtube 视频

    python - YouTube Python API

    php - 按邮政编码/经度/纬度选择结果

    php - 如何解决性能问题?

    php - Cucumber:如何在 PHP 中编写代码片段/步骤定义?

    Python - 发出 POST 请求

    youtube - YouTube for Chrome 上是否还有 Flash 残留?

    php - 在 Magento 1.9.1.1 中重新索引类别产品时出现问题

    google-apps-script - Apps 脚本不断通过 Youtube 的 API 请求离线许可

    php - 用 PHP 或 Javascript 生成实际的 Youtube 下载链接