Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Commit

Permalink
Use a PHP script (needs to run as a cron job) to save twitter avatars…
Browse files Browse the repository at this point in the history
… to a json file. Update frontend to read from json file.
  • Loading branch information
pifantastic committed Dec 10, 2011
1 parent 7227e92 commit ba45f0d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 89 deletions.
73 changes: 73 additions & 0 deletions 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');

This comment has been minimized.

Copy link
@mathiasbynens

mathiasbynens Dec 11, 2011

Member

Why not just use file_write_contents()?

This comment has been minimized.

Copy link
@addyosmani

addyosmani Dec 11, 2011

Contributor

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

This comment has been minimized.

Copy link
@mathiasbynens

mathiasbynens Dec 11, 2011

Member

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

fwrite($fp, json_encode($avatars));
fclose($fp);
19 changes: 13 additions & 6 deletions js/init.js
@@ -1,20 +1,27 @@
(function( $ ){ (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([{ Modernizr.load([{
test: window.JSON, test: window.JSON,
nope: 'js/libs/json2.min.js' nope: 'js/libs/json2.min.js',
}, {
test: Modernizr.localstorage,
nope: 'js/libs/storage.js',
complete: function() { complete: function() {
$(".task") $(".task")
.hashTask({ .hashTask({
message : "http://movethewebforward.org", message : "http://movethewebforward.org",
editTweetText : "(edit this tweet as you wish. ♡)", editTweetText : "(edit this tweet as you wish. ♡)",
linkSelector : function() { return this.find('.pledge') }, linkSelector : function() { return this.find('.pledge') },
avatarsSelector : function() { return this.find('.pledges') },
hashtag : function() { return this.data('hashtag') || '#movethewebforward' }, hashtag : function() { return this.data('hashtag') || '#movethewebforward' },
searchPrefix : '(ivegotmybluebeanieonnowwhat.com OR movethewebforward.com OR movethewebforward.org) AND '
}); });


if (window.__twitterIntentHandler) return; if (window.__twitterIntentHandler) return;
Expand Down
84 changes: 1 addition & 83 deletions js/plugins.js
Expand Up @@ -6,55 +6,11 @@
hashtag: undefined, hashtag: undefined,
message: 'oh yeah!', message: 'oh yeah!',
linkSelector: 'a', linkSelector: 'a',
avatarsSelector: 'div',
searchPrefix: '',
editTweetText: "(edit this tweet as you wish. ♡)" editTweetText: "(edit this tweet as you wish. ♡)"
}; };


var options = $.extend({}, defaults, o); 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() { return this.each(function() {
var $elem = $(this); var $elem = $(this);


Expand All @@ -63,11 +19,6 @@
? options.linkSelector.call($elem) ? options.linkSelector.call($elem)
: $elem.find(options.linkSelector); : $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. // The hashtag used to pre-fill twitter and to search twitter for users.
var hashtag = $.isFunction(options.hashtag) var hashtag = $.isFunction(options.hashtag)
? options.hashtag.call($elem) ? options.hashtag.call($elem)
Expand All @@ -86,40 +37,7 @@
// A URL that will pre-fill a twitter status message. // A URL that will pre-fill a twitter status message.
var prefillUrl = 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(message + ' ' + hashtag + ' ' + editTweetText); var prefillUrl = 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(message + ' ' + hashtag + ' ' + editTweetText);


linkElem.attr('href', prefillUrl).click(function() { linkElem.attr('href', prefillUrl);
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();
}
});
}

}); });
}; };


Expand Down

0 comments on commit ba45f0d

Please sign in to comment.