Created
June 25, 2011 21:48
-
-
Save lasconic/1046934 to your computer and use it in GitHub Desktop.
Youtube resolver for tomahawk
This file contains 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 getSettings() | |
{ | |
var settings = new Object(); | |
settings.name = "Youtube Resolver"; | |
settings.weight = 50; | |
settings.timeout = 5; | |
settings.maxResults = 5; | |
return settings; | |
} | |
function resolve( qid, artist, album, track ){ | |
var validUrlRe = "^((?:https?://)?(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/(?:(?:v/)|(?:(?:watch(?:_popup)?(?:\.php)?)?(?:\\?|#!?)(?:.+&)?v=))))?([0-9A-Za-z_-]+)?"; | |
var properties = getSettings(); | |
if(artist !== "" ){ | |
query = encodeURIComponent(artist) + "+"; | |
} | |
if(track !== "" ){ | |
query += encodeURIComponent(track); | |
} | |
var apiQuery = "http://gdata.youtube.com/feeds/api/videos?q="+ query + "&v=2&alt=jsonc&max-results=" + properties.maxResults; | |
apiQuery = apiQuery.replace(/\%20/g,'\+'); | |
var myJsonObject = {}; | |
var httpRequest = new XMLHttpRequest(); | |
httpRequest.open('GET', apiQuery, false); | |
httpRequest.onreadystatechange = function(){ | |
if (httpRequest.readyState == 4 && httpRequest.status == 200){ | |
myJsonObject = JSON.parse(httpRequest.responseText); | |
} | |
} | |
httpRequest.send(null); | |
var syncRequest = function(videoUrl) { | |
var xmlHttpRequest = new XMLHttpRequest(); | |
xmlHttpRequest.open('GET', videoUrl, false); | |
xmlHttpRequest.send(null); | |
return xmlHttpRequest.responseText; | |
} | |
var decodeurl = function(url) { return unescape(url); /*.replace(/%2C/g, ",").replace(/%20/g, " ").replace(/%3A/g, ":");*/ }; | |
/*var parseVideoUrlFromYtPage = function(html) { | |
var magic = "fmt_stream_map="; | |
var magicFmt = "18"; | |
var magicLimit = "%7C"; | |
var pos = html.indexOf(magic) + magic.length; | |
html = html.slice(pos); | |
html = html.slice(html.indexOf(magicFmt + magicLimit) + (magic+magicLimit).length); | |
finalUrl = html.slice(0, html.indexOf(magicLimit) ); | |
return "http://" + decodeurl( finalUrl ) + "&format=xml"; | |
}*/ | |
var process = function(videoPageUrl) { | |
var re = new RegExp(validUrlRe); | |
var m = re.exec(videoPageUrl); | |
var videoId = ""; | |
if (m == null || m.length <3) { | |
//console.log("Invalid URL : " + videoPageUrl); | |
return videoId; | |
} else { | |
videoId = m[2]; | |
//console.log("Video Id : " + videoId); | |
} | |
var els = ['&el=embedded', '&el=detailpage', '&el=vevo', '']; | |
for (x in els) { | |
var video_info_url = "http://www.youtube.com/get_video_info?&video_id="+videoId+els[x]+"&ps=default&eurl=&gl=US&hl=en"; | |
var video_info_webpage = syncRequest(video_info_url); | |
var queryString = {}; | |
video_info_webpage.replace( | |
new RegExp("([^?=&]+)(=([^&]*))?", "g"), | |
function($0, $1, $2, $3) { queryString[$1] = $3; } | |
); | |
if (queryString['token']){ | |
break; | |
//console.log('token found'); | |
} | |
} | |
//var token = queryString['token']; | |
//console.log('token ' + token); | |
//console.log('format ' +queryString['fmt_url_map']); | |
var available_formats = ['38', '37', '22', '45', '35', '34', '43', '18', '6', '5', '17', '13']; | |
if(queryString['fmt_url_map'] !== 'undefined') { | |
var formats = unescape(queryString['fmt_url_map']).split(','); | |
for(x in formats){ | |
var f = formats[x].split('|'); | |
for(y in available_formats) { | |
if(available_formats[y] == f[0]){ | |
//console.log(f[0]+"----"+f[1]); | |
return f[1]; | |
} | |
} | |
} | |
} | |
} | |
var results = new Array(); | |
if (myJsonObject.data.totalItems > 0){ | |
for (i=0;i<myJsonObject.data.totalItems && i<properties.maxResults;i++){ | |
var url = process(myJsonObject.data.items[i].player['default']); | |
if (url !== 'undefined') { | |
var result = new Object(); | |
result.artist = artist; | |
result.track = track; | |
//result.year = ; | |
result.source = properties.name; | |
result.url = url; | |
result.mimetype = "video/h264"; | |
//result.extension = "flv"; | |
//result.bitrate = 128; | |
result.duration = myJsonObject.data.items[i].duration; | |
result.score = 1.00; | |
results.push(result); | |
} | |
} | |
} | |
var response = new Object(); | |
response.qid = qid; | |
response.results = results; | |
return response; | |
} | |
/*var res = resolve(0, 'adele', '', 'rolling in the deep'); | |
console.log(res.results[0].url); | |
phantom.exit();*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment