Created
June 29, 2018 03:10
-
-
Save ahmedmusawir/b9f0f71cb55dfe20a2acf9f373255f0b to your computer and use it in GitHub Desktop.
JS - JQUERY - YOUTUBE API V3 - PULLING VIDEOS BY CHANNEL ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
| <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"> | |
| </head> | |
| <body> | |
| <style> | |
| .youtube { | |
| } | |
| </style> | |
| <div class="container-fluid"> | |
| <div class="row justify-content-center"> | |
| <article class="col-md col-sm col-lg"> | |
| <div class="youtube"></div> | |
| </article> | |
| </div> | |
| </div> | |
| <section id="results">Results go Here</section> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | |
| <script> | |
| //var channelName = 'TechGuyweb'; | |
| //var channelID = 'UCoqGuUsuGWnx4uUAL0pIKSQ'; //ahmed.musawir Channel ID | |
| var channelID = 'UCbmBY_XYZqCa2G0XmFA7ZWg'; | |
| $(document).ready(function(){ | |
| $.get("https://www.googleapis.com/youtube/v3/channels", { | |
| part: 'contentDetails', | |
| //forUsername: channelName, | |
| id: channelID, | |
| key: 'AIzaSyAIMCxrRnRcYiNIhXK0IH_AC7OvS5eX1nc'}, | |
| function(data) { | |
| $.each( data.items, function(index, item) { | |
| //console.log(item.contentDetails.relatedPlaylists.uploads); | |
| pid = item.contentDetails.relatedPlaylists.uploads; | |
| getVideos( pid ); | |
| }); | |
| }); | |
| function getVideos( pid ) { | |
| $.get( "https://www.googleapis.com/youtube/v3/playlistItems", { | |
| part: 'snippet', | |
| maxResults: 10, | |
| playlistId: pid, | |
| key: 'AIzaSyAIMCxrRnRcYiNIhXK0IH_AC7OvS5eX1nc'}, | |
| function( data ) { | |
| var output; | |
| $.each( data.items, function( i, item ) { | |
| console.log( item ); | |
| vidTitle = item.snippet.title; | |
| vidId = item.snippet.resourceId.videoId; | |
| //output = '<li>' + vidTitle + '</li>'; | |
| //output = '<iframe width="560" height="315" src="https://www.youtube.com/embed/' + vidId + '" frameborder="0"; encrypted-media" allowfullscreen></iframe>'; | |
| output = '<iframe width="400" height="200" src="https://www.youtube.com/embed/' + vidId + '" frameborder="0"; encrypted-media" allowfullscreen></iframe>'; | |
| $('.youtube').append( output ); | |
| }); | |
| }); | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment