Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use a PHP script (needs to run as a cron job) to save twitter avatars…

… to a json file. Update frontend to read from json file.
  • Loading branch information...
commit ba45f0dd84716379628176cb090931222a5c5c58 1 parent 7227e92
@pifantastic pifantastic authored
Showing with 87 additions and 89 deletions.
  1. +73 −0 avatars.php
  2. +13 −6 js/init.js
  3. +1 −83 js/plugins.js
View
73 avatars.php
@@ -0,0 +1,73 @@
+<?php
+
+error_reporting(E_ALL);
+ini_set('display_errors', '1');
+
+define('URL', 'http://search.twitter.com/search.json');
+define('SEARCH_PREFIX', '(ivegotmybluebeanieonnowwhat.com OR movethewebforward.com OR movethewebforward.org) AND ');
+define('RPP', 100);
+
+$queries = array(
+ "#learn",
+ "#ask4help",
+ "#helpothers",
+ "#feedback",
+ "#explore",
+ "#write",
+ "#filebugs",
+ "#hack"
+);
+
+if ($_SERVER['REMOTE_ADDR'] !== '127.0.0.1')
+ die('');
+
+function search($query, $page = 1) {
+ $url = URL . '?' . http_build_query(array(
+ 'q' => 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');
@mathiasbynens Owner

Why not just use file_write_contents()?

I'm guessing it's down to preference :) Isn't file_[put/get]_contents just a set of wrappers around the lower level methods?

@mathiasbynens Owner

No idea, but it sure is easier to type out ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+fwrite($fp, json_encode($avatars));
+fclose($fp);
View
19 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 = $('<img>', { src: this[user], title: user });
+ var link = $('<a/>', { 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;
View
84 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 = $('<img>', {
- src: this.profile_image_url,
- title: this.from_user
- });
-
- var link = $('<a/>', {
- 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);
});
};
@mathiasbynens

Why not just use file_write_contents()?

@addyosmani

I'm guessing it's down to preference :) Isn't file_[put/get]_contents just a set of wrappers around the lower level methods?

@mathiasbynens

No idea, but it sure is easier to type out ;)

Please sign in to comment.
Something went wrong with that request. Please try again.