User:Majr/twitterFeed.js

$( function { 'use strict';

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

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

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

function parseTweets( tweets ) { var out = '';

$.each( tweets, function {       var t = {}, iu = -1;        t.urls = {};        t.mentions = {};        t.hashtags = {};        t.name = this.user.screen_name;        t.fullName = this.user.name;        t.avatar = this.user.profile_image_url_https;        t.linkColour = this.user.profile_link_color;        t.date = this.created_at;        t.id = this.id_str;        t.text = this.text;        t.source = this.source;        if ( this.entities.urls.length ) {            $.each ( this.entities.urls, function( iu ) { t.urls[iu] = {}; t.urls[iu].short = this.url; t.urls[iu].real = this.expanded_url; t.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; t.urls[i] = {}; t.urls[i].short = this.url; t.urls[i].real = this.expanded_url; t.urls[i].display = this.display_url; } );           }        }        if ( this.entities.user_mentions.length ) {            $.each ( this.entities.user_mentions, function( i ) { t.mentions[i] = this.screen_name; } );       }        if ( this.entities.hashtags.length ) {            $.each ( this.entities.hashtags, function( i ) { t.mentions[i] = this.text; } );       }        out += styleTweet( t );    } ); return out; }

function styleTweet( tweet ) { var p = 'twitter-feed-tweet-', out =

'\t\n' + '\t\t\n' + '\t\t\t ' + tweet.fullName + ' @ ' + tweet.name + ' \n' + '\t\t \n' + '\t\t' + tweet.text + ' \n' + '\t\t' + tweet.date + ' via ' + tweet.source + ' \n' + '\t \n'; return out; }

} );