Changeset 88

Show
Ignore:
Timestamp:
04/02/06 14:06:28 (3 years ago)
Author:
miya
Message:

update iTunesRecentPlay

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plagger/trunk/lib/Plagger/Plugin/CustomFeed/iTunesRecentPlay.pm

    r81 r88  
    33use warnings; 
    44use base qw( Plagger::Plugin ); 
     5use File::Spec; 
    56use Encode; 
    6 use Date::Parse
     7use DateTime::Format::W3CDTF
    78use HTML::Entities; 
     9use Plagger::UserAgent; 
    810 
    911sub register { 
     
    1214        $self, 
    1315        'subscription.load' => \&load, 
    14         'aggregator.aggregate.itunesrecentplay' => \&aggregate, 
    1516    ); 
    1617} 
     
    2021 
    2122    my $feed = Plagger::Feed->new; 
    22     $feed->type('itunesrecentplay'); 
     23    $feed->aggregator(sub { $self->aggregate(@_) }); 
    2324    $context->subscription->add($feed); 
    2425} 
     
    2627sub aggregate { 
    2728    my($self, $context, $args) = @_; 
    28     my $conf = $self->conf; 
    2929 
    3030    my $file = $self->conf->{library_path}; 
    3131    unless ($file) { 
    3232        if ($^O eq 'MSWin32') { 
     33            require File::HomeDir::Windows; 
    3334            my $mymusic = File::HomeDir::Windows->my_win32_folder('My Music'); 
    3435            $file = File::Spec->catfile($mymusic, 'iTunes', 'iTunes Music Library.xml'); 
     
    3637            $file = File::Spec->catfile($ENV{HOME}, 'Music', 'iTunes', 'iTunes Music Library.xml'); 
    3738        } else { 
    38             $context->log(error => "I can't guess library.xml path using your OS name $^O. Specify using --library option."); 
     39            $context->log(error => "I can't guess library.xml path using your OS name $^O."); 
    3940            return; 
    4041        } 
    4142    } 
    4243 
    43     open my $fh, "<:encoding(utf-8)", $file or die "$file: $!"; 
     44    my $uri = URI->new($file); 
     45    if ($uri->scheme) { 
     46        $file = $self->cache->path_to('iTunes Music Library.xml'); 
     47 
     48        my $ua = Plagger::UserAgent->new; 
     49        my $response = $ua->mirror($uri => $file); 
     50        if ($response->is_error) { 
     51            $context->log(error => "GET $uri failed: " . $response->status_line); 
     52            return; 
     53        } 
     54 
     55        $context->log(info => "Downloaded $uri to $file"); 
     56    } 
     57 
     58    open my $fh, "<:encoding(utf-8)", $file 
     59        or return $context->log(error => "$file: $!"); 
    4460 
    4561    my $feed = Plagger::Feed->new; 
     
    6278            and do { 
    6379                my $entry = Plagger::Entry->new; 
    64                 if( $data->{date} and $data->{artist} and str2time($data->{date}) > time - $conf->{reload_period} ){ 
    65                     for(keys %$data){ 
    66                         $entry->meta->{$_} = $data->{$_}; 
     80 
     81                if( $data->{date} and $data->{artist} ){ 
     82                    my $dt = DateTime::Format::W3CDTF->parse_datetime($data->{date}); 
     83                    unless ($dt) { 
     84                        $context->log( warn => "Can't parse $data->{date}"); 
     85                        next; 
    6786                    } 
    68                     $context->log( info => $data->{artist} . ' ' . $data->{track}); 
    69                     $feed->add_entry($entry); 
     87                    if( !defined $self->conf->{duration} or $dt->epoch > time - $self->conf->{duration} * 60 ){ 
     88                        for my $key (keys %$data){ 
     89                            $entry->meta->{$key} = $data->{$key}; 
     90                        } 
     91                        $entry->date(Plagger::Date->from_epoch($dt->epoch)); 
     92                        $context->log( debug => $data->{artist} . ' ' . $data->{track}); 
     93                        $feed->add_entry($entry); 
     94                    } 
    7095                } 
    7196                $data = {}; 
     
    85110=head1 SYNOPSIS 
    86111 
     112  # entries updated within 120 minutes 
    87113  - module: CustomFeed::iTunesRecentPlay 
    88114    config: 
    89115      library_path: /path/to/iTunes Music Library.xml 
    90       reload_period: 360
     116      duration: 12
    91117 
    92118=head1 DESCRIPTION 
     
    103129this plugin try to find it automatically. 
    104130 
    105 =item reload_period 
     131=item duration 
    106132 
    107133This plugin find a music played recently if last played time is within 
    108 this parameter.It's good to define this parameter same as execution  
    109 period of plagger with cron
     134this parameter.It's good to define this parameter same as execution 
     135period of plagger with cron to reduce memory usage
    110136 
    111137=back 
     
    115141Gosuke Miyashita, E<lt>gosukenator@gmail.comE<gt> 
    116142 
     143Tatsuhiko Miyagawa 
     144 
    117145=head1 SEE ALSO 
    118146