如何修改此功能以获得最热门的视频? (YouTube API v3)
How to Modify This Function to Get Most Popular Videos? (YouTube API v3)
我使用以下方法获取 YouTube 用户的视频列表。
首先它找到用户频道的播放列表,然后找到该播放列表的视频标题。
It display the most recent videos. I need to get most popular videos.
How to modify this function to get most popular videos?
// Get Uploads Playlist
$.get(
"https://www.googleapis.com/youtube/v3/channels",{
part : 'contentDetails',
forUsername : 'USER_CHANNEL_NAME',
key: 'YOUR_API_KEY'},
function(data) {
$.each( data.items, function( i, item ) {
pid = item.contentDetails.relatedPlaylists.uploads;
getVids(pid);
});
}
);
//Get Videos
function getVids(pid){
$.get(
"https://www.googleapis.com/youtube/v3/playlistItems",{
part : 'snippet',
maxResults : 20,
playlistId : pid,
key: 'YOUR_API_KEY'},
function(data) {
var results;
$.each( data.items, function( i, item ) {
results = '<li>'+ item.snippet.title +'</li>';
$('#results').append(results);
});
}
<!--In your HTML -->
<ul id="results"></ul
您可以尝试使用videos.list方法,将图表参数的值设置为mostPopular。
chart
- The chart parameter identifies the chart that you want to retrieve.
Acceptable values are:
- mostPopular – Return the most popular videos for the specified content
region and video category.
https://www.googleapis.com/youtube/v3/videos?chart=mostPopular&key={YOUR_API_KEY}&part=snippet&maxResults=4
有关详细信息,请查看此 page。
我使用以下方法获取 YouTube 用户的视频列表。
首先它找到用户频道的播放列表,然后找到该播放列表的视频标题。
It display the most recent videos. I need to get most popular videos. How to modify this function to get most popular videos?
// Get Uploads Playlist
$.get(
"https://www.googleapis.com/youtube/v3/channels",{
part : 'contentDetails',
forUsername : 'USER_CHANNEL_NAME',
key: 'YOUR_API_KEY'},
function(data) {
$.each( data.items, function( i, item ) {
pid = item.contentDetails.relatedPlaylists.uploads;
getVids(pid);
});
}
);
//Get Videos
function getVids(pid){
$.get(
"https://www.googleapis.com/youtube/v3/playlistItems",{
part : 'snippet',
maxResults : 20,
playlistId : pid,
key: 'YOUR_API_KEY'},
function(data) {
var results;
$.each( data.items, function( i, item ) {
results = '<li>'+ item.snippet.title +'</li>';
$('#results').append(results);
});
}
<!--In your HTML -->
<ul id="results"></ul
您可以尝试使用videos.list方法,将图表参数的值设置为mostPopular。
chart - The chart parameter identifies the chart that you want to retrieve.
Acceptable values are:
- mostPopular – Return the most popular videos for the specified content region and video category.
https://www.googleapis.com/youtube/v3/videos?chart=mostPopular&key={YOUR_API_KEY}&part=snippet&maxResults=4
有关详细信息,请查看此 page。