0

我正在尝试使用在这里找到的用 Perl 编写的脚本,但它遇到了错误。我已经联系了作者,但还没有回复。我希望你能帮助我。

错误如下:

错误:无法将 /home/bruno321/.config/Clementine/albumcovers/271b13967f57caba893b366730337fb03439c60a.jpg 复制到 /media/mp3_1/Musica/Amarok/Two%20Lone%20Swordsmen/A%20Bag%20of%20Blue%20Sparks/。错误:文件或目录不存在

我相信错误在于处理空格,因为在没有空格的目录中它运行正常,但是它不会将文件重命名为“cover.jpg”,因为我相信脚本打算这样做(我当然需要它这样做)。

#!/usr/bin/perl

use strict;
use warnings;
use Carp;

use DBI;
use strict;

use Data::Dumper;
use File::Basename;
use File::Copy;

my $db_filename = shift(@ARGV);
$db_filename
  || croak
"missing mandatory param: sqlite filename\n(try ~/.config/Clementine/clementine.db\n";

if ( !-f $db_filename ) {
    croak "no such database file: $db_filename\n";
}

my $force_rewrite = shift(@ARGV) || 0;

my $dbh =
  DBI->connect( "dbi:SQLite:$db_filename", q{}, q{},
    { 'RaiseError' => 1, 'AutoCommit' => 1 } );

my $query = q{SELECT artist,album, art_automatic,art_manual,filename
             FROM songs WHERE art_manual IS NOT NULL GROUP BY(album)  ORDER BY artist};

my $sth = $dbh->prepare($query);
$sth || croak 'prepare error: ' . $dbh->errstr . "\n";

$sth->execute || croak 'execute error: ' . $dbh->errstr . "\n";

my %treated = ();
while ( my $res = $sth->fetchrow_hashref ) {

    ( undef, my $dest_dir, undef ) = fileparse( $res->{'filename'} );

    next if $treated{$dest_dir};
    $treated{$dest_dir}++;

    # strip leading file://  -> should do this better
    $dest_dir =~ s/^file:\/\///g;

    my $dest_file = $dest_dir . 'cover.jpg';

    # unless we are given overwrite option, skip dirs that have covers
    if ( !$force_rewrite ) {
        next if ( -f $dest_file );
    }

    #print "CMD: cp $res->{'art_manual'} $dest_file\n";
    copy( $res->{'art_manual'}, $dest_dir )
      || printf( "ERROR: unable to copy %s to %s. error: %s\n",
        $res->{'art_manual'}, $dest_dir, $! );
}
exit;
4

1 回答 1

7

显然$dest_dir是 URL 编码的,你需要它是文字的。换句话说,修剪file://前缀后,您需要将任何%NN十六进制转义解码为相应的文字字符。请参阅使用 Perl,如何在网络上解码或创建那些 %-encodings?基本上,你想

use URI::Escape 'uri_unescape';

接着

$dest_dir = uri_unescape($dest_dir);

此外,您正在创建$dest_file但随后忽略它并将文件复制到$dest_dir而不是毕竟。

作为一个额外的风格问题,警告和错误应该发送到 stderr,而不是 stdout。

于 2013-08-14T20:28:07.197 回答