Rhythmbox XML Parsing to Copy Playlists to Phone or USB Stick

I was recently faced with the issue of physically moving all the songs I’d given five stars in Rhythmbox to a phone.

As it happens that stuff is stored in an XML file (at least the version I’m using in Ubuntu 16.04).

This is my PHP solution:

function xmlToArr($xml){
        $simple_xml_obj = @simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
        // Not valid XML
        if(empty($simple_xml_obj))
            return [];
        $json = json_encode($simple_xml_obj);
        return json_decode($json, true);
}
    
$arr = xmlToArr(file_get_contents('~/.local/share/rhythmbox/playlists.xml'));
//print_r($arr['playlist'][5]['location']);
  
foreach($arr['playlist'][6]['location'] as $file){
	$fname = urldecode(array_pop(explode('/', $file)));
	$file = urldecode(str_replace(['file://', '!'], ['', '\!'], $file));
	$target = "/feting/for_phone/$fname";
	if(file_exists($target)){
		continue;
	}
	echo $file."\n";
	//exit;
	copy($file, $target);
	//exit; 
}

Related Posts

Tags: , ,