Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Forked from hearvox/twitter_chat_archive.php
Created June 6, 2013 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/5723391 to your computer and use it in GitHub Desktop.
Save wpsmith/5723391 to your computer and use it in GitHub Desktop.
<?php
/*
Twitter Chat Archive:
Get hashtag results from Twitter Search API (in JSON) for a weekly chat.
API takes date range via values: 'since' thru 'until' (YYYY-MM-DD).
API limits: 1 page per request, 100 results per page, 1500 total results: <7 days old.
Script below: Repeats page requests until no results found w/in date-range.
Then gathers only those results w/in specified timestamp range.
Then sort results from oldest to newest.
Then prints in HTML. API query script (using: wp_remote_get) from:
http://wordpress.org/extend/plugins/twitter-hash-tag-widget/
*/
// Set timezone for all time calculations.
date_default_timezone_set( 'America/New_York' );
// Variables for time-range of tweet-results to retrieve.
$day_start = 'Monday';
$hr = '20';
$min = '0';
$sec = '0';
$duration = '150'; // Length in minutes btwn start and end of time-range.
// Set date-range for Twitter API query values: 'since' and 'until'.
// (If $day_start='Monday', use 'last Monday', unless 'today' is Mon).
$date_since = ( date( 'l' ) != $day_start ) ? strtotime( 'last ' . $day_start ) : strtotime( 'today' );
$date_until = $date_since + 86400; // // Sets 'until' date to 1 day (86.4K sec) after 'since'.
// Timestamps for range, start and end, of results.
$time_start = $date_since + ($hr * 3600) + ($min * 60) + $sec;
$time_end = $time_start + ($duration * 60);
//Variables for Twitter API query.
$api_url = 'http://search.twitter.com/search.json';
$hashtag = 'pubmedia';
$rpp = 100;
$page = 1;
$since = date( 'Y-m-d', $date_since ); // API needs YYYY-MM-DD.
$until = date( 'Y-m-d', $date_until );
$arr_all = array(); // To hold results w/in timestamp-range.
//Text of info about Chat, for HTML.
$chat_text = 'Twitter Chat (8-10pm ET):';
do { // Always get first results page; then get successive pages until no results.
$api_query = wp_remote_get( "$api_url?q=%23$hashtag&rpp=$rpp&page=$page&since=$since&until=$until" );
if ( is_wp_error( $api_query ) ) {
$html = "<p>Failed to update from Twitter!</p>\n";
$html .= "<!-- {$api_query->errors['http_request_failed'][0]} -->\n";
} else { // Hold this API page of JSON results in an array ($arr).
$arr = get_object_vars( json_decode( $api_query['body'] ) );
for ( $i=0; $i < count( $arr['results'] ); $i++ ) {
$arr['results'][$i] = get_object_vars( $arr['results'][$i] );
// Hold results from all pages in an array ($arr_all),
// but only if result's timestamp is w/in specified timestamp-range.
$time_tweet = strtotime( $arr['results'][$i]['created_at'] );
if ( $time_tweet >= $time_start && $time_tweet <= $time_end ) //
$arr_all[] = $arr['results'][$i];
}
}
$page++; // For next page of API results.
} while ( $arr['results'] );
// Sort oldest to newest
$arr_asc = array_reverse ( $arr_all );
$arr_count = count($arr_asc); // Number of tweets w/in time range.
// HTML info about Chat that precedes results, w/ total tweets; open div for results:
$html = "<p><em><a href='http://search.twitter.com/search?q=%23$hashtag' title='Twitter hashtag'>#$hashtag</a> $chat_text $since ($arr_count tweets)</em></p><!--more-->\n<div id='tweets'>\n";
// HTML for each result:
foreach ( $arr_asc as $result ) {
$profile_image_url = $result['profile_image_url'];
$created_at = $result['created_at'];
$user = $result['from_user'];
$id = $result['id'];
$text = $result['text'];
$tweettime = date( 'Md h:i:sa', strtotime( $created_at ) ); // Timestamp.
// Make clickable: URLs, twitter users (@), and hashtags (#).
$text = preg_replace('|(https?://[^\ ]+)|', '<a href="$1">$1</a>', $text);
$text = preg_replace('|@(\w+)|', '<a href="http://twitter.com/$1">@$1</a>', $text);
$text = preg_replace('|#(\w+)|', '<a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $text);
$html .= "<div class='tweetchat'>";
if ( $profile_image_url ) // Add user image, if it exists.
$html .= "<img class='alignright' src='$profile_image_url' alt='Twitter user: $user' />";
// Add user's link, then tweet's text and time.
$html .= "<a href='http://twitter.com/$user' rel='nofollow' title='Twitter user: $user'>$user</a>: $text <span class='tweettime'>$tweettime</span></div><!-- .tweetchat -->\n";
}
$html .= "</div><!-- #tweets -->\n"; // Close div for all results.
// HTML that follows all results:
$html .= '<p><em>Every Monday 8-10pm Eastern: <a href="http://search.twitter.com/search?q=pubmedia" title="twitter hashtag">#pubmedia</a> twitter chat.</p>';
// Print the HMTL of all results w/in time-range.
echo $html;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment