User:OliverCharles/LookupDirectoryViaTracktimes: Difference between revisions
From MusicBrainz Wiki
Jump to navigationJump to search
((Imported from MoinMoin)) |
m (1 revision(s)) |
Latest revision as of 07:54, 15 March 2009
A python shell script to lookup a directory on a custom MusicBrainz server - mainly work by Faux.
#!/usr/bin/python """ MusicBrainz album lookup from track times A handy little script ported to Python by myself, from FauxFaux's original PHP script to lookup a cuesheet. Given a directory name, this script will find all MP3s in the directory, and try and find an album that matches the tracks. Thanks Faux! """ import sys from os import listdir from os.path import join, isfile import psycopg2 from mutagen.mp3 import MP3, HeaderNotFoundError from mutagen.flac import FLAC # ------------------------------------------------------------------------------ # Configuration stuff # ------------------------------------------------------------------------------ DBHOST = 'localhost' # The database hosting the *modded* tables DBNAME = 'musicbrainz_db' # The musicbrainz database name DBUSER = 'musicbrainz_user' # The user who can access the musicbrainz DB DBPASS = '' # A password for that user, if it requires one DIR = sys.argv[1] # ------------------------------------------------------------------------------ # Extract track times # ------------------------------------------------------------------------------ tracks = [] files = listdir(DIR) files.sort() for f in files: if isfile(join(DIR, f)): try: thing = MP3(join(DIR, f)) except HeaderNotFoundError: continue # Only handles MP3s atm tracks.append(thing.info.length * 1000) # ------------------------------------------------------------------------------ # Query the databes # ------------------------------------------------------------------------------ conn = psycopg2.connect('dbname=%s host=%s user=%s password=%s' % (DBNAME, DBHOST, DBUSER, DBPASS)) sql = """ SELECT DISTINCT album.id,artist.name,album.name as album FROM album_tracklist JOIN album ON (album.id = album_tracklist.album) JOIN artist ON (artist.id = album.artist) WHERE track_count = %i """ % (len(tracks)) number = 0 for track in tracks: sql += """ AND (tracklist[%i] BETWEEN %i AND %i) """ % (number + 1, track - 40000, track + 40000) number += 1 sql += "ORDER BY album.name LIMIT 20" cursor = conn.cursor() cursor.execute(sql) # ------------------------------------------------------------------------------ # Present the results # ------------------------------------------------------------------------------ rows = cursor.fetchall() if len(rows) > 0: print "I found %i matching albums" % (len(rows)) if len(rows) > 1: counter = 1 for row in rows: print " * %i. %s - %s" % (counter, row[1], row[2]) counter += 1 print "Please select an album: " albumNumber = int(sys.stdin.readline()) - 1 else: albumNumber = 0 albumRow = rows[albumNumber] print "Ok, I think that %s is %s by %s" % ('some dir', albumRow[2], albumRow[1])