【问题标题】:How can I list the uploads from a YouTube Channel?如何列出来自 YouTube 频道的上传内容?
【发布时间】:2019-07-27 14:15:50
【问题描述】:

我正在编写此代码以在我的网站上搜索 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> 

【问题讨论】:

    标签: jquery google-apps-script youtube


    【解决方案1】:

    一旦你有了channel_id如图所示更改highlighted letter

    频道:微软 Hololens

    Channel_id:UCT2rZIAL-zNqeK1OmLLUa6g

    上传(播放列表):UUT2rZIAL-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

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

    这是整个working jsfiddle

    【讨论】:

    • 我的请求可以正常工作,但是我的所有链接都已更改为频道列表 playlistid 中的最后一项。
    • @DanielRyan 可能是您没有为播放列表项制作正确的循环,或者您正在覆盖它们。我正在尝试复制结果并仅作为建议:尝试在您的代码中使用 cmets。如果您想创建一个工作示例,请使用 jsfiddle。我知道您的页面有效,但是,如果您使用 jsfiddle 会更好,而且不要在其上添加凭据,即您的 YouTube 数据 API 密钥。
    • 我能够弄清楚,因为这是我的代码结构的问题,因为它已经写完了所有内容。我不知道我是如何修复它的,但可以尝试使用它并删除一个功能以及简化它的工作方式来修复它。
    猜你喜欢
    • 2020-10-12
    • 2013-08-18
    • 1970-01-01
    • 2012-01-08
    • 2013-09-12
    • 2013-06-20
    • 1970-01-01
    • 2014-11-02
    • 2014-10-09
    相关资源
    最近更新 更多