Store time and not the time since string in the cached data file.

This commit is contained in:
Andrew Tomaka 2011-10-04 05:28:55 -04:00
parent 7708493ebb
commit db6a24cb96
2 changed files with 35 additions and 40 deletions

View file

@ -53,16 +53,14 @@ function twitter() {
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.',
'timeSince' => 0, 'time' => 0,
); );
} else { } else {
$tweet = urlify($tweetInfo->text); $tweet = urlify($tweetInfo->text);
$timeSince = ($tweetInfo->created_at != 0) ?
time_since(strtotime($tweetInfo->created_at)) : 0;
return array( return array(
'text' => $tweet, 'text' => $tweet,
'timeSince' => $timeSince, 'time' => strtotime($tweetInfo->created_at),
); );
} }
} }
@ -82,7 +80,7 @@ function github() {
return array( return array(
'commit' => $commits[0]->commit->message, 'commit' => $commits[0]->commit->message,
'repo' => $repos[0]->name, 'repo' => $repos[0]->name,
'url' => $commits[0]->commit->url, 'url' => $repos[0]->html_url,
); );
} }
@ -91,15 +89,13 @@ function lastfm() {
$xml = simplexml_load_file($url); $xml = simplexml_load_file($url);
$latestSong = $xml->recenttracks->track[0]; $latestSong = $xml->recenttracks->track[0];
$timeSince = ((bool)$latestSong->attributes()->nowplaying) ?
'Listening now' : time_since(strtotime($latestSong->date . ' UTC')) . ' ago';
$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];
return array( return array(
'song' => (string)$latestSong->name, 'song' => (string)$latestSong->name,
'artist' => (string)$latestSong->artist, 'artist' => (string)$latestSong->artist,
'timeSince' => $timeSince, 'time' => strtotime($latestSong->date . ' UTC'),
'url' => (string)$latestSong->url, 'url' => (string)$latestSong->url,
'cover' => $cover, 'cover' => $cover,
); );
@ -129,15 +125,14 @@ function hulu() {
// data for last show // data for last show
$lastShow = $xml->channel->item[0]; $lastShow = $xml->channel->item[0];
$timeSince = time_since(strtotime($lastShow->pubDate));
$title = explode(' - ', $lastShow->title); $title = explode(' - ', $lastShow->title);
preg_match('/<img src="(.*)" align="right"/',(string)$lastShow->description,$thumb); preg_match('/<img src="(.*)" align="right"/',(string)$lastShow->description,$thumb);
return array( return array(
'title' => $title[2], 'title' => $title[2],
'series' => $title[0], 'series' => $title[0],
'timeSince' => $timeSince, 'time' => strtotime($lastShow->pubDate),
'url' => (string)$lastShow->link, 'url' => (string)$lastShow->link,
'thumb' => $thumb[1], 'thumb' => $thumb[1],
); );
@ -182,31 +177,6 @@ function curl_request($url) {
return $contents; return $contents;
} }
function time_since($time) {
$periods = array(
'minute' => 60,
'hour' => 60 * 60,
'day' => 60 * 60 * 24,
'week' => 60 * 60 * 24 * 7,
'month' => 60 * 60 * 24 * 30,
'year' => 60 * 60 * 24 * 365,
);
$now = time();
$since = $now - $time;
$formatted_since = array($since,'seconds');
foreach($periods as $period => $seconds) {
$quotient = floor($since / $seconds);
if($quotient >= 1) $formatted_since = array($quotient,$period);
else break;
}
if($formatted_since[0] > 1) $formatted_since[1] .= 's';
return implode(' ',$formatted_since);
}
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);

View file

@ -38,7 +38,7 @@ $links = $db->query("SELECT id,url,text,status,released_date FROM wia_links WHER
<div id="twitter" class="box"> <div id="twitter" class="box">
<span class="right"><a href="http://www.twitter.com/atomaka"><img src="img/badges/twitter.png" class="icon" alt="Follow me on Twitter"/></a></span> <span class="right"><a href="http://www.twitter.com/atomaka"><img src="img/badges/twitter.png" class="icon" alt="Follow me on Twitter"/></a></span>
<span class="tweet"><?php echo $data->twitter->text ?></span><br/> <span class="tweet"><?php echo $data->twitter->text ?></span><br/>
<?php if($data->twitter->timeSince != 0) { echo $data->twitter->timeSince ?> ago <?php } ?> <?php if($data->twitter->time != 0) { echo time_since($data->twitter->time) ?> ago <?php } ?>
</div> </div>
<br/> <br/>
<h2>github</h2> <h2>github</h2>
@ -96,7 +96,7 @@ $links = $db->query("SELECT id,url,text,status,released_date FROM wia_links WHER
<span class="right"><a href="http://last.fm/user/atomaka"><img src="img/badges/lastfm.png" class="icon" alt="Last.fm" /></a></span> <span class="right"><a href="http://last.fm/user/atomaka"><img src="img/badges/lastfm.png" class="icon" alt="Last.fm" /></a></span>
<a href="<?php echo $data->lastfm->url ?>"><?php echo $data->lastfm->song ?></a><br/> <a href="<?php echo $data->lastfm->url ?>"><?php echo $data->lastfm->song ?></a><br/>
by <?php echo $data->lastfm->artist ?><br/><br/> by <?php echo $data->lastfm->artist ?><br/><br/>
<?php echo $data->lastfm->timeSince ?><br/> <?php echo time_since($data->lastfm->time) ?><br/>
</td> </td>
</tr> </tr>
</table> </table>
@ -111,7 +111,7 @@ $links = $db->query("SELECT id,url,text,status,released_date FROM wia_links WHER
<span class="right"><a href="http://www.hulu.com/profiles/atomaka"><img src="img/badges/hulu.png" class="icon" alt="Hulu" /></a></span> <span class="right"><a href="http://www.hulu.com/profiles/atomaka"><img src="img/badges/hulu.png" class="icon" alt="Hulu" /></a></span>
<a href="<?php echo $data->hulu->url ?>"><?php echo $data->hulu->series ?></a><br/> <a href="<?php echo $data->hulu->url ?>"><?php echo $data->hulu->series ?></a><br/>
from <?php echo $data->hulu->title ?><br/><br/> from <?php echo $data->hulu->title ?><br/><br/>
<?php echo $data->hulu->timeSince ?> ago<br/> <?php echo time_since($data->hulu->time) ?> ago<br/>
</td> </td>
</tr> </tr>
</table> </table>
@ -207,4 +207,29 @@ $(document).ready(function() {
</script> </script>
</body> </body>
</html> </html><?php
function time_since($time) {
$periods = array(
'minute' => 60,
'hour' => 60 * 60,
'day' => 60 * 60 * 24,
'week' => 60 * 60 * 24 * 7,
'month' => 60 * 60 * 24 * 30,
'year' => 60 * 60 * 24 * 365,
);
$now = time();
$since = $now - $time;
$formatted_since = array($since,'seconds');
foreach($periods as $period => $seconds) {
$quotient = floor($since / $seconds);
if($quotient >= 1) $formatted_since = array($quotient,$period);
else break;
}
if($formatted_since[0] > 1) $formatted_since[1] .= 's';
return implode(' ',$formatted_since);
}
?>