This Python script will select the titles and artists of all albums in a (MySQL) Amarok library, look them up in MusicBrainz and store the release date, ASIN and MBIDs of the best match for each album in a new table.
#! /usr/bin/env python
import sys, time, MySQLdb
from musicbrainz2.webservice import Query, ReleaseFilter
db = MySQLdb.connect(db="amarok", user="YOUR_DB_USER", passwd="YOUR_DB_PASSWORD", use_unicode=True, charset="utf8")
c = db.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS `musicbrainz` (`album_id` int(11) NOT NULL, `mb_album_id` varchar(255) default NULL, `artist_id` int(11) NOT NULL, `mb_artist_id` varchar(255) default NULL, `artist_name` text default NULL, `album_title` text default NULL, `asin` varchar(255) default NULL, `date` varchar(255) default NULL, `tracks` int(4) default NULL, UNIQUE KEY `artist_album` (`album_id`,`artist_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;""")
c.execute("""SELECT a.name as 'artist', a.id as 'artist_id', b.name as 'album', b.id as 'album_id' FROM `artist` a INNER JOIN `tags` t ON a.id = t.artist INNER JOIN `album` b ON t.album = b.id LEFT JOIN `musicbrainz` m ON t.album = m.album_id AND t.artist = m.artist_id WHERE m.album_id IS NULL AND t.sampler = 0 GROUP BY a.id, b.id ORDER BY a.name ASC""")
print "%d rows" % c.rowcount
for (artist, artist_id, album, album_id) in c.fetchall():
if (artist and album):
print artist_id, album_id
time.sleep(1)
q = Query()
print "Looking up: %s - %s" % (artist, album)
f = ReleaseFilter(artistName=artist, title=album, limit=1)
results = q.getReleases(f)
if results:
for result in results:
release = result.release
rartist = release.artist
if release.releaseEvents:
release_date = release.releaseEvents[0].date
else:
release_date = 0
print release.id, album_id, rartist.id, artist_id, rartist.name, release.title, release.asin, release_date, release.tracksCount
c.execute ("""INSERT IGNORE INTO musicbrainz (mb_album_id, album_id, mb_artist_id, artist_id, artist_name, album_title, asin, date, tracks) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)""", (release.id, album_id, rartist.id, artist_id, rartist.name, release.title, release.asin, release_date, release.tracksCount))
else:
c.execute ("""INSERT IGNORE INTO musicbrainz (album_id, artist_id) VALUES (%s, %s)""", (album_id, artist_id))
print
c.close()
db.close()