Skip to content

Instantly share code, notes, and snippets.

@kazu634
Created February 1, 2010 12:19
Show Gist options
  • Save kazu634/291649 to your computer and use it in GitHub Desktop.
Save kazu634/291649 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# === Libraries ===
use strict;
use warnings;
use Perl6::Say;
use utf8;
use Encode;
use WebService::Simple;
use XML::Simple;
use YAML::Syck;
use Config::Auto;
# === Initial Setting ===
my $howm_directory = "/mnt/nas/backup/howm";
my $user = "kazu634";
my $page = 1; # page counter
my $new_id = 0; # the newest id when obtaining the favorites.
my $howm_file = get_howm_filename(); # filename for the howm file
my $howm_header = get_howm_header(); # header string for the howm file
my $counter = 0; # counter for how many times this script outputs the favorites.
# === Until what ID number do I get from my favorites ===
my $recent_id = 0; # variant for storing the recent-obtained ID number
# Read the ID number from the configuration file, .get_favorite,
# if it exists.
if ( -f ".get_favorite" ) {
my $config = Config::Auto::parse(".get_favorite");
$recent_id = $config->{recent_id};
}
# === Get the favorites ===
my $twitter =
WebService::Simple->new( base_url => "http://twitter.com/favorites.xml", );
LOOP: while (1) {
my $response = $twitter->get( { id => $user, page => $page } );
# If the page you request is empty, get out of the while-loop.
last unless ( defined %{ XMLin( $response->content )->{status} } );
my @ids = reverse sort keys %{ XMLin( $response->content )->{status} };
# Store the newest id
# (after obtaining the favorites,
# this script outputs this id to the .get_favorite file).
$new_id = $ids[0] if ( $page == 1 );
foreach my $id (@ids) {
# If the favorite just getting is already obtained,
# get out of the while-loop.
last LOOP if ( $recent_id == $id );
if ( $recent_id < $id ) {
my $text =
encode( "utf8",
XMLin( $response->content )->{status}->{$id}->{text} );
my $user =
encode( "utf8",
XMLin( $response->content )->{status}->{$id}->{user}->{screen_name} );
open( HOWM, ">> $howm_file" ) or die "$!";
say HOWM $howm_header if ( $counter == 0 );
say HOWM "[memo][$user\@twitter] $text";
close(HOWM);
$counter++;
}
}
# Increment the page number.
$page++;
}
# === update the recent_id ===
open( FILE, '> .get_favorite' ) or die "$!";
say FILE "recent_id=$new_id";
close(FILE);
# === Sub-routines ===
sub get_howm_filename {
my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime(time);
$year += 1900;
$mon += 1;
$mon = "0$mon" if ( $mon < 10 );
$mday = "0$mday" if ( $mday < 10 );
$hour = "0$hour" if ( $hour < 10 );
$min = "0$min" if ( $min < 10 );
$sec = "0$sec" if ( $sec < 10 );
# Check if the directory exists.
# If not, make the directory.
if ( !-d "$howm_directory/$year" ) {
mkdir "$howm_directory/$year";
}
if ( !-d "$howm_directory/$year/$mon" ) {
mkdir "$howm_directory/$year/$mon";
}
return encode( "utf8",
"$howm_directory/$year/$mon/$year-$mon-$mday-$hour$min$sec.txt" );
}
sub get_howm_header {
my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime(time);
$year += 1900;
$mon += 1;
$mon = "0$mon" if ( $mon < 10 );
$mday = "0$mday" if ( $mday < 10 );
$hour = "0$hour" if ( $hour < 10 );
$min = "0$min" if ( $min < 10 );
$sec = "0$sec" if ( $sec < 10 );
return encode( "utf8",
"= favorites from twitter.\n\[$year-$mon-$mday $hour:$min\]\n" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment