Fixed library scanning to allow multiple artists/albums per file.

This commit is contained in:
Jonathan Bernard 2016-02-13 23:29:05 -06:00
parent 321b201cab
commit 6af528e46b

View File

@ -3,13 +3,15 @@ package com.jdbernard.wdiwtlt
import com.jdbernard.wdiwtlt.db.ORM
import com.jdbernard.wdiwtlt.db.models.*
import java.util.regex.Pattern
import org.apache.commons.codec.digest.DigestUtils
import org.jaudiotagger.audio.AudioFile
import org.jaudiotagger.audio.AudioFileIO
import org.jaudiotagger.tag.Tag as JATag
import org.jaudiotagger.tag.FieldKey
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.apache.commons.codec.digest.DigestUtils
import static org.jaudiotagger.tag.FieldKey.*
@ -85,7 +87,8 @@ public class MediaLibrary {
orm.withTransaction {
orm.create(mf)
associateWithArtistsAndAlbums(mf, artistNames, albumNames, fileTag)
associateWithArtistsAndAlbums(mf, artistNames, albumNames,
safeToInteger(fileTag.getFirst(YEAR)))
}
}
@ -101,12 +104,25 @@ public class MediaLibrary {
return [orm.getArtistByName(name)] ?: orm.getArtistsLikeName(name) }
public List<Album> getAlbumsByName(String name) {
return orm.getAlbumsByName(name) ?: orm.getAlbumsLikeName(name) }
return orm.getAlbumsWhere(name: name) ?: orm.getAlbumsLikeName(name) }
private void associateWithArtistsAndAlbums(MediaFile mf, String artistNames,
String albumNames, Integer albumYear) {
public List<Artist> splitArtist(Artist toSplit, Pattern splitPattern) {
return splitArtist(toSplit, pattern.split(toSplit.name)) }
def albums = []
public List<Artist> splitArtist(Artist toSplit, List<String> newNames) {
def albums = orm.getAlbumsWhere(artistId: toSplit.id)
def mediaFiles = orm.getMediaFilesByArtistId(toSplit.id)
toSplit.name = newNames[0]
List<Artist> newArtists = newNames[1..-1].collect { new Artist(it) }
newArtists.each { newArtist ->
albums.each { orm.associate(newArtist, it) }
mediaFiles.each { orm.associate(newArtist, it) } }
return [toSplit] + newArtists }
private void associateWithArtistsAndAlbums(MediaFile mf,
List<String> artistNames, List<String> albumNames, Integer albumYear) {
// Find or create artists.
def artists = artistNames.collect { artistName ->
@ -115,7 +131,7 @@ public class MediaLibrary {
return artist }
// Associate file with artists.
artists.each { orm.associate(artist, mf) }
artists.each { orm.associate(it, mf) }
// Find or create albums
def albums = albumNames.collect { albumName ->
@ -128,33 +144,40 @@ public class MediaLibrary {
// associated with one of the artists for this piece.
album = artists.inject(null, { foundAlbum, artist ->
if (foundAlbum) return foundAlbum
return orm.getAlbumByNameAndYearAndArtistId(albumName,
albumYear, artist.id) }
def cur = orm.getAlbumsWhere(name: albumName,
year: albumYear, artistId: artist.id)
return cur ? cur[0] : null })
// If we don't have it with one of the artists, see if we have
// one that matches the name and year.
if (!album)
album = orm.getAlbumByNameAndYear(albumName, albumYear) }
if (!album) {
def cur = orm.getAlbumsWhere(
name: albumName, year: albumYear)
album = cur ? cur[0] : null } }
else {
album = artists.inject(null, { foundAlbum, artist ->
if (foundAlbum) return foundAlbum
return orm.getAlbumByNameAndArtistId(albumName, artist.id) }
def cur = orm.getAlbumsWhere(
name: albumName, artistId: artistId)
return cur ? cur[0] : null })
if (!album) {
def foundAlbums = org.getAlbumsByName(albumName)
album = foundAlbums ? foundAlbums[0] : 0 } }
def cur = org.getAlbumsWhere(name: albumName)
album = cur ? cur[0] : 0 } }
// We still can't find the album at all. We'll need to create it
if (!album)
album = orm.create(new Album(name: albumName, albumYear))
album = orm.create(new Album(name: albumName, year: albumYear))
return album }
// Associate file with albums
albums.each { orm.associate(album, mf) }
albums.each { orm.associate(it, mf) }
// Make sure we have association between all of the artists and albums.
artists.each { artist ->
def albumsForArtist = orm.getAlbumsByArtistId(artist.id)
def albumsForArtist = orm.getAlbumsWhere(artistId: artist.id)
def albumsMissing = albums - albumsForArtist
albumsMissing.each { album -> orm.associate(artist, album) } }
}