Skip to content

Instantly share code, notes, and snippets.

@timdp
Created November 20, 2013 17:03
Show Gist options
  • Save timdp/7566846 to your computer and use it in GitHub Desktop.
Save timdp/7566846 to your computer and use it in GitHub Desktop.
<?php
# Idle tweeps
# by @tmdpw
# 1. Clone https://github.com/themattharris/tmhOAuth
require_once dirname(__FILE__) . '/tmhOAuth/tmhOAuth.php';
# 2. Enter your screen name
define('SCREEN_NAME', '...');
# 3. Register an app at https://dev.twitter.com/
define('CONSUMER_KEY', '...');
define('CONSUMER_SECRET', '...');
define('TOKEN', '...');
define('SECRET', '...');
# 4. Optional configuration
define('SLEEP_TIME', 5);
define('MAX_IDLE_TIME', 30 * 24 * 60 * 60);
# 5. Run php idle_tweeps.php
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => CONSUMER_KEY,
'consumer_secret' => CONSUMER_SECRET,
'token' => TOKEN,
'secret' => SECRET
));
$last_tweet_times = array();
$last_tweets = array();
$screen_names = array();
echo 'Loading ...', PHP_EOL;
get_friend_data();
echo PHP_EOL;
asort($last_tweet_times, SORT_NUMERIC);
$i = 0;
foreach ($last_tweet_times as $friend_id => $time) {
printf("%4d. %-40s %s\n", ++$i, $screen_names[$friend_id],
$time ? date('Y-m-d H:i:s', $time) : '<blank>');
if ($time) {
printf("%4s %s\n", '', $last_tweets[$friend_id]);
}
}
function get_friend_data($cursor = -1) {
global $tmhOAuth, $last_tweet_times, $last_tweets, $screen_names;
$params = array(
'method' => 'GET',
'url' => $tmhOAuth->url('1.1/friends/list'),
'params' => array(
'screen_name' => SCREEN_NAME,
'count' => 200,
'skip_status' => 0,
'cursor' => $cursor
)
);
$code = $tmhOAuth->user_request($params);
while ($code != 200) {
echo "HTTP $code, retrying soon ...", PHP_EOL;
sleep(SLEEP_TIME);
$code = $tmhOAuth->user_request($params);
}
$resp = json_decode($tmhOAuth->response['response']);
foreach ($resp->users as $user_obj) {
$created_at = isset($user_obj->status)
? strtotime($user_obj->status->created_at)
: 0;
if ($created_at + MAX_IDLE_TIME < time()) {
$last_tweet_times[$user_obj->id_str] = $created_at;
$screen_names[$user_obj->id_str] = $user_obj->screen_name;
if ($created_at) {
$last_tweets[$user_obj->id_str] = $user_obj->status->text;
}
}
}
if ($resp->next_cursor_str) {
echo "Sleeping ...", PHP_EOL;
sleep(SLEEP_TIME);
get_friend_data($resp->next_cursor_str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment