2016-01-12 02:53:41 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
import logging
|
2016-01-12 02:53:41 +11:00
|
|
|
import os
|
2016-03-17 15:17:44 +11:00
|
|
|
|
|
|
|
import xbmc
|
2016-09-02 03:11:57 +10:00
|
|
|
import xbmcaddon
|
2016-03-17 15:17:44 +11:00
|
|
|
import xbmcvfs
|
|
|
|
|
2016-09-02 03:11:57 +10:00
|
|
|
from mutagen.flac import FLAC, Picture
|
2016-01-12 02:53:41 +11:00
|
|
|
from mutagen.id3 import ID3
|
|
|
|
from mutagen import id3
|
2016-09-02 03:11:57 +10:00
|
|
|
import base64
|
2016-01-12 02:53:41 +11:00
|
|
|
|
2016-03-17 15:17:44 +11:00
|
|
|
import read_embyserver as embyserver
|
2016-08-30 02:44:27 +10:00
|
|
|
from utils import window
|
2016-03-17 15:17:44 +11:00
|
|
|
|
2016-01-12 02:53:41 +11:00
|
|
|
#################################################################################################
|
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
log = logging.getLogger("EMBY."+__name__)
|
|
|
|
|
|
|
|
#################################################################################################
|
2016-01-12 02:53:41 +11:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
# Helper for the music library, intended to fix missing song ID3 tags on Emby
|
2016-01-12 02:53:41 +11:00
|
|
|
|
2016-01-21 07:17:23 +11:00
|
|
|
def getRealFileName(filename, isTemp=False):
|
2016-01-12 02:53:41 +11:00
|
|
|
#get the filename path accessible by python if possible...
|
2016-01-21 07:17:23 +11:00
|
|
|
|
|
|
|
if not xbmcvfs.exists(filename):
|
2016-08-30 02:44:27 +10:00
|
|
|
log.warn("File does not exist! %s" % filename)
|
2016-01-12 02:53:41 +11:00
|
|
|
return (False, "")
|
|
|
|
|
2016-01-18 22:05:05 +11:00
|
|
|
#if we use os.path method on older python versions (sunch as some android builds), we need to pass arguments as string
|
|
|
|
if os.path.supports_unicode_filenames:
|
|
|
|
checkfile = filename
|
|
|
|
else:
|
2016-09-02 03:11:57 +10:00
|
|
|
checkfile = filename.encode("utf-8")
|
2016-01-18 22:05:05 +11:00
|
|
|
|
2016-01-12 02:53:41 +11:00
|
|
|
# determine if our python module is able to access the file directly...
|
2016-01-18 22:05:05 +11:00
|
|
|
if os.path.exists(checkfile):
|
2016-01-12 02:53:41 +11:00
|
|
|
filename = filename
|
2016-01-18 22:05:05 +11:00
|
|
|
elif os.path.exists(checkfile.replace("smb://","\\\\").replace("/","\\")):
|
2016-01-12 02:53:41 +11:00
|
|
|
filename = filename.replace("smb://","\\\\").replace("/","\\")
|
|
|
|
else:
|
|
|
|
#file can not be accessed by python directly, we copy it for processing...
|
2016-01-21 07:17:23 +11:00
|
|
|
isTemp = True
|
2016-01-12 02:53:41 +11:00
|
|
|
if "/" in filename: filepart = filename.split("/")[-1]
|
|
|
|
else: filepart = filename.split("\\")[-1]
|
|
|
|
tempfile = "special://temp/"+filepart
|
|
|
|
xbmcvfs.copy(filename, tempfile)
|
2016-09-02 03:11:57 +10:00
|
|
|
filename = xbmc.translatePath(tempfile).decode("utf-8")
|
2016-01-12 02:53:41 +11:00
|
|
|
|
2016-01-21 07:17:23 +11:00
|
|
|
return (isTemp,filename)
|
2016-01-12 02:53:41 +11:00
|
|
|
|
|
|
|
def getEmbyRatingFromKodiRating(rating):
|
|
|
|
# Translation needed between Kodi/ID3 rating and emby likes/favourites:
|
|
|
|
# 3+ rating in ID3 = emby like
|
|
|
|
# 5+ rating in ID3 = emby favourite
|
|
|
|
# rating 0 = emby dislike
|
|
|
|
# rating 1-2 = emby no likes or dislikes (returns 1 in results)
|
|
|
|
favourite = False
|
|
|
|
deletelike = False
|
|
|
|
like = False
|
|
|
|
if (rating >= 3): like = True
|
|
|
|
if (rating == 0): like = False
|
|
|
|
if (rating == 1 or rating == 2): deletelike = True
|
|
|
|
if (rating >= 5): favourite = True
|
|
|
|
return(like, favourite, deletelike)
|
2016-01-21 06:21:56 +11:00
|
|
|
|
2016-09-02 03:11:57 +10:00
|
|
|
def getAdditionalSongTags(embyid, emby_rating, API, kodicursor, emby_db, enableimportsongrating, enableexportsongrating, enableupdatesongrating):
|
2016-03-17 15:17:44 +11:00
|
|
|
|
|
|
|
emby = embyserver.Read_EmbyServer()
|
|
|
|
|
2016-01-21 06:21:56 +11:00
|
|
|
previous_values = None
|
|
|
|
filename = API.getFilePath()
|
|
|
|
rating = 0
|
2016-09-02 03:11:57 +10:00
|
|
|
emby_rating = int(round(emby_rating, 0))
|
2016-01-21 06:21:56 +11:00
|
|
|
|
|
|
|
#get file rating and comment tag from file itself.
|
|
|
|
if enableimportsongrating:
|
|
|
|
file_rating, comment, hasEmbeddedCover = getSongTags(filename)
|
|
|
|
else:
|
|
|
|
file_rating = 0
|
|
|
|
comment = ""
|
|
|
|
hasEmbeddedCover = False
|
|
|
|
|
|
|
|
|
2016-09-02 03:11:57 +10:00
|
|
|
emby_dbitem = emby_db.getItem_byId(embyid)
|
2016-01-21 06:21:56 +11:00
|
|
|
try:
|
|
|
|
kodiid = emby_dbitem[0]
|
|
|
|
except TypeError:
|
|
|
|
# Item is not in database.
|
|
|
|
currentvalue = None
|
|
|
|
else:
|
|
|
|
query = "SELECT rating FROM song WHERE idSong = ?"
|
|
|
|
kodicursor.execute(query, (kodiid,))
|
|
|
|
try:
|
|
|
|
currentvalue = int(round(float(kodicursor.fetchone()[0]),0))
|
|
|
|
except: currentvalue = None
|
|
|
|
|
|
|
|
# Only proceed if we actually have a rating from the file
|
|
|
|
if file_rating is None and currentvalue:
|
|
|
|
return (currentvalue, comment, False)
|
|
|
|
elif file_rating is None and not currentvalue:
|
2016-09-02 03:11:57 +10:00
|
|
|
return (emby_rating, comment, False)
|
2016-01-21 06:21:56 +11:00
|
|
|
|
2016-09-02 03:11:57 +10:00
|
|
|
log.info("getAdditionalSongTags --> embyid: %s - emby_rating: %s - file_rating: %s - current rating in kodidb: %s" %(embyid, emby_rating, file_rating, currentvalue))
|
2016-01-21 06:21:56 +11:00
|
|
|
|
|
|
|
updateFileRating = False
|
|
|
|
updateEmbyRating = False
|
|
|
|
|
|
|
|
if currentvalue != None:
|
|
|
|
# we need to translate the emby values...
|
2016-09-02 03:11:57 +10:00
|
|
|
if emby_rating == 1 and currentvalue == 2:
|
|
|
|
emby_rating = 2
|
|
|
|
if emby_rating == 3 and currentvalue == 4:
|
|
|
|
emby_rating = 4
|
2016-01-21 06:21:56 +11:00
|
|
|
|
|
|
|
#if updating rating into file is disabled, we ignore the rating in the file...
|
|
|
|
if not enableupdatesongrating:
|
|
|
|
file_rating = currentvalue
|
|
|
|
#if convert emby likes/favourites convert to song rating is disabled, we ignore the emby rating...
|
|
|
|
if not enableexportsongrating:
|
2016-09-02 03:11:57 +10:00
|
|
|
emby_rating = currentvalue
|
2016-01-21 06:21:56 +11:00
|
|
|
|
2016-09-02 03:11:57 +10:00
|
|
|
if (emby_rating == file_rating) and (file_rating != currentvalue):
|
2016-01-21 06:21:56 +11:00
|
|
|
#the rating has been updated from kodi itself, update change to both emby ands file
|
|
|
|
rating = currentvalue
|
|
|
|
updateFileRating = True
|
|
|
|
updateEmbyRating = True
|
2016-09-02 03:11:57 +10:00
|
|
|
elif (emby_rating != currentvalue) and (file_rating == currentvalue):
|
2016-01-21 06:21:56 +11:00
|
|
|
#emby rating changed - update the file
|
2016-09-02 03:11:57 +10:00
|
|
|
rating = emby_rating
|
2016-01-21 06:21:56 +11:00
|
|
|
updateFileRating = True
|
2016-09-02 03:11:57 +10:00
|
|
|
elif (file_rating != currentvalue) and (emby_rating == currentvalue):
|
2016-01-21 06:21:56 +11:00
|
|
|
#file rating was updated, sync change to emby
|
|
|
|
rating = file_rating
|
|
|
|
updateEmbyRating = True
|
2016-09-02 03:11:57 +10:00
|
|
|
elif (emby_rating != currentvalue) and (file_rating != currentvalue):
|
2016-01-21 06:21:56 +11:00
|
|
|
#both ratings have changed (corner case) - the highest rating wins...
|
2016-09-02 03:11:57 +10:00
|
|
|
if emby_rating > file_rating:
|
|
|
|
rating = emby_rating
|
2016-01-21 06:21:56 +11:00
|
|
|
updateFileRating = True
|
|
|
|
else:
|
|
|
|
rating = file_rating
|
|
|
|
updateEmbyRating = True
|
|
|
|
else:
|
|
|
|
#nothing has changed, just return the current value
|
|
|
|
rating = currentvalue
|
|
|
|
else:
|
|
|
|
# no rating yet in DB
|
|
|
|
if enableimportsongrating:
|
|
|
|
#prefer the file rating
|
|
|
|
rating = file_rating
|
|
|
|
#determine if we should also send the rating to emby server
|
|
|
|
if enableexportsongrating:
|
2016-09-02 03:11:57 +10:00
|
|
|
if emby_rating == 1 and file_rating == 2:
|
|
|
|
emby_rating = 2
|
|
|
|
if emby_rating == 3 and file_rating == 4:
|
|
|
|
emby_rating = 4
|
|
|
|
if emby_rating != file_rating:
|
2016-01-21 06:21:56 +11:00
|
|
|
updateEmbyRating = True
|
|
|
|
|
|
|
|
elif enableexportsongrating:
|
|
|
|
#set the initial rating to emby value
|
2016-09-02 03:11:57 +10:00
|
|
|
rating = emby_rating
|
2016-01-21 06:21:56 +11:00
|
|
|
|
|
|
|
if updateFileRating and enableupdatesongrating:
|
|
|
|
updateRatingToFile(rating, filename)
|
|
|
|
|
|
|
|
if updateEmbyRating and enableexportsongrating:
|
|
|
|
# sync details to emby server. Translation needed between ID3 rating and emby likes/favourites:
|
|
|
|
like, favourite, deletelike = getEmbyRatingFromKodiRating(rating)
|
2016-09-02 03:11:57 +10:00
|
|
|
window("ignore-update-%s" %embyid, "true") #set temp windows prop to ignore the update from webclient update
|
|
|
|
emby.updateUserRating(embyid, favourite)
|
2016-01-12 02:53:41 +11:00
|
|
|
|
2016-01-21 06:21:56 +11:00
|
|
|
return (rating, comment, hasEmbeddedCover)
|
|
|
|
|
2016-01-12 02:53:41 +11:00
|
|
|
def getSongTags(file):
|
|
|
|
# Get the actual ID3 tags for music songs as the server is lacking that info
|
2016-01-18 22:05:05 +11:00
|
|
|
rating = 0
|
2016-01-12 02:53:41 +11:00
|
|
|
comment = ""
|
2016-01-19 05:38:53 +11:00
|
|
|
hasEmbeddedCover = False
|
2016-01-12 02:53:41 +11:00
|
|
|
|
|
|
|
isTemp,filename = getRealFileName(file)
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info( "getting song ID3 tags for " + filename)
|
2016-01-12 02:53:41 +11:00
|
|
|
|
|
|
|
try:
|
2016-01-19 05:38:53 +11:00
|
|
|
###### FLAC FILES #############
|
2016-01-12 02:53:41 +11:00
|
|
|
if filename.lower().endswith(".flac"):
|
|
|
|
audio = FLAC(filename)
|
|
|
|
if audio.get("comment"):
|
|
|
|
comment = audio.get("comment")[0]
|
2016-01-19 05:38:53 +11:00
|
|
|
for pic in audio.pictures:
|
|
|
|
if pic.type == 3 and pic.data:
|
|
|
|
#the file has an embedded cover
|
|
|
|
hasEmbeddedCover = True
|
2016-04-26 21:53:19 +10:00
|
|
|
break
|
2016-01-12 02:53:41 +11:00
|
|
|
if audio.get("rating"):
|
|
|
|
rating = float(audio.get("rating")[0])
|
|
|
|
#flac rating is 0-100 and needs to be converted to 0-5 range
|
|
|
|
if rating > 5: rating = (rating / 100) * 5
|
2016-01-19 05:38:53 +11:00
|
|
|
|
|
|
|
###### MP3 FILES #############
|
2016-01-12 02:53:41 +11:00
|
|
|
elif filename.lower().endswith(".mp3"):
|
|
|
|
audio = ID3(filename)
|
2016-01-19 05:38:53 +11:00
|
|
|
|
|
|
|
if audio.get("APIC:Front Cover"):
|
|
|
|
if audio.get("APIC:Front Cover").data:
|
|
|
|
hasEmbeddedCover = True
|
|
|
|
|
2016-01-12 02:53:41 +11:00
|
|
|
if audio.get("comment"):
|
|
|
|
comment = audio.get("comment")[0]
|
|
|
|
if audio.get("POPM:Windows Media Player 9 Series"):
|
|
|
|
if audio.get("POPM:Windows Media Player 9 Series").rating:
|
|
|
|
rating = float(audio.get("POPM:Windows Media Player 9 Series").rating)
|
|
|
|
#POPM rating is 0-255 and needs to be converted to 0-5 range
|
|
|
|
if rating > 5: rating = (rating / 255) * 5
|
|
|
|
else:
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info( "Not supported fileformat or unable to access file: %s" %(filename))
|
2016-01-16 01:31:55 +11:00
|
|
|
|
2016-01-18 22:05:05 +11:00
|
|
|
#the rating must be a round value
|
|
|
|
rating = int(round(rating,0))
|
2016-01-12 02:53:41 +11:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
#file in use ?
|
2016-08-30 02:44:27 +10:00
|
|
|
log.error("Exception in getSongTags %s" % e)
|
2016-01-19 05:38:53 +11:00
|
|
|
rating = None
|
2016-01-21 07:17:23 +11:00
|
|
|
|
2016-01-12 02:53:41 +11:00
|
|
|
#remove tempfile if needed....
|
|
|
|
if isTemp: xbmcvfs.delete(filename)
|
|
|
|
|
2016-01-19 05:38:53 +11:00
|
|
|
return (rating, comment, hasEmbeddedCover)
|
2016-01-12 02:53:41 +11:00
|
|
|
|
|
|
|
def updateRatingToFile(rating, file):
|
|
|
|
#update the rating from Emby to the file
|
|
|
|
|
2016-01-21 07:17:23 +11:00
|
|
|
f = xbmcvfs.File(file)
|
|
|
|
org_size = f.size()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
#create tempfile
|
|
|
|
if "/" in file: filepart = file.split("/")[-1]
|
|
|
|
else: filepart = file.split("\\")[-1]
|
|
|
|
tempfile = "special://temp/"+filepart
|
|
|
|
xbmcvfs.copy(file, tempfile)
|
2016-09-02 03:11:57 +10:00
|
|
|
tempfile = xbmc.translatePath(tempfile).decode("utf-8")
|
2016-01-21 07:17:23 +11:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info( "setting song rating: %s for filename: %s - using tempfile: %s" %(rating,file,tempfile))
|
2016-01-12 02:53:41 +11:00
|
|
|
|
2016-01-21 06:21:56 +11:00
|
|
|
if not tempfile:
|
2016-01-12 02:53:41 +11:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2016-01-21 06:21:56 +11:00
|
|
|
if tempfile.lower().endswith(".flac"):
|
|
|
|
audio = FLAC(tempfile)
|
2016-01-12 02:53:41 +11:00
|
|
|
calcrating = int(round((float(rating) / 5) * 100, 0))
|
|
|
|
audio["rating"] = str(calcrating)
|
|
|
|
audio.save()
|
2016-01-21 06:21:56 +11:00
|
|
|
elif tempfile.lower().endswith(".mp3"):
|
|
|
|
audio = ID3(tempfile)
|
2016-01-12 02:53:41 +11:00
|
|
|
calcrating = int(round((float(rating) / 5) * 255, 0))
|
|
|
|
audio.add(id3.POPM(email="Windows Media Player 9 Series", rating=calcrating, count=1))
|
|
|
|
audio.save()
|
|
|
|
else:
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info( "Not supported fileformat: %s" %(tempfile))
|
2016-01-12 02:53:41 +11:00
|
|
|
|
2016-01-21 06:21:56 +11:00
|
|
|
#once we have succesfully written the flags we move the temp file to destination, otherwise not proceeding and just delete the temp
|
2016-01-21 07:17:23 +11:00
|
|
|
#safety check: we check the file size of the temp file before proceeding with overwite of original file
|
|
|
|
f = xbmcvfs.File(tempfile)
|
|
|
|
checksum_size = f.size()
|
|
|
|
f.close()
|
|
|
|
if checksum_size >= org_size:
|
2016-01-12 02:53:41 +11:00
|
|
|
xbmcvfs.delete(file)
|
2016-01-21 06:21:56 +11:00
|
|
|
xbmcvfs.copy(tempfile,file)
|
2016-01-21 07:17:23 +11:00
|
|
|
else:
|
2016-08-30 02:44:27 +10:00
|
|
|
log.info( "Checksum mismatch for filename: %s - using tempfile: %s - not proceeding with file overwite!" %(rating,file,tempfile))
|
2016-01-21 07:17:23 +11:00
|
|
|
|
|
|
|
#always delete the tempfile
|
2016-01-21 06:21:56 +11:00
|
|
|
xbmcvfs.delete(tempfile)
|
2016-01-12 02:53:41 +11:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
#file in use ?
|
2016-08-30 02:44:27 +10:00
|
|
|
log.error("Exception in updateRatingToFile %s" % e)
|
2016-01-12 02:53:41 +11:00
|
|
|
|
|
|
|
|
|
|
|
|