Amarok, MySQL, JSON and Greasemonkey

·

The nice thing about Amarok as a media library, apart from all the functions in the player, is that it can store library data in a MySQL database (the default is SQLite), making it accessible to external scripting. For example, you can have a PHP script like this, running locally (requires php-json; set the username, password and database name as appropriate):


<?php
$artist = $_GET['artist'];
$link = mysql_connect('localhost', 'DB_USER', 'DB_PASS');
mysql_select_db('DB_NAME');
$result = mysql_query("SELECT b.name as album FROM `artist` a JOIN `tags` t ON a.id = t.artist JOIN `album` b ON t.album = b.id WHERE a.name = '$artist' GROUP by album");
$albums = array();
while($album = mysql_fetch_object($result)){
    $albums[] = $album;
}
print json_encode($albums); 
mysql_close($link);
?>

called by a Greasemonkey script like this (edit the URL for the local script as appropriate), and it will show all the albums in your collection by a particular artist, right in the web page. To use the Greasemonkey script on a different site, just change the XPath selector so that it finds the artist name and add the site URL pattern to the list of included pages.