7 use Syntax::Keyword::Try;
10 use Time::Duration qw(duration_exact);
11 use Time::Duration::Parse qw(parse_duration);
12 has $target_queue_length :reader = 10;
13 has $mpd_host :reader = undef;
14 has $mpd_port :reader = undef;
15 has $db_path :reader = 'mpd-feeder';
16 has $db_user :reader = undef;
17 has $db_password :reader = undef;
18 has $min_album_interval :reader = parse_duration('5h');
19 has $min_song_interval :reader = parse_duration('13d');
20 has $min_artist_interval :reader = parse_duration('1h 15m');
21 has $verbose :reader = 0;
22 has $single :reader = 0;
23 has $one_shot :reader = 0;
24 has $dump_config :reader = 0;
26 method verb($message) {
27 return unless $self->opt->verbose;
30 method dbg($message) {
31 return unless $self->opt->verbose > 1;
35 method parse_command_line {
36 Getopt::Long::GetOptions(
37 'v|verbose+' => \$verbose,
38 'dump-config!' => \$dump_config,
39 's|single!' => \$single,
40 'one-shot!' => \$one_shot,
41 'tql|target-queue-length=n' => \$target_queue_length,
42 'mpd-host=s' => \$mpd_host,
43 'mpd-port=s' => \$mpd_port,
44 'db-path=s' => \$db_path,
45 'db-user=s' => \$db_user,
46 'min-album-interval=s' => sub {
47 $min_album_interval = parse_duration(pop);
49 'min-sing-interval=s' => sub {
50 $min_song_interval = parse_duration(pop);
52 'min-artist-interval=s' => sub {
53 $min_artist_interval = parse_duration(pop);
58 sub handle_config_option( $ini, $section, $option, $target_ref,
61 return undef unless exists $ini->{$section}{$option};
63 my $value = $ini->{$section}{$option};
65 $value = $converter->($value) if $converter;
67 $$target_ref = $value;
72 say "verbose = $verbose";
75 say "host = " . ( $mpd_host // '' );
76 say "port = " . ( $mpd_port // '' );
77 say "target-queue-length = $target_queue_length";
80 say "target-length = $target_queue_length";
81 say "min-song-interval = " . duration_exact($min_song_interval);
82 say "min-album-interval = " . duration_exact($min_album_interval);
83 say "min-artist-interval = " . duration_exact($min_artist_interval);
86 say "path = " . ( $db_path // '' );
87 say "user = " . ( $db_user // '' );
88 say "password = " . ( $db_password // '' );
91 method parse_config_file($path) {
92 use Config::INI::Reader;
93 my $ini = Config::INI::Reader->read_file($path);
95 handle_config_option( $ini => mpd => host => \$mpd_host );
96 handle_config_option( $ini => mpd => port => \$mpd_port );
98 handle_config_option( $ini => 'mpd-feeder' => verbose => \$verbose );
100 handle_config_option(
101 $ini => queue => 'target-length' => \$target_queue_length );
102 handle_config_option(
103 $ini => queue => 'min-song-interval' => \$min_song_interval,
106 handle_config_option(
107 $ini => queue => 'min-album-interval' => \$min_album_interval,
110 handle_config_option(
111 $ini => queue => 'min-artist-interval' => \$min_artist_interval,
115 handle_config_option( $ini => db => path => \$db_path );
116 handle_config_option( $ini => db => user => \$db_user );
117 handle_config_option( $ini => db => password => \$db_password );
119 # FIXME: complain about unknown sections/parameters
129 use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
136 Getopt::Long::Configure('pass_through');
137 Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
138 Getopt::Long::Configure('no_pass_through');
140 $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
142 $opt->parse_config_file($cfg_file) if $cfg_file;
145 $opt->parse_command_line;
147 unless ($opt->dump_config) {
148 $mpd = Net::Async::MPD->new(
149 host => $opt->mpd_host,
150 port => $opt->mpd_port,
163 DBI->connect( "dbi:Pg:dbname=" . $opt->db_path,
164 $opt->db_user, $opt->db_password,
165 { RaiseError => 1, AutoCommit => 1 } );
167 $db_generation = $self->db_get_option('generation');
170 method db_get_option($name) {
171 my $sth = $db->prepare_cached("select $name from options");
173 my @result = $sth->fetchrow_array;
178 method db_set_option( $name, $value ) {
179 my $sth = $db->prepare_cached("update options set $name = ?");
180 $sth->execute($value);
183 method db_store_song($song, $artist, $album) {
184 return unless length($song) and length($artist) and length($album);
187 <<'SQL')->execute( $song, $artist, $album, $db_generation );
188 INSERT INTO songs(path, artist, album, generation)
189 VALUES($1, $2, $3, $4)
190 ON CONFLICT ON CONSTRAINT songs_pkey DO
191 UPDATE SET artist = $2
195 $db->prepare_cached(<<'SQL')->execute( $artist, $album, $db_generation );
196 INSERT INTO albums(artist, album, generation)
198 ON CONFLICT ON CONSTRAINT albums_pkey DO
199 UPDATE SET generation = $3
201 $db->prepare_cached(<<'SQL')->execute( $artist, $db_generation );
202 INSERT INTO artists(artist, generation)
204 ON CONFLICT ON CONSTRAINT artists_pkey DO
205 UPDATE SET generation = $2
209 method db_remove_stale_entries {
210 $db->prepare_cached('DELETE FROM songs WHERE generation <> ?')
211 ->execute($db_generation);
212 $db->prepare_cached('DELETE FROM albums WHERE generation <> ?')
213 ->execute($db_generation);
214 $db->prepare_cached('DELETE FROM artists WHERE generation <> ?')
215 ->execute($db_generation);
218 method db_note_song_qeued($item) {
220 'UPDATE songs SET last_queued=current_timestamp WHERE path=?')
221 ->execute( $item->{song} );
223 'UPDATE artists SET last_queued=CURRENT_TIMESTAMP WHERE artist=?')
224 ->execute( $item->{artist} );
226 'UPDATE albums SET last_queued=CURRENT_TIMESTAMP WHERE artist=? AND album=?'
227 )->execute( $item->{artist}, $item->{album} );
231 $mpd->send('listallinfo')->on_done(
238 my ($song, $artist, $album);
240 foreach my $row (@_) {
243 if ($row =~ s/^file:\s*//) {
244 $self->db_store_song( $song, $artist, $album );
246 $artist = $album = undef;
248 elsif ( $row =~ s/^Artist:\s*// ) {
251 elsif ( $row =~ s/^Album:\s*// ) {
256 $self->db_store_song($song, $artist, $album);
258 $self->db_remove_stale_entries;
260 $self->db_set_option( generation => $db_generation );
277 method db_find_suitable_songs($num) {
279 my $sth = $db->prepare_cached(<<SQL);
280 SELECT s.path, s.artist, s.album
282 JOIN artists ar ON ar.artist=s.artist
283 JOIN albums al ON al.album=s.album
284 WHERE (s.last_queued IS NULL OR s.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
285 AND (ar.last_queued IS NULL OR ar.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
286 AND (al.last_queued IS NULL OR al.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
287 AND NOT EXISTS (SELECT 1 FROM blacklisted_artists bar WHERE bar.artist = s.artist)
288 AND NOT EXISTS (SELECT 1 FROM blacklisted_albums bal WHERE bal.album = s.album)
293 $self->opt->min_song_interval,
294 $self->opt->min_artist_interval,
295 $self->opt->min_album_interval,
298 while ( my @row = $sth->fetchrow_array ) {
300 { song => $row[0], artist => $row[1], album => $row[2] };
306 method queue_songs($num = undef) {
308 $mpd->send('playlist')->on_done( sub {
309 my $present = scalar @_;
311 $self->queue_songs( $opt->target_queue_length - $present )
312 if $present < $opt->target_queue_length;
316 my @list = $self->db_find_suitable_songs($num);
320 IO::Async::Timer::Countdown->new(
322 on_expire => sub { $self->queue_songs($num) },
327 $mpd->send( [ map {"add $_->{song}"} @list ] );
328 $self->db_note_song_qeued($_) for @list;
334 my $feeder = Feeder->new();
336 $feeder->opt->dump, exit if $feeder->opt->dump_config;
338 $feeder->queue_songs(1), exit if $feeder->opt->single;
340 # FIXME: handle blacklist manipulation
342 $feeder->queue_songs;
344 exit if $feeder->opt->one_shot;
354 $feeder->queue_songs;
358 $feeder->mpd->idle(qw(database playlist));