* http://purplecow.com/
* Created: January 7, 2002
* Updated: February 10, 2004
* This script is covered and released under the GNU General Public License (GPL)
* Read the license before use: http://www.gnu.org/copyleft/gpl.html
*
* I wrote this because Amazon Web Services doesn't have a feature to search titles
* of songs. CDNOW did, but then Amazon took CDNOW and threw out the great search
* feature. 'zonSearch mimics the CDNOW Song Title search.
*
* I'd appreciate it if would keep the above lines in tact,
* as well as the hidden credit and link to my site.
*
* Change Log
* v0.2 2/10/2004: Fixed offset for artist from 6 to 7; added hidden link to purplecow.com
* v0.1 1/7/2002: Original Release
*/
# this is the referal code that you want so you get your precious 5% baby!
$amazon_referer = "purplecowbook";
### NOTHING ELSE BELOW NEEDS EDITING ###
set_time_limit(300);
?>
echo "'ZonSearch The better way to find CD Tracks at Amazon
";
echo "Get the Source Code for your site
";
echo "by peter beckman <beckman SHIFT-2 purplecow.com> zonsearch
home
";
echo "
";
if (!empty($_REQUEST['q'])) {
$start = time();
$x = 1;
$output = getresults($_REQUEST['q']);
if ($output === FALSE) {
echo "Crap -- Cannot get results from Amazon. Exiting.";
exit();
}
$ret = amazon_parse_track_search_results($output);
while (($ret['total']-(25*$x))>0) {
$x++;
$output = getresults($_REQUEST['q'],$x);
if ($output === FALSE) {
echo "Crap -- Cannot get results from Amazon. Exiting.";
exit();
}
$ret2 = amazon_parse_track_search_results($output);
if (is_array($ret2)) $ret = $ret + $ret2;
if ($x>6) break;
}
if (!is_array($ret)) { print "Could not find that one ($artist - $title)\n"; }
else {
$ttime = time()-$start;
$fp = fopen("./logresults.txt","a"); fwrite($fp,stripslashes($_REQUEST['q'])."|".$ret['total']."|".$ttime."|".time()."\n"); fclose($fp);
echo " Query took {$ttime} seconds.
";
if ($ret['total']>150) echo "Found the first 150 out of {$ret['total']} matches.";
else echo "Found {$ret['total']} matches.";
echo "Artist | Album | Track |
";
while (list($x,$y)=each($ret)) {
if (!is_numeric($x)) continue;
if ($x%2==0) $ab = "a"; else $ab = "b";
echo "{$y['artist']} | {$y['album']} | {$y['track']} |
";
}
echo "
";
echo "";
#reset($ret);
#print_r($ret);
echo "
";
}
}
echo "
";
#print "\n\nend\n\n";
function amazon_parse_track_search_results($html) {
$debug = 0; // 1 for basic, 2 for full
$output = nl2br($html);
$outarray = preg_split("/\
/",$output);
$next = 0;
$x = 0; $y = 0; $match = 0;
while(list($k,$row) = each($outarray)) {
$row = trim($row);
if ($debug) $prow = htmlentities($row);
if (preg_match("/All (\d{1,6}) results for/",$row,$matches)) {
if ($debug) echo "Total Found: {$matches[1]}
";
$results['total'] = $matches[1];
continue;
}
if (preg_match("/\(\d{1,6})\.\<\/b\>/",$row,$matches)) {
$next = 1;
$mynum = $matches[1];
if ($debug) print "START:: ".$prow." ($mynum)
";
$x = 0;
continue;
}
if ($next) {
if ($debug==2) print "$x: $prow
";
if ($x==1) {
if (preg_match("/music-no-image/",$row)) $offset = 1;
elseif ($y>=10) $offset = 5;
else $offset = 0;
} elseif ($x==(4+$offset)) { // the track name
$track = strip_tags($row);
if ($debug) echo "$x: Track: {$track}
";
$results[$mynum]['track'] = $track;
} elseif ($x==(5+$offset)) { // album
preg_match_all("{B0........}x", $row, $matches);
$albumurl = $matches[0][0];
$album = trim(strip_tags($row));
if ($debug) echo "$x: Album: {$album} ($albumurl)
";
$results[$mynum]['album'] = $album;
$results[$mynum]['asin'] = $albumurl;
} elseif ($x==(7+$offset)) { // artist
$artist = trim(preg_replace("/~/","",strip_tags($row)));
if ($debug) echo "$x: Artist: {$artist}
";
$results[$mynum]['artist'] = $artist;
} else {
#echo "$x: $prow
";
}
$x++;
if ($x>=15) { $y++; $x = 0; $match = 0; $next = 0;}
}
}
return $results; // none found
}
function getresults($q,$pg=1,$sz=25) {
/**
* http://www.amazon.com/exec/obidos/search-handle-url/?url=index%3Dmusic-tracks&search-type=quick-search&field-keywords=".urlencode($q)."&sz=".$sz."&pg=".$pg."Go.x=11&Go.y=8&Go=Go
*
* Old URL:
* "http://www.amazon.com/exec/obidos/search-handle-url/ix=music-tracks&rank=%2Btitlerank&fqp=keywords%01".urlencode($q)."&nsp=Go%01Go%21&sz=".$sz."&pg=".$pg."/ref=s_m_np/002-1120767-9079251";
*/
$url =
"http://www.amazon.com/exec/obidos/search-handle-url/?url=index%3Dmusic-tracks&search-type=quick-search&field-keywords=".urlencode($q)."&sz=".$sz."&pg=".$pg."Go.x=11&Go.y=8&Go=Go";
print "Page $pg | ";
$fp = fopen($url, "r") or print("can't open $url");
if ($fp !== FALSE) {
while(!feof($fp)) {
$s = fgets($fp,4096);
$output .= $s;
}
fclose($fp);
} else {
return FALSE;
}
#echo "";
#echo htmlentities($output);
#echo "
";
return $output;
}