1// JSON-P Twitter fetcher for Octopress 2// (c) Brandon Mathis // MIT License 3 4/* Sky Slavin, Ludopoli. MIT license. * based on JavaScript Pretty Date * Copyright (c) 2008 John Resig (jquery.com) * Licensed under the MIT license. */ 5function prettyDate(time) { 6 if (navigator.appName === 'Microsoft Internet Explorer') { 7 return "<span>∞</span>"; // because IE date parsing isn't fun. 8 } 9 var say = { 10 just_now: " now", 11 minute_ago: "1m", 12 minutes_ago: "m", 13 hour_ago: "1h", 14 hours_ago: "h", 15 yesterday: "1d", 16 days_ago: "d", 17 last_week: "1w", 18 weeks_ago: "w" 19 }; 20 21 var current_date = new Date(), 22 current_date_time = current_date.getTime(), 23 current_date_full = current_date_time + (1 * 60000), 24 date = new Date(time), 25 diff = ((current_date_full - date.getTime()) / 1000), 26 day_diff = Math.floor(diff / 86400); 27 28 if (isNaN(day_diff) || day_diff < 0) { return "<span>∞</span>"; } 29 30 return day_diff === 0 && ( 31 diff < 60 && say.just_now || 32 diff < 120 && say.minute_ago || 33 diff < 3600 && Math.floor(diff / 60) + say.minutes_ago || 34 diff < 7200 && say.hour_ago || 35 diff < 86400 && Math.floor(diff / 3600) + say.hours_ago) || 36 day_diff === 1 && say.yesterday || 37 day_diff < 7 && day_diff + say.days_ago || 38 day_diff === 7 && say.last_week || 39 day_diff > 7 && Math.ceil(day_diff / 7) + say.weeks_ago; 40} 41 42function linkifyTweet(text, url) { 43 // Linkify urls, usernames, hashtags 44 text = text.replace(/(https?:\/\/)([\w\-:;?&=+.%#\/]+)/gi, '<a href="$1$2">$2</a>') 45 .replace(/(^|\W)@(\w+)/g, '$1<a href="https://twitter.com/$2">@$2</a>') 46 .replace(/(^|\W)#(\w+)/g, '$1<a href="https://search.twitter.com/search?q=%23$2">#$2</a>'); 47 48 // Use twitter's api to replace t.co shortened urls with expanded ones. 49 for (var u in url) { 50 if(url[u].expanded_url != null){ 51 var shortUrl = new RegExp(url[u].url, 'g'); 52 text = text.replace(shortUrl, url[u].expanded_url); 53 var shortUrl = new RegExp(">"+(url[u].url.replace(/https?:\/\//, '')), 'g'); 54 text = text.replace(shortUrl, ">"+url[u].display_url); 55 } 56 } 57 return text 58} 59 60function showTwitterFeed(tweets, twitter_user) { 61 var timeline = document.getElementById('tweets'), 62 content = ''; 63 64 for (var t in tweets) { 65 content += '<li>'+'<p>'+'<a href="https://twitter.com/'+twitter_user+'/status/'+tweets[t].id_str+'">'+prettyDate(tweets[t].created_at)+'</a>'+linkifyTweet(tweets[t].text.replace(/\n/g, '<br>'), tweets[t].entities.urls)+'</p>'+'</li>'; 66 } 67 timeline.innerHTML = content; 68} 69 70function getTwitterFeed(user, count, replies) { 71 count = parseInt(count, 10); 72 $.ajax({ 73 url: "https://api.twitter.com/1/statuses/user_timeline/" + user + ".json?trim_user=true&count=" + (count + 20) + "&include_entities=1&exclude_replies=" + (replies ? "0" : "1") + "&callback=?" 74 , type: 'jsonp' 75 , error: function (err) { $('#tweets li.loading').addClass('error').text("Twitter's busted"); } 76 , success: function(data) { showTwitterFeed(data.slice(0, count), user); } 77 }) 78}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.