Created
September 18, 2012 19:35
-
-
Save morion4000/3745323 to your computer and use it in GitHub Desktop.
Titanium commonjs module for Youtube
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
var Youtube = require('Youtube'); | |
var youtube = new Youtube(); | |
var youtubeId = 'HQ1z0Zzqg5U'; | |
youtube.h264videosWithYoutubeURL(youtubeId, function(e) { | |
videoPlayer.url = e.medium; | |
}, function(e) { | |
alert(JSON.stringify(e)); | |
}); |
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
function Youtube() { | |
var self = { | |
h264videosWithYoutubeURL: function(_youtubeId, _callbackOk, _callbackError) { | |
var youtubeInfoUrl = 'http://www.youtube.com/get_video_info?video_id=' + _youtubeId; | |
var request = Titanium.Network.createHTTPClient({ timeout : 10000 /* in milliseconds */}); | |
request.open("GET", youtubeInfoUrl); | |
request.onerror = function(_event){ | |
if (_callbackError) | |
_callbackError({status: this.status, error:_event.error}); | |
}; | |
request.onload = function(_event){ | |
var qualities = {}; | |
var response = this.responseText; | |
var args = getURLArgs(response); | |
if (!args.hasOwnProperty('url_encoded_fmt_stream_map')) | |
{ | |
if (_callbackError) | |
_callbackError(response); | |
} | |
else | |
{ | |
var fmtstring = args['url_encoded_fmt_stream_map']; | |
var fmtarray = fmtstring.split(','); | |
for(var i=0,j=fmtarray.length; i<j; i++){ | |
var args2 = getURLArgs(fmtarray[i]); | |
var type = decodeURIComponent(args2['type']); | |
if (type.indexOf('mp4') >= 0) | |
{ | |
var url = decodeURIComponent(args2['url']); | |
var quality = decodeURIComponent(args2['quality']); | |
qualities[quality] = url; | |
} | |
} | |
if (_callbackOk) | |
_callbackOk(qualities); | |
} | |
} | |
request.send(); | |
} | |
} | |
function getURLArgs(_string) { | |
var args = {}; | |
var pairs = _string.split("&"); | |
for(var i = 0; i < pairs.length; i++) { | |
var pos = pairs[i].indexOf('='); | |
if (pos == -1) continue; | |
var argname = pairs[i].substring(0,pos); | |
var value = pairs[i].substring(pos+1); | |
args[argname] = unescape(value); | |
} | |
return args; | |
} | |
return self; | |
} | |
module.exports = Youtube; |
Usage:
var youtube = new Youtube();
youtube.h264videosWithYoutubeURL(textField.value, function(e) {
videoPlayer.url = e.medium;
}, function(e) {
alert(JSON.stringify(e));
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
var youtube = new Youtube();
youtube.h264videosWithYoutubeURL(textField.value, function(e) {
videoPlayer.url = e.medium;
}, function(e) {
alert(JSON.stringify(e));
});