User:Majr/twitterFeed.js

$( function { 'use strict';

var $feeds = $( '.twitter-feed' ); var pre = 'twitter-feed-tweet-';

if ( $feeds.length ) { $feeds.each( function {       var element = $(this);        var $names = element.data( 'names' ).split( ' ' );        var $count = element.data( 'count' );        var out = {};        var arr = [];

$.each( $names, function {           var name = this;            arr.push( $.getJSON(                'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=1&screen_name=' + name + '&count=' + $count + '&callback=?',                function( resp ) {                    return out[name] = parseTweets( resp );                }            ) );        } ); $.when.apply( $, arr ).done( function {           element.html( order( out, $names ) );        } ); } ); } else {   return false; }

function order( resp, names ) { var out = ''; $.each( names, function {       out += resp[this];    } );

return out; }

function parseTweets( tweets ) { var out = ''; $.each( tweets, function {       var tweet = {};        tweet.name = this.user.screen_name;        tweet.fullName = this.user.name;        tweet.avatar = this.user.profile_image_url_https;        tweet.linkColour = this.user.profile_link_color;        tweet.date = this.created_at;        tweet.id = this.id_str;        tweet.text = this.text;        tweet.source = this.source;        tweet.urls = {};        tweet.mentions = {};        tweet.hashtags = {};        var iu = -1;        if ( this.entities.urls.length ) {            $.each ( this.entities.urls, function( iu ) { tweet.urls[iu] = {}; tweet.urls[iu].short = this.url; tweet.urls[iu].real = this.expanded_url; tweet.urls[iu].display = this.display_url; } );       }        if ( typeof this.entities.media === 'undefined' ) {            this.entities.media = '';        } else {            if ( this.entities.media.length ) {                iu++;                console.log(this.entities);                $.each ( this.entities.media, function( i ) { i = i + iu; tweet.urls[i] = {}; tweet.urls[i].short = this.url; tweet.urls[i].real = this.expanded_url; tweet.urls[i].display = this.display_url; } );           }        }        if ( this.entities.user_mentions.length ) {            $.each ( this.entities.user_mentions, function( i ) { tweet.mentions[i] = this.screen_name; } );       }        if ( this.entities.hashtags.length ) {            $.each ( this.entities.hashtags, function( i ) { tweet.mentions[i] = this.text; } );       }        out += displayTweet( tweet );    } ); return out; }

function displayTweet( tweet ) { var out = '\n' + '\t\n' + '\t\t ' + tweet.fullName + ' @ ' + tweet.name + ' \n' + '\t \n' + '\t' + tweet.text + ' \n' + '\t' + tweet.date + ' via ' + tweet.source + ' \n' + ' \n'; return out; }

} );