Allow music to be repaired

This commit is contained in:
angelblue05 2018-04-20 03:30:08 -05:00
parent c66ca3b99c
commit 49e673a873
2 changed files with 60 additions and 12 deletions

View file

@ -61,7 +61,7 @@ class KodiMusic(KodiItems):
) )
self.cursor.execute(query, (1, 'Composer')) self.cursor.execute(query, (1, 'Composer'))
def get_artist(self, name, musicbrainz): def get_artist(self, name, musicbrainz, artist_id=None):
query = ' '.join(( query = ' '.join((
@ -75,14 +75,14 @@ class KodiMusic(KodiItems):
artist_id = result[0] artist_id = result[0]
artist_name = result[1] artist_name = result[1]
except TypeError: except TypeError:
artist_id = self._add_artist(name, musicbrainz) artist_id = self._add_artist(name, musicbrainz, artist_id)
else: else:
if artist_name != name: if artist_name != name:
self.update_artist_name(artist_id, name) self.update_artist_name(artist_id, name)
return artist_id return artist_id
def _add_artist(self, name, musicbrainz): def _add_artist(self, name, musicbrainz, artist_id=None):
query = ' '.join(( query = ' '.join((
# Safety check, when musicbrainz does not exist # Safety check, when musicbrainz does not exist
@ -95,7 +95,7 @@ class KodiMusic(KodiItems):
try: try:
artist_id = self.cursor.fetchone()[0] artist_id = self.cursor.fetchone()[0]
except TypeError: except TypeError:
artist_id = self.create_entry() artist_id = artist_id or self.create_entry()
query = ( query = (
''' '''
INSERT INTO artist(idArtist, strArtist, strMusicBrainzArtistID) INSERT INTO artist(idArtist, strArtist, strMusicBrainzArtistID)
@ -159,7 +159,40 @@ class KodiMusic(KodiItems):
) )
self.cursor.execute(query, (kodi_id, album, year)) self.cursor.execute(query, (kodi_id, album, year))
def get_album(self, name, musicbrainz=None): def validate_artist(self, kodi_id):
query = "SELECT * FROM artist WHERE idArtist = ?"
self.cursor.execute(query, (kodi_id,))
try:
kodi_id = self.cursor.fetchone()[0]
except TypeError:
kodi_id = None
return kodi_id
def validate_album(self, kodi_id):
query = "SELECT * FROM album WHERE idAlbum = ?"
self.cursor.execute(query, (kodi_id,))
try:
kodi_id = self.cursor.fetchone()[0]
except TypeError:
kodi_id = None
return kodi_id
def validate_song(self, kodi_id):
query = "SELECT * FROM song WHERE idSong = ?"
self.cursor.execute(query, (kodi_id,))
try:
kodi_id = self.cursor.fetchone()[0]
except TypeError:
kodi_id = None
return kodi_id
def get_album(self, name, musicbrainz=None, album_id=None):
if musicbrainz is not None: if musicbrainz is not None:
query = ' '.join(( query = ' '.join((
@ -174,20 +207,20 @@ class KodiMusic(KodiItems):
"SELECT idAlbum", "SELECT idAlbum",
"FROM album", "FROM album",
"WHERE strMusicBrainzAlbumID = ?" "WHERE strAlbum = ?"
)) ))
self.cursor.execute(query, (name,)) self.cursor.execute(query, (name,))
try: try:
album_id = self.cursor.fetchone()[0] album_id = self.cursor.fetchone()[0]
except TypeError: except TypeError:
album_id = self._add_album(name, musicbrainz) album_id = self._add_album(name, musicbrainz, album_id)
return album_id return album_id
def _add_album(self, name, musicbrainz): def _add_album(self, name, musicbrainz, album_id=None):
album_id = self.create_entry_album() album_id = album_id or self.create_entry_album()
query = ( query = (
''' '''
INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strReleaseType) INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strReleaseType)

View file

@ -193,8 +193,12 @@ class Music(Items):
except TypeError: except TypeError:
update_item = False update_item = False
log.debug("artistid: %s not found", itemid) log.debug("artistid: %s not found", itemid)
artistid = None
else: else:
pass if self.kodi_db.validate_artist(artistid) is None:
# item is not found, let's recreate it.
update_item = False
log.info("artistid: %s missing from Kodi, repairing the entry", artistid)
##### The artist details ##### ##### The artist details #####
lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S') lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
@ -230,7 +234,7 @@ class Music(Items):
log.info("ADD artist itemid: %s - Name: %s", itemid, name) log.info("ADD artist itemid: %s - Name: %s", itemid, name)
# safety checks: It looks like Emby supports the same artist multiple times. # safety checks: It looks like Emby supports the same artist multiple times.
# Kodi doesn't allow that. In case that happens we just merge the artist entries. # Kodi doesn't allow that. In case that happens we just merge the artist entries.
artistid = self.kodi_db.get_artist(name, musicBrainzId) artistid = self.kodi_db.get_artist(name, musicBrainzId, artistid)
# Create the reference in emby table # Create the reference in emby table
emby_db.addReference(itemid, artistid, artisttype, "artist", checksum=checksum) emby_db.addReference(itemid, artistid, artisttype, "artist", checksum=checksum)
@ -262,6 +266,12 @@ class Music(Items):
except TypeError: except TypeError:
update_item = False update_item = False
log.debug("albumid: %s not found", itemid) log.debug("albumid: %s not found", itemid)
albumid = None
else:
if self.kodi_db.validate_album(albumid) is None:
# item is not found, let's recreate it.
update_item = False
log.info("albumid: %s missing from Kodi, repairing the entry", albumid)
##### The album details ##### ##### The album details #####
lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S') lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
@ -299,7 +309,7 @@ class Music(Items):
log.info("ADD album itemid: %s - Name: %s", itemid, name) log.info("ADD album itemid: %s - Name: %s", itemid, name)
# safety checks: It looks like Emby supports the same artist multiple times. # safety checks: It looks like Emby supports the same artist multiple times.
# Kodi doesn't allow that. In case that happens we just merge the artist entries. # Kodi doesn't allow that. In case that happens we just merge the artist entries.
albumid = self.kodi_db.get_album(name, musicBrainzId) albumid = self.kodi_db.get_album(name, musicBrainzId, albumid)
# Create the reference in emby table # Create the reference in emby table
emby_db.addReference(itemid, albumid, "MusicAlbum", "album", checksum=checksum) emby_db.addReference(itemid, albumid, "MusicAlbum", "album", checksum=checksum)
@ -372,6 +382,11 @@ class Music(Items):
update_item = False update_item = False
log.debug("songid: %s not found", itemid) log.debug("songid: %s not found", itemid)
songid = self.kodi_db.create_entry_song() songid = self.kodi_db.create_entry_song()
else:
if self.kodi_db.validate_song(songid) is None:
# item is not found, let's recreate it.
update_item = False
log.info("songid: %s missing from Kodi, repairing the entry", songid)
##### The song details ##### ##### The song details #####
checksum = API.get_checksum() checksum = API.get_checksum()