Skip to content

Instantly share code, notes, and snippets.

@sirkitree
Created February 9, 2012 15:51
Show Gist options
  • Select an option

  • Save sirkitree/1780766 to your computer and use it in GitHub Desktop.

Select an option

Save sirkitree/1780766 to your computer and use it in GitHub Desktop.
only show cached data if no response
/**
* Implementation of hook_block().
*/
function twitfaves_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
...
case 'view':
$screen_name = variable_get('twitfaves_screen_name', 'lullabot');
$count = variable_get('twitfaves_count', 3);
if ($tweets = twitfaves_generate($screen_name, $count)) {
$block['subject'] = '';
$block['content'] = theme('twitfaves', $screen_name, $tweets);
return $block;
}
break;
}
}
/**
* Does the heavy lifting, spits out formatted HTML.
*/
function twitfaves_generate($screen_name, $count = 3) {
$cached = cache_get('twitfaves');
if (empty($cached->data)) {
$tweets = array();
$response = drupal_http_request('http://api.twitter.com/1/favorites/' . $screen_name . '.xml');
if ($response->code == 200) {
$xml = new SimpleXMLElement($response->data);
for ($i = 0; ($i < count($xml->status)) && ($i < $count); $i++) {
$tweets[] = _twitfaves_extract($xml->status[$i]);
}
cache_set('twitfaves', $tweets, 'cache', time() + (variable_get('twitfaves_interval', 60) * 60));
}
}
else {
$tweets = $cached->data;
}
return $tweets;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment