Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: MusikPolice/musik
base: 0957c7d9b4
...
head fork: MusikPolice/musik
compare: 3cf455ca5a
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 52 additions and 7 deletions.
  1. +52 −7 musik/library/importer.py
View
59 musik/library/importer.py
@@ -1,6 +1,7 @@
from datetime import datetime
import mimetypes
import os
+import re
import threading
import time
@@ -143,10 +144,10 @@ def createTrack(self, uri):
self.log.error(u"One or more columns does not exist in the database. You may be able to repair it yourself.")
self.log.error(u"If you are developing and don't care about your data, just delete the musik.db file.")
# Fall through and print exception details so the user can adjust as necessary
-
+
self.log.error(u"An error occurred accessing the database: %s" % error_text)
return
-
+
if track == None:
track = Track(uri)
self.sa_session.add(track)
@@ -168,24 +169,34 @@ def createTrack(self, uri):
if artist != None:
if track.artist == None:
track.artist = artist
+ self.sa_session.add(artist)
elif track.artist.id != artist.id:
# TODO: conflict!
self.log.warning(u'Artist conflict for track %s: %s != %s', track, track.artist, artist)
- # album artist
+ # album artist - use the artist if metadata isn't set
album_artist = self.findArtist(metadata['albumartist'], metadata['albumartistsort'])
if album_artist != None:
if track.album_artist == None:
track.album_artist = album_artist
+ self.sa_session.add(album_artist)
elif track.album_artist.id != album_artist.id:
# TODO: conflict!
self.log.warning(u'Album artist conflict for track %s: %s != %s', track, track.album_artist, album_artist)
+ elif artist != None:
+ if track.album_artist == None:
+ track.album_artist = artist
+ self.sa_session.add(artist)
+ elif track.album_artist.id != artist.id:
+ # TODO: conflict!
+ self.log.warning(u'Album artist conflict for track %s: %s != %s', track, track.album_artist, artist)
# arranger
arranger = self.findArtist(metadata['arranger'])
if arranger != None:
if track.arranger == None:
track.arranger = arranger
+ self.sa_session.add(arranger)
elif track.arranger.id != arranger.id:
# TODO: conflict!
self.log.warning(u'Arranger conflict for track %s: %s != %s', track, track.arranger, arranger)
@@ -195,6 +206,7 @@ def createTrack(self, uri):
if author != None:
if track.author == None:
track.author = author
+ self.sa_session.add(author)
elif track.author.id != author.id:
# TODO: conflict!
self.log.warning(u'Author conflict for track %s: %s != %s', track, track.author, author)
@@ -204,6 +216,7 @@ def createTrack(self, uri):
if composer != None:
if track.composer == None:
track.composer = composer
+ self.sa_session.add(composer)
elif track.composer.id != composer.id:
# TODO: conflict!
self.log.warning(u'Composer conflict for track %s: %s != %s', track, track.composer, composer)
@@ -213,6 +226,7 @@ def createTrack(self, uri):
if conductor != None:
if track.conductor == None:
track.conductor = conductor
+ self.sa_session.add(conductor)
elif track.conductor.id != conductor.id:
# TODO: conflict!
self.log.warning(u'Conductor conflict for track %s: %s != %s', track, track.conductor, conductor)
@@ -222,6 +236,7 @@ def createTrack(self, uri):
if lyricist != None:
if track.lyricist == None:
track.lyricist = lyricist
+ self.sa_session.add(lyricist)
elif track.lyricist.id != lyricist.id:
# TODO: conflict!
self.log.warning(u'Lyricist conflict for track %s: %s != %s', track, track.lyricist, lyricist)
@@ -231,6 +246,7 @@ def createTrack(self, uri):
if performer != None:
if track.performer == None:
track.performer = performer
+ self.sa_session.add(performer)
elif track.performer.id != performer.id:
# TODO: conflict!
self.log.warning(u'Performer conflict for track %s: %s != %s', track, track.performer, performer)
@@ -240,6 +256,7 @@ def createTrack(self, uri):
if album != None:
if track.album == None:
track.album = album
+ self.sa_session.add(album)
elif track.album.id != album.id:
# TODO: conflict!
self.log.warning(u'Album conflict for track %s: %s != %s', track, track.album, album)
@@ -398,7 +415,7 @@ def createTrack(self, uri):
if type(id3[key]) == list:
metadata[key] = id3[key][0]
else:
- metadata[key] = id3[key]
+ metadata[key] = id3[key]
# play count can be stored in either the PCNT or the POPM frames.
# choose the largest of the two values as our official playcount.
@@ -420,12 +437,40 @@ def createTrack(self, uri):
if track.rating == None:
track.rating = int(metadata['POPM'].rating)
+ # if we couldn't determine track, album, artist from metadata, try
+ # to snag it from the path
+ # track name = file name, album title = last directory in path,
+ # artist name = second last directory in path.
+ (dirName, fileName) = os.path.split(uri)
+ (fileBaseName, fileExtension)=os.path.splitext(fileName)
+ if track.title == None:
+ track.title = fileBaseName
+ track.title_sort = fileBaseName
+
+ dirs = re.split(os.sep, dirName)
+ if len(dirs) > 0:
+ album = self.findAlbum(dirs[-1])
+ if album != None:
+ if track.album == None:
+ track.album = album
+ self.sa_session.add(album)
+
+ if len(dirs) > 1:
+ artist = self.findArtist(dirs[-2])
+ if artist != None:
+ if track.artist == None:
+ track.artist = artist
+ track.album_artist = artist
+ sa_session.add(artist)
+
self.log.info(u'Added track %s to the current session.', track)
def findArtist(self, name=None, name_sort=None, musicbrainz_id=None):
"""Searches the database for an existing artist that matches the specified criteria.
- If no existing artist can be found, a new artist is created with the criteria
+ If no existing artist can be found, a new artist is created with the criteria.
+ When a new artist is created, it is not added to the database. This is the responsibility
+ of the calling function.
"""
artist = None
if musicbrainz_id != None:
@@ -471,7 +516,6 @@ def findArtist(self, name=None, name_sort=None, musicbrainz_id=None):
artist.musicbrainz_artistid = musicbrainz_id
# add the artist object to the DB
self.log.debug(u'Artist not found in database. Created new artist %s' % artist)
- self.sa_session.add(artist)
# return the artist that we found and/or created
return artist
@@ -481,6 +525,8 @@ def findArtist(self, name=None, name_sort=None, musicbrainz_id=None):
def findAlbum(self, title=None, title_sort=None, musicbrainz_id=None, artist=None, metadata=None):
"""Searches the database for an existing album that matches the specified criteria.
If no existing album can be found, a new album is created with the criteria.
+ When a new album is created, it is not added to the database. This is the responsibility of
+ the calling function.
"""
album = None
if musicbrainz_id != None:
@@ -533,7 +579,6 @@ def findAlbum(self, title=None, title_sort=None, musicbrainz_id=None, artist=Non
if artist != None:
album.artist = artist
self.log.debug(u'Album not found in database. Created new album %s' % album)
- self.sa_session.add(album)
# we either found or created the album. now verify its metadata
if album != None and metadata != None:

No commit comments for this range

Something went wrong with that request. Please try again.