2015-07-22 13:16:08 +00:00
# -*- coding: utf-8 -*-
2015-05-07 22:04:40 +00:00
#################################################################################################
2015-05-11 13:02:34 +00:00
# WriteKodiMusicDB
2015-05-07 22:04:40 +00:00
#################################################################################################
2015-07-22 13:16:08 +00:00
import sqlite3
from datetime import datetime
2015-07-24 07:52:36 +00:00
from ntpath import split as ntsplit
2015-05-07 22:04:40 +00:00
import xbmc
import xbmcgui
import xbmcaddon
2015-07-22 13:16:08 +00:00
from ClientInformation import ClientInformation
import Utils as utils
from API import API
2015-05-07 22:04:40 +00:00
from PlayUtils import PlayUtils
from ReadKodiDB import ReadKodiDB
from ReadEmbyDB import ReadEmbyDB
2015-06-05 22:11:24 +00:00
from TextureCache import TextureCache
2015-05-07 22:04:40 +00:00
class WriteKodiMusicDB ( ) :
2015-06-05 22:11:24 +00:00
textureCache = TextureCache ( )
2015-07-22 13:16:08 +00:00
kodiversion = int ( xbmc . getInfoLabel ( " System.BuildVersion " ) [ : 2 ] )
addon = xbmcaddon . Addon ( )
addonName = ClientInformation ( ) . getAddonName ( )
WINDOW = xbmcgui . Window ( 10000 )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
username = WINDOW . getProperty ( ' currUser ' )
userid = WINDOW . getProperty ( ' userId %s ' % username )
server = WINDOW . getProperty ( ' server %s ' % username )
directpath = addon . getSetting ( ' useDirectPaths ' ) == " true "
def logMsg ( self , msg , lvl = 1 ) :
className = self . __class__ . __name__
utils . logMsg ( " %s %s " % ( self . addonName , className ) , msg , int ( lvl ) )
2015-05-07 22:04:40 +00:00
2015-07-30 19:23:50 +00:00
def addOrUpdateArtistToKodiLibrary ( self , MBitem , connection , cursor ) :
2015-05-07 22:04:40 +00:00
# If the item already exist in the local Kodi DB we'll perform a full item update
# If the item doesn't exist, we'll add it to the database
2015-07-30 19:23:50 +00:00
embyId = MBitem [ " Id " ]
2015-07-22 13:16:08 +00:00
cursor . execute ( " SELECT kodi_id FROM emby WHERE emby_id = ? " , ( embyId , ) )
try :
artistid = cursor . fetchone ( ) [ 0 ]
except :
2015-05-07 22:04:40 +00:00
artistid = None
2015-07-22 13:16:08 +00:00
##### The artist details #####
lastScraped = datetime . now ( ) . strftime ( ' % Y- % m- %d % H: % M: % S ' )
dateadded = API ( ) . getDateCreated ( MBitem )
checksum = API ( ) . getChecksum ( MBitem )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
name = MBitem [ ' Name ' ]
musicBrainzId = API ( ) . getProvider ( MBitem , " musicBrainzArtist " )
genres = " / " . join ( MBitem . get ( ' Genres ' ) )
bio = API ( ) . getOverview ( MBitem )
# Associate artwork
2015-05-08 10:21:11 +00:00
thumb = API ( ) . getArtwork ( MBitem , " Primary " )
if thumb :
2015-07-22 13:16:08 +00:00
thumb = " <thumb> %s </thumb> " % thumb
2015-05-08 10:21:11 +00:00
fanart = API ( ) . getArtwork ( MBitem , " Backdrop " )
if fanart :
2015-07-22 13:16:08 +00:00
fanart = " <fanart> %s </fanart> " % fanart
2015-07-30 19:23:50 +00:00
2015-07-22 13:16:08 +00:00
##### UPDATE THE ARTIST #####
if artistid :
self . logMsg ( " UPDATE artist to Kodi library, Id: %s - Artist: %s " % ( embyId , name ) , 1 )
query = " UPDATE artist SET strArtist = ?, strMusicBrainzArtistID = ?, strGenres = ?, strBiography = ?, strImage = ?, strFanart = ?, lastScraped = ?, dateadded = ? WHERE idArtist = ? "
cursor . execute ( query , ( name , musicBrainzId , genres , bio , thumb , fanart , lastScraped , dateadded , artistid ) )
# Update the checksum in emby table
query = " UPDATE emby SET checksum = ? WHERE emby_id = ? "
cursor . execute ( query , ( checksum , embyId ) )
##### OR ADD THE ARTIST #####
2015-05-07 22:04:40 +00:00
else :
2015-07-22 13:16:08 +00:00
self . logMsg ( " ADD artist to Kodi library, Id: %s - Artist: %s " % ( embyId , name ) , 1 )
2015-07-30 19:23:50 +00:00
#safety checks: It looks like Emby supports the same artist multiple times in the database while Kodi doesn't allow that. In case that happens we just merge the artist in the Kodi database.
# Safety check 1: does the artist already exist?
cursor . execute ( " SELECT idArtist FROM artist WHERE strArtist = ? COLLATE NOCASE " , ( name , ) )
try :
artistid = cursor . fetchone ( ) [ 0 ]
self . logMsg ( " Artist already exists in Kodi library - appending to existing object, Id: %s - Artist: %s - MusicBrainzId: %s - existing Kodi Id: %s " % ( embyId , name , musicBrainzId , str ( artistid ) ) , 1 )
except : pass
# Safety check 2: does the MusicBrainzArtistId already exist?
cursor . execute ( " SELECT idArtist FROM artist WHERE strMusicBrainzArtistID = ? " , ( musicBrainzId , ) )
try :
artistid = cursor . fetchone ( ) [ 0 ]
self . logMsg ( " Artist already exists in Kodi library - appending to existing object, Id: %s - Artist: %s - MusicBrainzId: %s - existing Kodi Id: %s " % ( embyId , name , musicBrainzId , str ( artistid ) ) , 1 )
except : pass
if not artistid :
# Create the artist
cursor . execute ( " select coalesce(max(idArtist),0) as artistid from artist " )
artistid = cursor . fetchone ( ) [ 0 ] + 1
query = " INSERT INTO artist(idArtist, strArtist, strMusicBrainzArtistID, strGenres, strBiography, strImage, strFanart, lastScraped, dateAdded) values(?, ?, ?, ?, ?, ?, ?, ?, ?) "
cursor . execute ( query , ( artistid , name , musicBrainzId , genres , bio , thumb , fanart , lastScraped , dateadded ) )
2015-07-22 13:16:08 +00:00
# Create the reference in emby table
query = " INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?) "
cursor . execute ( query , ( embyId , artistid , " artist " , checksum ) )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
# Update artwork
2015-05-07 22:04:40 +00:00
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Primary " ) , artistid , " artist " , " thumb " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Primary " ) , artistid , " artist " , " poster " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Banner " ) , artistid , " artist " , " banner " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Logo " ) , artistid , " artist " , " clearlogo " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Art " ) , artistid , " artist " , " clearart " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Thumb " ) , artistid , " artist " , " landscape " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Disc " ) , artistid , " artist " , " discart " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Backdrop " ) , artistid , " artist " , " fanart " , cursor )
2015-07-30 19:23:50 +00:00
def addOrUpdateAlbumToKodiLibrary ( self , MBitem , connection , cursor ) :
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
kodiVersion = self . kodiversion
2015-05-08 10:21:11 +00:00
2015-07-30 19:23:50 +00:00
embyId = MBitem [ " Id " ]
2015-05-07 22:04:40 +00:00
# If the item already exist in the local Kodi DB we'll perform a full item update
# If the item doesn't exist, we'll add it to the database
2015-07-22 13:16:08 +00:00
cursor . execute ( " SELECT kodi_id FROM emby WHERE emby_id = ? " , ( embyId , ) )
try :
albumid = cursor . fetchone ( ) [ 0 ]
except :
2015-05-07 22:04:40 +00:00
albumid = None
2015-07-22 13:16:08 +00:00
genres = MBitem . get ( ' Genres ' )
2015-07-26 12:09:09 +00:00
2015-07-22 13:16:08 +00:00
##### The album details #####
lastScraped = datetime . now ( ) . strftime ( ' % Y- % m- %d % H: % M: % S ' )
dateadded = API ( ) . getDateCreated ( MBitem )
checksum = API ( ) . getChecksum ( MBitem )
name = MBitem [ ' Name ' ]
musicBrainzId = API ( ) . getProvider ( MBitem , " musicBrainzAlbum " )
year = MBitem . get ( ' ProductionYear ' )
genre = " / " . join ( genres )
bio = API ( ) . getOverview ( MBitem )
2015-05-07 22:04:40 +00:00
MBartists = [ ]
2015-07-22 13:16:08 +00:00
for item in MBitem [ ' AlbumArtists ' ] :
MBartists . append ( item [ ' Name ' ] )
2015-05-07 22:04:40 +00:00
artists = " / " . join ( MBartists )
2015-07-22 13:16:08 +00:00
# Associate the artwork
2015-05-08 10:21:11 +00:00
thumb = API ( ) . getArtwork ( MBitem , " Primary " )
if thumb :
2015-07-22 13:16:08 +00:00
thumb = " <thumb> %s </thumb> " % thumb
2015-05-08 11:16:21 +00:00
2015-07-22 13:16:08 +00:00
##### UPDATE THE ALBUM #####
if albumid :
self . logMsg ( " UPDATE album to Kodi library, Id: %s - Title: %s " % ( embyId , name ) , 1 )
2015-07-24 11:29:41 +00:00
if kodiVersion == 15 or kodiVersion == 16 :
2015-07-22 13:16:08 +00:00
# Kodi Isengard
query = " UPDATE album SET strAlbum = ?, strMusicBrainzAlbumID = ?, strArtists = ?, iYear = ?, strGenres = ?, strReview = ?, strImage = ?, lastScraped = ?, dateAdded = ?, strReleaseType = ? WHERE idAlbum = ? "
cursor . execute ( query , ( name , musicBrainzId , artists , year , genre , bio , thumb , lastScraped , dateadded , " album " , albumid ) )
2015-05-08 10:21:11 +00:00
else :
2015-07-22 13:16:08 +00:00
# Kodi Gotham and Helix
query = " UPDATE album SET strAlbum = ?, strMusicBrainzAlbumID = ?, strArtists = ?, iYear = ?, strGenres = ?, strReview = ?, strImage = ?, lastScraped = ?, dateAdded = ? WHERE idAlbum = ? "
cursor . execute ( query , ( name , musicBrainzId , artists , year , genre , bio , thumb , lastScraped , dateadded , albumid ) )
# Update the checksum in emby table
query = " UPDATE emby SET checksum = ? WHERE emby_id = ? "
cursor . execute ( query , ( checksum , embyId ) )
##### OR ADD THE ALBUM #####
2015-05-07 22:04:40 +00:00
else :
2015-07-22 13:16:08 +00:00
self . logMsg ( " ADD album to Kodi library, Id: %s - Title: %s " % ( embyId , name ) , 1 )
# Safety check: does the strMusicBrainzAlbumID already exist?
cursor . execute ( " SELECT idAlbum FROM album WHERE strMusicBrainzAlbumID = ? " , ( musicBrainzId , ) )
2015-05-09 17:16:49 +00:00
try :
2015-07-22 13:16:08 +00:00
albumid = cursor . fetchone ( ) [ 0 ]
except :
# Create the album
cursor . execute ( " select coalesce(max(idAlbum),0) as albumid from album " )
albumid = cursor . fetchone ( ) [ 0 ] + 1
2015-07-24 11:29:41 +00:00
if kodiVersion == 15 or kodiVersion == 16 :
2015-07-22 13:16:08 +00:00
# Kodi Isengard
query = " INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strArtists, iYear, strGenres, strReview, strImage, lastScraped, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
cursor . execute ( query , ( albumid , name , musicBrainzId , artists , year , genre , bio , thumb , lastScraped , dateadded , " album " ) )
2015-05-09 17:16:49 +00:00
else :
2015-07-22 13:16:08 +00:00
# Kodi Gotham and Helix
query = " INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strArtists, iYear, strGenres, strReview, strImage, lastScraped, dateAdded) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
cursor . execute ( query , ( albumid , name , musicBrainzId , artists , year , genre , bio , thumb , lastScraped , dateadded ) )
# Create the reference in emby table
query = " INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?) "
cursor . execute ( query , ( embyId , albumid , " album " , checksum ) )
# Add genres
self . AddGenresToMedia ( albumid , genres , " album " , cursor )
2015-05-09 17:16:49 +00:00
2015-07-22 13:16:08 +00:00
# Update artwork
2015-05-07 22:04:40 +00:00
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Primary " ) , albumid , " album " , " thumb " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " BoxRear " ) , albumid , " album " , " poster " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Banner " ) , albumid , " album " , " banner " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Logo " ) , albumid , " album " , " clearlogo " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Art " ) , albumid , " album " , " clearart " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Thumb " ) , albumid , " album " , " landscape " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Disc " ) , albumid , " album " , " discart " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Backdrop " ) , albumid , " album " , " fanart " , cursor )
2015-07-22 13:16:08 +00:00
# Link album to artists
if MBartists :
album_artists = MBitem [ ' AlbumArtists ' ]
elif MBitem . get ( ' ArtistItems ' ) :
album_artists = MBitem [ ' ArtistItems ' ]
2015-05-08 00:44:32 +00:00
for artist in album_artists :
2015-07-22 13:16:08 +00:00
cursor . execute ( " SELECT kodi_id FROM emby WHERE emby_id = ? " , ( artist [ ' Id ' ] , ) )
try :
artistid = cursor . fetchone ( ) [ 0 ]
except : pass
else :
query = " INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?) "
cursor . execute ( query , ( artistid , albumid , artist [ ' Name ' ] ) )
# Update discography
query = " INSERT OR REPLACE INTO discography(idArtist, strAlbum, strYear) values(?, ?, ?) "
cursor . execute ( query , ( artistid , name , str ( year ) ) )
2015-05-07 22:04:40 +00:00
2015-07-30 19:23:50 +00:00
def addOrUpdateSongToKodiLibrary ( self , MBitem , connection , cursor ) :
2015-07-26 12:09:09 +00:00
2015-07-22 13:16:08 +00:00
kodiVersion = self . kodiversion
2015-07-30 19:23:50 +00:00
embyId = MBitem [ " Id " ]
2015-05-07 22:04:40 +00:00
# If the item already exist in the local Kodi DB we'll perform a full item update
# If the item doesn't exist, we'll add it to the database
2015-07-22 13:16:08 +00:00
cursor . execute ( " SELECT kodi_id FROM emby WHERE emby_id = ? " , ( embyId , ) )
try :
songid = cursor . fetchone ( ) [ 0 ]
except :
2015-05-07 22:04:40 +00:00
songid = None
2015-07-22 13:16:08 +00:00
timeInfo = API ( ) . getTimeInfo ( MBitem )
userData = API ( ) . getUserData ( MBitem )
genres = MBitem . get ( ' Genres ' )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
##### The song details #####
playcount = userData . get ( ' PlayCount ' )
lastplayed = userData . get ( ' LastPlayedDate ' )
dateadded = API ( ) . getDateCreated ( MBitem )
checksum = API ( ) . getChecksum ( MBitem )
name = MBitem [ ' Name ' ]
musicBrainzId = API ( ) . getProvider ( MBitem , " musicBrainzTrackId " )
genre = " / " . join ( genres )
artists = " / " . join ( MBitem . get ( ' Artists ' ) )
track = MBitem . get ( ' IndexNumber ' )
year = MBitem . get ( ' ProductionYear ' )
bio = API ( ) . getOverview ( MBitem )
duration = timeInfo . get ( ' TotalTime ' )
# Get the path and filename
playurl = PlayUtils ( ) . directPlay ( MBitem )
2015-07-26 12:09:09 +00:00
2015-07-22 13:16:08 +00:00
try :
2015-07-24 07:52:36 +00:00
path , filename = ntsplit ( playurl )
2015-07-26 12:09:09 +00:00
if " / " in playurl :
2015-07-24 07:52:36 +00:00
path = " %s / " % path
2015-07-26 12:09:09 +00:00
elif " \\ " in playurl :
path = " %s \\ " % path
2015-07-22 13:16:08 +00:00
except : # playurl returned false - using server streaming path, because could not figure out plugin paths for music DB
playurl = PlayUtils ( ) . directstream ( MBitem , self . server , embyId , " Audio " )
filename = " stream.mp3 "
path = playurl . replace ( filename , " " )
# Validate the path in database
cursor . execute ( " SELECT idPath as pathid FROM path WHERE strPath = ? " , ( path , ) )
try :
pathid = cursor . fetchone ( ) [ 0 ]
except :
cursor . execute ( " select coalesce(max(idPath),0) as pathid from path " )
pathid = cursor . fetchone ( ) [ 0 ] + 1
query = " INSERT INTO path(idPath, strPath) values(?, ?) "
cursor . execute ( query , ( pathid , path ) )
# Get the album
cursor . execute ( " SELECT kodi_id FROM emby WHERE emby_id = ? " , ( MBitem . get ( " AlbumId " ) , ) )
try :
2015-05-08 00:44:32 +00:00
albumid = cursor . fetchone ( ) [ 0 ]
2015-07-22 13:16:08 +00:00
except :
# No album found, create a single's album
cursor . execute ( " select coalesce(max(idAlbum),0) as albumid from album " )
albumid = cursor . fetchone ( ) [ 0 ] + 1
2015-07-24 11:29:41 +00:00
if kodiVersion == 15 or kodiVersion == 16 :
2015-07-22 13:16:08 +00:00
# Kodi Isengard
query = " INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?) "
cursor . execute ( query , ( albumid , artists , genre , year , dateadded , " single " ) )
2015-05-08 10:21:11 +00:00
else :
2015-07-22 13:16:08 +00:00
# Kodi Gotham and Helix
query = " INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded) values(?, ?, ?, ?, ?) "
cursor . execute ( query , ( albumid , artists , genre , year , dateadded ) )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
# Link album to artists
for artist in MBitem [ ' ArtistItems ' ] :
cursor . execute ( " SELECT kodi_id FROM emby WHERE emby_id = ? " , ( artist [ ' Id ' ] , ) )
try :
artistid = cursor . fetchone ( ) [ 0 ]
except : pass
else :
query = " INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?) "
cursor . execute ( query , ( artistid , albumid , artist [ ' Name ' ] ) )
##### UPDATE THE SONG #####
if songid :
self . logMsg ( " UPDATE song to Kodi library, Id: %s - Title: %s " % ( embyId , name ) , 1 )
query = " UPDATE song SET idAlbum = ?, strArtists = ?, strGenres = ?, strTitle = ?, iTrack = ?, iDuration = ?, iYear = ?, strFilename = ?, strMusicBrainzTrackID = ?, iTimesPlayed = ?, lastplayed = ? WHERE idSong = ? "
cursor . execute ( query , ( albumid , artists , genre , name , track , duration , year , filename , musicBrainzId , playcount , lastplayed , songid ) )
# Update the checksum in emby table
query = " UPDATE emby SET checksum = ? WHERE emby_id = ? "
cursor . execute ( query , ( checksum , embyId ) )
##### OR ADD THE SONG #####
2015-05-07 22:04:40 +00:00
else :
2015-07-22 13:16:08 +00:00
self . logMsg ( " ADD song to Kodi library, Id: %s - Title: %s " % ( embyId , name ) , 1 )
# Create the song
cursor . execute ( " select coalesce(max(idSong),0) as songid from song " )
songid = cursor . fetchone ( ) [ 0 ] + 1
query = " INSERT INTO song(idSong, idAlbum, idPath, strArtists, strGenres, strTitle, iTrack, iDuration, iYear, strFileName, strMusicBrainzTrackID, iTimesPlayed, lastplayed) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
cursor . execute ( query , ( songid , albumid , pathid , artists , genre , name , track , duration , year , filename , musicBrainzId , playcount , lastplayed ) )
# Create the reference in emby table
query = " INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?) "
cursor . execute ( query , ( embyId , songid , " song " , checksum ) )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
# Add genres
self . AddGenresToMedia ( songid , genres , " song " , cursor )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
# Link song to album
2015-05-07 22:04:40 +00:00
if albumid :
2015-07-22 13:16:08 +00:00
query = " INSERT OR REPLACE INTO albuminfosong(idAlbumInfoSong, idAlbumInfo, iTrack, strTitle, iDuration) values(?, ?, ?, ?, ?) "
cursor . execute ( query , ( songid , albumid , track , name , duration ) )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
# Link song to artist
for artist in MBitem . get ( ' ArtistItems ' ) :
cursor . execute ( " SELECT kodi_id FROM emby WHERE emby_id = ? " , ( artist [ ' Id ' ] , ) )
try :
artistid = cursor . fetchone ( ) [ 0 ]
except : pass
else :
query = " INSERT OR REPLACE INTO song_artist(idArtist, idSong, strArtist) values(?, ?, ?) "
cursor . execute ( query , ( artistid , songid , artist [ ' Name ' ] ) )
2015-05-07 22:04:40 +00:00
2015-07-22 13:16:08 +00:00
# Update artwork
2015-05-07 22:04:40 +00:00
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Primary " ) , songid , " song " , " thumb " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Primary " ) , songid , " song " , " poster " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Banner " ) , songid , " song " , " banner " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Logo " ) , songid , " song " , " clearlogo " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Art " ) , songid , " song " , " clearart " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Thumb " ) , songid , " song " , " landscape " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Disc " ) , songid , " song " , " discart " , cursor )
self . addOrUpdateArt ( API ( ) . getArtwork ( MBitem , " Backdrop " ) , songid , " song " , " fanart " , cursor )
2015-07-22 13:16:08 +00:00
def deleteItemFromKodiLibrary ( self , id , connection , cursor ) :
2015-05-07 22:04:40 +00:00
cursor . execute ( " SELECT kodi_id, media_type FROM emby WHERE emby_id=? " , ( id , ) )
2015-07-22 13:16:08 +00:00
try :
result = cursor . fetchone ( )
2015-05-07 22:04:40 +00:00
kodi_id = result [ 0 ]
media_type = result [ 1 ]
2015-07-22 13:16:08 +00:00
except : pass
else :
if " artist " in media_type :
self . logMsg ( " Deleting artist from Kodi library, Id: %s " % id , 1 )
2015-05-07 22:23:49 +00:00
cursor . execute ( " DELETE FROM artist WHERE idArtist = ? " , ( kodi_id , ) )
2015-07-22 13:16:08 +00:00
elif " song " in media_type :
self . logMsg ( " Deleting song from Kodi library, Id: %s " % id , 1 )
2015-05-07 22:23:49 +00:00
cursor . execute ( " DELETE FROM song WHERE idSong = ? " , ( kodi_id , ) )
2015-07-22 13:16:08 +00:00
elif " album " in media_type :
self . logMsg ( " Deleting album from Kodi library, Id: %s " % id , 1 )
2015-05-07 22:23:49 +00:00
cursor . execute ( " DELETE FROM album WHERE idAlbum = ? " , ( kodi_id , ) )
2015-07-22 13:16:08 +00:00
# Delete the record in emby table
2015-05-07 22:04:40 +00:00
cursor . execute ( " DELETE FROM emby WHERE emby_id = ? " , ( id , ) )
def addOrUpdateArt ( self , imageUrl , kodiId , mediaType , imageType , cursor ) :
2015-07-22 13:16:08 +00:00
2015-05-07 22:04:40 +00:00
if imageUrl :
2015-07-22 13:16:08 +00:00
cacheimage = False
cursor . execute ( " SELECT url FROM art WHERE media_id = ? AND media_type = ? AND type = ? " , ( kodiId , mediaType , imageType , ) )
try :
url = cursor . fetchone ( ) [ 0 ]
except : # Image does not exists
cacheimage = True
self . logMsg ( " Adding Art Link for kodiId: %s ( %s ) " % ( kodiId , imageUrl ) , 1 )
query = " INSERT INTO art(media_id, media_type, type, url) values(?, ?, ?, ?) "
cursor . execute ( query , ( kodiId , mediaType , imageType , imageUrl ) )
2015-05-07 22:04:40 +00:00
else :
2015-07-22 13:16:08 +00:00
if url != imageUrl :
cacheimage = True
self . logMsg ( " Updating Art Link for kodiId: %s ( %s ) -> ( %s ) " % ( kodiId , url , imageUrl ) , 1 )
query = " UPDATE art SET url = ? WHERE media_id = ? and media_type = ? and type = ? "
cursor . execute ( query , ( imageUrl , kodiId , mediaType , imageType ) )
2015-06-05 22:11:24 +00:00
2015-07-22 13:16:08 +00:00
# Cache fanart textures in Kodi texture cache
if cacheimage and " fanart " in imageType :
2015-06-05 22:11:24 +00:00
self . textureCache . CacheTexture ( imageUrl )
2015-05-07 22:04:40 +00:00
def AddGenresToMedia ( self , id , genres , mediatype , cursor ) :
if genres :
for genre in genres :
2015-07-30 19:23:50 +00:00
cursor . execute ( " SELECT idGenre as idGenre FROM genre WHERE strGenre = ? COLLATE NOCASE " , ( genre , ) )
2015-07-22 13:16:08 +00:00
try :
2015-05-07 22:04:40 +00:00
idGenre = cursor . fetchone ( ) [ 0 ]
2015-07-22 13:16:08 +00:00
except : # Create the genre
cursor . execute ( " select coalesce(max(idGenre),0) as idGenre from genre " )
idGenre = cursor . fetchone ( ) [ 0 ] + 1
query = " INSERT INTO genre(idGenre, strGenre) values(?, ?) "
cursor . execute ( query , ( idGenre , genre ) )
finally : # Assign the genre to item
if " album " in mediatype :
query = " INSERT OR REPLACE INTO album_genre(idGenre, idAlbum) values(?, ?) "
cursor . execute ( query , ( idGenre , id ) )
elif " song " in mediatype :
query = " INSERT OR REPLACE INTO song_genre(idGenre, idSong) values(?, ?) "
cursor . execute ( query , ( idGenre , id ) )