homepage Welcome to WebmasterWorld Guest from 23.22.59.252
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Itag problem in Youtube app
joe45



 
Msg#: 4503368 posted 9:40 am on Oct 3, 2012 (gmt 0)

Hi guys, I'm trying to fix a simple Javascript app for Smart TVs that grabs Youtube videos by making a call to get_video_info which returns URLs to various formats of the videos.

The app worked fine until last week when Youtube made some changes and now a signature parameter is required in the URLs. Because of that no videos would play anymore, but I addressed the issue by adding this to the original code

url = params['url_encoded_fmt_stream_map'].replace("url=","");
url = parser(url, '&url=', ',itag');
url = url.replace("&sig", "&signature");


And while this worked, it somehow screwed things up in terms of the formats of the videos that are fetched.

The thing is I had modified the original code to make 720p (itag/fmt=22) the default resolution for all videos, because where I'm currently living the connection is not fast enough to stream full HD stuff smoothly. I did this by removing all references to itags 46 and 37 (1080p) and only leaving in this part ("if (videoinfo[0] == '22'... [see below]) did the trick.

But now after fixing the signature issue all full HD videos are played in 1080p regardless of the rules set in the js code and I haven't been able to figure out why. Can any of you set me on the right path to resolving this issue? Thanks in advance for any suggestions.
Here's the relevant part of the code:

{

xhr=new XMLHttpRequest();
var param_array = new Array('&el=detailpage');
var txtDoc = "";
for(var i = 0; i < param_array.length; ++i)
{
var param = param_array[i];

var send_to_url = 'http://www.youtube.com/get_video_info?&video_id=' + videoID + param + '&ps=default&eurl=&gl=US&hl=en';
xhr.open("GET", send_to_url, false);
xhr.setRequestHeader("Accept-Encoding", "identity");
xhr.setRequestHeader("Accept-Language", "en-us,en;q=0.5");
xhr.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
xhr.setRequestHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.6) Gecko/20100627 Firefox/3.6.6");
xhr.setRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
xhr.setRequestHeader("Connection", "close");

xhr.send();

txtDoc=xhr.responseText;
if(txtDoc.indexOf('fmt_url_map'))
{
break;
}

if(txtDoc.indexOf('url_encoded_fmt_stream_map'))
{
break;
}
}

xhr.destroy();

var paare = txtDoc.split("&");

var paar, name, wert;
var params=new Array();
for (var i = 0; i < paare.length; i++)
{
paar = paare[i].split("=");
name = paar[0];
wert = paar[1];
params[name] = unescape(wert);
}
var url ="";

if(params['url_encoded_fmt_stream_map'])
{

var test = params['url_encoded_fmt_stream_map'].split(",url=");

for (var index = 0; index < test.length; index++)
{
var videoinfo = test[index].split("|");

if (videoinfo[0] == '22')
{
url = videoinfo[1];
alert(videoinfo[0]);
alert("HD 720p");
break;
}
}
}

url = params['url_encoded_fmt_stream_map'].replace("url=","");
url = parser(url, '&url=', ',itag');
url = url.replace("&sig", "&signature");

if(params['url_encoded_fmt_stream_map'])
{

var test = params['url_encoded_fmt_stream_map'].split(",url=");

for (var index = 0; index < test.length; index++)
{

var need_help = test[index].indexOf(',itag');
if(need_help>0)
{
var helper = test[index].substr(0,need_help);
alert('helper' + helper)
test[index] = helper
}

var videoinfo = test[index].split("&itag=");
videoinfo[0] = videoinfo[0].replace("url=","");

if (videoinfo[1] == '22')
{
url = videoinfo[0];
mode = "HD 720p"
break;
}
if (videoinfo[1] == '18')
{
url = videoinfo[0];
break;
}
if (videoinfo[1] == '5')
{
url = videoinfo[0];
break;
}
}
}


url = url.replace(/\%3A/g,":");
url = url.replace(/\%2F/g,"/");
url = url.replace(/\%3F/g,"?");
url = url.replace(/\%3D/g,"=");
url = url.replace(/\%252C/g,",");
url = url.replace(/\%26/g,"&");
url = url.replace(/\%253/g,":");

return url;
}

 

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved