Created
August 29, 2012 08:45
-
-
Save i-like-robots/3508681 to your computer and use it in GitHub Desktop.
Caching Tweets with tmhOAuth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Tweets | |
{ | |
/** | |
* Cached results as key => value array | |
*/ | |
private static $cache = array(); | |
/** | |
* Cached file directory | |
*/ | |
private static $cacheDir = '/cache/'; | |
/** | |
* Cache get | |
* @param string $name The name of a stored value | |
* @throws Exception If cached item not found | |
* @return any The cached value | |
*/ | |
private static function cacheGet($name) | |
{ | |
if ( array_key_exists($name, self::$cache) ) // undefined indexes do not throw an exception =S | |
{ | |
return self::$cache[$name]; | |
} | |
else | |
{ | |
throw new Exception('notfound'); | |
} | |
} | |
/** | |
* Cache set | |
* @param string $name Name of cached value | |
* @param any $value Value to cache | |
* @return any Returns the given value | |
*/ | |
private static function cacheSet($name, $value) | |
{ | |
return self::$cache[$name] = $value; | |
} | |
/** | |
* File cache path | |
* @param string $file File name | |
* @return string Fully qualified file path | |
*/ | |
private static function fileCachePath($file) | |
{ | |
return dirname( __FILE__ ) . self::$cacheDir . md5($file) . '.cache'; | |
} | |
/** | |
* File get | |
* @description Get cache file from file system | |
* @param string $file File name of cached value | |
* @param integer $expires Time to store cached file. Set to zero to get file regardless of last cached time. | |
* @throws Exception If cached item not found | |
* @throws Exception If cache exists but has expired | |
* @return any The cached value | |
*/ | |
private static function fileGet($file, $expires = 15) | |
{ | |
$file = self::fileCachePath($file); | |
if ( ! file_exists( $file ) ) | |
{ | |
throw new Exception('notfound'); | |
} | |
else | |
{ | |
if ( ! $expires || time() - filemtime($file) < $expires * 60 ) | |
{ | |
return unserialize( file_get_contents($file) ); | |
} | |
else | |
{ | |
throw new Exception('expired'); | |
} | |
} | |
} | |
/** | |
* File set | |
* @param string $file File name of cached value | |
* @param any $value Value to cache | |
* @return any Returns the given value | |
*/ | |
private static function fileSet($file, $value) | |
{ | |
$file = self::fileCachePath($file); | |
$cache = fopen($file, 'w'); | |
$write = fwrite($cache, serialize($value) ); | |
fclose($cache); | |
return $value; | |
} | |
/** | |
* Get latest tweets | |
* @param string $user Twitter Screen name | |
* @param integer $number Number of tweets to get | |
* @param integer $expire Time in minutes to cache result | |
* @return array Latest tweets | |
*/ | |
public static function tweets($user, $number = 10, $expire = 15) | |
{ | |
$tweets = array(); | |
$file = $user . '_' . $number; | |
try | |
{ | |
$tweets = self::fileGet($file, $expire); | |
} | |
catch(Exception $e) | |
{ | |
require_once 'lib/tmhOAuth/tmhOAuth.php'; | |
// Init OAuth | |
$tmhOAuth = new tmhOAuth( array( | |
'consumer_key' => TWITTER_CONSUMER_KEY, | |
'consumer_secret' => TWITTER_CONSUMER_SECRET, | |
'user_token' => TWITTER_USER_TOKEN, | |
'user_secret' => TWITTER_USER_SECRET | |
) ); | |
$method = "https://api.twitter.com/1/statuses/user_timeline.json"; | |
$params = array( | |
'screen_name' => $user, | |
'count' => $number, | |
'include_rts' => false, | |
'include_entities' => true | |
); | |
// Parse result if successful | |
if ( $tmhOAuth->request('GET', $method, $params) == 200 ) | |
{ | |
$tweets = json_decode($tmhOAuth->response['response'], true); | |
// Save to file | |
if ( count($tweets) ) | |
{ | |
self::fileSet($file, $tweets); | |
} | |
} | |
// Retrieve expired cache if it exists when request fails | |
else if ( $e->getMessage() == 'expired' ) | |
{ | |
try | |
{ | |
self::fileGet($file, 0); | |
} | |
catch(Exception $e){} | |
} | |
} | |
return $tweets; | |
} | |
/** | |
* Tweet entify | |
* @description Enables all tweet features as per API requirements | |
* @param object $tweet JSON decoded tweet object | |
* @return string The tweet content with entities replaced | |
*/ | |
public static function tweet_entify($tweet) | |
{ | |
try | |
{ | |
$tmhUtil = self::cacheGet('entify_tweet'); | |
} | |
catch(Exception $e) | |
{ | |
require_once 'lib/tmhOAuth/tmhUtilities.php'; | |
$tmhUtil = self::cacheSet('entify_tweet', new tmhUtilities() ); | |
} | |
return $tmhUtil->entify($tweet); | |
} | |
/** | |
* Tweet link | |
* @description Creates date link to original tweet | |
* @param object $tweet JSON decoded tweet object | |
* @param string $dateFormat PHP date format | |
* @return string HTML string | |
*/ | |
public static function tweet_permalink($tweet, $dateFormat = 'H:ia d.m.Y') | |
{ | |
$href = 'http://twitter.com/' . $tweet['user']['screen_name'] . '/status/' . $tweet['id_str']; | |
return '<a href="' . $href . '">' . date($dateFormat, strtotime($tweet['created_at']) ) . '</a>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment