-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.js
47 lines (47 loc) · 1.46 KB
/
youtube.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var youtube = {
name: "youtube",
search_embed: function(embed, callback) {
youtube.search_helper(embed, 1, true, callback);
},
search_another_embed: function(embed, callback) {
console.log("searching another embed for " + embed.key);
youtube.search_helper(embed, 2, false, callback);
},
search_helper: function(embed, max_results, use_first, callback) {
if(embed && embed.code && use_first) {
callback();
return;
}
if(!embed || !embed.key) {
callback();
return;
}
$.getJSON('https://gdata.youtube.com/feeds/api/videos?q=' + encodeURIComponent(embed.key) + '&orderby=relevance&v=2&max-results=' + max_results + '&alt=json&category=music', function(data) {
if(data.feed.openSearch$totalResults.$t == 0) {
callback();
return;
}
if(typeof(data.feed.entry) == 'undefined') {
callback();
return;
}
if(use_first) {
entry = data.feed.entry[0];
} else {
if(data.feed.entry[1] != undefined) {
entry = data.feed.entry[1];
} else {
callback();
}
}
resp = {
service_name: youtube.name,
service_id: entry.media$group.yt$videoid.$t,
title: entry.media$group.media$title.$t,
code: '<iframe id="youtube-player" type="text/html" height="350" src="http://www.youtube.com/embed/' + entry.media$group.yt$videoid.$t + '?enablejsapi=1" frameborder="0"></iframe>'
}
callback(resp);
});
}
};
available_services['youtube'] = youtube;