Changeset 32595

Show
Ignore:
Timestamp:
04/19/09 00:05:21 (4 years ago)
Author:
daisuke
Message:

miscellaneous fix

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Morris/trunk/lib/Morris/CLI/LogUpload.pm

    r32566 r32595  
    22use utf8; 
    33use Moose; 
     4use Moose::Util::TypeConstraints; 
    45use MooseX::AttributeHelpers; 
    5 use MooseX::Types::DateTime; 
    66use MooseX::Types::URI; 
     7use Config::Any; 
    78use DateTime; 
     9use DateTime::Format::Strptime; 
    810use DBI; 
     11use Encode qw(encode_utf8); 
    912use File::Temp; 
    1013use Template; 
     14use Template::Stash::ForceUTF8; 
    1115use WWW::Mechanize; 
    1216 
     
    2327); 
    2428 
     29subtype 'Morris::CLI::ValueList' 
     30    => as 'ArrayRef' 
     31; 
     32coerce 'Morris::CLI::ValueList' 
     33    => from 'Str' 
     34    => via { [ $_ ] } 
     35; 
    2536has connect_info => ( 
    2637    is => 'rw', 
    27     isa => 'ArrayRef', 
     38    isa => 'Morris::CLI::ValueList', 
    2839    required => 1, 
     40    coerce => 1, 
    2941    auto_deref => 1, 
    3042); 
     
    3345    metaclass => 'Collection::Array', 
    3446    is => 'ro', 
    35     isa => 'ArrayRef[Str]', 
    36     default => sub { +[] }, 
     47    isa => 'Morris::CLI::ValueList', 
     48    coerce => 1, 
    3749    required => 1, 
    3850    provides => { 
     
    4456    is => 'rw', 
    4557    isa => 'Str', 
    46     coerce => 1, 
     58    required => 1, 
     59); 
     60 
     61has target => ( 
     62    is => 'rw', 
     63    isa => 'Str', 
     64    trigger => sub { 
     65        my $date = DateTime::Format::Strptime->new( 
     66            pattern => '%Y-%m-%d', 
     67            time_zone => 'Asia/Tokyo' 
     68        )->parse_datetime($_[1] ); 
     69        $date->set(hour => 6); 
     70        $_[0]->date( $date ) 
     71    } 
    4772); 
    4873 
    4974has date => ( 
    50     is => 'ro', 
     75    is => 'rw', 
    5176    isa => 'DateTime', 
    52     coerce => 1, 
    5377    default => sub { 
    5478        DateTime->now(time_zone => 'Asia/Tokyo') 
     
    113137sub _build_dbh { 
    114138    my $self = shift; 
     139    $self->{connect_info}->[3] = { RaiseError => 1, AutoCommit => 1 }; 
    115140    DBI->connect($self->connect_info); 
    116141} 
     
    119144    my ($self, $channel) = @_; 
    120145 
    121     my $start = $self->date; 
    122  
    123     my $dbh = DBI->connect(); 
     146    my $start = $self->date->clone; 
     147 
     148    my $dbh = $self->dbh; 
    124149 
    125150    my $sth = $dbh->prepare("SELECT * FROM log WHERE channel = ? AND created_on >= ? AND created_on <= ?"); 
    126     $sth->execute($channel, $start->epoch, $start->clone->add(day => 1)->epoch); 
     151    $sth->execute($channel, $start->epoch, $start->clone->add(days => 1)->epoch); 
    127152 
    128153    my @messages; 
    129     while (my $h, $sth->fetchrow_hashref) { 
     154    while (my $h = $sth->fetchrow_hashref) { 
    130155        push @messages, $h; 
     156        $h->{message} =~ s/\|/\\|/g; 
     157        $h->{message} =~ s{((?:(?:https?):)(?://(?:[^\s/?#]*))(?:[^\s?#]*)(?:\?(?:[^\s#]*))?(?:#(?:.*))?)}{"$1":$1}g; 
    131158        $h->{created_on} = DateTime->from_epoch(epoch => $h->{created_on}, time_zone => 'Asia/Tokyo'); 
    132159    } 
    133  
    134     my $tt = Template->new(); 
     160    $sth->finish; 
     161    $dbh->disconnect; 
     162 
     163    return if !@messages; 
     164 
     165    my $tt = Template->new( 
     166        STASH => Template::Stash::ForceUTF8->new 
     167    ); 
    135168    my %vars = ( 
    136169        channel  => $channel, 
     
    141174    $tt->process(\*DATA, \%vars, \$output) or 
    142175        die $tt->error; 
    143  
     176    $output = encode_utf8($output); 
     177warn $output; 
    144178    my $ua = WWW::Mechanize->new(); 
    145179    if ($self->basic_auth_username && $self->basic_auth_password ) { 
     
    157191 
    158192    $ua->get($self->make_url( "/Log/" . $start->strftime('%Y-%m-%d') . ".edit" )); 
     193 
    159194    $ua->submit_form( 
    160195        form_number => 2,