Last active
December 11, 2015 06:08
-
-
Save tomnomnom/4556573 to your computer and use it in GitHub Desktop.
A quick and dirty way to get recent tweets
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
#!/usr/bin/env php | |
<?php | |
date_default_timezone_set('GMT'); | |
$params = array( | |
"screen_name" => "tomnomnom", | |
"include_rts" => "true" | |
); | |
if (!file_exists('last-id')){ | |
touch('last-id'); | |
} | |
$lastId = file_get_contents('last-id'); | |
if (is_numeric($lastId)){ | |
$params['since_id'] = $lastId; | |
} | |
$q = http_build_query($params); | |
$url = "http://api.twitter.com/1/statuses/user_timeline.json?{$q}"; | |
$r = file_get_contents($url); | |
if (!$r) exit(1); | |
$tweets = json_decode($r); | |
if (sizeOf($tweets) == 0) exit(1); | |
$lastId = $tweets[0]->id; | |
file_put_contents('last-id', $lastId); | |
foreach($tweets as $tweet){ | |
$row = array($tweet->id); | |
if (isset($tweet->in_reply_to_status_id)){ | |
$row[] = $tweet->in_reply_to_status_id; | |
} else { | |
$row[] = ''; | |
} | |
if (isset($tweet->in_reply_to_user_id)){ | |
$row[] = $tweet->in_reply_to_user_id; | |
} else { | |
$row[] = ''; | |
} | |
if (isset($tweet->retweeted_status)){ | |
$row[] = $tweet->retweeted_status->id; | |
} else { | |
$row[] = ''; | |
} | |
if (isset($tweet->retweeted_status)){ | |
$row[] = $tweet->retweeted_status->user->id; | |
} else { | |
$row[] = ''; | |
} | |
$row[] = $tweet->created_at; | |
$row[] = $tweet->source; | |
$row[] = $tweet->text; | |
fputcsv(STDOUT, $row); | |
} | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment