diff options
author | Rasmus Steinke <rasi@xssn.at> | 2017-09-23 15:18:31 +0200 |
---|---|---|
committer | Rasmus Steinke <rasi@xssn.at> | 2017-09-23 15:18:31 +0200 |
commit | d4cb48883528f531e9bac41808be97095809915f (patch) | |
tree | 543b3472682a5c18a30aff983b2c222d0d951f9c /clerk_rating_client | |
parent | 4a03ed7a11863b53597cd80bfd3f0613a3e8f82b (diff) | |
download | perl-app-clerk-d4cb48883528f531e9bac41808be97095809915f.tar.gz perl-app-clerk-d4cb48883528f531e9bac41808be97095809915f.tar.xz |
initial support for writing ratings to files, using a helper client
Diffstat (limited to 'clerk_rating_client')
-rwxr-xr-x | clerk_rating_client | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/clerk_rating_client b/clerk_rating_client new file mode 100755 index 0000000..4d56526 --- /dev/null +++ b/clerk_rating_client @@ -0,0 +1,123 @@ +#!/usr/bin/perl + +binmode(STDOUT, ":utf8"); +use v5.10; +use warnings; +use Array::Utils qw(:all); +use DDP; +use File::Spec; +use strict; +use utf8; +use Encode qw(decode encode); +use File::Find; +use Getopt::Std; +use Net::MPD; + +my $mpd_host = "tauron"; +my $mpd = Net::MPD->connect($ENV{MPD_HOST} // $mpd_host // 'localhost'); +my $music_root = "/mnt/raid/Audio/Rips"; + +sub main { + my %options=(); + getopts("rst", \%options); + + if ($options{r} // $options{s} // $options{t}) { + if (defined $options{r}) { subscribe_ratings_channel(); track_rating(); } + elsif (defined $options{s}) { subscribe_ratings_channel(); sync_ratings(); } + elsif (defined $options{t}) { tag_from_sticker(); } + } else { subscribe_ratings_channel(); track_rating(); }; +} + +sub subscribe_ratings_channel { + $mpd->subscribe('rating'); +} + +sub track_rating { + while(1) { + $mpd->idle('message'); + my @blub = $mpd->read_messages; + foreach (@blub) { + my $string = $_->{message}; + my @array = split("\t", $string); + my $uri = $array[0]; + my $mode = $array[1]; + my $rating = $array[2]; + $uri = decode('UTF-8', $uri ); + my @files = $mpd->search('filename', $uri); + my @song_tags = $files[0]; + my $albumartist = $song_tags[0]->{AlbumArtist}; + my $artist = $song_tags[0]->{Artist}; + my $title = $song_tags[0]->{Title}; + my $album = $song_tags[0]->{Album}; + if ($uri =~ /.*.flac$/) { + if ($mode eq "RATING") { + print ":: tagging track \"${title}\" by \"${artist}\" with rating of \"${rating}\"\n"; + } elsif ($mode eq "ALBUMRATING") { + print ":: tagging track \"${title}\" by \"${albumartist}\" with albumrating of \"${rating}\"\n"; + } + system('metaflac', '--remove-tag=RATING', "${music_root}/${uri}"); + system('metaflac', "--set-tag=${mode}=${rating}", "${music_root}/${uri}"); + } + elsif ($uri =~ /.*.mp3$/) { + if ($mode eq "RATING") { + print ":: tagging track \"${title}\" by \"${artist}\" with rating of \"${rating}\"\n"; + } elsif ($mode eq "ALBUMRATING") { + print ":: tagging track \"${title}\" by \"${albumartist}\" with albumrating of \"${rating}\"\n"; + } + system('mid3v2', "--TXXX:${mode}:${rating}", "${music_root}/${uri}"); + } + elsif ($uri =~ /.*.ogg$/) { + print "!! OGG files not supported, yet\n"; + } + } + } +} + +sub sync_ratings { + my @sticker_uris; + my @actual_uris; + my @available_stickers = $mpd->sticker_find('song', 'rating', ''); + foreach my $rated_song (@available_stickers) { + push @sticker_uris, "$rated_song->{file}"; + } + + my @absolute; + find({ + wanted => sub { push @absolute, $_ if -f and -r }, + no_chdir => 1, + }, $music_root); + my @relative = map { File::Spec->abs2rel($_, $music_root) } @absolute; + push @actual_uris, $_ for @relative; + + my @diff = array_diff(@sticker_uris, @actual_uris); + foreach my $unrated_song (@diff) { + if ( $unrated_song =~ /.*.flac$/) { + my $rating = system('metaflac', '--show-tag=RATING', "${music_root}/${unrated_song}"); + print "$rating\n"; + if ($rating ne "0") { + print "rating ${music_root}/${unrated_song} with $rating\n"; + $mpd->sticker_value("song", "$unrated_song", "rating", "$rating"); + } + } + } +} + +sub tag_from_sticker { + my @available_stickers = $mpd->sticker_find('song', 'rating', ''); + foreach my $rated_song (@available_stickers) { + my $uri = $rated_song->{file}; + my $rating = $rated_song->{sticker}; + if ($uri =~ /.*.flac$/) { + system('metaflac', '--remove-tag=RATING', "${music_root}/${uri}"); + system('metaflac', "--set-tag=RATING=$rating", "${music_root}/${uri}"); + } + elsif ($uri =~ /.*.mp3$/) { + system('mid3v2', "--TXXX:RATING:${rating}", "${music_root}/${uri}"); + } + elsif ($uri =~ /.*.ogg$/) { + print "!! OGG files not supported, yet\n"; + } + } +} + +main(); |