Google MP3 Index finder

Filed under code, geek, projects
Tagged as , ,

Heres another little function I’m using for the new Musotik. Don’t mind the sloppy code. It searches Google for mp3 indexes and returns the results in an array. Try it out here

function getindexes($q) {
	$api = "SET YOUR GOOGLE API KEY HERE";
        $cx = "SET YOUR GOOGLE CX VAR HERE";
	$q = '-inurl:htm -inurl:html -inurl:php -inurl:asp -inurl:doc -inurl:pdf -inurl:shtml -inurl:txt AND (“index of|"last modified"|"parent of") AND mp3 -ringtone -lyric -playlist  AND '.$q;
 
	$q=urlencode($q);
	$url="https://www.googleapis.com/customsearch/v1?key=$api&cx=$cx&q=$q&alt=atom";
	$xml = new XMLReader(); 
	$xml->open($url); 
	$assoc = xml2assoc($xml); 
	$xml->close(); 
 
	$entries = array();    
	foreach($assoc[feed][0] as $node) {
 
		foreach($node[entry] as $entry) {
		   	$entries[link][] = $entry[value][id][0][value];
		   	$entries[title][] = $entry[value][title][0][value];
	    }
	}
	return $entries;
}

My return array is a little wonky here, but you can return it how ever you want. Below is a utility function used above taken from php.net

function xml2assoc($xml) {
	$assoc = null;
	while($xml->read()){
		switch ($xml->nodeType) {
			case XMLReader::END_ELEMENT: return $assoc;
			case XMLReader::ELEMENT:
				$assoc[$xml->name][] = array('value' => $xml->isEmptyElement ? '' : xml2assoc($xml));
				if($xml->hasAttributes){
					$el =& $assoc[$xml->name][count($assoc[$xml->name]) - 1];
					while($xml->moveToNextAttribute()) $el['attributes'][$xml->name] = $xml->value;
				}
			break;
			case XMLReader::TEXT:
			case XMLReader::CDATA: $assoc .= $xml->value;
		}	
	}
	return $assoc;
}

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*