Youtube data api v3 按特定频道搜索所有上传和发布的视频
Posted
技术标签:
【中文标题】Youtube data api v3 按特定频道搜索所有上传和发布的视频【英文标题】:Youtube data api v3 search all uploaded and posted videos by a specific channel 【发布时间】:2017-07-07 20:46:37 【问题描述】:我正在使用 Youtube 数据 API 版本 3。使用提供的 java 代码搜索特定频道内的所有视频。
在 youtube.com 中,我可以在频道的“视频”标签中看到两种视频
已发布视频(其他渠道上传)
上传的视频(由本频道上传)
通过api搜索时,通过设置特定的channelId,api只返回该通道上传的视频。 还有什么方法可以获取已发布的视频吗?
public static void main(String[] args)
// Read the developer key from the properties file.
Properties properties = new Properties();
try
InputStream in = Search.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
properties.load(in);
catch (IOException e)
System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause()
+ " : " + e.getMessage());
System.exit(1);
try
// This object is used to make YouTube Data API requests. The last
// argument is required, but since we don't need anything
// initialized when the HttpRequest is initialized, we override
// the interface and provide a no-op function.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer()
public void initialize(HttpRequest request) throws IOException
).setApplicationName("youtube-cmdline-search-sample").build();
// Prompt the user to enter a query term.
String queryTerm = getInputQuery();
// Define the API request for retrieving search results.
YouTube.Search.List search = youtube.search().list("id,snippet");
// Set your developer key from the Google Cloud Console for
// non-authenticated requests. See:
// https://cloud.google.com/console
String apiKey = properties.getProperty("youtube.apikey");
search.setKey(apiKey);
search.setQ(queryTerm);
search.setChannelId("UCEgdi0XIXXZ-qJOFPf4JSKw");
// Restrict the search results to only include videos. See:
// https://developers.google.com/youtube/v3/docs/search/list#type
search.setType("video");
// To increase efficiency, only retrieve the fields that the
// application uses.
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
// Call the API and print results.
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
if (searchResultList != null)
prettyPrint(searchResultList.iterator(), queryTerm);
catch (GoogleJsonResponseException e)
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
catch (IOException e)
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
catch (Throwable t)
t.printStackTrace();
【问题讨论】:
【参考方案1】:通过使用Search: list
指定channelId,您将获得248 个结果。这意味着这些结果是用户上传的视频。但是,这并不意味着他拥有它。
为了更好的解释,我使用了这个参数。
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&channelId=UCEgdi0XIXXZ-qJOFPf4JSKw&_h=1&
我使用您在问题中指定的channelId
。我们会得到这样的第一个结果。
"snippet":
"publishedAt": "2015-12-03T17:14:46.000Z",
"channelId": "UCEgdi0XIXXZ-qJOFPf4JSKw",
"title": "Kobe's Farewell Tour",
"description": "Kobe Bryant announced that this season, his 20th, will be his last, and is saying goodbye to fans around the league.",
"thumbnails":
"default":
"url": "https://i.ytimg.com/vi/FR0AqkteAYw/default.jpg",
"width": 120,
"height": 90
,
"medium":
"url": "https://i.ytimg.com/vi/FR0AqkteAYw/mqdefault.jpg",
"width": 320,
"height": 180
,
"high":
"url": "https://i.ytimg.com/vi/FR0AqkteAYw/hqdefault.jpg",
"width": 480,
"height": 360
,
"channelTitle": "Sports",
"liveBroadcastContent": "none"
,
您会注意到第一个结果的标题是“Kobe's Farewell Tour”,通过获取此视频,我发现这是一个播放列表,如果您查看其内容,它是由不同的不同用户上传的视频。
https://www.youtube.com/watch?v=FR0AqkteAYw&list=PL8fVUTBmJhHLB3FW_53W1P0mtmwRTCEK_
所以这些是您在 channelId=UCEgdi0XIXXZ-qJOFPf4JSKw 中看到的视频。因此,要获取所有视频,请使用您在 search.list 中获得的所有播放列表上的 PlaylistItems: list
。
希望对你有帮助。
【讨论】:
谢谢,这在我的情况下不起作用,因为我将“类型”参数值设置为“视频”,您可能在我的代码中错过了它。我只搜索视频,而不是播放列表,但您的解决方案在某种意义上是有效的。以上是关于Youtube data api v3 按特定频道搜索所有上传和发布的视频的主要内容,如果未能解决你的问题,请参考以下文章
使用 YouTube Data API v3 确定 YouTube 频道的上传速率
YouTube API V3 在根据特定国家/地区获取热门频道时遇到困难
如何使用新的 YouTube Data API (V3) 获取某个频道的上传视频列表?
如何使用新的 YouTube Data API (V3) 获取某个频道的上传视频列表?