In this post I am going to discuss about how to fetch YouTube playlist from a channel using Appcelerator Titanium.
YouTube offer API for category, search term, playlist, favorites, subscriptions... etc. Here you can found list of YouTube API
https://developers.google.com/youtube/getting_started
Below API will retrieve the user's playlists. This request does not require authentication
https://gdata.youtube.com/feeds/api/users/userId/playlists?v=2
The above API response containing a playlists feed in XML format.
The response contains a series of <entry> tags, with each entry describing a playlist.
Each entry contains the playlist's title, description, author and modification date as well as
a <content> tag that specifies the URL for retrieving the list of videos in the playlist
Here I am using Apple's YouTube username to fetch their YouTube playlist
In below code I am going to retrieve all the videos in a particular playlist using playlist id.
http://gdata.youtube.com/feeds/api/playlists/playlist_id?v=2
Here you can download the complete working project from my Github Titanium YouTube Playlist
CodeStrong
YouTube offer API for category, search term, playlist, favorites, subscriptions... etc. Here you can found list of YouTube API
https://developers.google.com/youtube/getting_started
Below API will retrieve the user's playlists. This request does not require authentication
https://gdata.youtube.com/feeds/api/users/userId/playlists?v=2
* Here userId represents YouTube username.
The above API response containing a playlists feed in XML format.
The response contains a series of <entry> tags, with each entry describing a playlist.
Each entry contains the playlist's title, description, author and modification date as well as
a <content> tag that specifies the URL for retrieving the list of videos in the playlist
Here I am using Apple's YouTube username to fetch their YouTube playlist
var channelName = "apple"; var loader = Titanium.Network.createHTTPClient(); loader.open("GET", "http://gdata.youtube.com/feeds/api/users/"+channelName+"/playlists?v=2"); loader.onload = function() { var doc = this.responseXML.documentElement; var items = doc.getElementsByTagName("entry"); for (var i = 0; i < items.length; i++) { var item = items.item(i); var playlist_title = item.getElementsByTagName("title").item(0).text;//playlist title var vid_count = item.getElementsByTagName("yt:countHint").item(0).text; //video count for this playlist var play_list_id = item.getElementsByTagName("yt:playlistId").item(0).text; //play_list_id } }; loader.onerror = function(e) { alert("Error:"+e.error); }; loader.timeout = 10000; loader.send();
In below code I am going to retrieve all the videos in a particular playlist using playlist id.
http://gdata.youtube.com/feeds/api/playlists/playlist_id?v=2
var loader = Titanium.Network.createHTTPClient(); loader.open("GET", "http://gdata.youtube.com/feeds/api/playlists/" + playlist_id + "?v=2"); loader.onload = function() { var doc = this.responseXML.documentElement; var items = doc.getElementsByTagName("entry"); for (var c = 0; c < items.length; c++) { var item = items.item(c); var thumbnails = item.getElementsByTagName("media:thumbnail"); if (thumbnails && thumbnails.length > 0) { var media = thumbnails.item(0).getAttribute("url"); //thumbnail image var title = item.getElementsByTagName("title").item(0).text; //video title var videoId = item.getElementsByTagName("yt:videoid").item(0).text; //video id } } }; loader.onerror = function(e) { alert("Error:" + e.error); }; loader.timeout = 5000; loader.send();
Playlist listing for Apple |
Video List for Particular Playlist |
CodeStrong
Can I use this code for a specific chanel/playlist?
ReplyDeleteYes, you can. Change the Channel Name here // var channelName = "apple";
ReplyDeleteMine has an error playlist_id line 27
ReplyDeleteThanks for this tutorial. Short and sweet and to the point :)
ReplyDeleteHi Karthi,its a wonderful tutorial.Helped me a lot in my project.I checked the option for playing videos from a particular channel.I was wondering how can we play a particular playlist or videos of a user on youtube.I am sure there might be some functionality already there in your code, I just need guidance how to use it for playlist and user videos. Thanks
ReplyDeletethis code is not working for android can you please help
ReplyDeleteI have donwloaded Github project, changed the channelName but i get an error : Unable to connect to the server. I m sorry but im im new to titanium....for what is needed the cloud setted up here ? I see also highlighted errors in the code.... can you give me an hand to setup this script pls ? I wont bother you for more than 5 mins , Contact me on the email please, ill make a donation.
ReplyDeleteHi Karthi, I m making an app to search Youtube directly from the app Searchbar, for that I m looking for a related API, can you plz tell me where did u download this API from? Or can u help me in some other way for this?
ReplyDeleteGreaat read
ReplyDelete