name = substr("$artist:$album", 0, $this->nameLimit);
if (DEBUG == YES) { print ("name: $this->name
");}
$this->dsn = "mysql://$this->SQLuser:$this->SQLpass@localhost/$this->dbname";
$this->db = DB::connect($this->dsn);
if (DB::isError($this->db)) {
die($this->db->getMessage());
}
//SQl escaping
$this->SQL = "SELECT * FROM $this->SQLtable WHERE Name ='$this->name';";
$this->result = $this->db->query($this->SQL);
if (DB::isError($this->result)) {
print("huh?
");
die($this->result->getMessage());
}
$this->row = $this->result->fetchRow(DB_FETCHMODE_ASSOC);
if (DB::isError($this->row)) {
die($this->row->getMessage());
}
// Album not found in SQL database
if (is_null($this->row)) {
// Search Amazon
$this->agent = new SearchAgent;
$this->agent->search($artist, $album);
if ($this->agent->found()) {
// store relevant info
$this->imgURL = $this->agent->getImage();
$this->URL = $this->agent->getURL();
$this->ASIN = $this->agent->getASIN();
// fix with SQL-escaping
$this->SQL = "INSERT INTO $this->SQLtable (ASIN, URL, imgURL, Name) ";
$this->SQL .= "VALUES ('$this->ASIN', '$this->URL', '$this->imgURL', '$this->name');";
}
// Create not-found information and store in the table.
else {
$this->imgURL = $this->blankImgURL;
$this->URL = "http://www.amazon.com/exec/obidos/redirect-home/" . TAG;
$this->SQL = "INSERT INTO amazon (URL, imgURL, Name) VALUES ('$this->URL', '$this->imgURL', '$this->name');";
}
$this->result = $this->db->query($this->SQL);
if (DB::iserror($this->result)) {
print ("insertion error
");
die($this->result->getMessage());
}
}
// Album found in SQL database
else {
$this->name = $this->row['Name'];
$this->imgURL = $this->row['imgURL'];
$this->URL = $this->row['URL'];
}
}
// Return image found by Lookup().
function getImage() {
return $this->imgURL;
}
// Return URL found by Lookup().
function getURL() {
return $this->URL;
}
}
/* Perform a keyword search for artist and album name using the
* XML interface to Amazon Web Services. Parse out the URL for the album
* and thumbnail image, along with the ASIN number (Amazon's unique identifier).
* search($artist, $album) is used to perform lookups, and data is returned through
* getImage(), getURL(), and getASIN(). */
class SearchAgent {
var $SearchType = 'lite'; // don't return too much info from Amazon
var $Token = token; // Web Services Developer token. Amazon won't return results without it.
// These are set to YES when the XML parser is within the appropriate element.
var $ThumbnailElement = NO;
var $IDElement = NO;
// The XML tags Amazon uses to represent this info.
var $ThumbnailTag = "IMAGEURLMEDIUM";
var $IDTag = "ASIN";
// Amazon often returns multiple results for a search. We choose the first every time.
var $FirstThumb = YES;
var $FirstID = YES;
// The data we're doing all this work to get.
var $ASIN = "";
var $itemURL = "";
var $imgURL = "";
// XML paper-pushing.
var $webpage; // XML response from Amazon.
var $data; // XML data in string form for xml_parse().
var $parser; // XML parser;
// Arbitrary block size for XML data.
var $AmazonLimit = 16384;
function search($artist, $album) {
// Load XML page from Amazon
$this->URL = "http://xml.amazon.com/onca/xml3?t=" . TAG;
$this->URL .= "&dev-t=$this->Token&KeywordSearch=$album%20$artist&mode=music&type=$this->SearchType&page=1&f=xml";
if (DEBUG == YES) { print ("URL: $this->URL
");}
$this->webpage = fopen($this->URL, "r");
// Register parser with this object and parse away.
$this->parser = xml_parser_create();
xml_set_object($this->parser, &$this);
xml_set_element_handler($this->parser, 'start_element', 'end_element');
xml_set_character_data_handler($this->parser, 'character_data');
while ($this->data = fread($this->webpage, $this->AmazonLimit)) {
if (DEBUG == YES) { // View raw XML
print ("PURE XML:
" . htmlentities($this->data) . "