Use playing item filename and path to find ID
- Fixes #67 - Everything else doesn 't work
This commit is contained in:
parent
be63414893
commit
cfbc7f770c
3 changed files with 115 additions and 127 deletions
|
@ -792,86 +792,83 @@ class Kodidb_Functions():
|
||||||
ids.append(row[0])
|
ids.append(row[0])
|
||||||
return ids
|
return ids
|
||||||
|
|
||||||
def getIdFromTitle(self, itemdetails):
|
def getIdFromFilename(self, filename, path):
|
||||||
"""
|
"""
|
||||||
Returns the Kodi id (e.g. idMovie, idEpisode) from the item's
|
Returns the tuple (itemId, type) where
|
||||||
title (c00), if there is exactly ONE found for the itemtype.
|
itemId: Kodi DB unique Id for either movie or episode
|
||||||
(None otherwise)
|
type: either 'movie' or 'episode'
|
||||||
|
|
||||||
itemdetails is the data['item'] response from Kodi
|
Returns None if not found OR if too many entries were found
|
||||||
|
|
||||||
itemdetails for movies:
|
|
||||||
{
|
|
||||||
"title":"Kung Fu Panda",
|
|
||||||
"type":"movie",
|
|
||||||
"year":2008
|
|
||||||
}
|
|
||||||
|
|
||||||
itemdetails for episodes:
|
|
||||||
{
|
|
||||||
"episode":5
|
|
||||||
"season":5,
|
|
||||||
"showtitle":"Girls",
|
|
||||||
"title":"Queen for Two Days",
|
|
||||||
"type":"episode"
|
|
||||||
}
|
|
||||||
"""
|
"""
|
||||||
try:
|
query = ' '.join((
|
||||||
typus = itemdetails['type']
|
"SELECT idFile, idPath",
|
||||||
except:
|
"FROM files",
|
||||||
|
"WHERE strFilename = ?"
|
||||||
|
))
|
||||||
|
self.cursor.execute(query, (filename,))
|
||||||
|
files = self.cursor.fetchall()
|
||||||
|
if len(files) == 0:
|
||||||
|
self.logMsg('Did not find any file, abort', 1)
|
||||||
return
|
return
|
||||||
|
query = ' '.join((
|
||||||
if typus == 'movie':
|
"SELECT strPath",
|
||||||
query = ' '.join((
|
"FROM path",
|
||||||
"SELECT idMovie",
|
"WHERE idPath = ?"
|
||||||
"FROM movie",
|
))
|
||||||
"WHERE c00 = ?"
|
# result will contain a list of all idFile with matching filename and
|
||||||
))
|
# matching path
|
||||||
|
result = []
|
||||||
|
for file in files:
|
||||||
|
# Use idPath to get path as a string
|
||||||
|
self.cursor.execute(query, (file[1],))
|
||||||
try:
|
try:
|
||||||
rows = self.cursor.execute(query, (itemdetails['title'],))
|
strPath = self.cursor.fetchone()[0]
|
||||||
except:
|
except TypeError:
|
||||||
return
|
# idPath not found; skip
|
||||||
elif typus == 'episode':
|
continue
|
||||||
query = ' '.join((
|
# For whatever reason, double might have become triple
|
||||||
"SELECT idShow",
|
strPath = strPath.replace('///', '//')
|
||||||
"FROM tvshow",
|
strPath = strPath.replace('\\\\\\', '\\\\')
|
||||||
"WHERE c00 = ?"
|
if strPath == path:
|
||||||
))
|
result.append(file[0])
|
||||||
try:
|
if len(result) == 0:
|
||||||
rows = self.cursor.execute(query, (itemdetails['showtitle'],))
|
self.logMsg('Did not find matching paths, abort', 1)
|
||||||
except:
|
return
|
||||||
return
|
self.logMsg('Result: %s' % result)
|
||||||
ids = []
|
# Kodi seems to make ONE temporary entry; we only want the earlier,
|
||||||
for row in rows:
|
# permanent one
|
||||||
ids.append(row[0])
|
if len(result) > 2:
|
||||||
if len(ids) > 1:
|
self.logMsg('We found too many items with matching filenames and '
|
||||||
self.logMsg('No unique match possible. Rows: %s' % rows, 1)
|
' paths, aborting', 1)
|
||||||
return
|
return
|
||||||
|
idFile = result[0]
|
||||||
|
self.logMsg('idFile: %s' % idFile)
|
||||||
|
|
||||||
|
# Try movies first
|
||||||
|
query = ' '.join((
|
||||||
|
"SELECT idMovie",
|
||||||
|
"FROM movie",
|
||||||
|
"WHERE idFile = ?"
|
||||||
|
))
|
||||||
|
self.cursor.execute(query, (idFile,))
|
||||||
|
try:
|
||||||
|
itemId = self.cursor.fetchone()[0]
|
||||||
|
typus = 'movie'
|
||||||
|
except TypeError:
|
||||||
|
# Try tv shows next
|
||||||
query = ' '.join((
|
query = ' '.join((
|
||||||
"SELECT idEpisode",
|
"SELECT idEpisode",
|
||||||
"FROM episode",
|
"FROM episode",
|
||||||
"WHERE c12 = ? AND c13 = ? AND idShow = ?"
|
"WHERE idFile = ?"
|
||||||
))
|
))
|
||||||
|
self.cursor.execute(query, (idFile,))
|
||||||
try:
|
try:
|
||||||
rows = self.cursor.execute(
|
itemId = self.cursor.fetchone()[0]
|
||||||
query,
|
typus = 'episode'
|
||||||
(itemdetails['season'],
|
except TypeError:
|
||||||
itemdetails['episode'],
|
self.logMsg('Unexpectantly did not find a match!', 1)
|
||||||
ids[0]))
|
|
||||||
except:
|
|
||||||
return
|
return
|
||||||
else:
|
return itemId, typus
|
||||||
return
|
|
||||||
|
|
||||||
ids = []
|
|
||||||
for row in rows:
|
|
||||||
ids.append(row[0])
|
|
||||||
if len(ids) == 1:
|
|
||||||
return ids[0]
|
|
||||||
else:
|
|
||||||
# No unique match possible
|
|
||||||
return
|
|
||||||
|
|
||||||
def getUnplayedItems(self):
|
def getUnplayedItems(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -177,7 +177,9 @@ class KodiMonitor(xbmc.Monitor):
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
count += 1
|
count += 1
|
||||||
log("Currently playing file is: %s" % utils.tryDecode(currentFile), 1)
|
# Just to be on the safe side
|
||||||
|
currentFile = utils.tryDecode(currentFile)
|
||||||
|
log("Currently playing file is: %s" % currentFile, 1)
|
||||||
|
|
||||||
# Get the type of media we're playing
|
# Get the type of media we're playing
|
||||||
try:
|
try:
|
||||||
|
@ -185,81 +187,67 @@ class KodiMonitor(xbmc.Monitor):
|
||||||
except (TypeError, KeyError):
|
except (TypeError, KeyError):
|
||||||
log("Item is invalid for PMS playstate update.", 0)
|
log("Item is invalid for PMS playstate update.", 0)
|
||||||
return
|
return
|
||||||
log("Playing itemtype is: %s" % typus, 1)
|
log("Playing itemtype is (or appears to be): %s" % typus, 1)
|
||||||
|
|
||||||
# Try to get a Kodi ID
|
# Try to get a Kodi ID
|
||||||
try:
|
# If PKC was used - native paths, not direct paths
|
||||||
kodiid = data['item']['id']
|
plexid = utils.window('emby_%s.itemid'
|
||||||
except (TypeError, KeyError):
|
% utils.tryEncode(currentFile))
|
||||||
log('Could not get Kodi id directly, trying jsonrpc', 1)
|
# Get rid of the '' if the window property was not set
|
||||||
|
plexid = None if not plexid else plexid
|
||||||
|
kodiid = None
|
||||||
|
if plexid is None:
|
||||||
|
log('Did not get Plex id from window properties', 1)
|
||||||
try:
|
try:
|
||||||
playerid = data["player"]["playerid"]
|
kodiid = data['item']['id']
|
||||||
except (TypeError, KeyError):
|
except (TypeError, KeyError):
|
||||||
log("Could not get Kodi playerid. Abort playback report", 0)
|
log('Did not get a Kodi id from Kodi, darn', 1)
|
||||||
return
|
# For direct paths, if we're not streaming something
|
||||||
# Get details of the playing media
|
# When using Widgets, Kodi doesn't tell us shit so we need this hack
|
||||||
result = xbmc.executeJSONRPC(json.dumps({
|
if (kodiid is None and plexid is None and typus in ('movie', 'episode')
|
||||||
"jsonrpc": "2.0",
|
and not currentFile.startswith('http')):
|
||||||
"id": 1,
|
|
||||||
"method": "Player.GetItem",
|
|
||||||
"params": {
|
|
||||||
"playerid": playerid,
|
|
||||||
# Just ask something so we get the item's id (for movies)
|
|
||||||
"properties": [
|
|
||||||
"tvshowid", "title"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
result = json.loads(result)
|
|
||||||
|
|
||||||
kodiid = None
|
|
||||||
if typus in ('movie', 'song'):
|
|
||||||
key = 'id'
|
|
||||||
elif typus == 'episode':
|
|
||||||
key = 'tvshowid'
|
|
||||||
else:
|
|
||||||
log('Unknown type, abort playback report', 1)
|
|
||||||
return
|
|
||||||
try:
|
try:
|
||||||
kodiid = result["result"]["item"][key]
|
filename = currentFile.rsplit('/', 1)[1]
|
||||||
except (TypeError, KeyError):
|
path = currentFile.rsplit('/', 1)[0] + '/'
|
||||||
pass
|
except IndexError:
|
||||||
# Kodi might return -1 for last element
|
filename = currentFile.rsplit('\\', 1)[1]
|
||||||
if kodiid in (None, -1) and typus in ('movie', 'episode'):
|
path = currentFile.rsplit('\\', 1)[0] + '\\'
|
||||||
log('Could not get Kodi id directly. Kodi said: %s'
|
log('Trying to figure out playing item from filename: %s and '
|
||||||
% result, 1)
|
'path: %s' % (filename, path), 1)
|
||||||
log('Trying to get Kodi id from the items name', 1)
|
with kodidb.GetKodiDB('video') as kodi_db:
|
||||||
with kodidb.GetKodiDB('video') as kodi_db:
|
try:
|
||||||
kodiid = kodi_db.getIdFromTitle(data.get('item'))
|
kodiid, typus = kodi_db.getIdFromFilename(filename, path)
|
||||||
|
except TypeError:
|
||||||
|
log('Aborting playback report', 1)
|
||||||
|
return
|
||||||
|
|
||||||
if kodiid in (None, -1):
|
if plexid is None:
|
||||||
log("Skip playstate update. No unique Kodi title found"
|
# Get Plex' item id
|
||||||
" for %s" % data.get('item'), 0)
|
with embydb.GetEmbyDB() as emby_db:
|
||||||
|
emby_dbitem = emby_db.getItem_byKodiId(kodiid, typus)
|
||||||
|
try:
|
||||||
|
plexid = emby_dbitem[0]
|
||||||
|
except TypeError:
|
||||||
|
log("No Plex id returned for kodiid %s" % kodiid, 1)
|
||||||
|
log('Aborting playback report', 1)
|
||||||
return
|
return
|
||||||
|
log("Found Plex id %s for Kodi id %s for type %s"
|
||||||
# Get Plex' item id
|
% (plexid, kodiid, typus), 1)
|
||||||
with embydb.GetEmbyDB() as emby_db:
|
|
||||||
emby_dbitem = emby_db.getItem_byKodiId(kodiid, typus)
|
|
||||||
try:
|
|
||||||
plexid = emby_dbitem[0]
|
|
||||||
except TypeError:
|
|
||||||
log("No Plex id returned for kodiid %s" % kodiid, 1)
|
|
||||||
log('Aborting playback report', 1)
|
|
||||||
return
|
|
||||||
log("Found Plex id %s for Kodi id %s" % (plexid, kodiid), 1)
|
|
||||||
|
|
||||||
# Set some stuff if Kodi initiated playback
|
# Set some stuff if Kodi initiated playback
|
||||||
if ((utils.settings('useDirectPaths') == "1" and not typus == "song")
|
if ((utils.settings('useDirectPaths') == "1" and not typus == "song")
|
||||||
or
|
or
|
||||||
(typus == "song" and utils.settings('enableMusic') == "true")):
|
(typus == "song" and utils.settings('enableMusic') == "true")):
|
||||||
if self.StartDirectPath(plexid, typus, currentFile) is False:
|
if self.StartDirectPath(plexid,
|
||||||
|
typus,
|
||||||
|
utils.tryEncode(currentFile)) is False:
|
||||||
log('Could not initiate monitoring; aborting', -1)
|
log('Could not initiate monitoring; aborting', -1)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Save currentFile for cleanup later and to be able to access refs
|
# Save currentFile for cleanup later and to be able to access refs
|
||||||
window('plex_lastPlayedFiled', value=utils.tryDecode(currentFile))
|
window('plex_lastPlayedFiled', value=currentFile)
|
||||||
window('Plex_currently_playing_itemid', value=plexid)
|
window('Plex_currently_playing_itemid', value=plexid)
|
||||||
window("emby_%s.itemid" % currentFile, value=plexid)
|
window("emby_%s.itemid" % utils.tryEncode(currentFile), value=plexid)
|
||||||
log('Finish playback startup', 1)
|
log('Finish playback startup', 1)
|
||||||
|
|
||||||
def StartDirectPath(self, plexid, type, currentFile):
|
def StartDirectPath(self, plexid, type, currentFile):
|
||||||
|
|
|
@ -941,6 +941,9 @@ def tryEncode(uniString, encoding='utf-8'):
|
||||||
uniString = uniString.encode(encoding, "ignore")
|
uniString = uniString.encode(encoding, "ignore")
|
||||||
except TypeError:
|
except TypeError:
|
||||||
uniString = uniString.encode()
|
uniString = uniString.encode()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# already encoded
|
||||||
|
pass
|
||||||
return uniString
|
return uniString
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue