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) . "

"); } if (!xml_parse($this->parser, $this->data, feof($this->webpage))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } } xml_parser_free($this->parser); // Create URL's dynamically b/c the ones Amazon returns are a mess. if ($this->found()) { $this->itemURL = "http://www.amazon.com/exec/obidos/ASIN/" . $this->ASIN. "/" . TAG . "/"; } } // Check for Thumbnail Tag or ASIN function start_element($inParser, $inName, &$inAttributes) { $inName = trim($inName); if (($inName == $this->ThumbnailTag) && ($this->FirstThumb == YES)) { $this->ThumbnailElement = YES; $this->FirstThumb = NO; } elseif (($inName == $this->IDTag) && ($this->FirstID == YES)) { $this->IDElement = YES; $this->FirstID = NO; } else { $this->ThumbnailElement = NO; $this->IDElement = NO; } } function end_element($inParser, $inName) { $this->ThumbnailElement = NO; $this->IDElement = NO; } // Store Thumbnail URL or ASIN number. function character_data($inParser, $inData) { if ($this->ThumbnailElement == YES) { $this->imgURL .= $inData; } if ($this->IDElement == YES) { $this->ASIN .= $inData; } } // Return Thumbnail URL from Amazon. function getImage() { return $this->imgURL; } // Return Amazon link for this album. function getURL() { return $this->itemURL; } // Return ASIN number for this album. function getASIN() { return $this->ASIN; } function found() { if ($this->imgURL == "") { return FALSE; } else return TRUE; } } ?>