-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.jTube.js
266 lines (225 loc) · 7.39 KB
/
jquery.jTube.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* jTube
* http://jtube.monkeecreate.com
*
* jQuery Youtube API Feed Plugin
*
* Developed by John Hoover <[email protected]>
* Another project from monkeeCreate <http://monkeecreate.com>
*
* Version 1.3.6 - Last updated: May 25, 2010
*/
(function($) {
$.extend({
jTube: function(options){
var options = $.extend({
user: '',
userType: 'uploads',
search: '',
feed: '',
playlist: '',
format: 'flash',
order: 'published',
time: 'all_time',
limit: 5,
page: 1,
success: function(videos, pages){},
error: function(message){}
}, options);
var youtubeUrl = 'http://gdata.youtube.com/feeds/';
var videoElem = this;
var imageUrl = '';
if(options.user != '')
youtubeUrl += 'api/users/'+options.user+'/'+options.userType+'?';
else if(options.search != '')
youtubeUrl += 'api/videos?q='+options.search+'&';
else if(options.feed != '')
youtubeUrl += 'api/standardfeeds/'+options.feed+'?';
else if (options.playlist != '')
youtubeUrl += 'api/playlists/'+options.playlist+'?';
else {
options.error("No feed choices given.");
return false;
}
youtubeUrl += 'alt=json-in-script';
youtubeUrl += '&max-results='+options.limit;
youtubeUrl += '&start-index='+(((options.page * options.limit) - options.limit) + 1);
youtubeUrl += '&orderby='+options.order;
youtubeUrl += '&time='+options.time;
if(options.format == "mpeg")
youtubeUrl += '&format=6';
else if(options.format == "h263")
youtubeUrl += '&format=1';
else
youtubeUrl += '&format=5';
youtubeUrl +='&callback=?';
$.getJSON(
youtubeUrl,
function(data) {
if(data != '' && data != null) {
if(options.user != '' && options.userType == 'playlists') {
var playlists = [];
$(data.feed.entry).each(function(){
var playlist = {
title: this.title.$t,
description: this.yt$description.$t,
id: this.yt$playlistId.$t,
link: this.link[1].href,
published: new Date(this.published.$t)
};
playlists[playlists.length] = playlist;
});
var videos = playlists;
} else if(options.user != '' && options.userType == 'subscriptions') {
var subscriptions = [];
$(data.feed.entry).each(function(){
var subscription = {
title: this.title.$t,
username: this.yt$username.$t,
link: this.link[1].href,
thumbnail: this.media$thumbnail.url,
published: new Date(this.published.$t),
updated: new Date(this.updated.$t)
};
subscriptions[subscriptions.length] = subscription;
});
var videos = subscriptions;
} else if(options.user != '' && options.userType == 'contacts') {
var contacts = [];
$(data.feed.entry).each(function(){
var contact = {
username: this.yt$username.$t,
status: this.yt$status.$t,
link: this.link[1].href
};
contacts[contacts.length] = contact;
});
var videos = contacts;
} else {
var videos = [];
$(data.feed.entry).each(function(){
//Create a clean category array
var categories = [];
$(this.category).each(function(index){
if(index != 0) {
categories[index - 1] = this.term
}
});
var video = {
title: this.title.$t,
link: this.link[0].href,
categories: categories,
author: {
name: this.author[0].name.$t,
link: this.author[0].uri.$t
}
};
// Description
if(this.media$group.media$description) {
video.description = this.media$group.media$description.$t;
}
// Video Thumbnail
if(this.media$group.media$thumbnail) {
video.thumbnail = this.media$group.media$thumbnail[3].url;
}
// Create array of available formats
videoFormats = [];
$(this.media$group.media$content).each(function(){
videoFormats[this.yt$format] = this.url;
});
// Get video url based on requested video type
if(options.format == "mpeg")
video.video = videoFormats[6];
else if(options.format == "h263")
video.video = videoFormats[1];
else
video.video = videoFormats[5];
// Video published date/time
if(this.published)
video.published = new Date(this.published.$t);
// Video formated duration
if(this.media$group.yt$duration) {
duration = this.media$group.yt$duration.seconds;
hours = 0;
minutes = 0;
seconds = 0;
// Hours
while(duration >= 3600) {
hours = hours + 1;
duration = duration - 3600;
}
// Minutes
while(duration >= 60) {
minutes = minutes + 1;
duration = duration - 60;
}
// Seconds is remainder
seconds = duration;
// Add leading 0
if(seconds < 10)
seconds = '0'+seconds;
// Put minutes and seconds together
video.length = minutes+':'+seconds;
// If video is an hour or more, add to video length
if(hours > 0)
video.length = hours+':'+video.length;
}
// View count
if(this.yt$statistics)
video.views = this.yt$statistics.viewCount;
// Add video to array to pass back
videos[videos.length] = video;
});
}
pages = Math.ceil(data.feed.openSearch$totalResults.$t / options.limit);
options.success(videos, pages);
} else {
options.error("Bad request.");
}
}
);
return this;
},
jTubeEmbed: function(video, options) {
var options = $.extend({
// Embed Options
width: 290,
height: 250,
// Player Options
autoplay: true,
fullscreeen: false,
related: true,
loop: false,
keyboard: true,
genie: false,
border: false,
highdef: true,
start: 0
}, options);
var videoUrl = video+"?";
videoUrl += 'autoplay='+(options.autoPlay?1:0);
videoUrl += '&fs='+(options.fullscreen?1:0);
videoUrl += '&rl=1'+(options.related?1:0);
videoUrl += '&loop=1'+(options.loop?1:0);
videoUrl += '&disablekb=0'+(options.keyboard?0:1);
videoUrl += '&egm=1'+(options.genie?1:0);
videoUrl += '&border=1'+(options.border?1:0);
videoUrl += '&hd=1'+(options.highdef?1:0);
videoUrl += '&start='+options.start;
var videoEmbed = '<object width="'+options.width+'" height="'+options.height+'">';
videoEmbed += '<param name="movie" value="'+videoUrl+'"</param>';
videoEmbed += '<param name="allowScriptAccess" value="always"></param>';
if(options.fullscreen == true)
videoEmbed += '<param name="allowFullScreen" value="true"></param>';
videoEmbed += '<embed src="'+videoUrl+'"';
videoEmbed += ' type="application/x-shockwave-flash"';
if(options.fullscreen == true)
videoEmbed += ' allowfullscreen="true"';
videoEmbed += ' allowscriptaccess="always"';
videoEmbed += ' width="'+options.width+'" height="'+options.height+'">';
videoEmbed += ' </embed>';
videoEmbed += '</object>';
return videoEmbed;
}
});
})(jQuery);