Skip to content

Commit

Permalink
UPDATED YOUTUBE API to V3
Browse files Browse the repository at this point in the history
API KEY BASED USAGE
  • Loading branch information
Anagani Sai Kiran committed Mar 5, 2016
1 parent e6ec878 commit 71c43c6
Showing 1 changed file with 60 additions and 69 deletions.
129 changes: 60 additions & 69 deletions public/js/remote.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,66 @@
var host = document.location.origin;
var socket = io.connect(host);
var socket = io.connect(host);
socket.on('connect', function(data){
socket.emit('remote');

//Youtube
var Youtube = {
getVideo: function(query, socket){
var max_videos = 12;
var url = "http://gdata.youtube.com/feeds/api/videos?vq=" + escape(query) + "&max-results=" + max_videos + "&alt=json-in-script&callback=?";

$.getJSON(url, function(data){
$("ul.video").html("");
var jsonObj = [];
$(data.feed.entry).each(function(key, item){
var a = item.id.$t.split("/"),
id = a[6],
title = item.title.$t,
thumbnail = item.media$group.media$thumbnail[0].url,
totalSec = item.media$group.yt$duration.seconds,
hours = parseInt( totalSec / 3600 ) % 24,
minutes = parseInt( totalSec / 60 ) % 60,
seconds = totalSec % 60;

var duration = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);


jsonObj = {
id:id,
title:title,
thumbnail:thumbnail,
duration:duration};


var template = $('#videoTpl').html(),
html = Mustache.to_html(template, jsonObj);
$('ul.video').append(html);

});
$(".watch").on("click",function(){
var video_id = $(this).data('id');
socket.emit('video',{action:"play", video_id:video_id});
});
});

}
}

$$(".r-container").swipeLeft(function(){
socket.emit('controll',{action:"swipeLeft"});
});
var Youtube = {
getVideo: function(query, socket){
var apiKey = "{YOUR API KEY HERE}"
var max_videos = 25;
var url = "https://www.googleapis.com/youtube/v3/search?order=viewcount&part=snippet&q=" + escape(query) + "&type=video+&videoDefinition=high&key=" + apiKey + "&maxResults="+ max_videos;

$$(".r-container").swipeRight(function(){
socket.emit('controll',{action:"swipeRight"});
});
$$(".r-header").tap(function(){
socket.emit('controll',{action:"tap"});
$(".app-body").fadeToggle("fast", function () {});
$.get(host+'/omx/quit',function(data){
console.log(data);
});
});
$$(".app-body").tap(function(){
$.get(host+'/omx/pause',function(data){
console.log(data);
$.getJSON(url, function(json){
$("ul.video").html("");
var jsonObj = [];
$(json.items).each(function(key, item){
console.log(item.id.videoId+" "+item.snippet.title+" "+item.snippet.thumbnails.default.url);
jsonObj = {
id: item.id.videoId,
title: item.snippet.title,
thumbnail: item.snippet.thumbnails.medium.url,
description: item.snippet.description
};


var template = $('#videoTpl').html(),
html = Mustache.to_html(template, jsonObj);
$('ul.video').append(html);

});
$(".watch").on("click",function(){
var video_id = $(this).data('id');
socket.emit('video',{action:"play", video_id:video_id});
});
});

}
}

$$(".r-container").swipeLeft(function(){
socket.emit('controll',{action:"swipeLeft"});
});

$$(".r-container").swipeRight(function(){
socket.emit('controll',{action:"swipeRight"});
});
$$(".r-header").tap(function(){
socket.emit('controll',{action:"tap"});
$(".app-body").fadeToggle("fast", function () {});
$.get(host+'/omx/quit',function(data){
console.log(data);
});
});
$$(".app-body").tap(function(){
$.get(host+'/omx/pause',function(data){
console.log(data);
});
});
$(".search input").change(function() {
Youtube.getVideo($(this).val(), socket);
});

socket.on("loading", function(data){
console.log(data);
})
});
});
$(".search input").change(function() {
Youtube.getVideo($(this).val(), socket);
});

socket.on("loading", function(data){
console.log(data);
})
});

0 comments on commit 71c43c6

Please sign in to comment.