Test Z Wiki
Advertisement

//

/**
 * YouTube.js
 *
 * Fetches data of YouTube channels
 * Needs to be used along with [[Template:YouTube]]
 * @todo Subscribe button maybe?
 *
 * @author sqm
 */

$(function() {
    'use strict';
    if (!$('.youtube').length) return;
    $('.youtube').each(function() {
        var $this = $(this),
            channel = $this.data('channel');
        $.when(
            $.get('http://gdata.youtube.com/feeds/api/users/' + encodeURIComponent(channel) + '/uploads?alt=json'),
            $.get('http://gdata.youtube.com/feeds/api/users/' + encodeURIComponent(channel) + '?v=2&fields=yt:statistics,gd:feedLink&alt=json')
        ).then(function(a, b) {
            function formatNum(n) { // Original source: http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
                return n.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
            }
            $this
                .find('.channel').html(a[0].feed.author[0].name.$t).end()
                .find('.subscribers').html(formatNum(b[0].entry.yt$statistics.subscriberCount)).end()
                .find('.total-views').html(formatNum(b[0].entry.yt$statistics.totalUploadViews)).end()
                .find('.video').html('<iframe src="http://youtube.com/embed/' + a[0].feed.entry[0].id.$t.substr(42) + '" height="100%" width="100%" allowfullscreen></iframe>').end()
                .find('.title').html(a[0].feed.entry[0].title.$t).end()
                .find('.length').html(function() { // Original source: http://stackoverflow.com/questions/1322732/convert-seconds-to-hh-mm-ss-with-javascript
                    var ts = a[0].feed.entry[0].media$group.yt$duration.seconds,
                        h = Math.floor(ts/3600),
                        m = Math.floor((ts - (h*3600))/60),
                        s = ts - (h*3600) - (m*60);
                    s = Math.round(s*100)/100;
                    var str = '';
                    if (h > 0) str += (h < 10 ? '0' + h : h) + ':';
                        str += (m < 10 ? '0' + m : m);
                        str += ':' + (s < 10 ? '0' + s : s);
                    return str;
                }).end()
                .find('.views').html(formatNum(a[0].feed.entry[0].yt$statistics.viewCount)).end()
                .find('.published').html(new Date(a[0].feed.entry[0].published.$t).toUTCString().replace(/ GMT/g, '')).end()
                .find('.description').html(function() {
                    var str = a[0].feed.entry[0].content.$t;
                    if (str.length > 200) str = str.substr(0, 200) + '...<sup><span style="font-size:small" title="' + str.substr(200, str.length) + '">?</span></sup>';
                    return str;
                }).end()
                .find('.rating').html(a[0].feed.entry[0].gd$rating.average + ' (' + formatNum(a[0].feed.entry[0].gd$rating.numRaters) + ' votes)').end()
        }, function(e) {
            $this.html('<td style="text-align:center">' + channel + ' doesn\'t exist on YouTube.</td>');
        });
    });
});
//
Advertisement