Be smarter when trying to tell what Kodi plays

- Fixes #67
This commit is contained in:
tomkat83 2016-06-25 12:25:01 +02:00
parent e70ce89c34
commit be63414893
2 changed files with 44 additions and 101 deletions

View file

@ -179,52 +179,6 @@ class Kodidb_Functions():
return fileid 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 not 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): def getFile(self, fileid):
query = ' '.join(( query = ' '.join((
@ -891,7 +845,7 @@ class Kodidb_Functions():
for row in rows: for row in rows:
ids.append(row[0]) ids.append(row[0])
if len(ids) > 1: if len(ids) > 1:
# No unique match possible self.logMsg('No unique match possible. Rows: %s' % rows, 1)
return return
query = ' '.join(( query = ' '.join((

View file

@ -189,64 +189,53 @@ class KodiMonitor(xbmc.Monitor):
# Try to get a Kodi ID # Try to get a Kodi ID
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('Could not get Kodi id directly, trying jsonrpc', 1)
return try:
# Get details of the playing media playerid = data["player"]["playerid"]
result = xbmc.executeJSONRPC(json.dumps({ except (TypeError, KeyError):
"jsonrpc": "2.0", log("Could not get Kodi playerid. Abort playback report", 0)
"id": 1, return
"method": "Player.GetItem", # Get details of the playing media
"params": { result = xbmc.executeJSONRPC(json.dumps({
"playerid": playerid, "jsonrpc": "2.0",
# Just ask something so we get the item's id (for movies) "id": 1,
"properties": [ "method": "Player.GetItem",
"tvshowid", "title" "params": {
] "playerid": playerid,
} # Just ask something so we get the item's id (for movies)
})) "properties": [
result = json.loads(result) "tvshowid", "title"
]
}
}))
result = json.loads(result)
kodiid = None kodiid = None
if typus in ('movie', 'song'): if typus in ('movie', 'song'):
key = 'id' key = 'id'
elif typus == 'episode': elif typus == 'episode':
key = 'tvshowid' key = 'tvshowid'
else: else:
log('Unknown type, abort playback report', 1) log('Unknown type, abort playback report', 1)
return return
try: try:
kodiid = result["result"]["item"][key] kodiid = result["result"]["item"][key]
except KeyError: except (TypeError, KeyError):
pass pass
# Kodi might return -1 for last element # Kodi might return -1 for last element
if kodiid in (None, -1): if kodiid in (None, -1) and typus in ('movie', 'episode'):
log('Could not get Kodi id directly. Kodi said: %s' % result, 1) log('Could not get Kodi id directly. Kodi said: %s'
if currentFile.startswith('http'): % result, 1)
# Native paths - should have launched directly via PKC log('Trying to get Kodi id from the items name', 1)
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: with kodidb.GetKodiDB('video') as kodi_db:
kodiid = kodi_db.getIdFromFilename( kodiid = kodi_db.getIdFromTitle(data.get('item'))
utils.tryDecode(filename))
if not kodiid and typus in ('movie', 'episode'): if kodiid in (None, -1):
log('Trying to get Kodi id from the items name', 1) log("Skip playstate update. No unique Kodi title found"
with kodidb.GetKodiDB('video') as kodi_db: " for %s" % data.get('item'), 0)
kodiid = kodi_db.getIdFromTitle(data.get('item')) return
if kodiid is None:
log("Skip playstate update. No unique Kodi title found"
" for %s" % data.get('item'), 0)
return
# Get Plex' item id # Get Plex' item id
with embydb.GetEmbyDB() as emby_db: with embydb.GetEmbyDB() as emby_db: