-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclerk_rating_client
executable file
·199 lines (177 loc) · 6.26 KB
/
clerk_rating_client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env perl
binmode(STDOUT, ":utf8");
use v5.10;
use warnings;
use Array::Utils qw(:all);
#use DDP { show_unicode => 1 };
use Data::Dumper;
use File::stat;
use Try::Tiny;
use Config::Simple;
use File::Spec;
use IPC::Run;
use strict;
use utf8;
use Encode qw(decode encode);
use File::Find;
use Getopt::Std;
use Net::MPD;
my $config_file = $ENV{'HOME'} . "/.config/clerk/clerk.conf";
if ($ENV{CLERK_CONF}) {
$config_file = $ENV{CLERK_CONF};
}
my $cfg = new Config::Simple(filename=>"$config_file");
my $general_cfg = $cfg->param(-block=>"General");
my $mpd_host = $general_cfg->{mpd_host};
my $music_root = $general_cfg->{music_root};
my $mpd = Net::MPD->connect($ENV{MPD_HOST} // $mpd_host // 'localhost');
sub main {
my %options=();
getopts("rst", \%options);
if ($options{r} // $options{s} // $options{t}) {
if (defined $options{r}) { subscribe_ratings_channel(); idle_loop(); }
elsif (defined $options{s}) { subscribe_ratings_channel(); sync_ratings(); }
elsif (defined $options{t}) { tag_from_sticker(); }
} else { subscribe_ratings_channel(); idle_loop(); };
}
sub subscribe_ratings_channel {
$mpd->subscribe('rating');
}
sub idle_loop {
while(1) {
$mpd->idle('message');
song_handler();
}
}
sub song_handler {
my @messages = $mpd->read_messages;
for my $msg (@messages) {
my ($uri, $mode, $rating) = get_info_from_message($msg->{message});
my ($albumartist, $artist, $title, $album) = get_track_tags($uri);
my ($stats) = get_timestamp($uri);
my ($file_atime, $file_mtime) = ($stats->atime, $stats->mtime);
if ($uri =~ /\.flac$/) {
tag_flacs($uri, $mode, $rating, $artist, $albumartist, $title, $album, $file_atime, $file_mtime);
}
elsif ($uri =~ /\.mp3$/) {
tag_mp3s($uri, $mode, $rating, $artist, $albumartist, $title, $album, $file_atime, $file_mtime);
}
elsif ($uri =~ /\.ogg$/) {
tag_oggs($uri, $mode, $rating, $artist, $albumartist, $title, $album, $file_atime, $file_mtime);
}
}
}
sub get_info_from_message {
my ($string) = @_;
my @array = split("\t", $string);
my ($uri, $mode, $rating) = (@array[0,1,2]);
$uri = decode('UTF-8', $uri );
return($uri, $mode, $rating);
}
sub get_track_tags {
my ($uri) = @_;
my @files = $mpd->search('filename', $uri);
my @song_tags = $files[0];
my ($albumartist, $artist, $title, $album) = $song_tags[0]->@{qw/AlbumArtist Artist Title Album/};
return($albumartist, $artist, $title, $album);
}
sub get_timestamp {
my $file_name = $_[0];
return my $stats = stat("${music_root}/$file_name");
}
sub set_timestamp {
my ($file_name, $atime, $mtime) = (@_);
utime($atime, $mtime, $file_name);
}
sub tag_flacs {
my ($uri, $mode, $rating, $artist, $albumartist, $title, $album, $atime, $mtime) = @_;
if ($mode eq "rating") {
my $fmps_rating = $rating/10;
print ":: tagging track \"${title}\" by \"${artist}\" with rating of \"${rating}\"\n";
system('metaflac', '--remove-tag=FMPS_RATING', "${music_root}/${uri}");
system('metaflac', "--set-tag=FMPS_RATING=${fmps_rating}", "${music_root}/${uri}");
} elsif ($mode eq "albumrating") {
print ":: tagging track \"${title}\" by \"${albumartist}\" with albumrating of \"${rating}\"\n";
system('metaflac', '--remove-tag=ALBUMRATING', "${music_root}/${uri}");
system('metaflac', "--set-tag=ALBUMRATING=${rating}", "${music_root}/${uri}");
}
set_timestamp("${music_root}/$uri", $atime, $mtime);
}
sub tag_mp3s {
my ($uri, $mode, $rating, $artist, $albumartist, $title, $album, $atime, $mtime) = @_;
if ($mode eq "rating") {
my $fmps_rating = $rating/10;
print ":: tagging track \"${title}\" by \"${artist}\" with rating of \"${rating}\"\n";
system('mid3v2', "--TXXX", "FMPS_RATING:${fmps_rating}", "${music_root}/${uri}");
} elsif ($mode eq "albumrating") {
print ":: tagging track \"${title}\" by \"${albumartist}\" with albumrating of \"${rating}\"\n";
system('mid3v2', "--TXXX", "ALBUMRATING:${rating}", "${music_root}/${uri}");
}
set_timestamp("${music_root}/$uri", $atime, $mtime);
}
sub tag_oggs {
my ($uri, $mode, $rating, $artist, $albumartist, $title, $album, $atime, $mtime) = @_;
my @values = `vorbiscomment "${music_root}/${uri}"`;
if ($mode eq "rating") {
my $fmps_rating = $rating/10;
@values = grep !/^FMPS_RATING=?$/, @values;
print ":: tagging track \"${title}\" by \"${artist}\" with rating of \"${rating}\"\n";
push (@values, "FMPS_RATING=$fmps_rating");
} elsif ($mode eq "albumrating") {
@values = grep !/^ALBUMRATING=?$/, @values;
print ":: tagging track \"${title}\" by \"${albumartist}\" with albumrating of \"${rating}\"\n";
push (@values, "ALBUMRATING=$rating");
}
open(my $CMD, '|-', 'vorbiscomment', '-a', "$music_root/$uri");
for my $vorbiscomment (@values) {
print $CMD "${vorbiscomment}";
}
close($CMD);
set_timestamp("${music_root}/$uri", $atime, $mtime);
}
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 $fmps_rating = system('metaflac', '--show-tag=FMPS_RATING', "${music_root}/${unrated_song}");
my $rating = $fmps_rating*10;
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};
my $fmps_rating = $rating/10;
if ($uri =~ /\.flac$/) {
system('metaflac', '--remove-tag=FMPS_RATING', "${music_root}/${uri}");
system('metaflac', "--set-tag=FMPS_RATING=$fmps_rating", "${music_root}/${uri}");
}
elsif ($uri =~ /\.mp3$/) {
system('mid3v2', "--TXXX", "FMPS_RATING:${fmps_rating}", "${music_root}/${uri}");
}
elsif ($uri =~ /\.ogg$/) {
print "!! OGG files not supported, yet\n";
}
}
}
main();