Various adjustments along with several comment additions.

This commit is contained in:
Andrew Tomaka 2011-10-15 09:02:44 -04:00
parent 582a9dc062
commit 888ac1fdfe

View file

@ -7,6 +7,7 @@
$startTime = time(); $startTime = time();
$interruptedExecution = false; $interruptedExecution = false;
register_shutdown_function('shutdown'); register_shutdown_function('shutdown');
// make sure our file_exist returns fresh data. Likely not necessary.
clearstatcache(); clearstatcache();
if(in_array($_SERVER['SERVER_NAME'],array('localhost','a.io'))) { if(in_array($_SERVER['SERVER_NAME'],array('localhost','a.io'))) {
@ -24,8 +25,9 @@ if(in_array($_SERVER['SERVER_NAME'],array('localhost','a.io'))) {
$LOCK_FILE = '/home/atomaka/data/whoisandrew.lock'; $LOCK_FILE = '/home/atomaka/data/whoisandrew.lock';
} }
// All the sources we intend on pulling data from with a corresponding
// cache lifetime.
$dataSources = array( $dataSources = array(
//function, cache_duration
'twitter' => 300, 'twitter' => 300,
'github' => 300, 'github' => 300,
'hulu' => 600, 'hulu' => 600,
@ -35,6 +37,7 @@ $dataSources = array(
'wow' => 43200, 'wow' => 43200,
); );
// Make sure that the script does not begin execution if it is already.
if(!file_exists($LOCK_FILE)) { if(!file_exists($LOCK_FILE)) {
touch($LOCK_FILE); touch($LOCK_FILE);
} else { } else {
@ -42,6 +45,7 @@ if(!file_exists($LOCK_FILE)) {
exit(); exit();
} }
// In case our data files are not present
if(file_exists($CACHE_FILE)) { if(file_exists($CACHE_FILE)) {
$cacheData = json_decode(file_get_contents($CACHE_FILE),true); $cacheData = json_decode(file_get_contents($CACHE_FILE),true);
} else { } else {
@ -77,6 +81,8 @@ function twitter() {
$url = 'http://www.twitter.com/statuses/user_timeline/atomaka.json?count=1'; $url = 'http://www.twitter.com/statuses/user_timeline/atomaka.json?count=1';
$tweetInfo = json_decode(file_get_contents($url)); $tweetInfo = json_decode(file_get_contents($url));
// An empty result set currently (always?) means that the last post was
// retweeted.
if(empty($tweetInfo)) { if(empty($tweetInfo)) {
return array( return array(
'text' => 'Last post was a retweet and cannot be listed.', 'text' => 'Last post was a retweet and cannot be listed.',
@ -97,6 +103,9 @@ function github() {
$url = 'https://api.github.com/users/atomaka/repos'; $url = 'https://api.github.com/users/atomaka/repos';
$repos = json_decode(curl_request($url)); $repos = json_decode(curl_request($url));
// & notation for a variable to be passed by reference is actually
// deprecated and will cause a warning in 5.3. However, it is
// required to work in 5.2
usort(&$repos,'github_sort'); usort(&$repos,'github_sort');
// and then get the last commit to that repo // and then get the last commit to that repo
@ -121,7 +130,6 @@ function lastfm() {
$time = (isset($latestSong->attributes()->nowplaying) && $time = (isset($latestSong->attributes()->nowplaying) &&
(bool)$latestSong->attributes()->nowplaying) ? (bool)$latestSong->attributes()->nowplaying) ?
0 : strtotime($latestSong->date . ' UTC'); 0 : strtotime($latestSong->date . ' UTC');
return array( return array(
'song' => (string)$latestSong->name, 'song' => (string)$latestSong->name,
@ -204,8 +212,7 @@ function wow() {
9 => 'warlock', 9 => 'warlock',
3 => 'hunter', 3 => 'hunter',
); );
$benchmark_start = time();
$characters = array( $characters = array(
'Gaffer' => false, 'Gaffer' => false,
'Getburnt' => false, 'Getburnt' => false,
@ -242,7 +249,7 @@ function wow() {
curl_multi_getcontent($characters[$character]) curl_multi_getcontent($characters[$character])
); );
// merge the two ragnaros bosses // merge heroic and normal ragnaros
$bosses = $json->progression->raids[$currentInstance]->bosses; $bosses = $json->progression->raids[$currentInstance]->bosses;
$bosses[6]->heroicKills = $bosses[7]->heroicKills; $bosses[6]->heroicKills = $bosses[7]->heroicKills;
unset($bosses[7]); unset($bosses[7]);
@ -251,6 +258,8 @@ function wow() {
$leastN = 1000; $leastN = 1000;
$leastH = 1000; $leastH = 1000;
foreach($bosses as $boss) { foreach($bosses as $boss) {
// -1 means that the boss has never but killed on normal, but
// has been on heroic so it's safe to reset to 0 for our purposes.
if($boss->normalKills == -1) $boss->normalKills = 0; if($boss->normalKills == -1) $boss->normalKills = 0;
if(($boss->normalKills + $boss->heroicKills) < $leastN) { if(($boss->normalKills + $boss->heroicKills) < $leastN) {
$leastN = $boss->normalKills + $boss->heroicKills; $leastN = $boss->normalKills + $boss->heroicKills;
@ -259,10 +268,8 @@ function wow() {
} }
//find our active talent tree //find our active talent tree
$spec = null;
foreach($json->talents as $talent) { foreach($json->talents as $talent) {
if(isset($talent->selected)) { if(isset($talent->selected)) {
$spec = $talent;
break; break;
} }
} }
@ -278,6 +285,9 @@ function wow() {
); );
} }
// & notation for a variable to be passed by reference is actually
// deprecated and will cause a warning in 5.3. However, it is
// required to work in 5.2
usort(&$characterData,'progression_sort'); usort(&$characterData,'progression_sort');
return $characterData; return $characterData;
@ -314,6 +324,7 @@ function curl_prep($url) {
function urlify($string) { function urlify($string) {
$pattern ="{\\b((https?|telnet|gopher|file|wais|ftp) : [\\w/\\#~:.?+=&%@!\\-]+?) (?= [.:?\\-]* (?:[^\\w/\\#~:.?+=&%@!\\-] |$) ) }x"; $pattern ="{\\b((https?|telnet|gopher|file|wais|ftp) : [\\w/\\#~:.?+=&%@!\\-]+?) (?= [.:?\\-]* (?:[^\\w/\\#~:.?+=&%@!\\-] |$) ) }x";
return preg_replace($pattern,"<a href=\"$1\">$1</a>", $string); return preg_replace($pattern,"<a href=\"$1\">$1</a>", $string);
} }
@ -326,6 +337,7 @@ function progression_sort($a, $b) {
} }
function shutdown() { function shutdown() {
// need to make the variables we need available
global $interruptedExecution, $startTime, $LOCK_FILE, $DATABASE_FILE; global $interruptedExecution, $startTime, $LOCK_FILE, $DATABASE_FILE;
$db_conf = json_decode(file_get_contents($DATABASE_FILE)); $db_conf = json_decode(file_get_contents($DATABASE_FILE));
@ -333,21 +345,24 @@ function shutdown() {
$db->real_connect($db_conf->hostname,$db_conf->username,$db_conf->password, $db->real_connect($db_conf->hostname,$db_conf->username,$db_conf->password,
$db_conf->database); $db_conf->database);
if(!$interruptedExecution) { // $interruptedExecution is true if our lock file still existed when the
unlink($LOCK_FILE); // script began execution. true also implies that the lock file does not
} else { // exist.
$errorTime = time(); if($interruptedExecution) {
$query = "INSERT INTO wia_log (time,type,description) VALUES(NOW(), $query = "INSERT INTO wia_log (time,type,description) VALUES(NOW(),
'warning', 'warning',
'The script attempted to run while another copy was already processing')"; 'The script attempted to run while another copy was already processing')";
$db->query($query); $db->query($query);
} else {
unlink($LOCK_FILE);
} }
$completionTime = time() - $startTime; $completionTime = time() - $startTime;
// If the script took longer to execute than the server allows and the server
// does not have an unlimited execution time
if($completionTime >= ini_get('max_execution_time') && if($completionTime >= ini_get('max_execution_time') &&
ini_get('max_execution_time') != 0) { ini_get('max_execution_time') != 0) {
$errorTime = time();
$message = 'The script reached the maximum execution time: ' . $message = 'The script reached the maximum execution time: ' .
$completionTime; $completionTime;
$query = "INSERT INTO wia_log (time,type,description) VALUES(NOW(), $query = "INSERT INTO wia_log (time,type,description) VALUES(NOW(),