如何列出来自YouTube频道的上传内容?

时间:2019-03-06 01:40:44

标签: jquery google-apps-script youtube

我正在编写此代码来搜索我网站上的youtube频道,然后在单击它们后列出其上载内容。但是,目前我将所有频道上的所有链接都链接到列出的最后一个频道的上载,而我无法说出代码中如何允许这种情况发生。有人可以看看吗,让我知道出了什么问题。您也可以在www.youtubebypass.dx.am

上尝试/查看网站
   <!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube</title>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="app.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
<link href="style.min.css" rel="stylesheet">
<script>

     function channelsearch(){
             gapi.client.setApiKey('AIzaSyAhXRvLpBIyLlOQ9N0W6qUNNqNrhmQBovY')

             gapi.client.load('youtube', 'v3', function() {
                     makechannelRequest();
        });
     }

      function makechannelRequest() {
        var q = $('#Channelquery').val();
        var request = gapi.client.youtube.search.list({
                q: q,
        type: 'channel',
                part: 'snippet', 
                maxResults: 25
        });
        request.execute(function(response)  {
                console.log(response)
                $('#results').empty()
        $("#search-results").html("Search Results");
                var srchItems = response.result.items;                      
                $.each(srchItems, function(index, item) {
                channelTitle = "<h4>"+item.snippet.channelTitle+"</h4>";  
                channelThumburl = item.snippet.thumbnails.default.url;
                id = item.id.channelId;
                channelThumbimg = '<img id="thumb" src="'+channelThumburl+'" alt="No  Image Available.">';                   

                $('#results').append("<a onclick=\'ChannelUploadId()'>" + channelTitle + "<br>" + channelThumbimg + "<br>" + "</a>")
        })  
    })  
}

     function ChannelUploadId(){
             var request = gapi.client.youtube.channels.list({id: id, part:'contentDetails'});
             request.execute(function(response){
                             console.log(response);
                     var srchItems = response.result.items;
                     $.each(srchItems, function(index, item) {
                     playlistId = item.contentDetails.relatedPlaylists.uploads;


             })
             ChannelUploads(playlistId);
     })
     }
     function ChannelUploads(i){
        console.log(i)

        var request = gapi.client.youtube.playlistItems.list({

             playlistId: i,
             type:'video',
             order:'date',
             part: 'snippet',
             maxResults: 25


     });
     request.execute(function(response){
                             console.log(response)
                   $('#results').empty()
                        $("#search-results").html("Search Results");
                var srchItems = response.result.items;                      
                $.each(srchItems, function(index, item) {
                vidTitle = "<h4>"+item.snippet.title+"</h4>";  
                vidThumburl = item.snippet.thumbnails.high.url;
                vidChannel = "<h4>"+item.snippet.channelTitle+"</h4>";
                vidPublish = "<h4>"+item.snippet.publishedAt+"</h4>";
                id = item.id.videoId;
                vidThumbimg = '<img id="thumb" src="'+vidThumburl+'" alt="No  Image Available.">';                   

                $('#results').append("<a onclick=\'getScreenshots(\"https://www.youtube.com/watch?v="+id+"\")'>" + vidTitle + "<br>" + "Published: " +vidPublish + "<br>"+ "Channel: "+vidChannel + "<br>"+ vidThumbimg + "</a><hr>");                      
        })
     })

   }




     function keyWordsearch(){
        gapi.client.setApiKey('AIzaSyAhXRvLpBIyLlOQ9N0W6qUNNqNrhmQBovY')


        gapi.client.load('youtube', 'v3', function() {
                makeRequest();
        });
}
    function makeRequest() {
        var q = $('#query').val();
        var request = gapi.client.youtube.search.list({
                q: q,
        type: 'video',
                part: 'snippet', 
                maxResults: 25
        }); 
        request.execute(function(response)  {
                console.log(response)
                $('#results').empty()
                $("#search-results").html("Search Results");
                var srchItems = response.result.items;                      
                $.each(srchItems, function(index, item) {
                vidTitle = "<h4>"+item.snippet.title+"</h4>";  
                vidThumburl = item.snippet.thumbnails.high.url;
                vidChannel = "<h4>"+item.snippet.channelTitle+"</h4>";
                vidPublish = "<h4>"+item.snippet.publishedAt+"</h4>";
                id = item.id.videoId;
                vidThumbimg = '<img id="thumb" src="'+vidThumburl+'" alt="No  Image Available.">';                   

                $('#results').append("<a onclick=\'getScreenshots(\"https://www.youtube.com/watch?v="+id+"\")'>" + vidTitle + "<br>" + "Published: " +vidPublish + "<br>"+ "Channel: "+vidChannel + "<br>"+ vidThumbimg + "</a><hr>");                      
        })  
    })  
}
  </script> 

1 个答案:

答案 0 :(得分:1)

拥有channel_id后,按如下所示更改highlighted letter

  

频道:Microsoft Hololens

     

Channel_id:U C T2rZIAL-zNqeK1OmLLUa6g

     

上传(播放列表):U U T2rZIAL-zNqeK1OmLLUa6g

有了上传的播放列表后,您可以使用此请求获取上传的视频:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=<UPLOAD_PLAYLIST_ID>&fields=items(contentDetails(videoId%2CvideoPublishedAt)%2Csnippet(publishedAt%2Ctitle))&key={YOUR_API_KEY}

这些是结果:

{
 "nextPageToken": "CAUQAA",
 "pageInfo": {
  "totalResults": 61,
  "resultsPerPage": 5
 },
 "items": [
  {
   "snippet": {
    "publishedAt": "2019-02-24T17:38:51.000Z",
    "title": "Industry Partner Solutions for Microsoft HoloLens 2"
   },
   "contentDetails": {
    "videoId": "FWYcuHUgcng",
    "videoPublishedAt": "2019-02-24T17:38:51.000Z"
   }
  },
  {
   "snippet": {
    "publishedAt": "2019-02-24T17:32:14.000Z",
    "title": "Introducing Microsoft HoloLens 2 (Audio Description)"
   },
   "contentDetails": {
    "videoId": "NtRGESjKT6M",
    "videoPublishedAt": "2019-02-24T17:32:14.000Z"
   }
  },
  {
   "snippet": {
    "publishedAt": "2019-02-24T17:31:30.000Z",
    "title": "Introducing Microsoft HoloLens 2"
   },
   "contentDetails": {
    "videoId": "eqFqtAJMtYE",
    "videoPublishedAt": "2019-02-24T17:31:30.000Z"
   }
  },
  {
   "snippet": {
    "publishedAt": "2019-02-24T17:39:08.000Z",
    "title": "PACCAR is exploring Dynamics 365 Guides and HoloLens 2 to improve employee onboarding"
   },
   "contentDetails": {
    "videoId": "OQDhTtDgir0",
    "videoPublishedAt": "2019-02-24T17:39:08.000Z"
   }
  },
  {
   "snippet": {
    "publishedAt": "2019-02-24T17:41:16.000Z",
    "title": "Introducing Dynamics 365 Guides for HoloLens 2"
   },
   "contentDetails": {
    "videoId": "V8c3pDKdHEc",
    "videoPublishedAt": "2019-02-24T17:41:16.000Z"
   }
  }
 ]
}

这是我制作的Google API Explorer demo

NB,this channel has 61 uploaded videos,每页的最大结果为50。

这是整个working jsfiddle