Warrior renamed.

This commit is contained in:
Andrew Tomaka 2011-12-04 03:44:06 -05:00
parent a6c7ecc721
commit 7bec443bc7

View file

@ -1,425 +1,425 @@
<?php <?php
//***************************************************************************// //***************************************************************************//
// index_data.php - schedule on the system: // index_data.php - schedule on the system:
// * * * * * php cronjobs/index_data.php >/dev/null 2>&1 // * * * * * php cronjobs/index_data.php >/dev/null 2>&1
// Collect data from various services and store locally for later display. // Collect data from various services and store locally for later display.
//***************************************************************************// //***************************************************************************//
$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. // make sure our file_exist returns fresh data. Likely not necessary.
clearstatcache(); clearstatcache();
if(isset($_SERVER['SERVER_NAME']) && in_array($_SERVER['SERVER_NAME'],array('localhost','a.io'))) { if(isset($_SERVER['SERVER_NAME']) && in_array($_SERVER['SERVER_NAME'],array('localhost','a.io'))) {
$conf = json_decode(file_get_contents(getcwd() . '/../conf/wia.conf')); $conf = json_decode(file_get_contents(getcwd() . '/../conf/wia.conf'));
} else { } else {
$conf = json_decode(file_get_contents('/home/atomaka/conf/wia.conf')); $conf = json_decode(file_get_contents('/home/atomaka/conf/wia.conf'));
} }
define('CACHE',$conf->site->path . '/data/cache.txt'); define('CACHE',$conf->site->path . '/data/cache.txt');
define('DATA',$conf->site->path . '/data/index.txt'); define('DATA',$conf->site->path . '/data/index.txt');
define('LOCK',$conf->site->path . '/data/whoisandrew.lock'); define('LOCK',$conf->site->path . '/data/whoisandrew.lock');
// All the sources we intend on pulling data from with a corresponding // All the sources we intend on pulling data from with a corresponding
// cache lifetime. // cache lifetime.
$dataSources = array( $dataSources = array(
'twitter' => 300, 'twitter' => 300,
'github' => 300, 'github' => 300,
'hulu' => 600, 'hulu' => 600,
'lastfm' => 60, 'lastfm' => 60,
// 'sc2ranks' => 43200, // 'sc2ranks' => 43200,
'steam' => 3600, 'steam' => 3600,
'wow' => 43200, 'wow' => 43200,
); );
// Make sure that the script does not begin execution if it is already. // Make sure that the script does not begin execution if it is already.
if(!file_exists(LOCK)) { if(!file_exists(LOCK)) {
touch(LOCK); touch(LOCK);
} else { } else {
$interruptedExecution = true; $interruptedExecution = true;
exit(); exit();
} }
// In case our data files are not present // In case our data files are not present
if(file_exists(CACHE)) { if(file_exists(CACHE)) {
$cacheData = json_decode(file_get_contents(CACHE),true); $cacheData = json_decode(file_get_contents(CACHE),true);
} else { } else {
$cacheData = array(); $cacheData = array();
} }
if(file_exists(DATA)) { if(file_exists(DATA)) {
$sourceData = json_decode(file_get_contents(DATA),true); $sourceData = json_decode(file_get_contents(DATA),true);
} else { } else {
$sourceData = array(); $sourceData = array();
} }
foreach($dataSources as $dataSource=>$refreshTime) { foreach($dataSources as $dataSource=>$refreshTime) {
// check last time the data was updated // check last time the data was updated
$lastModified = (array_key_exists($dataSource, $cacheData)) ? $lastModified = (array_key_exists($dataSource, $cacheData)) ?
$cacheData[$dataSource] : 0; $cacheData[$dataSource] : 0;
// and see if we need to retrieve new information // and see if we need to retrieve new information
if(time() - $lastModified > $refreshTime) { if(time() - $lastModified > $refreshTime) {
$cacheData[$dataSource] = time(); $cacheData[$dataSource] = time();
$data = call_user_func($dataSource); $data = call_user_func($dataSource);
if($data != false) { if($data != false) {
echo 'updating ' . $dataSource . '<br/>'; echo 'updating ' . $dataSource . '<br/>';
$sourceData[$dataSource] = $data; $sourceData[$dataSource] = $data;
} else { } else {
echo 'failed ' . $dataSource . '<br/>'; echo 'failed ' . $dataSource . '<br/>';
$cacheData[$dataSource] = 0; $cacheData[$dataSource] = 0;
} }
} }
} }
file_put_contents(CACHE,json_encode($cacheData)); file_put_contents(CACHE,json_encode($cacheData));
file_put_contents(DATA,json_encode($sourceData)); file_put_contents(DATA,json_encode($sourceData));
//***************************************************************************// //***************************************************************************//
// Data sources // Data sources
//***************************************************************************// //***************************************************************************//
function twitter() { function twitter() {
$url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=atomaka&count=1'; $url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=atomaka&count=1';
$tweetInfo = json_decode(curl_request($url)); $tweetInfo = json_decode(curl_request($url));
// An empty result set currently (always?) means that the last post was // An empty result set currently (always?) means that the last post was
// retweeted. // retweeted.
if(empty($tweetInfo)) { if(empty($tweetInfo)) {
$data = array( $data = array(
'text' => 'Last post was a retweet and cannot be listed.', 'text' => 'Last post was a retweet and cannot be listed.',
'time' => 0, 'time' => 0,
); );
} else { } else {
$tweet = urlify($tweetInfo[0]->text); $tweet = urlify($tweetInfo[0]->text);
$data = array( $data = array(
'text' => $tweet, 'text' => $tweet,
'time' => strtotime($tweetInfo[0]->created_at), 'time' => strtotime($tweetInfo[0]->created_at),
); );
} }
if(isset($data['text']) && isset($data['time'])) { if(isset($data['text']) && isset($data['time'])) {
return $data; return $data;
} else { } else {
return false; return false;
} }
} }
function github() { function github() {
// get the last repo we worked on // get the last repo we worked on
$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 // & notation for a variable to be passed by reference is actually
// deprecated and will cause a warning in 5.3. However, it is // deprecated and will cause a warning in 5.3. However, it is
// required to work in 5.2 // 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
$url = sprintf('https://api.github.com/repos/atomaka/%s/commits', $url = sprintf('https://api.github.com/repos/atomaka/%s/commits',
$repos[0]->name); $repos[0]->name);
$commits = json_decode(curl_request($url)); $commits = json_decode(curl_request($url));
$data = array( $data = array(
'commit' => $commits[0]->commit->message, 'commit' => $commits[0]->commit->message,
'repo' => $repos[0]->name, 'repo' => $repos[0]->name,
'url' => $repos[0]->html_url, 'url' => $repos[0]->html_url,
); );
if(isset($data['commit']) && isset($data['repo']) && isset($data['url'])) { if(isset($data['commit']) && isset($data['repo']) && isset($data['url'])) {
return $data; return $data;
} else { } else {
return false; return false;
} }
} }
function lastfm() { function lastfm() {
$url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=atomaka&limit=1&api_key=27ea07733c17562cf1fe512586954825'; $url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=atomaka&limit=1&api_key=27ea07733c17562cf1fe512586954825';
$xml = simplexml_load_file($url); $xml = simplexml_load_file($url);
$latestSong = $xml->recenttracks->track[0]; $latestSong = $xml->recenttracks->track[0];
$cover = (is_array($latestSong->image)) ? $cover = (is_array($latestSong->image)) ?
'img/lastfm/blank_album64.png' : (string)$latestSong->image[1]; 'img/lastfm/blank_album64.png' : (string)$latestSong->image[1];
$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');
$data = array( $data = array(
'song' => (string)$latestSong->name, 'song' => (string)$latestSong->name,
'artist' => (string)$latestSong->artist, 'artist' => (string)$latestSong->artist,
'time' => $time, 'time' => $time,
'url' => (string)$latestSong->url, 'url' => (string)$latestSong->url,
'cover' => $cover, 'cover' => $cover,
); );
if(isset($data['song']) && isset($data['artist']) && isset($data['time']) && isset($data['url']) && isset($data['cover'])) { if(isset($data['song']) && isset($data['artist']) && isset($data['time']) && isset($data['url']) && isset($data['cover'])) {
return $data; return $data;
} else { } else {
return false; return false;
} }
} }
function sc2ranks() { function sc2ranks() {
$url = 'http://sc2ranks.com/api/base/teams/us/Gaffer$888.json?appKey=whoisandrew.com'; $url = 'http://sc2ranks.com/api/base/teams/us/Gaffer$888.json?appKey=whoisandrew.com';
$profile = json_decode(file_get_contents($url)); $profile = json_decode(file_get_contents($url));
// find the 1v1 team // find the 1v1 team
foreach($profile->teams as $team) { foreach($profile->teams as $team) {
if($team->bracket == 1) break; if($team->bracket == 1) break;
} }
$data = array( $data = array(
'league' => $team->league, 'league' => $team->league,
'division' => $team->division, 'division' => $team->division,
'rank' => $team->division_rank, 'rank' => $team->division_rank,
'points' => $team->points, 'points' => $team->points,
'wins' => $team->wins, 'wins' => $team->wins,
); );
if(isset($data['league']) && isset($data['division']) && isset($data['rank']) && isset($data['points']) && isset($data['wins'])) { if(isset($data['league']) && isset($data['division']) && isset($data['rank']) && isset($data['points']) && isset($data['wins'])) {
return $data; return $data;
} else { } else {
return false; return false;
} }
} }
function hulu() { function hulu() {
$url = 'http://www.hulu.com/feed/history/atomaka'; $url = 'http://www.hulu.com/feed/history/atomaka';
$xml = simplexml_load_file($url); $xml = simplexml_load_file($url);
// data for last show // data for last show
$lastShow = $xml->channel->item[0]; $lastShow = $xml->channel->item[0];
$title = explode(' - ', $lastShow->title); $title = explode(' - ', $lastShow->title);
preg_match('/<img src="(.*)" align="right"/',(string)$lastShow->description, preg_match('/<img src="(.*)" align="right"/',(string)$lastShow->description,
$thumb); $thumb);
$data = array( $data = array(
'series' => isset($title[2]) ? $title[0] : 'Not Available', 'series' => isset($title[2]) ? $title[0] : 'Not Available',
'title' => isset($title[2]) ? $title[2] : $title[0], 'title' => isset($title[2]) ? $title[2] : $title[0],
'time' => strtotime($lastShow->pubDate), 'time' => strtotime($lastShow->pubDate),
'url' => (string)$lastShow->link, 'url' => (string)$lastShow->link,
'thumb' => $thumb[1], 'thumb' => $thumb[1],
); );
if(isset($data['series']) && isset($data['title']) && isset($data['time']) && isset($data['url']) && isset($data['thumb'])) { if(isset($data['series']) && isset($data['title']) && isset($data['time']) && isset($data['url']) && isset($data['thumb'])) {
return $data; return $data;
} else { } else {
return false; return false;
} }
} }
function steam() { function steam() {
$url = 'http://steamcommunity.com/profiles/76561197993725971/?xml=1'; $url = 'http://steamcommunity.com/profiles/76561197993725971/?xml=1';
$xml = simplexml_load_file($url); $xml = simplexml_load_file($url);
// find the most recently played games // find the most recently played games
$recentGames = array(); $recentGames = array();
if(isset($xml->mostPlayedGames)) { if(isset($xml->mostPlayedGames)) {
foreach($xml->mostPlayedGames->mostPlayedGame as $game) { foreach($xml->mostPlayedGames->mostPlayedGame as $game) {
$recentGames[] = array( $recentGames[] = array(
'name' => (string)$game->gameName, 'name' => (string)$game->gameName,
'link' => (string)$game->gameLink, 'link' => (string)$game->gameLink,
'hours' => (float)$game->hoursPlayed, 'hours' => (float)$game->hoursPlayed,
); );
} }
} }
$data = array( $data = array(
'hours' => (float)$xml->hoursPlayed2Wk, 'hours' => (float)$xml->hoursPlayed2Wk,
'recent' => $recentGames, 'recent' => $recentGames,
); );
if(isset($data['hours']) && isset($data['recent'])) { if(isset($data['hours']) && isset($data['recent'])) {
return $data; return $data;
} else { } else {
return false; return false;
} }
} }
function wow() { function wow() {
$CLASSES = array( $CLASSES = array(
6 => 'deathknight', 6 => 'deathknight',
5 => 'priest', 5 => 'priest',
11 => 'druid', 11 => 'druid',
4 => 'rogue', 4 => 'rogue',
8 => 'mage', 8 => 'mage',
7 => 'shaman', 7 => 'shaman',
1 => 'warrior', 1 => 'warrior',
9 => 'warlock', 9 => 'warlock',
3 => 'hunter', 3 => 'hunter',
); );
$characters = array( $characters = array(
'Gaffer' => false, 'Gaffer' => false,
'Getburnt' => false, 'Getburnt' => false,
'Veincane' => false, 'Veincane' => false,
'Toppazz' => false, 'Toppazz' => false,
'Toopro' => false, 'Toopro' => false,
'Levita' => false, 'Levita' => false,
'Ttg' => false, 'Trinikwan' => false,
'Notgaffer' => false, 'Notgaffer' => false,
'Loveglove' => false, 'Loveglove' => false,
); );
$currentInstance = 25; // 25 = Firelands $currentInstance = 25; // 25 = Firelands
// build our mutli curl request // build our mutli curl request
$mh = curl_multi_init(); $mh = curl_multi_init();
foreach($characters as $character=>$data) { foreach($characters as $character=>$data) {
$url = sprintf('http://us.battle.net/api/wow/character/crushridge/%s?fields=progression,talents', $url = sprintf('http://us.battle.net/api/wow/character/crushridge/%s?fields=progression,talents',
$character); $character);
$characters[$character] = curl_prep($url); $characters[$character] = curl_prep($url);
curl_multi_add_handle($mh, $characters[$character]); curl_multi_add_handle($mh, $characters[$character]);
} }
// execute the multi curl request // execute the multi curl request
$running = 0; $running = 0;
do { do {
curl_multi_exec($mh, $running); curl_multi_exec($mh, $running);
} while($running > 0); } while($running > 0);
// and process the results // and process the results
$characterData = array(); $characterData = array();
foreach($characters as $character=>$data) { foreach($characters as $character=>$data) {
$json = json_decode( $json = json_decode(
curl_multi_getcontent($characters[$character]) curl_multi_getcontent($characters[$character])
); );
// merge heroic and normal ragnaros // 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]);
// find the boss with the lowest kills // find the boss with the lowest kills
$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 // -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. // 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;
} }
if($boss->heroicKills < $leastH) $leastH = $boss->heroicKills; if($boss->heroicKills < $leastH) $leastH = $boss->heroicKills;
} }
//find our active talent tree //find our active talent tree
foreach($json->talents as $talent) { foreach($json->talents as $talent) {
if(isset($talent->selected)) { if(isset($talent->selected)) {
break; break;
} }
} }
$characterData[$character] = array( $characterData[$character] = array(
'name' => $character, 'name' => $character,
'level' => $json->level, 'level' => $json->level,
'class' => $CLASSES[$json->class], 'class' => $CLASSES[$json->class],
'progression' => $leastH > 0 ? $leastH * 100 : $leastN, 'progression' => $leastH > 0 ? $leastH * 100 : $leastN,
'armory' => sprintf('http://us.battle.net/wow/en/character/crushridge/%s/advanced',$character), 'armory' => sprintf('http://us.battle.net/wow/en/character/crushridge/%s/advanced',$character),
'spec_icon' => $talent->icon, 'spec_icon' => $talent->icon,
'spec_name' => $talent->name, 'spec_name' => $talent->name,
); );
} }
// & notation for a variable to be passed by reference is actually // & notation for a variable to be passed by reference is actually
// deprecated and will cause a warning in 5.3. However, it is // deprecated and will cause a warning in 5.3. However, it is
// required to work in 5.2 // required to work in 5.2
usort(&$characterData,'progression_sort'); usort(&$characterData,'progression_sort');
$data = $characterData; $data = $characterData;
foreach($data as $character) { foreach($data as $character) {
if(!isset($character['name'])) return false; if(!isset($character['name'])) return false;
if(!isset($character['level'])) return false; if(!isset($character['level'])) return false;
if(!isset($character['class'])) return false; if(!isset($character['class'])) return false;
if(!isset($character['progression'])) return false; if(!isset($character['progression'])) return false;
if(!isset($character['armory'])) return false; if(!isset($character['armory'])) return false;
if(!isset($character['spec_icon'])) return false; if(!isset($character['spec_icon'])) return false;
if(!isset($character['spec_name'])) return false; if(!isset($character['spec_name'])) return false;
} }
return $data; return $data;
} }
//***************************************************************************// //***************************************************************************//
// Helper functions // Helper functions
//***************************************************************************// //***************************************************************************//
function curl_request($url) { function curl_request($url) {
$curl = curl_init(); $curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$contents = curl_exec($curl); $contents = curl_exec($curl);
curl_close($curl); curl_close($curl);
return $contents; return $contents;
} }
function curl_prep($url) { function curl_prep($url) {
$curl = curl_init(); $curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip'); curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
return $curl; return $curl;
} }
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);
} }
function github_sort($a, $b) { function github_sort($a, $b) {
return strtotime($a->pushed_at) < strtotime($b->pushed_at); return strtotime($a->pushed_at) < strtotime($b->pushed_at);
} }
function progression_sort($a, $b) { function progression_sort($a, $b) {
return $a['progression'] < $b['progression']; return $a['progression'] < $b['progression'];
} }
function shutdown() { function shutdown() {
// need to make the variables we need available // need to make the variables we need available
global $interruptedExecution, $startTime, $conf; global $interruptedExecution, $startTime, $conf;
$db = mysqli_init(); $db = mysqli_init();
$db->real_connect($conf->db->hostname,$conf->db->username,$conf->db->password, $db->real_connect($conf->db->hostname,$conf->db->username,$conf->db->password,
$conf->db->database); $conf->db->database);
// $interruptedExecution is true if our lock file still existed when the // $interruptedExecution is true if our lock file still existed when the
// script began execution. true also implies that the lock file does not // script began execution. true also implies that the lock file does not
// exist. // exist.
if($interruptedExecution) { 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 { } else {
unlink(LOCK); unlink(LOCK);
} }
$completionTime = time() - $startTime; $completionTime = time() - $startTime;
// If the script took longer to execute than the server allows and the server // If the script took longer to execute than the server allows and the server
// does not have an unlimited execution time // 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) {
$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(),
'warning','$message')"; 'warning','$message')";
$db->query($query); $db->query($query);
} }
} }
?> ?>