Amarok: Record Labels from MusicBrainz

This code should* select all the albums in a MySQL Amarok database for which the release date is known, look each one up from MusicBrainz and associate a record label with the album's tracks using the addLabels DCOP call.

There's no ID3 tag for 'record label', so I was thinking of using the 'Genre' field, but Amarok's generic free-text 'Label' field seems to work well enough if you're not using it for anything else, although the record label metadata won't be written to the MP3 files. is hosting a UK mirror of MusicBrainz now, so this script uses that. Change the host to "" if you're elsewhere.

* NOTE: backup your database first

#! /usr/bin/env python
import sys, time, MySQLdb, os
from musicbrainz2.webservice import Query, ReleaseFilter, ReleaseIncludes, WebService
mb = WebService(host='')
inc = ReleaseIncludes(releaseEvents=True, labels=True)
db = MySQLdb.connect(db="DB_NAME", user="DB_USER", passwd="DB_PASS", use_unicode=True, charset="utf8")
c = db.cursor()
c.execute("""SELECT as 'artist', as 'artist_id', as 'album', as 'album_id' FROM `artist` a INNER JOIN `tags` t ON = t.artist INNER JOIN `album` b ON t.album =  WHERE t.sampler = 0 AND t.year > 1 GROUP BY, ORDER BY ASC""")
print "%d rows" % c.rowcount
albums = c.fetchall()
for (artist, artist_id, album, album_id) in albums:  
  if (artist and album):
    q = Query(mb)
    print "Looking up: %s - %s" % (artist, album)
    f = ReleaseFilter(artistName=artist, title=album, limit=1)
    search_results = q.getReleases(f)
    if search_results:
      release = search_results[0].release
      print "Found: %s - %s" % (, release.title)
      release_result = q.getReleaseById(, inc)
      if (release_result and release_result.releaseEvents):      
        if release_result.releaseEvents[0].label:
          label = release_result.releaseEvents[0].label
          c.execute("""SELECT CONCAT_WS('/', d.lastmountpoint, t.url) AS track_url FROM `tags` t LEFT JOIN `devices` d ON t.deviceid = WHERE artist = %s AND album = %s ORDER by track_url ASC""", (artist_id, album_id))
          tracks = c.fetchall()
          for track_url in tracks:
            url = track_url[0].replace('"', r'\"')
            name ='"', r'\"')
            print 'addLabels "%s" [ "%s" ]' % (url, name)
            os.system('dcop amarok collection addLabels "%s" [ "%s" ]' %  (url, name))
          print "No label"  
        print "No release results"
      print "No results"
You can use something like this to display them:

$link = mysql_connect('localhost', 'DB_USER', 'DB_PASS');
$result = mysql_query("SELECT, COUNT( as c FROM tags_labels tl LEFT JOIN labels l ON tl.labelid = GROUP BY ORDER BY c DESC");
$items = array();
while($label = mysql_fetch_object($result)){
  $items[] = "<tr><td><a href='{$label->name}'>{$label->name}</a></td><td>{$label->c}</td></td>";
print "<table><tr><th>Label</th><th>Tracks</th></tr>" . implode("\n", $items) . "</table>";