| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use warnings; |
|---|
| 5 |
use Encode; |
|---|
| 6 |
use Date::Parse; |
|---|
| 7 |
use HTML::Entities; |
|---|
| 8 |
use XML::Atom::Client; |
|---|
| 9 |
use XML::Atom::Entry; |
|---|
| 10 |
|
|---|
| 11 |
my $user = 'xxx@xxxxx'; |
|---|
| 12 |
my $password = 'xxx'; |
|---|
| 13 |
my $file = $ARGV[0]; |
|---|
| 14 |
my $last_update_time_file = 'last_update_time'; |
|---|
| 15 |
|
|---|
| 16 |
open my $in, $last_update_time_file or die $!; |
|---|
| 17 |
my $last_update_time = <$in> || str2time('2006-02-28T00:00:00Z'); |
|---|
| 18 |
close($in); |
|---|
| 19 |
|
|---|
| 20 |
unless ($file) { |
|---|
| 21 |
if ($^O eq 'MSWin32') { |
|---|
| 22 |
my $mymusic = File::HomeDir::Windows->my_win32_folder('My Music'); |
|---|
| 23 |
$file = File::Spec->catfile($mymusic, 'iTunes', 'iTunes Music Library.xml'); |
|---|
| 24 |
} elsif ($^O eq 'darwin') { |
|---|
| 25 |
$file = File::Spec->catfile($ENV{HOME}, 'Music', 'iTunes', 'iTunes Music Library.xml'); |
|---|
| 26 |
} else { |
|---|
| 27 |
die "I can't guess library.xml path using your OS name $^O. Specify using --library option."; |
|---|
| 28 |
} |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
open my $fh, "<:encoding(utf-8)", $file or die "$file: $!"; |
|---|
| 32 |
|
|---|
| 33 |
my @otologs; |
|---|
| 34 |
|
|---|
| 35 |
my $data; |
|---|
| 36 |
while (<$fh>) { |
|---|
| 37 |
m!<key>Name</key><string>(.*?)</string>! |
|---|
| 38 |
and $data->{track} = HTML::Entities::decode($1); |
|---|
| 39 |
m!<key>Artist</key><string>(.*?)</string>! |
|---|
| 40 |
and $data->{artist} = HTML::Entities::decode($1); |
|---|
| 41 |
m!<key>Album</key><string>(.*?)</string>! |
|---|
| 42 |
and $data->{album} = HTML::Entities::decode($1); |
|---|
| 43 |
m!<key>Total Time</key><integer>(.*?)</integer>! |
|---|
| 44 |
and $data->{duration} = HTML::Entities::decode($1); |
|---|
| 45 |
m!<key>Play Date UTC</key><date>(.*?)</date>! |
|---|
| 46 |
and $data->{date} = HTML::Entities::decode($1); |
|---|
| 47 |
m!</dict>! |
|---|
| 48 |
and do { |
|---|
| 49 |
if($data->{date} and $data->{artist} and str2time($data->{date}) > $last_update_time){ |
|---|
| 50 |
push @otologs, $data; |
|---|
| 51 |
} |
|---|
| 52 |
$data = {}; |
|---|
| 53 |
}; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
close($fh); |
|---|
| 57 |
|
|---|
| 58 |
my $api = XML::Atom::Client->new; |
|---|
| 59 |
$api->username($user); |
|---|
| 60 |
$api->password($password); |
|---|
| 61 |
|
|---|
| 62 |
for (@otologs){ |
|---|
| 63 |
my $entry = XML::Atom::Entry->new; |
|---|
| 64 |
my $otolog = XML::Atom::Namespace->new(otolog => 'http://otolog.org/ns/music#'); |
|---|
| 65 |
for my $key (keys %$_){ |
|---|
| 66 |
$entry->set($otolog, 'otolog:' . $key, $_->{$key}); |
|---|
| 67 |
} |
|---|
| 68 |
print $_->{artist} . " - " . $_->{track} . "\n"; |
|---|
| 69 |
$api->createEntry('http://mss.playlog.jp/playlog', $entry); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
open my $out, ">$last_update_time_file" or die $!; |
|---|
| 73 |
print $out time; |
|---|
| 74 |
close($out); |
|---|
| 75 |
|
|---|
| 76 |
exit; |
|---|