Skip to content

Instantly share code, notes, and snippets.

@mtsukamoto
Created February 5, 2010 16:30
Show Gist options
  • Save mtsukamoto/295938 to your computer and use it in GitHub Desktop.
Save mtsukamoto/295938 to your computer and use it in GitHub Desktop.
use Net::Twitter;
use Encode;
use File::Slurp;
use YAML;
use utf8;
my $username = 'yourname';
my $password = 'yourpassword';
my $max_id = undef;
my $nt = Net::Twitter->new(
traits => [qw/API::REST/],
username => $username,
password => $password
);
foreach my $method (qw(user_timeline retweeted_by_me favorites)) {
my $current_max_id = $max_id;
while (1) {
my $statuses;
my $args = { count => 100 };
$args->{'max_id'} = $current_max_id if (defined($current_max_id));
printf("Fetching %s (max_id: %s, count: %s)\n", $method, $args->{'max_id'}, $args->{'count'});
eval { $statuses = $nt->$method($args); };
if ( my $err = $@ ) {
die $@ unless blessed $err && $err->isa('Net::Twitter::Error');
warn "HTTP Response Code: ", $err->code, "\n",
"HTTP Message......: ", $err->message, "\n",
"Twitter error.....: ", $err->error, "\n";
}
if (not scalar @$statuses) {
print "No entry exists.\n";
last;
}
$current_max_id = $statuses->[-1]->{'id'} - 1;
my $result = encode('utf8', YAML::Dump($statuses));
my $file = sprintf('%s_%010s-%010s.yml', $method, $statuses->[-1]->{'id'}, $statuses->[0]->{'id'});
print "Writing '$file'\n";
write_file($file, $result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment