diff --git a/avatars.php b/avatars.php new file mode 100644 index 0000000..7f00af7 --- /dev/null +++ b/avatars.php @@ -0,0 +1,73 @@ + SEARCH_PREFIX . $query, + 'rpp' => RPP, + 'page' => $page + )); + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_VERBOSE, 1); + curl_setopt($ch, CURLOPT_NOBODY, 0); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_USERAGENT, 'movethewebforward.org'); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + + $response = curl_exec($ch); + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($status == 200) { + return json_decode($response); + } + return FALSE; +} + +function getAll($query) { + $page = 0; + $avatars = array(); + + do { + $results = search($query, ++$page); + + foreach ($results->results as $result) { + $avatars[$result->from_user] = $result->profile_image_url; + } + } + while (count($results->results) == RPP); + + return $avatars; +} + +$avatars = json_decode(file_get_contents('avatars.json')); + +foreach ($queries as $query) { + $avatars->$query = array_merge((array)$avatars->$query, getAll($query)); +} + +$fp = fopen('avatars.json', 'w'); +fwrite($fp, json_encode($avatars)); +fclose($fp); diff --git a/js/init.js b/js/init.js index ee00896..6601717 100644 --- a/js/init.js +++ b/js/init.js @@ -1,20 +1,27 @@ (function( $ ){ + $.getJSON('avatars.json') + .success(function(avatars) { + $.each(avatars, function(hashtag) { + var avatarsElem = $(".task[data-hashtag=" + hashtag + "] .pledges"); + for (var user in this) { + var image = $('', { src: this[user], title: user }); + var link = $('', { href: 'http://twitter.com/' + user }); + avatarsElem.append(link.append(image)); + } + }); + }); + Modernizr.load([{ test: window.JSON, - nope: 'js/libs/json2.min.js' - }, { - test: Modernizr.localstorage, - nope: 'js/libs/storage.js', + nope: 'js/libs/json2.min.js', complete: function() { $(".task") .hashTask({ message : "http://movethewebforward.org", editTweetText : "(edit this tweet as you wish. ♡)", linkSelector : function() { return this.find('.pledge') }, - avatarsSelector : function() { return this.find('.pledges') }, hashtag : function() { return this.data('hashtag') || '#movethewebforward' }, - searchPrefix : '(ivegotmybluebeanieonnowwhat.com OR movethewebforward.com OR movethewebforward.org) AND ' }); if (window.__twitterIntentHandler) return; diff --git a/js/plugins.js b/js/plugins.js index c27da2a..1411ab7 100644 --- a/js/plugins.js +++ b/js/plugins.js @@ -6,55 +6,11 @@ hashtag: undefined, message: 'oh yeah!', linkSelector: 'a', - avatarsSelector: 'div', - searchPrefix: '', editTweetText: "(edit this tweet as you wish. ♡)" }; var options = $.extend({}, defaults, o); - function cacheSet(key, value, expires) { - window.localStorage.setItem(key, JSON.stringify(value)); - if (expires) { - window.localStorage.setItem(key + '__expires', expires); - } - } - - function cacheGet(key) { - var value = window.localStorage.getItem(key); - var expires = window.localStorage.getItem(key + '__expires'); - if (expires && (+new Date) > expires) { - return undefined; - } - return JSON.parse(value); - } - - function cacheDel(key) { - window.localStorage.removeItem(key); - window.localStorage.removeItem(key + '__expires'); - } - - function twitterSearch(query, callback) { - if (options.searchPrefix) { - query = options.searchPrefix + query; - } - - var searchUrl = 'http://search.twitter.com/search.json?rpp=100&callback=?&q='; - var results = cacheGet(query); - if (results) { - callback(results); - } - else { - $.getJSON(searchUrl + encodeURIComponent(query), function(json) { - if (json.results.length) { - cacheSet(query, json, (+new Date) + 1000 * 60 * 60); - } - - callback(json); - }); - } - } - return this.each(function() { var $elem = $(this); @@ -63,11 +19,6 @@ ? options.linkSelector.call($elem) : $elem.find(options.linkSelector); - // The element that will have user avatars appended to it. - var avatarsElem = $.isFunction(options.avatarsSelector) - ? options.avatarsSelector.call($elem) - : $elem.find(options.avatarsSelector); - // The hashtag used to pre-fill twitter and to search twitter for users. var hashtag = $.isFunction(options.hashtag) ? options.hashtag.call($elem) @@ -86,40 +37,7 @@ // A URL that will pre-fill a twitter status message. var prefillUrl = 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(message + ' ' + hashtag + ' ' + editTweetText); - linkElem.attr('href', prefillUrl).click(function() { - cacheDel(options.searchPrefix + hashtag); - }); - - if (hashtag) { - twitterSearch(hashtag, function(json) { - // De-dupe. - var users = {}; - - $.each(json.results, function(i) { - if (this.from_user in users) { - return; - } - - var image = $('', { - src: this.profile_image_url, - title: this.from_user - }); - - var link = $('', { - href: 'http://twitter.com/' + this.from_user - }); - - avatarsElem.append( link.append( image ) ); - - users[this.from_user] = true; - }); - - if (json.results.length) { - avatarsElem.find('p').show(); - } - }); - } - + linkElem.attr('href', prefillUrl); }); };