Skip to content

Instantly share code, notes, and snippets.

@themattharris
Created February 5, 2012 01:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themattharris/1741669 to your computer and use it in GitHub Desktop.
Save themattharris/1741669 to your computer and use it in GitHub Desktop.
How @themattharris is following @nvvotecount
<?php
/**
* This script relies on tmhOAuth, which can be downloaded here:
* https://github.com/themattharris/tmhOAuth
*
* Instructions:
* 1) If you don't have one already, create a Twitter application on
* https://dev.twitter.com/apps
* 2) From the application details page copy the consumer key and consumer
* secret into the place in this code marked with (YOUR_CONSUMER_KEY
* and YOUR_CONSUMER_SECRET)
* 3) From the application details page copy the access token and access token
* secret into the place in this code marked with (A_USER_TOKEN
* and A_USER_SECRET)
* 4) In a terminal or server type:
* php /path/to/here/nvvotecount.php
* 5) To stop the Streaming API either press CTRL-C or, in the folder the
* script is running from type:
* touch STOP
* 6) Results will be saved to the file nvvotecount in the same directory
* as this file. Full tweet objects will be saved to nvvotecount_raw.
*
* @author themattharris
*/
function save_to_file($data, $length, $metrics) {
$out_file = dirname(__FILE__) . '/nvvotecount';
$raw_file = dirname(__FILE__) . '/nvvotecount_raw';
$tweet = json_decode($data, true);
if (!empty($tweet)) {
// don't accept tweets from anyone other than @NVVoteCount
if ($tweet['user']['id'] != '475526099') return;
$row = $tweet['id_str'] . ',' . $tweet['created_at'] . ',' . $tweet['text'] . PHP_EOL;
file_put_contents($out_file, $row, FILE_APPEND);
file_put_contents($raw_file, $data, FILE_APPEND);
}
//echo $data .PHP_EOL;
return file_exists(dirname(__FILE__) . '/STOP');
}
require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'YOUR_CONSUMER_KEY',
'consumer_secret' => 'YOUR_CONSUMER_SECRET',
'user_token' => 'A_USER_TOKEN',
'user_secret' => 'A_USER_SECRET',
));
$method = 'https://stream.twitter.com/1/statuses/filter.json';
$params = array(
'follow' => '475526099'
);
$tmhOAuth->streaming_request('POST', $method, $params, 'save_to_file');
// output any response we get back AFTER the Stream has stopped -- or it errors
tmhUtilities::pr($tmhOAuth);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment