jonndoe45 Posted March 1, 2009 Share Posted March 1, 2009 Here are some functions to access gracenotes via curl, to compare album/track names , just some functions i have been playing with, critique or suggestions most welcome, JD TIA <?php define('NEWLINE', "\n"); define('CACHE_PATH' , "C:\\webserver\\cache\\") ; define('SPLIT_VARIABLENAME', 0); define('SPLIT_TAG_PRESENT', 1); define('SPLIT_TAG_BEGIN', 2); define('SPLIT_TAG_END', 3); # var, present tag, begin tag, end tag $split1 = array(); $split2 = array(); $album = array(); $artist = array(); $tracks = array(); $id = array(); $split1[] = array('album', '<div class="album-name large"><strong>Album:</strong>','>','</a></div>'); $split1[] = array('artist', '<div class="artist-name"><strong>Artist:</strong>', '>', '</div>'); $split1[] = array('id', '?tui_id=', '=', '&tui_tag=">'); $split2[] = array('trackno', '<div class="track_num">', '>', '</div>'); $split2[] = array('trackname', '<div class="track_name">', '>', '</div>'); $pathname = "E:\\music\\artists, single\\athlete, tourist"; getnameinfo($pathname, $hierarchy, $hd_artist, $hd_album); $search = str_replace(' ', '+', $hd_album); $method = "album"; getalbumids($search, $method, true, $split1, $id, $album, $artist); $hdtracks = hd_gettracks($pathname, true, $hd_artist, $hd_album ); echo "ids " . count($id) . "<br>"; echo "album " . count($album) . "<br>"; echo "artist " . count($artist) . "<br>"; $compare=array(); compare_albumnames($hd_album, false, $compare, $id, $album, $artist); gettracks($id[0], true, $split2, $tracks); compare_tracks($hdtracks, $tracks, true); ################################################################################ function getalbumids($search, $method, $show, $split1, &$id, &$album, &$artist){ ################################################################################ $url= "http://www.gracenote.com/search/?query=$search\&search_type=$method"; $filename = CACHE_PATH . "search_" . $method . "_" . $search ; if ( file_exists($filename) ) { $store = file_get_contents($filename);} else { $ch = curl_init( ($url) ); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $store = curl_exec ($ch); curl_close ($ch); file_put_contents($filename, $store);} $split = explode(chr(10), $store); $max = count($split); $max2 = count($split1); if ($show){ echo "searching for album by $method called $search<br>"; } for ($n=0; $n < $max; $n++){ for ( $n2=0; $n2 < $max2; $n2++){ if ( is_numeric( stripos($split[$n], $split1[$n2][sPLIT_TAG_PRESENT])) ){ $temp = split3($split[$n], $split1[$n2][sPLIT_TAG_BEGIN], $split1[$n2][sPLIT_TAG_END]); $name = $split1[$n2][sPLIT_VARIABLENAME]; ${$name}[] = $temp; if ($show){ echo "found " . $name . "=$temp<br>"; }} } } return ;} ################################################################################ function gettracks($id, $show, $split2, &$tracks){ ################################################################################ $trackno = array(); $trackname = array(); $url= "http://www.gracenote.com/search/album_details.php?tui_id=$id&tui_tag="; if ($show){ echo "<br><br>looking up tracks for album id ".$id."<br>";} $filename = CACHE_PATH . "album_" . $id ; if ( file_exists($filename) ) { $store = file_get_contents($filename);} else { $ch = curl_init( ($url) ); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $store = curl_exec ($ch); curl_close ($ch); file_put_contents($filename, $store);} $split = explode(chr(10), $store); $max = count($split); $max2 = count($split2); for ($n=0; $n < $max; $n++){ for ( $n2=0; $n2 < $max2; $n2++){ if ( is_numeric( stripos($split[$n], $split2[$n2][sPLIT_TAG_PRESENT])) ){ $temp = split3($split[$n], $split2[$n2][sPLIT_TAG_BEGIN], $split2[$n2][sPLIT_TAG_END]); $name = $split2[$n2][sPLIT_VARIABLENAME]; ${$name}[] = $temp; if ($show){ echo "found " . $name . "=$temp<br>"; }} } } $tracks = array_merge2($trackno, $trackname); return ;} ################################################################################ function hd_gettracks($dir, $show, $hd_artist, $hd_album){ ################################################################################ $dir = @$_GET["directory"] ? realpath($_GET["directory"]) : $dir; if ($show){ echo "<br><br>reading tracks from directorty $dir<br><br>";} $last_author = ""; $last_series = ""; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $full_name = "$dir/$file"; if (is_file($full_name) AND is_numeric(stripos($full_name, ".mp3"))) { if ($show){ echo "track $file<br>"; } tidyname($file); $tracks[] = array($file); } } } } tracknofix($tracks); tracknamefix($tracks, $hd_artist, $hd_album); return $tracks;} ################################################################################ function tidyname(&$name){ ################################################################################ $search1 = array( ".mp3", "\"", "\'", "." , "!", "[", "]"); $search2 = array( "_", "=", "-", ","); $name = strtolower($name); $name = str_replace($search1, "", $name); $name = str_replace($search2, " ", $name); #get rid of double spaces $name = str_replace(" ", " ", $name); $name = str_replace(" ", " ", $name); $name = trim($name); return ;} ################################################################################ function compare_name2($name1, $name2, $show){ ################################################################################ $return = 0; $hits = 0; tidyname($name1); tidyname($name2); $split_name1 = explode(" ", $name1); $split_name2 = explode(" ", $name2); if (count($split_name2) > count($split_name1)){ swap($split_name1, $split_name2); swap($name1, $name2);} if ($show){ echo "compare: $name1 to: $name2<br>";} $max = count($split_name1); if ( $max == 1 ) { $pos = array_search($split_name1[0],$split_name2, true); if (is_numeric($pos) AND $pos == 0) { $return = 100; $hits = 1;}} else { $per = 100/($max); $hits = 0; if ($show){ echo "max is : " . ($max) . " per is : " . $per ;} for ($n = 0; $n < $max ; $n++){ $pos = array_search($split_name1[$n],$split_name2, true); if (!is_numeric($pos)){ $pos = array_search2($split_name1[$n],$split_name2);} if (is_numeric($pos)){ if ($show){ echo "pos $pos value " . $split_name2[$pos] . " n $n value " . $split_name1[$n] . "<br>";} $hits++; $return += $per;} } } if ($show){ echo "hits : " . $hits . "<br>" ;} if ($show){ echo "return is : " . $return . "<br>" ;} return $return;} ################################################################################ function compare_albumnames($hd_album, $show, &$compare, &$id, &$album, &$artist){ ################################################################################ if (!empty($id)){ $max = count($id); if ($show){ echo "<br><br>album name on HD is: " . $hd_album . "<br><br>";} for ($n=0; $n < $max; $n++){ $compare[] = round(compare_name2($hd_album, $album[$n], false));} array_multisort($compare, SORT_DESC, SORT_NUMERIC, $album, $id, $artist); if ($show){ for ($n=0; $n < $max; $n++){ echo "$n id " . $id[$n] . " artist " . $artist[$n] . " name " . $album[$n] . " % " . $compare[$n]."<br>"; }} } return ;} ################################################################################ function compare_tracks(&$hdtracks, $tracks, $show){ ################################################################################ if (!empty($hdtracks)){ $max = count($hdtracks); $max2 = count($tracks); for ($n=0; $n < $max; $n++){ $compare = array(); for ($n2 = 0; $n2 < $max2; $n2++){ $compare[] = round(compare_name2($hdtracks[$n][0], $tracks[$n2][1], false) );} array_multisort($compare, SORT_DESC, SORT_NUMERIC, $tracks); $hdtracks[$n][] = $compare[0]; $hdtracks[$n][] = $tracks[0][0]; $hdtracks[$n][] = $tracks[0][1];} if ($show){ echo "<br><br>";} for ($n=0; $n < $max; $n++){ if ($show){ echo " track " . $hdtracks[$n][0] . " original track no " . $hdtracks[$n][1] . " compare % " . $hdtracks[$n][2] . " track no " . $hdtracks[$n][3] . " track name " . $hdtracks[$n][4] . "<br>"; }} } return ;} ################################################################################ function split2($string, $begin, $beginlength, $end){ ################################################################################ $middle = false; $start = stripos($string, $begin); $stop = stripos($string, $end); if (is_numeric($start) AND is_numeric($stop)) { $length = $stop - ($start + $beginlength); $middle = substr( $string, ($start + $beginlength), $length);} return $middle;} ################################################################################ function split3($string, $begin, $end){ ################################################################################ $middle = false; $start = false; $stop = stripos($string, $end); if (is_numeric($stop)) { $start = backsearch($string, $begin, $stop);} if (is_numeric($start) AND is_numeric($stop)) { $length = $stop - ($start + 1); $middle = substr( $string, ($start + 1), $length); } return $middle;} ################################################################################ function backsearch($str, $begin, $p){ ################################################################################ while($p > -1 and $str{$p} <> $begin) $p--; if ($p == -1){ $return = false;} else { $return = $p;} return $return; } ################################################################################ function array_merge2($array1, $array2){ ################################################################################ $return = array(); $max1 = count($array1); $max2 = count($array2); if ( $max1 == $max2 and !empty($array1) ){ for ($n=0; $n < $max1; $n++){ $return[] = array($array1[$n], $array2[$n]);}} return $return;} ################################################################################ function getnameinfo($pathname, &$hierarchy, &$hd_artist, &$hd_album){ ################################################################################ $hierarchy = explode("\\", $pathname); $temp = $hierarchy[(count($hierarchy)-1)]; $count = substr_count($temp, ","); if ($count == 1){ list($hd_artist, $hd_album) = explode(",", $temp);} else { $hd_album = $temp; $hd_artist = "";} return ;} ################################################################################ function swap(&$var1, &$var2){ ################################################################################ $temp = $var1; $var1 = $var2; $var2 = $temp; return ;} ################################################################################ function array_search2($needle, $haystack){ ################################################################################ $count = count($haystack); for ($n=1;$n<$count;$n++){ if ( soundex($haystack[$n]) == soundex($needle) ){ return $n;} } return false;} ################################################################################ function tracknofix(&$tracks){ ################################################################################ $pos = array(); $poshits = array(); $poslast = array(); $count = count($tracks); for ($n=0; $n<$count; $n++){ $split = explode(' ', $tracks[$n][0]); $splits[] = $split; $count2 = count($split); for ($n2 = 0; $n2 < $count2; $n2++){ if (is_numeric($splits[$n][$n2]) AND array_search($n2, $pos) === false ) { $poshits[] = 0; $poslast[] = 0; $pos[] = $n2;} } } $count2 = count($pos); # first find all numeric columns that are ascending for ($n=0; $n<$count; $n++){ for ($n2 = 0; $n2<$count2;$n2++){ if ( isset($splits[$n][$pos[$n2]]) AND is_numeric($splits[$n][$pos[$n2]]) AND $splits[$n][$pos[$n2]] > $poslast[$n2] ) { $poslast[$n2] = $splits[$n][$pos[$n2]]; $poshits[$n2]++ ;} } } # remove these columns from the exploded array for ($n=0; $n<$count; $n++){ $tracknoremoved = false; for ($n2 = 0; $n2<$count2;$n2++){ if ( $poshits[$n2] == $count ){ if ( !$tracknoremoved ){ $tracknoremoved = true; $tracks[$n][1] = $splits[$n][$pos[$n2]];} unset( $splits[$n][$pos[$n2]] );} } } # rejoin split for ($n=0; $n<$count; $n++){ $join = implode(' ', $splits[$n]); $tracks[$n][0] = $join;} return ;} ################################################################################ function tracknamefix(&$tracks, $hd_artist, $hd_album){ ################################################################################ $count = count($tracks); for ($n=0; $n < $count; $n++){ $tracks[$n] = str_ireplace($hd_artist, '', $tracks[$n]); } return ;} ?> Link to comment https://forums.phpfreaks.com/topic/147368-phpcurl-code-for-gracenotes-cddb-access/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.