]> git.ktnx.net Git - mpd-feeder.git/blobdiff - bin/mpd-feeder
fix interval handling when finding suitable songs
[mpd-feeder.git] / bin / mpd-feeder
index 6cc9c196dcf02d7cee1488e367251237cef97139..c6792e8329cdaf4650b0d7e891d0dc2445c54b91 100755 (executable)
@@ -159,7 +159,8 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
     method connect_db {
         return if $db;
 
-        $db = DBI->connect( "dbi:" . $opt->db_path,
+        $db =
+            DBI->connect( "dbi:Pg:dbname=" . $opt->db_path,
             $opt->db_user, $opt->db_password,
             { RaiseError => 1, AutoCommit => 1 } );
 
@@ -185,7 +186,7 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
         $db->prepare_cached(
             <<'SQL')->execute( $song, $artist, $album, $db_generation );
 INSERT INTO songs(path, artist, album, generation)
-VALUES($1, $2, $3, $3)
+VALUES($1, $2, $3, $4)
 ON CONFLICT ON CONSTRAINT songs_pkey DO
 UPDATE SET artist = $2
          , album = $3
@@ -275,23 +276,23 @@ SQL
 
     method db_find_suitable_songs($num) {
         my @result;
-        my $sth = $self->db->prepare_cached(<<SQL);
+        my $sth = $db->prepare_cached(<<SQL);
 SELECT s.path, s.artist, s.album
 FROM songs s
 JOIN artists ar ON ar.artist=s.artist
 JOIN albums al ON al.album=s.album
-WHERE (s.last_queued IS NULL OR s.last_queued < CURRENT_TIMESTAMP - CAST(? AS float))
-  AND (ar.last_queued IS NULL OR ar.last_queued < CURRENT_TIMESTAMP - CAST(? AS float))
-  AND (al.last_queued IS NULL OR al.last_queued < CURRENT_TIMESTAMP - CAST(? AS float))
+WHERE (s.last_queued IS NULL OR s.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
+  AND (ar.last_queued IS NULL OR ar.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
+  AND (al.last_queued IS NULL OR al.last_queued < CURRENT_TIMESTAMP - (? || ' seconds')::interval)
   AND NOT EXISTS (SELECT 1 FROM blacklisted_artists bar WHERE bar.artist = s.artist)
   AND NOT EXISTS (SELECT 1 FROM blacklisted_albums  bal WHERE bal.album  = s.album)
 ORDER BY random()
 LIMIT ?
 SQL
         $sth->execute(
-            $self->opt->min_song_interval / 3600.0 / 24.0,
-            $self->opt->min_artist_interval / 3600.0 / 24.0,
-            $self->opt->min_album_interval / 3600.0 / 24.0,
+            $self->opt->min_song_interval,
+            $self->opt->min_artist_interval,
+            $self->opt->min_album_interval,
             $num,
         );
         while ( my @row = $sth->fetchrow_array ) {