0

I'm trying to get, among other details, the title of a given video for a certain YouTube video. Because they're all probably rooted in the same problem, I'll just show a simplified example with just getting the title using the API. Here's what I have so far:

$.ajax({
    url: "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2&alt=json",
    dataType: "jsonp",
    success: function(data) {
        videoName = data.entry.title.$t;
        $('#video_name').text(videoName);
    }
});

For some videos, such as this one, this works perfectly. For others, such as this one, it doesn't grab that information ever.

Why does this not work? That second example is just one video that does not work with this. About 1/3 don't work it seems.

I'd really appreciate some insight on this.

EDIT How I'm getting videoID:

var videoID = getUrlVars()["v"];
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, 
    function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

The example video ends like so: ?v=J66NOwZegc4

EDIT2 So I've added the $.ajax function with what seems to be causing the problem instead:

$.ajax({
    url: "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2&alt=json",
    dataType: "jsonp",
    async: "false",
    success: function(data) {
        commentCount = data.entry.gd$comments.gd$feedLink.countHint;
        iterations = Math.floor(commentCount/50);
        remaining = commentCount - (iterations*50);
        videoDesc = data.entry.title.$t;
        $('#video_name').text(videoDesc);
        document.title = 'Watching "' + videoDesc + '" on YTRT';
        $('#shareLink').val('http://www.ytrealtime.com?v=' + videoID);
    }
});
Mark Lyons
  • 1,354
  • 6
  • 21
  • 57
  • 1
    works fine here for the 2 you provided http://jsfiddle.net/9e2TP/ Show other code that passes `videoID` to `$.ajax` – charlietfl Jun 23 '12 at 03:43
  • works perfectly as well http://jsfiddle.net/9e2TP/1/ – Viren Rajput Jun 23 '12 at 04:00
  • @VirendraRajput just realized that. If you look at my 2nd edit, you'll see what I've found. For whatever reason, that second example doesn't have that `data.entry.gd$comments.gd$feedLink.countHint` to show the number of comments. – Mark Lyons Jun 23 '12 at 04:04
  • you will have to check the API, see what is optional in response, and test if it exists or not. API should give you a full breakdown of response schema – charlietfl Jun 23 '12 at 04:11
  • The only thing I can find is talking about the `feedLink`, but even that doesn't exist in this response. – Mark Lyons Jun 23 '12 at 04:22

0 Answers0