Skip to content

Instantly share code, notes, and snippets.

@djave-co
Created May 22, 2014 10:11
Show Gist options
  • Save djave-co/b72b5acc2355f3b010db to your computer and use it in GitHub Desktop.
Save djave-co/b72b5acc2355f3b010db to your computer and use it in GitHub Desktop.
Check time since file was edited.
<?php
$filename = SITE_ROOT . '/public/layouts/programmatic/twitter.php';
if (file_exists($filename)) {
$minutes = 20;
// 60 seconds * 20 = 20 minutes
$time_to_check = 60 * $minutes;
// Current time (time() will tell you the time... in seconds) minus the time the file was modified = how old the file is
$time_since_creation = time() - filemtime($filename);
if($time_since_creation > $time_to_check){
// Do all the api stuff
$api = new OAuthDamnit(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET);
$opts = array(
'screen_name'=>'YSLBeautyUK',
'count'=>'5',
);
$raw = $api->get('https://api.twitter.com/1.1/favorites/list.json', $opts);
$response = json_decode($raw, true);
if(isset($_GET['debug'])){
echo "<!-- ";
print_r($response);
echo "-->";
}
$twitter_feed = "<ul class='twitter-tweets'>";
foreach($response as $tweet){
echo "<!-- Refreshed -->";
$tweet_text = $tweet['text'];
//Convert urls to <a> links
$tweet_text = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet_text);
//Convert hashtags to twitter searches in <a> links
$tweet_text = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet_text);
//Convert attags to twitter profiles in <a> links
$tweet_text = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet_text);
$twitter_feed .= "<li>$tweet_text</li>";
}
$twitter_feed .= "</ul>";
file_put_contents($filename, $twitter_feed);
}else{
// Don't refresh
}
}
echo "<!-- " . round($time_since_creation / 60) . " minutes old refresh at " . $minutes . " -->";
layout('programmatic/twitter.php');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment