13 KiB
13 KiB
title | author | date | wordtwit_post_info | categories | |||
---|---|---|---|---|---|---|---|
Twitterのfavoriteをhowmファイルにするスクリプト | kazu634 | 2010-01-02 |
|
|
Twitter の favorite を howm ファイルにするスクリプトを作成しました。
設定を変更したい場合には、ソース中の下記を変更してください:
# howm のデータを格納するディレクトリ my $howm_direcotory = "/Users/kazu634/Documents/howm"; # favorite を取得したいユーザー名 my $user = "kazu634";
ソース
# === 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_direcotory = "/Users/kazu634/Documents/howm"; my $user = "kazu634"; my $page = 1; # page counter my $new_id = ; # 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 = ; # counter for how many times this script outputs the favorites. # === Until what ID number do I get from my favorites === my $recent_id = ; # 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[] 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} ); open( HOWM, ">> $howm_file" ) or die "$!"; say HOWM $howm_header if ( $counter == ); say HOWM "[memo][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 ); return encode( "utf8", "$howm_direcotory/$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" ); }