Created
October 10, 2013 18:13
-
-
Save dawsontoth/6922935 to your computer and use it in GitHub Desktop.
Record to 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 win = Ti.UI.createWindow({ backgroundColor: '#000' }); | |
var progress = Ti.UI.createProgressBar({ max: 1, min: 0, value: 0, visible: true }); | |
var videoBlob, videoName = 'todo.mp4'; | |
// Get your own API key from the following URL: http://code.google.com/apis/youtube/dashboard/ | |
var yourApiKey = '<< YOUR API KEY GOES HERE >>'; | |
function checkToken() { | |
win.add(progress); | |
var sessionToken = Ti.App.Properties.getString('GoogleAuthSubSessionToken', ''); | |
if (sessionToken.length > 0) { | |
requestUpload(sessionToken); | |
} | |
else { | |
signInToGoogle(); | |
} | |
} | |
function signInToGoogle() { | |
var webView = Ti.UI.createWebView({ | |
url: 'https://www.google.com/accounts/AuthSubRequest?next=http%3A%2F%2Fwww.appcelerator.com&scope=http%3A%2F%2Fgdata.youtube.com&session=1&secure=0' | |
}); | |
webView.addEventListener('load', function() { | |
var url = webView.evalJS('window.location.href'); | |
Ti.API.info(url); | |
if (url.split('www.appcelerator.com').length > 1 && url.split('&token=').length > 1) { | |
win.remove(webView); | |
var token = url.split('&token=')[1]; | |
getSessionToken(token); | |
} | |
}); | |
win.add(webView); | |
} | |
function getSessionToken(token) { | |
var xhr = Ti.Network.createHTTPClient({ | |
ondatastream: function(e) { | |
progress.value = e.progress * 0.2; | |
}, | |
onload: function() { | |
var sessionToken = this.responseText.split('\n')[0].split('oken=')[1]; | |
Ti.App.Properties.setString('GoogleAuthSubSessionToken', sessionToken); | |
requestUpload(sessionToken); | |
}, | |
onerror: function(evt) { | |
alert(evt); | |
} | |
}); | |
xhr.open('GET', 'https://www.google.com/accounts/AuthSubSessionToken'); | |
xhr.setRequestHeader('Authorization', 'AuthSub token=' + token); | |
xhr.send(); | |
} | |
function requestUpload(sessionToken) { | |
var xhr = Ti.Network.createHTTPClient({ | |
ondatastream: function(e) { | |
progress.value = e.progress * 0.2 + 0.2; | |
}, | |
onload: function() { | |
var uploadURL = this.getResponseHeader('Location'); | |
uploadVideo(uploadURL); | |
}, | |
onerror: function(evt) { | |
alert(evt); | |
} | |
}); | |
xhr.open('POST', 'http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads'); | |
xhr.setRequestHeader('Authorization', 'AuthSub token=' + sessionToken); | |
xhr.setRequestHeader('GData-Version', '2'); | |
xhr.setRequestHeader('X-GData-Key', 'key=' + yourApiKey); | |
xhr.setRequestHeader('Slug', videoName); | |
xhr.setRequestHeader('Content-Length', '0'); | |
xhr.setRequestHeader('Content-Type', null); | |
xhr.send(); | |
} | |
function uploadVideo(url) { | |
var xhr = Ti.Network.createHTTPClient({ | |
onsendstream: function(e) { | |
progress.value = e.progress * 0.55 + 0.4; | |
}, | |
ondatastream: function(e) { | |
progress.value = e.progress * 0.05 + 0.95; | |
}, | |
onload: function() { | |
var videoID = this.responseText.split('<yt:videoid>')[1].split('</yt:videoid>')[0]; | |
var thumbnail = 'http://i.ytimg.com/vi/' + videoID + '/default.jpg'; | |
var url = 'http://www.youtube.com/v/' + videoID; | |
alert(videoID); | |
}, | |
onerror: function(evt) { | |
alert(evt); | |
} | |
}); | |
xhr.open('PUT', url); | |
xhr.send(videoBlob); | |
} | |
win.open(); | |
Ti.Media.showCamera({ | |
success:function(event) { | |
videoBlob = event.media; | |
checkToken(); | |
}, | |
error:function(error) { | |
var a = Titanium.UI.createAlertDialog({title:'Camera'}); | |
if (error.code == Titanium.Media.NO_CAMERA) | |
a.setMessage('This device does not have a video camera!'); | |
else | |
a.setMessage('Unexpected error: ' + error.code); | |
a.show(); | |
}, | |
saveToPhotoGallery: true, | |
allowEditing: true, | |
mediaTypes:[ Ti.Media.MEDIA_TYPE_VIDEO ] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment