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 $skip_db_update :reader = 0;
25 has $dump_config :reader = 0;
27 method verb($message) {
28 return unless $self->opt->verbose;
31 method dbg($message) {
32 return unless $self->opt->verbose > 1;
36 method parse_command_line {
37 Getopt::Long::GetOptions(
38 'v|verbose+' => \$verbose,
39 'dump-config!' => \$dump_config,
40 's|single!' => \$single,
41 'one-shot!' => \$one_shot,
42 'skip-db-update!' => \$skip_db_update,
43 'tql|target-queue-length=n' => \$target_queue_length,
44 'mpd-host=s' => \$mpd_host,
45 'mpd-port=s' => \$mpd_port,
46 'db-path=s' => \$db_path,
47 'db-user=s' => \$db_user,
48 'min-album-interval=s' => sub {
49 $min_album_interval = parse_duration(pop);
51 'min-sing-interval=s' => sub {
52 $min_song_interval = parse_duration(pop);
54 'min-artist-interval=s' => sub {
55 $min_artist_interval = parse_duration(pop);
60 sub handle_config_option( $ini, $section, $option, $target_ref,
63 return undef unless exists $ini->{$section}{$option};
65 my $value = $ini->{$section}{$option};
67 $value = $converter->($value) if $converter;
69 $$target_ref = $value;
74 say "verbose = $verbose";
77 say "host = " . ( $mpd_host // '' );
78 say "port = " . ( $mpd_port // '' );
79 say "target-queue-length = $target_queue_length";
82 say "target-length = $target_queue_length";
83 say "min-song-interval = " . duration_exact($min_song_interval);
84 say "min-album-interval = " . duration_exact($min_album_interval);
85 say "min-artist-interval = " . duration_exact($min_artist_interval);
88 say "path = " . ( $db_path // '' );
89 say "user = " . ( $db_user // '' );
90 say "password = " . ( $db_password // '' );
93 method parse_config_file($path) {
94 use Config::INI::Reader;
95 my $ini = Config::INI::Reader->read_file($path);
97 handle_config_option( $ini => mpd => host => \$mpd_host );
98 handle_config_option( $ini => mpd => port => \$mpd_port );
100 handle_config_option( $ini => 'mpd-feeder' => verbose => \$verbose );
102 handle_config_option(
103 $ini => queue => 'target-length' => \$target_queue_length );
104 handle_config_option(
105 $ini => queue => 'min-song-interval' => \$min_song_interval,
108 handle_config_option(
109 $ini => queue => 'min-album-interval' => \$min_album_interval,
112 handle_config_option(
113 $ini => queue => 'min-artist-interval' => \$min_artist_interval,
117 handle_config_option( $ini => db => path => \$db_path );
118 handle_config_option( $ini => db => user => \$db_user );
119 handle_config_option( $ini => db => password => \$db_password );
121 # FIXME: complain about unknown sections/parameters
131 use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
138 Getopt::Long::Configure('pass_through');
139 Getopt::Long::GetOptions('cfg|config=s' => \$cfg_file);
140 Getopt::Long::Configure('no_pass_through');
142 $cfg_file //= DEFAULT_CONFIG_FILE if -e DEFAULT_CONFIG_FILE;
144 $opt->parse_config_file($cfg_file) if $cfg_file;
147 $opt->parse_command_line;
149 unless ($opt->dump_config) {
150 $mpd = Net::Async::MPD->new(
151 host => $opt->mpd_host,
152 port => $opt->mpd_port,
157 $self->update_db unless $self->opt->skip_db_update;
165 DBI->connect( "dbi:Pg:dbname=" . $opt->db_path,
166 $opt->db_user, $opt->db_password,
167 { RaiseError => 1, AutoCommit => 1 } );
169 $db_generation = $self->db_get_option('generation');
172 method db_get_option($name) {
173 my $sth = $db->prepare_cached("select $name from options");
175 my @result = $sth->fetchrow_array;
180 method db_set_option( $name, $value ) {
181 my $sth = $db->prepare_cached("update options set $name = ?");
182 $sth->execute($value);
185 method db_store_song($song, $artist, $album) {
186 return unless length($song) and length($artist) and length($album);
189 <<'SQL')->execute( $song, $artist, $album, $db_generation );
190 INSERT INTO songs(path, artist, album, generation)
191 VALUES($1, $2, $3, $4)
192 ON CONFLICT ON CONSTRAINT songs_pkey DO
193 UPDATE SET artist = $2
197 $db->prepare_cached(<<'SQL')->execute( $artist, $album, $db_generation );
198 INSERT INTO albums(artist, album, generation)
200 ON CONFLICT ON CONSTRAINT albums_pkey DO
201 UPDATE SET generation = $3
203 $db->prepare_cached(<<'SQL')->execute( $artist, $db_generation );
204 INSERT INTO artists(artist, generation)
206 ON CONFLICT ON CONSTRAINT artists_pkey DO
207 UPDATE SET generation = $2
211 method db_remove_stale_entries {
212 $db->prepare_cached('DELETE FROM songs WHERE generation <> ?')
213 ->execute($db_generation);
214 $db->prepare_cached('DELETE FROM albums WHERE generation <> ?')
215 ->execute($db_generation);
216 $db->prepare_cached('DELETE FROM artists WHERE generation <> ?')
217 ->execute($db_generation);
220 method db_note_song_qeued($item) {
222 'UPDATE songs SET last_queued=current_timestamp WHERE path=?')
223 ->execute( $item->{song} );
225 'UPDATE artists SET last_queued=CURRENT_TIMESTAMP WHERE artist=?')
226 ->execute( $item->{artist} );
228 'UPDATE albums SET last_queued=CURRENT_TIMESTAMP WHERE artist=? AND album=?'
229 )->execute( $item->{artist}, $item->{album} );
233 $mpd->send('listallinfo')->on_done(
240 my ($song, $artist, $album);
242 foreach my $row (@_) {
245 if ($row =~ s/^file:\s*//) {
246 $self->db_store_song( $song, $artist, $album );
248 $artist = $album = undef;
250 elsif ( $row =~ s/^Artist:\s*// ) {
253 elsif ( $row =~ s/^Album:\s*// ) {
258 $self->db_store_song($song, $artist, $album);
260 $self->db_remove_stale_entries;
262 $self->db_set_option( generation => $db_generation );
279 method db_find_suitable_songs($num) {
281 my $sth = $db->prepare_cached(<<SQL);
282 SELECT s.path, s.artist, s.album
284 JOIN artists ar ON ar.artist=s.artist
285 JOIN albums al ON al.album=s.album
286 WHERE (s.last_queued IS NULL OR s.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
287 AND (ar.last_queued IS NULL OR ar.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
288 AND (al.last_queued IS NULL OR al.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
289 AND NOT EXISTS (SELECT 1 FROM blacklisted_artists bar WHERE bar.artist = s.artist)
290 AND NOT EXISTS (SELECT 1 FROM blacklisted_albums bal WHERE bal.album = s.album)
295 $self->opt->min_song_interval,
296 $self->opt->min_artist_interval,
297 $self->opt->min_album_interval,
300 while ( my @row = $sth->fetchrow_array ) {
302 { song => $row[0], artist => $row[1], album => $row[2] };
308 method queue_songs($num = undef) {
310 $mpd->send('playlist')->on_done( sub {
311 my $present = scalar @_;
313 $self->queue_songs( $opt->target_queue_length - $present )
314 if $present < $opt->target_queue_length;
318 my @list = $self->db_find_suitable_songs($num);
322 IO::Async::Timer::Countdown->new(
324 on_expire => sub { $self->queue_songs($num) },
329 $mpd->send( [ map {"add $_->{song}"} @list ] );
330 $self->db_note_song_qeued($_) for @list;
336 my $feeder = Feeder->new();
338 $feeder->opt->dump, exit if $feeder->opt->dump_config;
340 $feeder->queue_songs(1), exit if $feeder->opt->single;
342 # FIXME: handle blacklist manipulation
344 $feeder->queue_songs;
346 exit if $feeder->opt->one_shot;
356 $feeder->queue_songs;
360 $feeder->mpd->idle(qw(database playlist));