Be smarter when trying to tell what Kodi plays

- Fixes #67
- Episodes may still throw a -1 for the id - useless!
This commit is contained in:
tomkat83 2016-06-24 21:28:30 +02:00
parent 4d54f056f5
commit 5210e7c442
2 changed files with 116 additions and 24 deletions

View file

@ -179,6 +179,52 @@ class Kodidb_Functions():
return fileid
def getIdFromFilename(self, filename):
"""
Returns None if not found OR if several entries found
"""
query = ' '.join((
"SELECT idFile",
"FROM files",
"WHERE strFilename = ?"
))
self.cursor.execute(query, (filename,))
try:
idFile = self.cursor.fetchone()[0]
except TypeError:
idFile = None
else:
# Try to fetch again - if successful, we got >1 result
if self.cursor.fetchone() is None:
self.logMsg('We found several items with the same filename', 1)
idFile = None
if idFile is None:
return
# Try movies first
itemId = None
query = ' '.join((
"SELECT idMovie",
"FROM movie",
"WHERE idFile = ?"
))
self.cursor.execute(query, (idFile,))
try:
itemId = self.cursor.fetchone()[0]
except TypeError:
# Try tv shows next
query = ' '.join((
"SELECT idEpisode",
"FROM episode",
"WHERE idFile = ?"
))
self.cursor.execute(query, (idFile,))
try:
itemId = self.cursor.fetchone()[0]
except TypeError:
pass
return itemId
def getFile(self, fileid):
query = ' '.join((

View file

@ -160,7 +160,6 @@ class KodiMonitor(xbmc.Monitor):
"""
log = self.logMsg
window = utils.window
# Get currently playing file - can take a while. Will be utf-8!
try:
currentFile = self.xbmcplayer.getPlayingFile()
@ -180,44 +179,91 @@ class KodiMonitor(xbmc.Monitor):
count += 1
log("Currently playing file is: %s" % utils.tryDecode(currentFile), 1)
# Try to get a Kodi ID
item = data.get('item')
# Get the type of media we're playing
try:
type = item['type']
except:
typus = data['item']['type']
except (TypeError, KeyError):
log("Item is invalid for PMS playstate update.", 0)
return
log("Playing itemtype is: %s" % typus, 1)
# Try to get a Kodi ID
try:
kodiid = item['id']
except (KeyError, TypeError):
itemType = window("emby_%s.type" % currentFile)
log("No kodi id passed. Playing itemtype is: %s" % itemType, 1)
if itemType in ('movie', 'episode'):
# Window was setup by PKC and is NOT a trailer ('clip')
playerid = data["player"]["playerid"]
except (TypeError, KeyError):
log("Could not get Kodi playerid. Abort playback report", 0)
return
# Get details of the playing media
result = xbmc.executeJSONRPC(json.dumps({
"jsonrpc": "2.0",
"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:
kodiid = result["result"]["item"][key]
except KeyError:
pass
# Kodi might return -1 for last element
if kodiid in (None, -1):
log('Could not get Kodi id directly. Kodi said: %s' % result, 1)
if currentFile.startswith('http'):
# Native paths - should have launched directly via PKC
log('Trying to get Kodi id from window properties', 1)
kodiid = utils.window('emby_%s.itemid' % currentFile)
elif typus in ('movie', 'episode'):
try:
filename = currentFile.rsplit('/', 1)[1]
except IndexError:
filename = currentFile.rsplit('\\', 1)[1]
log('Trying to get Kodi id from filename: %s'
% utils.tryDecode(filename), 1)
with kodidb.GetKodiDB('video') as kodi_db:
kodiid = kodi_db.getIdFromTitle(data.get('item'))
if kodiid is None:
log("Skip playstate update. No unique Kodi title found"
" for %s" % data.get('item'), 0)
return
else:
log("Item is invalid for PMS playstate update.", 0)
return
kodiid = kodi_db.getIdFromFilename(
utils.tryDecode(filename))
if not kodiid and typus in ('movie', 'episode'):
log('Trying to get Kodi id from the items name', 1)
with kodidb.GetKodiDB('video') as kodi_db:
kodiid = kodi_db.getIdFromTitle(data.get('item'))
if kodiid is None:
log("Skip playstate update. No unique Kodi title found"
" for %s" % data.get('item'), 0)
return
# Get Plex' item id
with embydb.GetEmbyDB() as emby_db:
emby_dbitem = emby_db.getItem_byKodiId(kodiid, type)
emby_dbitem = emby_db.getItem_byKodiId(kodiid, typus)
try:
plexid = emby_dbitem[0]
except TypeError:
log("No Plex id returned for kodiid %s" % kodiid, 0)
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
if ((utils.settings('useDirectPaths') == "1" and not type == "song") or
(type == "song" and utils.settings('enableMusic') == "true")):
if self.StartDirectPath(plexid, type, currentFile) is False:
if ((utils.settings('useDirectPaths') == "1" and not typus == "song")
or
(typus == "song" and utils.settings('enableMusic') == "true")):
if self.StartDirectPath(plexid, typus, currentFile) is False:
log('Could not initiate monitoring; aborting', -1)
return