saAlbumCoverArt: Download album covers from Amazon

Abstract:

These set of classes allow you to use Amazon to download your music album cover images.  Your script will need to pass it the search string, which for amazon works best as “[album name] [artist]“.

Given these set of base classes you are able to add new search providers.  I’ve only written it for Amazon, but if there are others, extend the abstract class saAlbumCoverArtSearch and implement the execute() method.

Requirements:

  • PHP5 compiled with XML, file_get_contents open URL
  • An Amazon Web Services account and private keys

Download and terms:

You may download the archive free of charge.  The classes are fully documented in phpdoc style. The terms for this code is simple. It’s released under the Free Beer license. I will do my best to support the code, fix bugs, but it is provided as is with no warranty.

Download saAlbumCoverArt.tar.bz2

Example:

In it’s simplest form, here is an example:

$album = new saAlbumCoverArt(
  'Album Name',
  'Artist ',
  'path-to-save-cover-image'
)->execute();

Below is an example script showing how one could get all album covers from a music collection stored in a database, save the file to the directory of that music album, and then update the database.  This example runs from the CLI.  Changing this to run in web should be pretty trivial as lines 43 to 53 are the meat of the implementation.

#!/usr/bin/env php
<?php

$conn = mysql_connect('localhost', 'dbuser', 'dbpass')
    or die('cannot connect');
mysql_select_db('dbname');

$sql = "
  SELECT
     a.name              AS albumname,
     a.id                AS albumid,
     art.name            AS artistname,
     m.music_artist_id   AS music_artist_id,
     m.file_name         AS file_name,
     m.full_path         AS full_path
  FROM
    music as m,
    music_album AS a,
    music_artist AS art
  WHERE
    m.music_album_id = a.id      AND
    m.music_artist_id=art.id     AND
    a.cover IS NULL
";

$result = mysql_query($sql, $conn);
if (!$result || mysql_num_rows($result) == 0)
{
  echo mysql_error(); echo 'errrrrrrrrrr'; exit;
}
echo "number of albums: " . mysql_num_rows($result) . "\n";

$found = array();

while (($row = mysql_fetch_assoc($result)))
{
  // do not do duplicates
  if (isset($found[$row['albumname']])) { continue; }

  echo 'Running: ' . $row['artistname']
     . ' ' . $row['albumname'] . "\n";

  $cover = new saAlbumCoverArt(
    $row['albumname'],
    $row['artistname'],
    str_replace($row['file_name'],
      '',
      $row['full_path']
    ),
    array(
      'search' => array('basedir' => 0, 'amazon' => 1)
    )
  );
  $coverFile = $cover->execute();
  if ($coverFile === false)
  {
    $coverFile = 'none';
    echo "   None\n";
  }
  else
  {
    echo "    FOUND " . $coverFile . "\n";
  }

  // Update the database table setting the cover file name
  $sql = "
    UPDATE music_album
    SET cover='" . addslashes($coverFile) . "'
    WHERE id=" . $row['albumid']
  ;
  $result2 = mysql_query($sql, $conn);

  $found[$row['albumname']] = true;

}
echo "done \n\n";
This entry was posted in Code. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>