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

View file

@ -189,64 +189,53 @@ class KodiMonitor(xbmc.Monitor):
# Try to get a Kodi ID
try:
playerid = data["player"]["playerid"]
kodiid = data['item']['id']
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)
log('Could not get Kodi id directly, trying jsonrpc', 1)
try:
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)
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 (TypeError, KeyError):
pass
# Kodi might return -1 for last element
if kodiid in (None, -1) and typus in ('movie', 'episode'):
log('Could not get Kodi id directly. Kodi said: %s'
% result, 1)
log('Trying to get Kodi id from the items name', 1)
with kodidb.GetKodiDB('video') as kodi_db:
kodiid = kodi_db.getIdFromFilename(
utils.tryDecode(filename))
kodiid = kodi_db.getIdFromTitle(data.get('item'))
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
if kodiid in (None, -1):
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: