def add_album(album_name, tags_dicts, source=str(datetime.date.today()), gordonDir=DEF_GORDON_DIR, prompt_aname=False, import_md=False, fast_import=False): """Add an album from a list of metadata in <tags_dicts> v "1.0 CSV" """ log.debug('Adding album "%s"', album_name) # create set of artists from tag_dicts artists = set() for track in tags_dicts.itervalues(): artists.add(track['artist']) if len(artists) == 0: log.debug('Nothing to add') return # no songs else: log.debug('Found %d artists in directory: %s', len(artists), artists) #add album to Album table log.debug('Album has %d tracks', len(tags_dicts)) albumrec = Album(name = album_name, trackcount = len(tags_dicts)) #if we have an *exact* string match we will use the existing artist artist_dict = dict() for artist in artists: match = Artist.query.filter_by(name=artist) if match.count() == 1 : log.debug('Matched %s to %s in database', artist, match[0]) artist_dict[artist] = match[0] #todo: (eckdoug) what happens if match.count()>1? This means we have multiple artists in db with same # name. Do we try harder to resolve which one? Or just add a new one. I added a new one (existing code) # but it seems risky.. like we will generate lots of new artists. # Anyway, we resolve this in the musicbrainz resolver.... else : # add our Artist to artist table newartist = Artist(name = artist) artist_dict[artist] = newartist #add artist to album (album_artist table) albumrec.artists.append(artist_dict[artist]) # Commit these changes in order to have access to this album # record when adding tracks. commit() # Now add our tracks to album. for filename in sorted(tags_dicts.keys()): add_track(filename, source=source, gordonDir=gordonDir, tag_dict=tags_dicts[filename], artist=artist_dict[tags_dicts[filename][u'artist']], album=albumrec, fast_import=fast_import, import_md=import_md) log.debug('Added "%s"', filename) #now update our track counts for aname, artist in artist_dict.iteritems() : artist.update_trackcount() log.debug('Updated trackcount for artist %s', artist) albumrec.update_trackcount() log.debug('Updated trackcount for album %s', albumrec) commit()
def add_album(album_name, tags_dicts, source=str(datetime.date.today()), gordonDir=DEF_GORDON_DIR, prompt_aname=False, import_md=False, fast_import=False): """Add an album from a list of metadata in <tags_dicts> v "1.0 CSV" """ log.debug('Adding album "%s"', album_name) # create set of artists from tag_dicts artists = set() for track in tags_dicts.itervalues(): artists.add(track['artist']) if len(artists) == 0: log.debug('Nothing to add') return # no songs else: log.debug('Found %d artists in directory: %s', len(artists), artists) #add album to Album table log.debug('Album has %d tracks', len(tags_dicts)) albumrec = Album(name=album_name, trackcount=len(tags_dicts)) #if we have an *exact* string match we will use the existing artist artist_dict = dict() for artist in artists: match = Artist.query.filter_by(name=artist) if match.count() == 1: log.debug('Matched %s to %s in database', artist, match[0]) artist_dict[artist] = match[0] #todo: (eckdoug) what happens if match.count()>1? This means we have multiple artists in db with same # name. Do we try harder to resolve which one? Or just add a new one. I added a new one (existing code) # but it seems risky.. like we will generate lots of new artists. # Anyway, we resolve this in the musicbrainz resolver.... else: # add our Artist to artist table newartist = Artist(name=artist) artist_dict[artist] = newartist #add artist to album (album_artist table) albumrec.artists.append(artist_dict[artist]) # Commit these changes in order to have access to this album # record when adding tracks. commit() # Now add our tracks to album. for filename in sorted(tags_dicts.keys()): add_track(filename, source=source, gordonDir=gordonDir, tag_dict=tags_dicts[filename], artist=artist_dict[tags_dicts[filename][u'artist']], album=albumrec, fast_import=fast_import, import_md=import_md) log.debug('Added "%s"', filename) #now update our track counts for aname, artist in artist_dict.iteritems(): artist.update_trackcount() log.debug('Updated trackcount for artist %s', artist) albumrec.update_trackcount() log.debug('Updated trackcount for album %s', albumrec) commit()
def add_album(albumDir, source = str(datetime.date.today()), gordonDir = DEF_GORDON_DIR, prompt_aname = False, fast_import = False, import_md=False): """Add a directory with audio files v 1.2 * when we do an album we need to read all files in before trying anything * we can't just add each track individually. We have to make Artist ids for all artists * we will presume that 2 songs by same artist string are indeed same artist """ log.debug(' Adding album "%s" - add_album()', albumDir) tags_dicts = dict() albums = set() artists = set() cwd = os.getcwd() os.chdir(albumDir) for filename in os.listdir('.') : (xxx, ext) = os.path.splitext(filename) if not os.path.isdir(os.path.join(cwd, filename)) : log.debug(' Checking "%s" ...', filename) csvtags = False # if ext.lower() == '.mp3' : # gets ID3 tags from mp3s if id3.isValidMP3(os.path.join(cwd, filename)): log.debug(' %s is MP3', filename) tags_dicts[filename] = _get_id3_dict(filename) if not tags_dicts[filename]['empty']: # non empty tags obtained :) log.debug(' .. w/ ID3 tags', tags_dicts[filename]) del(tags_dicts[filename]['empty']) tags_dicts[filename]['func'] = 'add_mp3' albums.add(tags_dicts[filename]['album']) artists.add(tags_dicts[filename]['artist']) elif ext.lower() in ['.wav', '.aif', '.aiff']: # since this is wav/aif, use possible csv tags file #todo: check for file binary content to determine wether it is wav/aiff instead of extension check... if not csvtags : csvtags = _read_csv_tags(os.getcwd(), 'tags.csv') log.debug(' %s is %s', filename, ext) try: # if csvtags[filename] is not empty: if csvtags[filename] : log.debug(' .. w/ CSV tags (tags.csv)', csvtags[filename]) tags_dicts[filename] = csvtags[filename] except: # make empty tags on the fly tags_dicts[filename] = _empty_tags() tags_dicts[filename]['func'] = 'add_uncomp' albums.add(tags_dicts[filename]['album']) artists.add(tags_dicts[filename]['artist']) else: log.debug(' %s is not a supported audio file format', filename) #todo: work with other non-mp3 audio files/tags! albums = list(albums) if len(artists) == 0 : os.chdir(cwd) log.debug(' Nothing to add') return # no songs else: log.debug(' %d artists in directory: %s', len(artists), artists) if len(albums) <> 1 : # if more than 1 album found found in ID3 tags if prompt_aname : # prompt user to choose an album album_name = _prompt_aname(albumDir, tags_dicts, albums, cwd) if album_name == False : return # why? see _prompt_aname() -- return else : os.chdir(cwd) log.debug(' Not adding %d album names in album %s %s', len(albums), albumDir, str(albums)) return # more than one album in directory else : # there's only one album in the directory (as should be) album_name = albums[0] #add our album to Album table log.debug(' Album has %d recordings', len(tags_dicts)) albumrec = Album(name = unicode(album_name), trackcount = len(tags_dicts)) # collection = None source = unicode(source) match = Collection.query.filter_by(name = source) if match.count() == 1: log.debug(' Matched source %s in database', match[0]) # collection = match[0] # else: # collection = Collection(name=unicode(source)) # albumrec.collections.append(collection) #if we have an *exact* string match we will use the existing artist artist_dict = dict() for artist in set(artists) : match = Artist.query.filter_by(name = artist) if match.count() == 1 : log.debug(' Matched %s %s in database', artist, match[0]) artist_dict[artist] = match[0] #todo: (eckdoug) what happens if match.count()>1? This means we have multiple artists in db with same # name. Do we try harder to resolve which one? Or just add a new one. I added a new one (existing code) # but it seems risky.. like we will generate lots of new artists. # Anyway, we resolve this in the musicbrainz resolver.... else : # add our Artist to artist table newartist = Artist(name = unicode(artist)) # newartist.collections.append(collection) artist_dict[artist] = newartist #add artist to album (album_artist table) albumrec.artists.append(artist_dict[artist]) commit() #commit these changes in order to have access to this album record when adding tracks #now add our tracks to album for file in tags_dicts.keys() : # calls add_mp3(), add_uncomp(), or other... addfunction = tags_dicts[file].pop('func') eval(addfunction + "(file, source, gordonDir, tags_dicts[file], artist_dict[tags_dicts[file]['artist']], albumrec, fast_import, import_md)") log.debug(' Added "%s"!', file) #now update our track counts for aname, artist in artist_dict.iteritems() : artist.update_trackcount() log.debug(' * Updated trackcount for artist %s', artist) albumrec.update_trackcount() log.debug(' * Updated trackcount for album %s', albumrec) commit() os.chdir(cwd) # and return to original working dir