焼きサンマ綺麗に食べるブログ

焼きサンマ食べたい

自分の発言をダウンロードしたくて(修正版)

前のソースコードは日付周りでバグがあったため、修正。それとタイムゾーンの変換を行うようにした。

日付を表現する方法なんでこんなにあるねん。

以下ソース

#!/usr/bin/env perl

use strict;
use warnings;
use Carp;
use utf8;

use encoding 'utf8';

use YAML::Syck;
use Net::Twitter;
use DateTime::Timezone;
use DateTime::Format::Mail;
use DateTime::Format::MySQL;
use DateTime::Format::RSS;
use SQL::Abstract;
use DBI;

use Readonly;
Readonly our %DEFAULT => (MESSAGE_TABLE => 'messages');

our $config = init_config( @ARGV );

{
    my $message_table = $config->{table}->{message};
    my $dbh = DBI->connect("dbi:SQLite:dbname=$config->{db_file}", '', '', { RaiseError => 1 });
    $dbh->{unicode} = 1;
    
    my $max_id = $dbh->selectrow_array("SELECT max(id) FROM $message_table");
    
    my $twit = Net::Twitter->new(username => $config->{username},
                                 password => $config->{password});
    my $timeline = $twit->user_timeline( { id=> $config->{username}, count => 20 } );
    
    insert_timeline($dbh, $timeline, $max_id) if (defined $timeline);
    
    $dbh->disconnect;
}

sub init_config {
    my ($config_filename, $db_file) = @_;
    unless (-e $config_filename) { die "Can not open $config_filename"; }
    
    my $config_file = LoadFile($config_filename);
    my $config = {};
    $config->{username} = $config_file->{username} || die 'Not input username';
    $config->{password} = $config_file->{password} || die 'Not input password';
    $config->{db_file} = $db_file || $config_file->{db_file} || die 'Not input DB File';
    unless (-e $config->{db_file} ) { die "Can not open $config->{db_file}"; }
    
    $config->{table} = {};
    if (defined $config_file->{table}) {
        $config->{table}->{message} = $config_file->{table}->{message} || $DEFAULT{MESSAGE_TABLE};
    }
    else {
        $config->{table}->{message} = $DEFAULT{MESSAGE_TABLE};
    }
    return $config;
}

sub insert_timeline {
    my $dbh = shift;
    my $timeline = shift;
    my $max_id = shift;
    
    my $tzhere = DateTime::TimeZone->new( name => 'local' );

    my $message_table = $config->{table}->{message};
    my $table_item = { id => 0, text => q//, created_at => q// };
    
    my $sql = SQL::Abstract->new;
    my ($stmt, @bind) = $sql->insert($message_table, $table_item);
    my $sth = $dbh->prepare($stmt);
    
    for my $item (@{$timeline}) {
        if (defined $max_id && $item->{id} <= $max_id ) { last; }
        my $date = DateTime::Format::RSS->parse_datetime( $item->{created_at} );
        $date->set_time_zone('local');
        my $date_str = DateTime::Format::MySQL->format_datetime( $date );
        $table_item->{id} = $item->{id};
        $table_item->{text} = $item->{text};
        $table_item->{created_at} = $date_str;
        
        @bind = $sql->values( $table_item );
        $sth->execute(@bind);
    }
    $sth->finish;
}

1;
__END__