User:Majr/twitterFeed.js

$( function { 'use strict';

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

$feeds.empty.removeClass( 'twitter-success twitter-failure' );

if ( $feeds.length ) { $feeds.each( function {       var $element = $(this), $count = 1, feed = {}, promise = [], out = '';        var $names = $element.data( 'names' ).split( ' ' );        if ( $element.data( 'count' ) !== undefined ) {            $count = $element.data( 'count' );        }        $.each( $names, function { var name = this; promise.push(               $.ajax( { url: 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=1&screen_name=' + name + '&count=' + $count, dataType: 'jsonp', timeout: 5000, success: function( resp ) { return feed[name] = parseTweets( resp ); }               } )            );        } );        $.when.apply( $, promise ).then( function { $.each( $names, function {               out += feed[this];            } ); $element.addClass( 'twitter-success' ).html( out ); }, function { $element.addClass( 'twitter-failure' ).html( ' Failed to connect to Twitter ' ); } );   } ); }

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

$.each( tweets, function {       var t = {}, iu = -1, re = '';        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.id = this.id_str;        t.source = this.source.replace( 'rel="nofollow"', 'class="' + p + 'ext-link" rel="nofollow"' );        t.date = this.created_at.replace( /\w+ (\w+) 0?(\d+) 0?(\d+):(\d+):\d{2} \+\d+ \d{2}(\d+)/, function( $, $1, $2, $3, $4, $5 ) { var m = ' AM - '; if ( $3 >= 12 ) { m = ' PM - '; if ( $3 > 12 ) { $3 = $3 - 12;                   }                } else if ( $3 == 0 ) { $3 = 12;               }                return $3 + ':' + $4 + m + [$2, $1, $5].join(' '); }       );        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 ( this.entities.media !== undefined && this.entities.media.length ) {            iu++;            $.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.hashtags[i] = this.text; } );       }        t.text = this.text;        if ( !$.isEmptyObject( t.urls ) ) {                $.each ( t.urls, function { t.text = t.text.replace( this.short, '' + this.display + '' ); } );       }        if ( !$.isEmptyObject( t.mentions ) ) {                $.each ( t.mentions, function { re = new RegExp( '@' + this, 'i' ); t.text = t.text.replace( re, '@ ' + this + ' ' ); } );       }        if ( !$.isEmptyObject( t.hashtags ) ) {                $.each ( t.hashtags, function { re = new RegExp( '#' + this, 'i' ); t.text = t.text.replace( re, '# ' + this + ' ' ); } );       }        out += styleTweet( t );    } ); return out; }

function styleTweet( tweet ) { var 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; }

} );