Yusaf Khaliq
Yusaf Khaliq

Reputation: 3393

Check if youtube video embedding is allowed using jQuery

I am trying to show info on a YouTube video and one bit of info will be to see if the video is embeddable i am trying to use yt$accessControl but i don't know how too return the permission {"action":"embed","permission":"allowed"} http://jsfiddle.net/YourBlogspot/eWJWZ/29/

function getYouTubeInfo() {
                $.ajax({
                        url: "http://gdata.youtube.com/feeds/api/videos/VA770wpLX-Q?v=2&alt=json",
                        dataType: "jsonp",
                        success: function (data) {parseresults(data)}
                });
        }

        function parseresults(data) {
                var title = data.entry.title.$t;
                var description = data.entry.media$group.media$description.$t;
                var viewcount = data.entry.yt$statistics.viewCount;
                var author = data.entry.author[0].name.$t;
                var embedallow = data.entry.yt$statistics.action.embed.permission;
                $('#title').html(title);
                $('#description').html('<b>Description</b>: ' + description);
                $('#extrainfo').html('<b>Author</b>: ' + author + '<br/><b>Views</b>: ' + viewcount);
            $('#embeddallowed').html('<b>allowed</b>: ' + embedallow + '<br/>');
        }

$(document).ready(function () {
        getYouTubeInfo();
});

<div>
                <br/><br/>
                <div id="title" style="color: #dddddd">Could not find a title</div><br/>

                <br/><br/>
                <div id="description">Could not find a description</div>
                <div id="extrainfo">Could not find extra information</div>
    <div id="embeddallowed">Could not find extra information</div>
        </div>

Upvotes: 1

Views: 1052

Answers (2)

Mat_
Mat_

Reputation: 61

I haven't checked your code, but if i suppose that it's right, you have to modify like that:

var result = function(myData){
    var isEmbeddable = null;
    $.ajax({
        url: "http://gdata.youtube.com/feeds/api/videos/VA770wpLX-Q?v=2&alt=json",
        dataType: "jsonp",
        async: false,
        success: function (data) { isEmbeddable = parseresults(data) }
        });

    return isEmbeddable;
}

function parseresults(data) {
    var title = data.entry.title.$t;
    var description = data.entry.media$group.media$description.$t;
    var viewcount = data.entry.yt$statistics.viewCount;
    var author = data.entry.author[0].name.$t;
    var embedallow = data.entry.yt$accessControl[5].permission;
    $('#title').html(title);
    $('#description').html('<b>Description</b>: ' + description);
    $('#extrainfo').html('<b>Author</b>: ' + author + '<br/><b>Views</b>: ' + viewcount);
    $('#embeddallowed').html('<b>allowed</b>: ' + embedallow + '<br/>');

    return embedallow;
}

It should return the value of the permission that you seek.

By the way, i've change the path to the embed value, it is: var embedallow = data.entry.yt$accessControl[5].permission;

Test it, and tell me if that works for you !

Upvotes: 0

acanimal
acanimal

Reputation: 5020

Take a look to the jsfiddle: http://jsfiddle.net/eWJWZ/35/

Make the request using 'alt=jsonc' and for dataType set it to JSON. JSONC format is very easy to use.

        function getYouTubeInfo() {
                $.ajax({
                        url: "http://gdata.youtube.com/feeds/api/videos/VA770wpLX-Q?v=2&alt=jsonc",
                        dataType: "json",
                        success: function (data) {parseresults(data)}
                });
        }

        function parseresults(result) {
                console.log(result);
                var title = result.data.title;
                var description = result.data.description;
                var viewcount = result.data.viewCount;
                var author = result.data.uploader;
                var embedallow = result.data.accessControl.embed;
                $('#title').html(title);
                $('#description').html('<b>Description</b>: ' + description);
                $('#extrainfo').html('<b>Author</b>: ' + author + '<br/><b>Views</b>: ' + viewcount);
            $('#embeddallowed').html('<b>allowed</b>: ' + embedallow + '<br/>');
        }

$(document).ready(function () {
        getYouTubeInfo();
});

Upvotes: 2

Related Questions