Changeset 88
- Timestamp:
- 04/02/06 14:06:28 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plagger/trunk/lib/Plagger/Plugin/CustomFeed/iTunesRecentPlay.pm
r81 r88 3 3 use warnings; 4 4 use base qw( Plagger::Plugin ); 5 use File::Spec; 5 6 use Encode; 6 use Date ::Parse;7 use DateTime::Format::W3CDTF; 7 8 use HTML::Entities; 9 use Plagger::UserAgent; 8 10 9 11 sub register { … … 12 14 $self, 13 15 'subscription.load' => \&load, 14 'aggregator.aggregate.itunesrecentplay' => \&aggregate,15 16 ); 16 17 } … … 20 21 21 22 my $feed = Plagger::Feed->new; 22 $feed-> type('itunesrecentplay');23 $feed->aggregator(sub { $self->aggregate(@_) }); 23 24 $context->subscription->add($feed); 24 25 } … … 26 27 sub aggregate { 27 28 my($self, $context, $args) = @_; 28 my $conf = $self->conf;29 29 30 30 my $file = $self->conf->{library_path}; 31 31 unless ($file) { 32 32 if ($^O eq 'MSWin32') { 33 require File::HomeDir::Windows; 33 34 my $mymusic = File::HomeDir::Windows->my_win32_folder('My Music'); 34 35 $file = File::Spec->catfile($mymusic, 'iTunes', 'iTunes Music Library.xml'); … … 36 37 $file = File::Spec->catfile($ENV{HOME}, 'Music', 'iTunes', 'iTunes Music Library.xml'); 37 38 } 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."); 39 40 return; 40 41 } 41 42 } 42 43 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: $!"); 44 60 45 61 my $feed = Plagger::Feed->new; … … 62 78 and do { 63 79 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; 67 86 } 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 } 70 95 } 71 96 $data = {}; … … 85 110 =head1 SYNOPSIS 86 111 112 # entries updated within 120 minutes 87 113 - module: CustomFeed::iTunesRecentPlay 88 114 config: 89 115 library_path: /path/to/iTunes Music Library.xml 90 reload_period: 3600116 duration: 120 91 117 92 118 =head1 DESCRIPTION … … 103 129 this plugin try to find it automatically. 104 130 105 =item reload_period131 =item duration 106 132 107 133 This 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 .134 this parameter.It's good to define this parameter same as execution 135 period of plagger with cron to reduce memory usage. 110 136 111 137 =back … … 115 141 Gosuke Miyashita, E<lt>gosukenator@gmail.comE<gt> 116 142 143 Tatsuhiko Miyagawa 144 117 145 =head1 SEE ALSO 118 146
