2011-10-04 03:11:56 -04:00
|
|
|
<?php
|
|
|
|
//***************************************************************************//
|
2011-10-04 05:18:19 -04:00
|
|
|
// index_data.php - schedule on the system:
|
|
|
|
// * * * * * php cronjobs/index_data.php >/dev/null 2>&1
|
2011-10-04 03:11:56 -04:00
|
|
|
// Collect data from various services and store locally for later display.
|
|
|
|
//***************************************************************************//
|
2011-10-15 07:16:54 -04:00
|
|
|
$startTime = time();
|
|
|
|
$interruptedExecution = false;
|
|
|
|
register_shutdown_function('shutdown');
|
2011-10-15 09:02:44 -04:00
|
|
|
// make sure our file_exist returns fresh data. Likely not necessary.
|
2011-10-15 07:16:54 -04:00
|
|
|
clearstatcache();
|
|
|
|
|
2011-10-14 05:04:21 -04:00
|
|
|
if(in_array($_SERVER['SERVER_NAME'],array('localhost','a.io'))) {
|
2011-10-15 07:59:07 -04:00
|
|
|
$DATABASE_FILE = getcwd() . '/../conf/database.conf';
|
2011-10-04 05:13:43 -04:00
|
|
|
$CACHE_FILE = '../data/cache.txt';
|
|
|
|
$DATA_FILE = '../data/index.txt';
|
2011-10-15 07:16:54 -04:00
|
|
|
$LOCK_FILE = getcwd() . '/../data/whoisandrew.lock';
|
2011-10-04 05:13:43 -04:00
|
|
|
} else {
|
|
|
|
chdir('/home/atomaka/data');
|
|
|
|
|
2011-10-15 07:59:07 -04:00
|
|
|
$DATABASE_FILE = '/home/atomaka/conf/database.conf';
|
2011-10-04 05:13:43 -04:00
|
|
|
$CACHE_FILE = 'cache.txt';
|
|
|
|
$DATA_FILE = 'index.txt';
|
2011-10-15 07:16:54 -04:00
|
|
|
// path changes in the shutdown() function so we need the full path
|
|
|
|
$LOCK_FILE = '/home/atomaka/data/whoisandrew.lock';
|
2011-10-04 05:13:43 -04:00
|
|
|
}
|
2011-10-04 03:11:56 -04:00
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// All the sources we intend on pulling data from with a corresponding
|
|
|
|
// cache lifetime.
|
2011-10-04 03:11:56 -04:00
|
|
|
$dataSources = array(
|
|
|
|
'twitter' => 300,
|
|
|
|
'github' => 300,
|
|
|
|
'hulu' => 600,
|
|
|
|
'lastfm' => 60,
|
|
|
|
'sc2ranks' => 43200,
|
|
|
|
'steam' => 3600,
|
2011-10-14 03:11:17 -04:00
|
|
|
'wow' => 43200,
|
2011-10-04 03:11:56 -04:00
|
|
|
);
|
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// Make sure that the script does not begin execution if it is already.
|
2011-10-15 07:16:54 -04:00
|
|
|
if(!file_exists($LOCK_FILE)) {
|
|
|
|
touch($LOCK_FILE);
|
|
|
|
} else {
|
|
|
|
$interruptedExecution = true;
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// In case our data files are not present
|
2011-10-14 01:18:16 -04:00
|
|
|
if(file_exists($CACHE_FILE)) {
|
|
|
|
$cacheData = json_decode(file_get_contents($CACHE_FILE),true);
|
|
|
|
} else {
|
|
|
|
$cacheData = array();
|
|
|
|
}
|
|
|
|
if(file_exists($DATA_FILE)) {
|
|
|
|
$sourceData = json_decode(file_get_contents($DATA_FILE),true);
|
|
|
|
} else {
|
|
|
|
$sourceData = array();
|
|
|
|
}
|
|
|
|
|
2011-10-04 03:11:56 -04:00
|
|
|
foreach($dataSources as $dataSource=>$refreshTime) {
|
|
|
|
// check last time the data was updated
|
2011-10-04 03:37:44 -04:00
|
|
|
$lastModified = (array_key_exists($dataSource, $cacheData)) ?
|
|
|
|
$cacheData[$dataSource] : 0;
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
// and see if we need to retrieve new information
|
|
|
|
if(time() - $lastModified > $refreshTime) {
|
2011-10-04 03:37:44 -04:00
|
|
|
$cacheData[$dataSource] = time();
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
$sourceData[$dataSource] = call_user_func($dataSource);
|
|
|
|
}
|
|
|
|
}
|
2011-10-04 05:13:43 -04:00
|
|
|
|
|
|
|
file_put_contents($CACHE_FILE,json_encode($cacheData));
|
|
|
|
file_put_contents($DATA_FILE,json_encode($sourceData));
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
//***************************************************************************//
|
|
|
|
// Data sources
|
|
|
|
//***************************************************************************//
|
|
|
|
function twitter() {
|
|
|
|
$url = 'http://www.twitter.com/statuses/user_timeline/atomaka.json?count=1';
|
|
|
|
$tweetInfo = json_decode(file_get_contents($url));
|
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// An empty result set currently (always?) means that the last post was
|
|
|
|
// retweeted.
|
2011-10-04 03:11:56 -04:00
|
|
|
if(empty($tweetInfo)) {
|
|
|
|
return array(
|
|
|
|
'text' => 'Last post was a retweet and cannot be listed.',
|
2011-10-04 05:28:55 -04:00
|
|
|
'time' => 0,
|
2011-10-04 03:11:56 -04:00
|
|
|
);
|
|
|
|
} else {
|
2011-10-11 00:43:34 -04:00
|
|
|
$tweet = urlify($tweetInfo[0]->text);
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'text' => $tweet,
|
2011-10-11 00:43:34 -04:00
|
|
|
'time' => strtotime($tweetInfo[0]->created_at),
|
2011-10-04 03:11:56 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function github() {
|
|
|
|
// get the last repo we worked on
|
|
|
|
$url = 'https://api.github.com/users/atomaka/repos';
|
|
|
|
$repos = json_decode(curl_request($url));
|
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// & 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
|
2011-10-04 03:11:56 -04:00
|
|
|
usort(&$repos,'github_sort');
|
|
|
|
|
|
|
|
// and then get the last commit to that repo
|
|
|
|
$url = sprintf('https://api.github.com/repos/atomaka/%s/commits',
|
|
|
|
$repos[0]->name);
|
|
|
|
$commits = json_decode(curl_request($url));
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'commit' => $commits[0]->commit->message,
|
|
|
|
'repo' => $repos[0]->name,
|
2011-10-04 05:28:55 -04:00
|
|
|
'url' => $repos[0]->html_url,
|
2011-10-04 03:11:56 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function lastfm() {
|
|
|
|
$url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=atomaka&limit=1&api_key=27ea07733c17562cf1fe512586954825';
|
2011-10-04 03:22:50 -04:00
|
|
|
$xml = simplexml_load_file($url);
|
2011-10-04 03:11:56 -04:00
|
|
|
$latestSong = $xml->recenttracks->track[0];
|
|
|
|
|
|
|
|
$cover = (is_array($latestSong->image)) ?
|
|
|
|
'img/lastfm/blank_album64.png' : (string)$latestSong->image[1];
|
2011-10-15 07:59:07 -04:00
|
|
|
$time = (isset($latestSong->attributes()->nowplaying) &&
|
|
|
|
(bool)$latestSong->attributes()->nowplaying) ?
|
2011-10-14 05:17:04 -04:00
|
|
|
0 : strtotime($latestSong->date . ' UTC');
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'song' => (string)$latestSong->name,
|
|
|
|
'artist' => (string)$latestSong->artist,
|
2011-10-14 05:17:04 -04:00
|
|
|
'time' => $time,
|
2011-10-04 03:11:56 -04:00
|
|
|
'url' => (string)$latestSong->url,
|
|
|
|
'cover' => $cover,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sc2ranks() {
|
|
|
|
$url = 'http://sc2ranks.com/api/base/teams/us/Gaffer$888.json?appKey=whoisandrew.com';
|
|
|
|
$profile = json_decode(file_get_contents($url));
|
|
|
|
|
|
|
|
// find the 1v1 team
|
|
|
|
foreach($profile->teams as $team) {
|
|
|
|
if($team->bracket == 1) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'league' => $team->league,
|
|
|
|
'division' => $team->division,
|
|
|
|
'rank' => $team->division_rank,
|
|
|
|
'points' => $team->points,
|
|
|
|
'wins' => $team->wins,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hulu() {
|
|
|
|
$url = 'http://www.hulu.com/feed/history/atomaka';
|
2011-10-04 03:22:50 -04:00
|
|
|
$xml = simplexml_load_file($url);
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
// data for last show
|
|
|
|
$lastShow = $xml->channel->item[0];
|
2011-10-04 05:28:55 -04:00
|
|
|
|
2011-10-04 03:11:56 -04:00
|
|
|
$title = explode(' - ', $lastShow->title);
|
2011-10-15 07:59:07 -04:00
|
|
|
preg_match('/<img src="(.*)" align="right"/',(string)$lastShow->description,
|
|
|
|
$thumb);
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
return array(
|
2011-10-16 04:15:48 -04:00
|
|
|
'series' => isset($title[2]) ? $title[0] : 'Not Available',
|
|
|
|
'title' => isset($title[2]) ? $title[2] : $title[0],
|
2011-10-04 05:28:55 -04:00
|
|
|
'time' => strtotime($lastShow->pubDate),
|
2011-10-04 03:11:56 -04:00
|
|
|
'url' => (string)$lastShow->link,
|
|
|
|
'thumb' => $thumb[1],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function steam() {
|
|
|
|
$url = 'http://steamcommunity.com/profiles/76561197993725971/?xml=1';
|
2011-10-04 03:22:50 -04:00
|
|
|
$xml = simplexml_load_file($url);
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
// find the most recently played games
|
|
|
|
$recentGames = array();
|
|
|
|
if(isset($xml->mostPlayedGames)) {
|
|
|
|
foreach($xml->mostPlayedGames->mostPlayedGame as $game) {
|
|
|
|
$recentGames[] = array(
|
|
|
|
'name' => (string)$game->gameName,
|
|
|
|
'link' => (string)$game->gameLink,
|
|
|
|
'hours' => (float)$game->hoursPlayed,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'hours' => (float)$xml->hoursPlayed2Wk,
|
|
|
|
'recent' => $recentGames,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-04 19:19:16 -04:00
|
|
|
function wow() {
|
2011-10-14 02:54:29 -04:00
|
|
|
$CLASSES = array(
|
|
|
|
6 => 'deathknight',
|
|
|
|
5 => 'priest',
|
|
|
|
11 => 'druid',
|
|
|
|
4 => 'rogue',
|
|
|
|
8 => 'mage',
|
|
|
|
7 => 'shaman',
|
|
|
|
1 => 'warrior',
|
|
|
|
9 => 'warlock',
|
|
|
|
3 => 'hunter',
|
|
|
|
);
|
2011-10-15 09:02:44 -04:00
|
|
|
|
2011-10-04 19:19:16 -04:00
|
|
|
$characters = array(
|
|
|
|
'Gaffer' => false,
|
|
|
|
'Getburnt' => false,
|
|
|
|
'Veincane' => false,
|
|
|
|
'Toppazz' => false,
|
|
|
|
'Toopro' => false,
|
|
|
|
'Levita' => false,
|
|
|
|
'Ttg' => false,
|
|
|
|
'Notgaffer' => false,
|
|
|
|
'Loveglove' => false,
|
|
|
|
);
|
|
|
|
$currentInstance = 25; // 25 = Firelands
|
|
|
|
|
|
|
|
// build our mutli curl request
|
|
|
|
$mh = curl_multi_init();
|
|
|
|
foreach($characters as $character=>$data) {
|
2011-10-14 02:54:29 -04:00
|
|
|
$url = sprintf('http://us.battle.net/api/wow/character/crushridge/%s?fields=progression,talents',
|
2011-10-04 19:19:16 -04:00
|
|
|
$character);
|
|
|
|
|
|
|
|
$characters[$character] = curl_prep($url);
|
|
|
|
curl_multi_add_handle($mh, $characters[$character]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute the multi curl request
|
|
|
|
$running = 0;
|
|
|
|
do {
|
|
|
|
curl_multi_exec($mh, $running);
|
|
|
|
} while($running > 0);
|
|
|
|
|
|
|
|
// and process the results
|
|
|
|
$characterData = array();
|
|
|
|
foreach($characters as $character=>$data) {
|
|
|
|
$json = json_decode(
|
|
|
|
curl_multi_getcontent($characters[$character])
|
|
|
|
);
|
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// merge heroic and normal ragnaros
|
2011-10-04 19:19:16 -04:00
|
|
|
$bosses = $json->progression->raids[$currentInstance]->bosses;
|
|
|
|
$bosses[6]->heroicKills = $bosses[7]->heroicKills;
|
|
|
|
unset($bosses[7]);
|
|
|
|
|
|
|
|
// find the boss with the lowest kills
|
|
|
|
$leastN = 1000;
|
|
|
|
$leastH = 1000;
|
|
|
|
foreach($bosses as $boss) {
|
2011-10-15 09:02:44 -04:00
|
|
|
// -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.
|
2011-10-14 02:54:29 -04:00
|
|
|
if($boss->normalKills == -1) $boss->normalKills = 0;
|
2011-10-15 07:59:07 -04:00
|
|
|
if(($boss->normalKills + $boss->heroicKills) < $leastN) {
|
|
|
|
$leastN = $boss->normalKills + $boss->heroicKills;
|
|
|
|
}
|
2011-10-04 19:19:16 -04:00
|
|
|
if($boss->heroicKills < $leastH) $leastH = $boss->heroicKills;
|
|
|
|
}
|
2011-10-14 02:54:29 -04:00
|
|
|
|
|
|
|
//find our active talent tree
|
|
|
|
foreach($json->talents as $talent) {
|
|
|
|
if(isset($talent->selected)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-10-04 19:19:16 -04:00
|
|
|
|
|
|
|
$characterData[$character] = array(
|
|
|
|
'name' => $character,
|
|
|
|
'level' => $json->level,
|
2011-10-14 02:54:29 -04:00
|
|
|
'class' => $CLASSES[$json->class],
|
|
|
|
'progression' => $leastH > 0 ? $leastH * 100 : $leastN,
|
|
|
|
'armory' => sprintf('http://us.battle.net/wow/en/character/crushridge/%s/advanced',$character),
|
|
|
|
'spec_icon' => $talent->icon,
|
|
|
|
'spec_name' => $talent->name,
|
2011-10-04 19:19:16 -04:00
|
|
|
);
|
|
|
|
}
|
2011-10-14 02:54:29 -04:00
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// & 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
|
2011-10-14 02:54:29 -04:00
|
|
|
usort(&$characterData,'progression_sort');
|
2011-10-04 19:19:16 -04:00
|
|
|
|
|
|
|
return $characterData;
|
|
|
|
}
|
|
|
|
|
2011-10-04 03:11:56 -04:00
|
|
|
|
|
|
|
//***************************************************************************//
|
|
|
|
// Helper functions
|
|
|
|
//***************************************************************************//
|
|
|
|
function curl_request($url) {
|
|
|
|
$curl = curl_init();
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
|
|
|
$contents = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
|
|
|
|
return $contents;
|
|
|
|
}
|
|
|
|
|
2011-10-04 19:19:16 -04:00
|
|
|
function curl_prep($url) {
|
|
|
|
$curl = curl_init();
|
|
|
|
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
|
|
|
return $curl;
|
|
|
|
}
|
|
|
|
|
2011-10-04 03:11:56 -04:00
|
|
|
function urlify($string) {
|
|
|
|
$pattern ="{\\b((https?|telnet|gopher|file|wais|ftp) : [\\w/\\#~:.?+=&%@!\\-]+?) (?= [.:?\\-]* (?:[^\\w/\\#~:.?+=&%@!\\-] |$) ) }x";
|
2011-10-15 09:02:44 -04:00
|
|
|
|
2011-10-04 03:11:56 -04:00
|
|
|
return preg_replace($pattern,"<a href=\"$1\">$1</a>", $string);
|
|
|
|
}
|
|
|
|
|
|
|
|
function github_sort($a, $b) {
|
|
|
|
return strtotime($a->pushed_at) < strtotime($b->pushed_at);
|
|
|
|
}
|
|
|
|
|
2011-10-14 02:54:29 -04:00
|
|
|
function progression_sort($a, $b) {
|
|
|
|
return $a['progression'] < $b['progression'];
|
|
|
|
}
|
|
|
|
|
2011-10-15 07:16:54 -04:00
|
|
|
function shutdown() {
|
2011-10-15 09:02:44 -04:00
|
|
|
// need to make the variables we need available
|
2011-10-15 07:59:07 -04:00
|
|
|
global $interruptedExecution, $startTime, $LOCK_FILE, $DATABASE_FILE;
|
|
|
|
|
|
|
|
$db_conf = json_decode(file_get_contents($DATABASE_FILE));
|
|
|
|
$db = mysqli_init();
|
|
|
|
$db->real_connect($db_conf->hostname,$db_conf->username,$db_conf->password,
|
|
|
|
$db_conf->database);
|
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// $interruptedExecution is true if our lock file still existed when the
|
|
|
|
// script began execution. true also implies that the lock file does not
|
|
|
|
// exist.
|
|
|
|
if($interruptedExecution) {
|
2011-10-15 08:27:08 -04:00
|
|
|
$query = "INSERT INTO wia_log (time,type,description) VALUES(NOW(),
|
2011-10-15 07:59:07 -04:00
|
|
|
'warning',
|
|
|
|
'The script attempted to run while another copy was already processing')";
|
|
|
|
$db->query($query);
|
2011-10-15 09:02:44 -04:00
|
|
|
} else {
|
|
|
|
unlink($LOCK_FILE);
|
2011-10-15 07:16:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$completionTime = time() - $startTime;
|
2011-10-15 07:59:07 -04:00
|
|
|
|
2011-10-15 09:02:44 -04:00
|
|
|
// If the script took longer to execute than the server allows and the server
|
|
|
|
// does not have an unlimited execution time
|
2011-10-15 08:27:08 -04:00
|
|
|
if($completionTime >= ini_get('max_execution_time') &&
|
|
|
|
ini_get('max_execution_time') != 0) {
|
|
|
|
$message = 'The script reached the maximum execution time: ' .
|
|
|
|
$completionTime;
|
|
|
|
$query = "INSERT INTO wia_log (time,type,description) VALUES(NOW(),
|
|
|
|
'warning','$message')";
|
2011-10-15 07:59:07 -04:00
|
|
|
$db->query($query);
|
|
|
|
}
|
2011-10-15 07:16:54 -04:00
|
|
|
}
|
|
|
|
|
2011-10-04 03:11:56 -04:00
|
|
|
?>
|