Amarok Amazon Cover Disk Saver

From xbe wiki

Jump to: navigation, search

Contents

[edit] Introduction

The following script connects to the Amarok database and tries to move all downloaded Amazon covers to the respective directory of the corresponding album(s). It basically removes the need to refetch all the covers from Amazon once it's downloaded by copying the downloaded cover art to your mp3 directory structure. After the copying, it makes all the necessary db queries to the amarok database and removes the downloaded cover from the amarok share directory.

What you need

  • PHP interpreter (php-cli) with MySQL support
  • Amarok ready & running with MySQL, collection done, have some covers fetched from Amazon with the cover manager.

Once you ran the script successfully, you can re-run it everytime you want. The script just loops all amazon covers in the database..

[edit] Install/Usage

Just download the script to any directory you wish and run it with the php interpreter on your shell giving the required parameters.

The usage on the shell is:

php amarok_amazon_saver.php kde-dir mysql-host mysql-db mysql-user mysql-password

A call could be:

php amarok_amazon_saver.php ~/.kde localhost amarok root myrootpassword

This script is made for Amarok 1.4.6. As Amarok evolves, the database structure will possibly change (especially with the 2.x release), so the script is likely to need some adaptations.

[edit] Important notes

  • Give the script some time to make it's query at beginning ;-)
  • Don't forget that you have to run the script under a user which has delete permission on your kde directory and write permission on your mp3 directories (as it will try to copy the cover there). Normally this is the user which runs Amarok ;-)
  • I have no idea if the work that this script does is legal by the terms of Amazon. I personally just wanted to do this because I don't want to bother Amazon with downloading some 3000 albums cover every month! Just unnecessary traffic in my eyes. I just do this for my private collection! So us this at your own risk..

[edit] Get the script

Save this file as amarok_amazon_saver.php

<?php


try {
   if(!is_dir($argv[1])) {
      throw new Exception("Directory \"".$argv[1]."\" is not present. Provide a valid kde user directory (Usually ~/.kde)");
   }

   if(substr($argv[1],-1) != "/"$argv[1] .= "/";

   $coverDir $argv[1]."share/apps/amarok/albumcovers/large/";

   if(!is_dir($coverDir)) {
      throw new Exception("Amazon cover directory not found, ".$coverDir);
   }

   $conn mysql_connect($argv[2], $argv[4], $argv[5]);
   mysql_select_db($argv[3]);
   if(mysql_errno() != 0) {
       throw new Exception("Could not connect to MySQL using the provided data. Message: ".mysql_error());
   }
   
    // let's begin..
    $sql "
        SELECT t.url, t.deviceid, t.dir, am.filename amid, a.name albumname, ar.name artistname, CONCAT(d.lastmountpoint,'/',t.url) fullurl, CONCAT(d.lastmountpoint,'/',t.dir) fulldir FROM tags t
                JOIN album a ON a.id = t.album
                JOIN artist ar ON ar.id = t.artist
                JOIN amazon am ON am.filename = md5( concat( lower(ar.name), lower(a.name) ))
                JOIN devices d ON d.id = t.deviceid
                GROUP BY t.album
                ORDER BY am.filename asc;
    ";
    
    echo "Beginning process.. first query can take a while.. please wait...\n";
    
    $rs mysql_query($sql);
    $activeASIN '';
    while($resi mysql_fetch_array($rs)) {
        if($activeASIN != $resi['amid'] && $activeASIN != '') {
            // skip to next asin, delete old one..
            echo "[".$activeASIN."] Deleting Amazon image from disk & db..\n";

            if(!unlink($coverDir.$activeASIN)) {
                throw new Exception("Could not delete file ".$coverDir.$activeASIN);
            }
            
            $sql2 "
                DELETE FROM `amazon` WHERE `filename`='".$resi["amid"]."';
            ";
            mysql_query($sql2);
        }
        
        $coverTarget $resi['fulldir'].'/cover.png';
        $coverSource $coverDir.$resi['amid'];
        if(!file_exists($coverSource)) {
            echo "[".$resi['amid']."] Does not exist on file system, skipping!\n";    
        }else{
            if(!file_exists($coverTarget)) {
                echo "[".$resi['amid']."] Copy cover: ".$coverSource." => ".$coverTarget."..\n";
                if(!copy($coverSource$coverTarget)) {
                    throw new Exception("Could not copy file ".$coverSource." => ".$coverTarget);
                }
            }
            
            // enter image..
            $sql2 "
                INSERT INTO `images`
                (`path`, `deviceid`, `artist`, `album`) VALUES
                ('".mysql_escape_string($resi['dir'])."/cover.png', '".mysql_escape_string($resi['deviceid'])."', '".mysql_escape_string($resi['artistname'])."', '".mysql_escape_string($resi['albumname'])."');
            ";
            mysql_query($sql2);            
        }
        
        $activeASIN $resi['amid'];
    }

} catch (Exception $e) {
   echo "An error orcurred: ".$e->getMessage()."\n\n";
   echo "Usage: php amarok_amazon_saver.php kde-directory mysql-host mysql-db mysql-user mysql-password\n";

   
}


?>
Personal tools