2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
import urllib
|
|
|
|
from ntpath import dirname
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcvfs
|
|
|
|
|
|
|
|
import artwork
|
|
|
|
import utils
|
|
|
|
import embydb_functions as embydb
|
|
|
|
import kodidb_functions as kodidb
|
|
|
|
import read_embyserver as embyserver
|
2016-02-20 06:03:06 +11:00
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
import PlexAPI
|
2016-02-13 02:53:49 +11:00
|
|
|
from PlexFunctions import GetPlexMetadata
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
2015-12-25 06:51:47 +11:00
|
|
|
class Items(object):
|
2016-01-11 01:16:59 +11:00
|
|
|
"""
|
2016-02-13 02:53:49 +11:00
|
|
|
Items to be called with "with Items() as xxx:" to ensure that __enter__
|
2016-01-11 18:10:36 +11:00
|
|
|
method is called (opens db connections)
|
2016-02-13 02:53:49 +11:00
|
|
|
|
|
|
|
Input:
|
|
|
|
kodiType: optional argument; e.g. 'video' or 'music'
|
2016-01-11 01:16:59 +11:00
|
|
|
"""
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-01-08 03:27:48 +11:00
|
|
|
def __init__(self):
|
2015-12-25 06:51:47 +11:00
|
|
|
self.kodiversion = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
|
2016-03-04 01:28:44 +11:00
|
|
|
# self.directpath = utils.settings('useDirectPaths') == "1"
|
2016-03-22 23:25:46 +11:00
|
|
|
self.directpath = True if utils.window('useDirectPaths') == 'true' \
|
2016-03-16 06:26:45 +11:00
|
|
|
else False
|
2016-03-04 01:28:44 +11:00
|
|
|
# self.music_enabled = utils.settings('enableMusic') == "true"
|
|
|
|
# self.contentmsg = utils.settings('newContent') == "true"
|
2016-01-08 03:27:48 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
self.artwork = artwork.Artwork()
|
|
|
|
self.emby = embyserver.Read_EmbyServer()
|
2016-03-14 21:47:14 +11:00
|
|
|
self.userid = utils.window('currUserId')
|
|
|
|
self.server = utils.window('pms_server')
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
def __enter__(self):
|
2016-01-11 01:16:59 +11:00
|
|
|
"""
|
|
|
|
Open DB connections and cursors
|
|
|
|
"""
|
2016-01-08 03:27:48 +11:00
|
|
|
self.embyconn = utils.kodiSQL('emby')
|
|
|
|
self.embycursor = self.embyconn.cursor()
|
|
|
|
self.kodiconn = utils.kodiSQL('video')
|
|
|
|
self.kodicursor = self.kodiconn.cursor()
|
|
|
|
self.emby_db = embydb.Embydb_Functions(self.embycursor)
|
|
|
|
self.kodi_db = kodidb.Kodidb_Functions(self.kodicursor)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
"""
|
|
|
|
Make sure DB changes are committed and connection to DB is closed.
|
|
|
|
"""
|
|
|
|
self.embyconn.commit()
|
|
|
|
self.kodiconn.commit()
|
|
|
|
self.embyconn.close()
|
|
|
|
self.kodiconn.close()
|
|
|
|
return self
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def itemsbyId(self, items, process, pdialog=None):
|
|
|
|
# Process items by itemid. Process can be added, update, userdata, remove
|
|
|
|
emby = self.emby
|
|
|
|
embycursor = self.embycursor
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
music_enabled = self.music_enabled
|
|
|
|
|
|
|
|
itemtypes = {
|
|
|
|
|
|
|
|
'Movie': Movies,
|
|
|
|
'BoxSet': Movies,
|
|
|
|
'Series': TVShows,
|
|
|
|
'Season': TVShows,
|
|
|
|
'Episode': TVShows,
|
|
|
|
'MusicAlbum': Music,
|
|
|
|
'MusicArtist': Music,
|
|
|
|
'AlbumArtist': Music,
|
2016-01-12 08:20:34 +11:00
|
|
|
'Audio': Music
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
|
|
|
|
2016-02-10 15:50:31 +11:00
|
|
|
update_videolibrary = False
|
2015-12-25 06:51:47 +11:00
|
|
|
total = 0
|
|
|
|
for item in items:
|
|
|
|
total += len(items[item])
|
|
|
|
|
|
|
|
if total == 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.logMsg("Processing %s: %s" % (process, items), 1)
|
|
|
|
if pdialog:
|
|
|
|
pdialog.update(heading="Processing %s: %s items" % (process, total))
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
for itemtype in items:
|
|
|
|
|
|
|
|
# Safety check
|
|
|
|
if not itemtypes.get(itemtype):
|
|
|
|
# We don't process this type of item
|
|
|
|
continue
|
|
|
|
|
|
|
|
itemlist = items[itemtype]
|
|
|
|
if not itemlist:
|
|
|
|
# The list to process is empty
|
|
|
|
continue
|
|
|
|
|
|
|
|
musicconn = None
|
|
|
|
|
2016-01-09 05:56:21 +11:00
|
|
|
if itemtype in ('MusicAlbum', 'MusicArtist', 'AlbumArtist', 'Audio'):
|
2015-12-25 06:51:47 +11:00
|
|
|
if music_enabled:
|
|
|
|
musicconn = utils.kodiSQL('music')
|
|
|
|
musiccursor = musicconn.cursor()
|
|
|
|
items_process = itemtypes[itemtype](embycursor, musiccursor)
|
|
|
|
else:
|
|
|
|
# Music is not enabled, do not proceed with itemtype
|
|
|
|
continue
|
|
|
|
else:
|
2016-02-10 15:50:31 +11:00
|
|
|
update_videolibrary = True
|
2015-12-25 06:51:47 +11:00
|
|
|
items_process = itemtypes[itemtype](embycursor, kodicursor)
|
|
|
|
|
|
|
|
if itemtype == "Movie":
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added,
|
|
|
|
'update': items_process.add_update,
|
|
|
|
'userdata': items_process.updateUserdata,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
elif itemtype == "BoxSet":
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added_boxset,
|
|
|
|
'update': items_process.add_updateBoxset,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
elif itemtype == "MusicVideo":
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added,
|
|
|
|
'update': items_process.add_update,
|
|
|
|
'userdata': items_process.updateUserdata,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
elif itemtype == "Series":
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added,
|
|
|
|
'update': items_process.add_update,
|
|
|
|
'userdata': items_process.updateUserdata,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
elif itemtype == "Season":
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added_season,
|
|
|
|
'update': items_process.add_updateSeason,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
elif itemtype == "Episode":
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added_episode,
|
|
|
|
'update': items_process.add_updateEpisode,
|
|
|
|
'userdata': items_process.updateUserdata,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
elif itemtype == "MusicAlbum":
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added_album,
|
|
|
|
'update': items_process.add_updateAlbum,
|
|
|
|
'userdata': items_process.updateUserdata,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
elif itemtype in ("MusicArtist", "AlbumArtist"):
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added,
|
|
|
|
'update': items_process.add_updateArtist,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
elif itemtype == "Audio":
|
|
|
|
actions = {
|
|
|
|
'added': items_process.added_song,
|
|
|
|
'update': items_process.add_updateSong,
|
|
|
|
'userdata': items_process.updateUserdata,
|
|
|
|
'remove': items_process.remove
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
self.logMsg("Unsupported itemtype: %s." % itemtype, 1)
|
|
|
|
actions = {}
|
|
|
|
|
|
|
|
if actions.get(process):
|
|
|
|
|
|
|
|
if process == "remove":
|
|
|
|
for item in itemlist:
|
|
|
|
actions[process](item)
|
|
|
|
|
|
|
|
elif process == "added":
|
|
|
|
actions[process](itemlist, pdialog)
|
|
|
|
|
|
|
|
else:
|
|
|
|
processItems = emby.getFullItems(itemlist)
|
|
|
|
for item in processItems:
|
|
|
|
|
|
|
|
title = item['Name']
|
|
|
|
|
|
|
|
if itemtype == "Episode":
|
|
|
|
title = "%s - %s" % (item['SeriesName'], title)
|
|
|
|
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
|
|
|
pdialog.update(percentage, message=title)
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
actions[process](item)
|
|
|
|
|
|
|
|
|
|
|
|
if musicconn is not None:
|
|
|
|
# close connection for special types
|
2016-02-10 15:50:31 +11:00
|
|
|
self.logMsg("Updating music database.", 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
musicconn.commit()
|
|
|
|
musiccursor.close()
|
|
|
|
|
2016-02-10 15:50:31 +11:00
|
|
|
return (True, update_videolibrary)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-04 22:42:17 +11:00
|
|
|
def contentPop(self, name, time=5000):
|
2015-12-25 06:51:47 +11:00
|
|
|
xbmcgui.Dialog().notification(
|
|
|
|
heading="Emby for Kodi",
|
|
|
|
message="Added: %s" % name,
|
2015-12-27 21:49:14 +11:00
|
|
|
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
|
2016-02-04 22:42:17 +11:00
|
|
|
time=time,
|
2015-12-25 06:51:47 +11:00
|
|
|
sound=False)
|
|
|
|
|
2016-03-11 04:34:11 +11:00
|
|
|
def updateUserdata(self, xml, viewtag=None, viewid=None):
|
2016-02-04 00:44:11 +11:00
|
|
|
"""
|
|
|
|
Updates the Kodi watched state of the item from PMS. Also retrieves
|
|
|
|
Plex resume points for movies in progress.
|
2016-03-11 04:34:11 +11:00
|
|
|
|
|
|
|
viewtag and viewid only serve as dummies
|
2016-02-04 00:44:11 +11:00
|
|
|
"""
|
|
|
|
for mediaitem in xml:
|
|
|
|
API = PlexAPI.API(mediaitem)
|
|
|
|
# Get key and db entry on the Kodi db side
|
2016-02-11 22:44:12 +11:00
|
|
|
try:
|
|
|
|
fileid = self.emby_db.getItem_byId(API.getRatingKey())[1]
|
|
|
|
except:
|
|
|
|
continue
|
2016-02-04 00:44:11 +11:00
|
|
|
# Grab the user's viewcount, resume points etc. from PMS' answer
|
|
|
|
userdata = API.getUserData()
|
|
|
|
# Write to Kodi DB
|
|
|
|
self.kodi_db.addPlaystate(fileid,
|
|
|
|
userdata['Resume'],
|
|
|
|
userdata['Runtime'],
|
|
|
|
userdata['PlayCount'],
|
|
|
|
userdata['LastPlayedDate'])
|
|
|
|
|
2016-03-25 04:52:02 +11:00
|
|
|
def updatePlaystate(self, item):
|
2016-03-28 01:57:35 +11:00
|
|
|
"""
|
|
|
|
Use with websockets, not xml
|
|
|
|
"""
|
|
|
|
# If the playback was stopped, check whether we need to increment the
|
|
|
|
# playcount. PMS won't tell us the playcount via websockets
|
|
|
|
if item['state'] in ('stopped', 'ended'):
|
|
|
|
complete = float(item['viewOffset']) / float(item['duration'])
|
|
|
|
complete = complete * 100
|
|
|
|
self.logMsg('Item %s stopped with completion rate %s percent.'
|
|
|
|
'Mark item played at %s percent.'
|
|
|
|
% (item['ratingKey'],
|
|
|
|
str(complete),
|
|
|
|
utils.settings('markPlayed')), 1)
|
|
|
|
if complete >= float(utils.settings('markPlayed')):
|
|
|
|
self.logMsg('Marking as completely watched in Kodi', 1)
|
2016-04-11 23:01:25 +10:00
|
|
|
try:
|
|
|
|
item['viewCount'] += 1
|
|
|
|
except TypeError:
|
|
|
|
item['viewCount'] = 1
|
2016-03-28 01:57:35 +11:00
|
|
|
item['viewOffset'] = 0
|
|
|
|
# Do the actual update
|
2016-03-25 04:52:02 +11:00
|
|
|
self.kodi_db.addPlaystate(item['file_id'],
|
|
|
|
item['viewOffset'],
|
|
|
|
item['duration'],
|
|
|
|
item['viewCount'],
|
|
|
|
item['lastViewedAt'])
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
class Movies(Items):
|
|
|
|
|
|
|
|
def added(self, items, pdialog):
|
|
|
|
|
|
|
|
total = len(items)
|
|
|
|
count = 0
|
|
|
|
for movie in items:
|
|
|
|
|
|
|
|
title = movie['Name']
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
|
|
|
pdialog.update(percentage, message=title)
|
|
|
|
count += 1
|
|
|
|
self.add_update(movie)
|
|
|
|
if not pdialog and self.contentmsg:
|
2016-02-04 22:42:17 +11:00
|
|
|
self.contentPop(title, self.newvideo_time)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def added_boxset(self, items, pdialog):
|
|
|
|
|
|
|
|
total = len(items)
|
|
|
|
count = 0
|
|
|
|
for boxset in items:
|
|
|
|
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
2016-04-26 21:53:19 +10:00
|
|
|
pdialog.update(percentage, message=boxset['Name'])
|
2015-12-25 06:51:47 +11:00
|
|
|
count += 1
|
|
|
|
self.add_updateBoxset(boxset)
|
|
|
|
|
|
|
|
def add_update(self, item, viewtag=None, viewid=None):
|
2016-03-10 04:36:36 +11:00
|
|
|
try:
|
|
|
|
self.run_add_update(item, viewtag, viewid)
|
|
|
|
except Exception as e:
|
|
|
|
self.logMsg('itemtypes.py for movies has crashed for item %s. '
|
|
|
|
'Error:' % item.attrib.get('ratingKey', None), -1)
|
|
|
|
self.logMsg(e, -1)
|
2016-03-10 19:58:26 +11:00
|
|
|
import traceback
|
|
|
|
self.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
|
2016-03-10 19:30:20 +11:00
|
|
|
utils.window('plex_scancrashed', value='true')
|
2016-03-10 04:36:36 +11:00
|
|
|
# skip this item for now
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_add_update(self, item, viewtag=None, viewid=None):
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process single movie
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
emby_db = self.emby_db
|
|
|
|
artwork = self.artwork
|
2015-12-29 04:47:16 +11:00
|
|
|
API = PlexAPI.API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# If the item already exist in the local Kodi DB we'll perform a full
|
|
|
|
# item update
|
2015-12-25 06:51:47 +11:00
|
|
|
# If the item doesn't exist, we'll add it to the database
|
|
|
|
update_item = True
|
2016-01-30 06:07:21 +11:00
|
|
|
itemid = API.getRatingKey()
|
2016-01-03 23:36:00 +11:00
|
|
|
# Cannot parse XML, abort
|
|
|
|
if not itemid:
|
|
|
|
self.logMsg("Cannot parse XML data for movie", -1)
|
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
movieid = emby_dbitem[0]
|
|
|
|
fileid = emby_dbitem[1]
|
|
|
|
pathid = emby_dbitem[2]
|
2016-02-05 06:23:04 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
# movieid
|
2016-02-13 02:53:49 +11:00
|
|
|
update_item = False
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor.execute("select coalesce(max(idMovie),0) from movie")
|
|
|
|
movieid = kodicursor.fetchone()[0] + 1
|
|
|
|
|
2016-02-14 15:42:16 +11:00
|
|
|
else:
|
|
|
|
# Verification the item is still in Kodi
|
|
|
|
query = "SELECT * FROM movie WHERE idMovie = ?"
|
|
|
|
kodicursor.execute(query, (movieid,))
|
|
|
|
try:
|
|
|
|
kodicursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
# item is not found, let's recreate it.
|
|
|
|
update_item = False
|
|
|
|
self.logMsg("movieid: %s missing from Kodi, repairing the entry." % movieid, 1)
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# fileId information
|
|
|
|
checksum = API.getChecksum()
|
|
|
|
dateadded = API.getDateCreated()
|
|
|
|
userdata = API.getUserData()
|
|
|
|
playcount = userdata['PlayCount']
|
|
|
|
dateplayed = userdata['LastPlayedDate']
|
2016-02-01 20:33:33 +11:00
|
|
|
resume = userdata['Resume']
|
|
|
|
runtime = userdata['Runtime']
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# item details
|
|
|
|
people = API.getPeople()
|
2015-12-29 04:47:16 +11:00
|
|
|
writer = API.joinList(people['Writer'])
|
|
|
|
director = API.joinList(people['Director'])
|
|
|
|
genres = API.getGenres()
|
2016-02-01 20:33:33 +11:00
|
|
|
genre = API.joinList(genres)
|
2015-12-31 20:53:22 +11:00
|
|
|
title, sorttitle = API.getTitle()
|
|
|
|
plot = API.getPlot()
|
2015-12-29 04:47:16 +11:00
|
|
|
shortplot = None
|
2015-12-31 20:53:22 +11:00
|
|
|
tagline = API.getTagline()
|
2015-12-30 23:25:37 +11:00
|
|
|
votecount = None
|
2016-03-02 02:18:12 +11:00
|
|
|
collections = API.getCollections()
|
2016-01-11 19:57:45 +11:00
|
|
|
|
2016-04-02 19:31:21 +11:00
|
|
|
rating = userdata['Rating']
|
2015-12-31 20:53:22 +11:00
|
|
|
year = API.getYear()
|
2016-02-03 23:01:13 +11:00
|
|
|
imdb = API.getProvider('imdb')
|
2015-12-25 06:51:47 +11:00
|
|
|
mpaa = API.getMpaa()
|
2015-12-29 04:47:16 +11:00
|
|
|
countries = API.getCountry()
|
|
|
|
country = API.joinList(countries)
|
2015-12-25 06:51:47 +11:00
|
|
|
studios = API.getStudios()
|
|
|
|
try:
|
|
|
|
studio = studios[0]
|
|
|
|
except IndexError:
|
|
|
|
studio = None
|
|
|
|
|
2016-01-12 21:47:48 +11:00
|
|
|
# Find one trailer
|
2015-12-29 04:47:16 +11:00
|
|
|
trailer = None
|
2016-01-12 21:47:48 +11:00
|
|
|
extras = API.getExtras()
|
2016-01-30 06:07:21 +11:00
|
|
|
for extra in extras:
|
2016-01-12 23:57:26 +11:00
|
|
|
# Only get 1st trailer element
|
2016-01-30 06:07:21 +11:00
|
|
|
if extra['extraType'] == '1':
|
2016-02-01 20:33:33 +11:00
|
|
|
trailer = ("plugin://plugin.video.plexkodiconnect/trailer/?"
|
|
|
|
"id=%s&mode=play") % extra['key']
|
2016-01-12 23:57:26 +11:00
|
|
|
break
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-16 06:26:45 +11:00
|
|
|
# GET THE FILE AND PATH #####
|
|
|
|
doIndirect = not self.directpath
|
2015-12-25 06:51:47 +11:00
|
|
|
if self.directpath:
|
|
|
|
# Direct paths is set the Kodi way
|
2016-03-16 06:26:45 +11:00
|
|
|
playurl = API.getFilePath()
|
|
|
|
if playurl is None:
|
|
|
|
# Something went wrong, trying to use non-direct paths
|
|
|
|
doIndirect = True
|
|
|
|
else:
|
2016-04-13 01:18:32 +10:00
|
|
|
playurl = API.validatePlayurl(playurl, API.getType())
|
|
|
|
if playurl is None:
|
2016-03-16 22:26:31 +11:00
|
|
|
return False
|
2016-03-16 06:26:45 +11:00
|
|
|
if "\\" in playurl:
|
|
|
|
# Local path
|
|
|
|
filename = playurl.rsplit("\\", 1)[1]
|
|
|
|
else:
|
|
|
|
# Network share
|
|
|
|
filename = playurl.rsplit("/", 1)[1]
|
|
|
|
path = playurl.replace(filename, "")
|
|
|
|
if doIndirect:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Set plugin path and media flags using real filename
|
2016-01-12 22:14:18 +11:00
|
|
|
path = "plugin://plugin.video.plexkodiconnect.movies/"
|
2015-12-25 06:51:47 +11:00
|
|
|
params = {
|
2016-05-08 06:17:12 +10:00
|
|
|
'filename': API.getKey(),
|
2015-12-25 06:51:47 +11:00
|
|
|
'id': itemid,
|
|
|
|
'dbid': movieid,
|
|
|
|
'mode': "play"
|
|
|
|
}
|
|
|
|
filename = "%s?%s" % (path, urllib.urlencode(params))
|
2016-04-23 19:35:37 +10:00
|
|
|
playurl = filename
|
|
|
|
|
|
|
|
# Even if the item is only updated, the file may have been moved or updated.
|
|
|
|
# In the worst case we get exactly the same values as we had before.
|
2016-04-26 22:41:58 +10:00
|
|
|
pathid = self.kodi_db.addPath(path)
|
|
|
|
fileid = self.kodi_db.addFile(filename, pathid)
|
2016-04-23 19:35:37 +10:00
|
|
|
|
|
|
|
# movie table:
|
|
|
|
# c22 - playurl
|
|
|
|
# c23 - pathid
|
|
|
|
# This information is used later by file browser.
|
2016-03-16 06:26:45 +11:00
|
|
|
|
|
|
|
# UPDATE THE MOVIE #####
|
2015-12-25 06:51:47 +11:00
|
|
|
if update_item:
|
|
|
|
self.logMsg("UPDATE movie itemid: %s - Title: %s" % (itemid, title), 1)
|
|
|
|
|
|
|
|
# Update the movie entry
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE movie",
|
|
|
|
"SET c00 = ?, c01 = ?, c02 = ?, c03 = ?, c04 = ?, c05 = ?, c06 = ?,",
|
|
|
|
"c07 = ?, c09 = ?, c10 = ?, c11 = ?, c12 = ?, c14 = ?, c15 = ?,",
|
2016-04-23 19:35:37 +10:00
|
|
|
"c16 = ?, c18 = ?, c19 = ?, c21 = ?, c22 = ?, c23 = ?",
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idMovie = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (title, plot, shortplot, tagline, votecount, rating, writer,
|
|
|
|
year, imdb, sorttitle, runtime, mpaa, genre, director, title, studio, trailer,
|
2016-04-23 19:35:37 +10:00
|
|
|
country, playurl, pathid, movieid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Update the checksum in emby table
|
|
|
|
emby_db.updateReference(itemid, checksum)
|
|
|
|
|
|
|
|
##### OR ADD THE MOVIE #####
|
|
|
|
else:
|
|
|
|
self.logMsg("ADD movie itemid: %s - Title: %s" % (itemid, title), 1)
|
2016-04-23 19:35:37 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Create the movie entry
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO movie(
|
|
|
|
idMovie, idFile, c00, c01, c02, c03, c04, c05, c06, c07,
|
2016-04-23 19:35:37 +10:00
|
|
|
c09, c10, c11, c12, c14, c15, c16, c18, c19, c21, c22, c23)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-04-23 19:35:37 +10:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2015-12-25 06:51:47 +11:00
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (movieid, fileid, title, plot, shortplot, tagline, votecount,
|
|
|
|
rating, writer, year, imdb, sorttitle, runtime, mpaa, genre, director, title,
|
2016-04-23 19:35:37 +10:00
|
|
|
studio, trailer, country, playurl, pathid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Create the reference in emby table
|
|
|
|
emby_db.addReference(itemid, movieid, "Movie", "movie", fileid, pathid, None, checksum, viewid)
|
|
|
|
|
|
|
|
# Update the path
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE path",
|
|
|
|
"SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ?",
|
|
|
|
"WHERE idPath = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (path, "movies", "metadata.local", 1, pathid))
|
|
|
|
|
|
|
|
# Update the file
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE files",
|
|
|
|
"SET idPath = ?, strFilename = ?, dateAdded = ?",
|
|
|
|
"WHERE idFile = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (pathid, filename, dateadded, fileid))
|
|
|
|
|
|
|
|
# Process countries
|
2016-04-26 22:41:58 +10:00
|
|
|
self.kodi_db.addCountries(movieid, countries, "movie")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process cast
|
2015-12-29 04:47:16 +11:00
|
|
|
people = API.getPeopleList()
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addPeople(movieid, people, "movie")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process genres
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addGenres(movieid, genres, "movie")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process artwork
|
2015-12-31 01:57:55 +11:00
|
|
|
allartworks = API.getAllArtwork()
|
|
|
|
artwork.addArtwork(allartworks, movieid, "movie", kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process stream details
|
|
|
|
streams = API.getMediaStreams()
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addStreams(fileid, streams, runtime)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process studios
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addStudios(movieid, studios, "movie")
|
2016-03-02 02:18:12 +11:00
|
|
|
# Process tags: view, Plex collection tags
|
2016-01-12 01:56:56 +11:00
|
|
|
tags = [viewtag]
|
2016-03-02 02:18:12 +11:00
|
|
|
tags.extend(collections)
|
|
|
|
if userdata['Favorite']:
|
|
|
|
tags.append("Favorite movies")
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addTags(movieid, tags, "movie")
|
2016-05-17 01:56:48 +10:00
|
|
|
# Add any sets from Plex collection tags
|
|
|
|
self.kodi_db.addSets(movieid, collections)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process playstates
|
2016-04-26 22:41:58 +10:00
|
|
|
self.kodi_db.addPlaystate(fileid, resume, runtime, playcount, dateplayed)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def remove(self, itemid):
|
|
|
|
# Remove movieid, fileid, emby reference
|
|
|
|
emby_db = self.emby_db
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
artwork = self.artwork
|
|
|
|
|
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
kodiid = emby_dbitem[0]
|
|
|
|
fileid = emby_dbitem[1]
|
|
|
|
mediatype = emby_dbitem[4]
|
|
|
|
self.logMsg("Removing %sid: %s fileid: %s" % (mediatype, kodiid, fileid), 1)
|
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Remove the emby reference
|
|
|
|
emby_db.removeItem(itemid)
|
|
|
|
# Remove artwork
|
|
|
|
artwork.deleteArtwork(kodiid, mediatype, kodicursor)
|
|
|
|
|
|
|
|
if mediatype == "movie":
|
|
|
|
# Delete kodi movie and file
|
|
|
|
kodicursor.execute("DELETE FROM movie WHERE idMovie = ?", (kodiid,))
|
|
|
|
kodicursor.execute("DELETE FROM files WHERE idFile = ?", (fileid,))
|
|
|
|
|
|
|
|
elif mediatype == "set":
|
|
|
|
# Delete kodi boxset
|
|
|
|
boxset_movies = emby_db.getItem_byParentId(kodiid, "movie")
|
|
|
|
for movie in boxset_movies:
|
2016-05-31 16:06:42 +10:00
|
|
|
plexid = movie[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
movieid = movie[1]
|
|
|
|
self.kodi_db.removefromBoxset(movieid)
|
|
|
|
# Update emby reference
|
2016-05-31 16:06:42 +10:00
|
|
|
emby_db.updateParentId(plexid, None)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
kodicursor.execute("DELETE FROM sets WHERE idSet = ?", (kodiid,))
|
|
|
|
|
|
|
|
self.logMsg("Deleted %s %s from kodi database" % (mediatype, itemid), 1)
|
|
|
|
|
2016-01-11 01:16:59 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
class MusicVideos(Items):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, embycursor, kodicursor):
|
|
|
|
Items.__init__(self, embycursor, kodicursor)
|
|
|
|
|
|
|
|
def added(self, items, pdialog):
|
|
|
|
|
|
|
|
total = len(items)
|
|
|
|
count = 0
|
|
|
|
for mvideo in items:
|
|
|
|
|
|
|
|
title = mvideo['Name']
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
|
|
|
pdialog.update(percentage, message=title)
|
|
|
|
count += 1
|
|
|
|
self.add_update(mvideo)
|
|
|
|
if not pdialog and self.contentmsg:
|
2016-02-04 22:42:17 +11:00
|
|
|
self.contentPop(title, self.newvideo_time)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
|
|
|
|
def add_update(self, item, viewtag=None, viewid=None):
|
|
|
|
# Process single music video
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
emby_db = self.emby_db
|
|
|
|
artwork = self.artwork
|
2016-04-26 22:41:58 +10:00
|
|
|
API = PlexAPI.API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# If the item already exist in the local Kodi DB we'll perform a full item update
|
|
|
|
# If the item doesn't exist, we'll add it to the database
|
|
|
|
update_item = True
|
|
|
|
itemid = item['Id']
|
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
mvideoid = emby_dbitem[0]
|
|
|
|
fileid = emby_dbitem[1]
|
|
|
|
pathid = emby_dbitem[2]
|
|
|
|
self.logMsg("mvideoid: %s fileid: %s pathid: %s" % (mvideoid, fileid, pathid), 1)
|
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
update_item = False
|
|
|
|
self.logMsg("mvideoid: %s not found." % itemid, 2)
|
|
|
|
# mvideoid
|
|
|
|
kodicursor.execute("select coalesce(max(idMVideo),0) from musicvideo")
|
|
|
|
mvideoid = kodicursor.fetchone()[0] + 1
|
|
|
|
|
2016-02-14 15:42:16 +11:00
|
|
|
else:
|
|
|
|
# Verification the item is still in Kodi
|
|
|
|
query = "SELECT * FROM musicvideo WHERE idMVideo = ?"
|
|
|
|
kodicursor.execute(query, (mvideoid,))
|
|
|
|
try:
|
|
|
|
kodicursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
# item is not found, let's recreate it.
|
|
|
|
update_item = False
|
|
|
|
self.logMsg("mvideoid: %s missing from Kodi, repairing the entry." % mvideoid, 1)
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
if not viewtag or not viewid:
|
|
|
|
# Get view tag from emby
|
2016-05-31 16:06:42 +10:00
|
|
|
viewtag, viewid, mediatype = self.emby.getView_plexid(itemid)
|
2015-12-25 06:51:47 +11:00
|
|
|
self.logMsg("View tag found: %s" % viewtag, 2)
|
|
|
|
|
|
|
|
# fileId information
|
|
|
|
checksum = API.getChecksum()
|
|
|
|
dateadded = API.getDateCreated()
|
|
|
|
userdata = API.getUserData()
|
|
|
|
playcount = userdata['PlayCount']
|
|
|
|
dateplayed = userdata['LastPlayedDate']
|
|
|
|
|
|
|
|
# item details
|
|
|
|
runtime = API.getRuntime()
|
|
|
|
plot = API.getOverview()
|
|
|
|
title = item['Name']
|
|
|
|
year = item.get('ProductionYear')
|
|
|
|
genres = item['Genres']
|
|
|
|
genre = " / ".join(genres)
|
|
|
|
studios = API.getStudios()
|
|
|
|
studio = " / ".join(studios)
|
|
|
|
artist = " / ".join(item.get('Artists'))
|
|
|
|
album = item.get('Album')
|
|
|
|
track = item.get('Track')
|
|
|
|
people = API.getPeople()
|
|
|
|
director = " / ".join(people['Director'])
|
|
|
|
|
|
|
|
|
|
|
|
##### GET THE FILE AND PATH #####
|
2016-01-30 06:07:21 +11:00
|
|
|
playurl = API.getKey()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
if "\\" in playurl:
|
|
|
|
# Local path
|
|
|
|
filename = playurl.rsplit("\\", 1)[1]
|
|
|
|
else: # Network share
|
|
|
|
filename = playurl.rsplit("/", 1)[1]
|
|
|
|
|
|
|
|
if self.directpath:
|
|
|
|
# Direct paths is set the Kodi way
|
2016-05-31 16:06:42 +10:00
|
|
|
if utils.window('plex_pathverified') != "true" and not xbmcvfs.exists(playurl):
|
2015-12-25 06:51:47 +11:00
|
|
|
# Validate the path is correct with user intervention
|
|
|
|
resp = xbmcgui.Dialog().yesno(
|
|
|
|
heading="Can't validate path",
|
|
|
|
line1=(
|
|
|
|
"Kodi can't locate file: %s. Verify the path. "
|
|
|
|
"You may to verify your network credentials in the "
|
|
|
|
"add-on settings or use the emby path substitution "
|
|
|
|
"to format your path correctly. Stop syncing?"
|
|
|
|
% playurl))
|
|
|
|
if resp:
|
2016-05-31 16:06:42 +10:00
|
|
|
utils.window('plex_shouldStop', value="true")
|
2015-12-25 06:51:47 +11:00
|
|
|
return False
|
|
|
|
|
|
|
|
path = playurl.replace(filename, "")
|
2016-05-31 16:06:42 +10:00
|
|
|
utils.window('plex_pathverified', value="true")
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Set plugin path and media flags using real filename
|
2015-12-27 21:49:14 +11:00
|
|
|
path = "plugin://plugin.video.plexkodiconnect.musicvideos/"
|
2015-12-25 06:51:47 +11:00
|
|
|
params = {
|
|
|
|
|
2016-05-07 21:15:02 +10:00
|
|
|
'filename': utils.tryEncode(filename),
|
2015-12-25 06:51:47 +11:00
|
|
|
'id': itemid,
|
|
|
|
'dbid': mvideoid,
|
|
|
|
'mode': "play"
|
|
|
|
}
|
|
|
|
filename = "%s?%s" % (path, urllib.urlencode(params))
|
|
|
|
|
|
|
|
|
|
|
|
##### UPDATE THE MUSIC VIDEO #####
|
|
|
|
if update_item:
|
|
|
|
self.logMsg("UPDATE mvideo itemid: %s - Title: %s" % (itemid, title), 1)
|
|
|
|
|
|
|
|
# Update path
|
|
|
|
query = "UPDATE path SET strPath = ? WHERE idPath = ?"
|
|
|
|
kodicursor.execute(query, (path, pathid))
|
|
|
|
|
|
|
|
# Update the filename
|
|
|
|
query = "UPDATE files SET strFilename = ?, dateAdded = ? WHERE idFile = ?"
|
|
|
|
kodicursor.execute(query, (filename, dateadded, fileid))
|
|
|
|
|
|
|
|
# Update the music video entry
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE musicvideo",
|
|
|
|
"SET c00 = ?, c04 = ?, c05 = ?, c06 = ?, c07 = ?, c08 = ?, c09 = ?, c10 = ?,",
|
|
|
|
"c11 = ?, c12 = ?"
|
|
|
|
"WHERE idMVideo = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (title, runtime, director, studio, year, plot, album,
|
|
|
|
artist, genre, track, mvideoid))
|
|
|
|
|
|
|
|
# Update the checksum in emby table
|
|
|
|
emby_db.updateReference(itemid, checksum)
|
|
|
|
|
|
|
|
##### OR ADD THE MUSIC VIDEO #####
|
|
|
|
else:
|
|
|
|
self.logMsg("ADD mvideo itemid: %s - Title: %s" % (itemid, title), 1)
|
|
|
|
|
|
|
|
# Add path
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idPath",
|
|
|
|
"FROM path",
|
|
|
|
"WHERE strPath = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (path,))
|
|
|
|
try:
|
|
|
|
pathid = kodicursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
kodicursor.execute("select coalesce(max(idPath),0) from path")
|
|
|
|
pathid = kodicursor.fetchone()[0] + 1
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO path(
|
|
|
|
idPath, strPath, strContent, strScraper, noUpdate)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (pathid, path, "musicvideos", "metadata.local", 1))
|
|
|
|
|
|
|
|
# Add the file
|
|
|
|
kodicursor.execute("select coalesce(max(idFile),0) from files")
|
|
|
|
fileid = kodicursor.fetchone()[0] + 1
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO files(
|
|
|
|
idFile, idPath, strFilename, dateAdded)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (fileid, pathid, filename, dateadded))
|
|
|
|
|
|
|
|
# Create the musicvideo entry
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO musicvideo(
|
|
|
|
idMVideo, idFile, c00, c04, c05, c06, c07, c08, c09, c10, c11, c12)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (mvideoid, fileid, title, runtime, director, studio,
|
|
|
|
year, plot, album, artist, genre, track))
|
|
|
|
|
|
|
|
# Create the reference in emby table
|
|
|
|
emby_db.addReference(itemid, mvideoid, "MusicVideo", "musicvideo", fileid, pathid,
|
|
|
|
checksum=checksum, mediafolderid=viewid)
|
|
|
|
|
|
|
|
|
|
|
|
# Process cast
|
|
|
|
people = item['People']
|
|
|
|
artists = item['ArtistItems']
|
|
|
|
for artist in artists:
|
|
|
|
artist['Type'] = "Artist"
|
|
|
|
people.extend(artists)
|
|
|
|
people = artwork.getPeopleArtwork(people)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addPeople(mvideoid, people, "musicvideo")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process genres
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addGenres(mvideoid, genres, "musicvideo")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process artwork
|
|
|
|
artwork.addArtwork(artwork.getAllArtwork(item), mvideoid, "musicvideo", kodicursor)
|
|
|
|
# Process stream details
|
|
|
|
streams = API.getMediaStreams()
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addStreams(fileid, streams, runtime)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process studios
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addStudios(mvideoid, studios, "musicvideo")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process tags: view, emby tags
|
|
|
|
tags = [viewtag]
|
|
|
|
tags.extend(item['Tags'])
|
|
|
|
if userdata['Favorite']:
|
|
|
|
tags.append("Favorite musicvideos")
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addTags(mvideoid, tags, "musicvideo")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process playstates
|
|
|
|
resume = API.adjustResume(userdata['Resume'])
|
|
|
|
total = round(float(runtime), 6)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addPlaystate(fileid, resume, total, playcount, dateplayed)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def updateUserdata(self, item):
|
|
|
|
# This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks
|
|
|
|
# Poster with progress bar
|
|
|
|
emby_db = self.emby_db
|
|
|
|
API = api.API(item)
|
|
|
|
|
|
|
|
# Get emby information
|
|
|
|
itemid = item['Id']
|
|
|
|
checksum = API.getChecksum()
|
|
|
|
userdata = API.getUserData()
|
|
|
|
runtime = API.getRuntime()
|
|
|
|
|
|
|
|
# Get Kodi information
|
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
mvideoid = emby_dbitem[0]
|
|
|
|
fileid = emby_dbitem[1]
|
|
|
|
self.logMsg(
|
|
|
|
"Update playstate for musicvideo: %s fileid: %s"
|
|
|
|
% (item['Name'], fileid), 1)
|
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Process favorite tags
|
|
|
|
if userdata['Favorite']:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addTag(mvideoid, "Favorite musicvideos", "musicvideo")
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.removeTag(mvideoid, "Favorite musicvideos", "musicvideo")
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Process playstates
|
|
|
|
playcount = userdata['PlayCount']
|
|
|
|
dateplayed = userdata['LastPlayedDate']
|
|
|
|
resume = API.adjustResume(userdata['Resume'])
|
|
|
|
total = round(float(runtime), 6)
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addPlaystate(fileid, resume, total, playcount, dateplayed)
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_db.updateReference(itemid, checksum)
|
|
|
|
|
|
|
|
def remove(self, itemid):
|
|
|
|
# Remove mvideoid, fileid, pathid, emby reference
|
|
|
|
emby_db = self.emby_db
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
artwork = self.artwork
|
|
|
|
|
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
mvideoid = emby_dbitem[0]
|
|
|
|
fileid = emby_dbitem[1]
|
|
|
|
pathid = emby_dbitem[2]
|
|
|
|
self.logMsg("Removing mvideoid: %s fileid: %s" % (mvideoid, fileid, pathid), 1)
|
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Remove artwork
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT url, type",
|
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = 'musicvideo'"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (mvideoid,))
|
2016-04-26 21:53:19 +10:00
|
|
|
for row in kodicursor.fetchall():
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
url = row[0]
|
|
|
|
imagetype = row[1]
|
|
|
|
if imagetype in ("poster", "fanart"):
|
|
|
|
artwork.deleteCachedArtwork(url)
|
|
|
|
|
|
|
|
kodicursor.execute("DELETE FROM musicvideo WHERE idMVideo = ?", (mvideoid,))
|
|
|
|
kodicursor.execute("DELETE FROM files WHERE idFile = ?", (fileid,))
|
|
|
|
if self.directpath:
|
|
|
|
kodicursor.execute("DELETE FROM path WHERE idPath = ?", (pathid,))
|
|
|
|
self.embycursor.execute("DELETE FROM emby WHERE emby_id = ?", (itemid,))
|
|
|
|
|
|
|
|
self.logMsg("Deleted musicvideo %s from kodi database" % itemid, 1)
|
|
|
|
|
|
|
|
|
2016-01-10 02:14:02 +11:00
|
|
|
class TVShows(Items):
|
2015-12-25 06:51:47 +11:00
|
|
|
def added(self, items, pdialog):
|
|
|
|
|
|
|
|
total = len(items)
|
|
|
|
count = 0
|
|
|
|
for tvshow in items:
|
|
|
|
|
|
|
|
title = tvshow['Name']
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
|
|
|
pdialog.update(percentage, message=title)
|
|
|
|
count += 1
|
|
|
|
self.add_update(tvshow)
|
|
|
|
# Add episodes
|
|
|
|
all_episodes = self.emby.getEpisodesbyShow(tvshow['Id'])
|
|
|
|
self.added_episode(all_episodes['Items'], pdialog)
|
|
|
|
|
|
|
|
def added_season(self, items, pdialog):
|
|
|
|
|
|
|
|
total = len(items)
|
|
|
|
count = 0
|
|
|
|
for season in items:
|
|
|
|
|
|
|
|
title = "%s - %s" % (season.get('SeriesName', "Unknown"), season['Name'])
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
|
|
|
pdialog.update(percentage, message=title)
|
|
|
|
count += 1
|
|
|
|
self.add_updateSeason(season)
|
|
|
|
# Add episodes
|
|
|
|
all_episodes = self.emby.getEpisodesbySeason(season['Id'])
|
|
|
|
self.added_episode(all_episodes['Items'], pdialog)
|
|
|
|
|
|
|
|
def added_episode(self, items, pdialog):
|
|
|
|
|
|
|
|
total = len(items)
|
|
|
|
count = 0
|
|
|
|
for episode in items:
|
|
|
|
title = "%s - %s" % (episode.get('SeriesName', "Unknown"), episode['Name'])
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
|
|
|
pdialog.update(percentage, message=title)
|
|
|
|
count += 1
|
|
|
|
self.add_updateEpisode(episode)
|
|
|
|
if not pdialog and self.contentmsg:
|
2016-02-04 22:42:17 +11:00
|
|
|
self.contentPop(title, self.newvideo_time)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def add_update(self, item, viewtag=None, viewid=None):
|
2016-03-10 04:36:36 +11:00
|
|
|
try:
|
|
|
|
self.run_add_update(item, viewtag, viewid)
|
|
|
|
except Exception as e:
|
|
|
|
self.logMsg('itemtypes.py for tv show has crashed for item %s. '
|
|
|
|
'Error:' % item.attrib.get('ratingKey', None), -1)
|
|
|
|
self.logMsg(e, -1)
|
2016-03-10 19:58:26 +11:00
|
|
|
import traceback
|
|
|
|
self.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
|
2016-03-10 19:30:20 +11:00
|
|
|
utils.window('plex_scancrashed', value='true')
|
2016-03-10 04:36:36 +11:00
|
|
|
# skip this item for now
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_add_update(self, item, viewtag=None, viewid=None):
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process single tvshow
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
emby_db = self.emby_db
|
|
|
|
artwork = self.artwork
|
2016-01-10 02:14:02 +11:00
|
|
|
API = PlexAPI.API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
update_item = True
|
2016-01-30 06:07:21 +11:00
|
|
|
itemid = API.getRatingKey()
|
2016-03-10 04:36:36 +11:00
|
|
|
|
2016-01-10 02:14:02 +11:00
|
|
|
if not itemid:
|
|
|
|
self.logMsg("Cannot parse XML data for TV show", -1)
|
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
# If the item already exist in the local Kodi DB we'll perform a full item update
|
|
|
|
# If the item doesn't exist, we'll add it to the database
|
|
|
|
update_item = True
|
2016-02-14 15:42:16 +11:00
|
|
|
force_episodes = False
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
showid = emby_dbitem[0]
|
|
|
|
pathid = emby_dbitem[2]
|
|
|
|
except TypeError:
|
|
|
|
update_item = False
|
2016-02-14 15:42:16 +11:00
|
|
|
kodicursor.execute("select coalesce(max(idShow),0) from tvshow")
|
|
|
|
showid = kodicursor.fetchone()[0] + 1
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Verification the item is still in Kodi
|
|
|
|
query = "SELECT * FROM tvshow WHERE idShow = ?"
|
|
|
|
kodicursor.execute(query, (showid,))
|
|
|
|
try:
|
|
|
|
kodicursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
# item is not found, let's recreate it.
|
|
|
|
update_item = False
|
|
|
|
self.logMsg("showid: %s missing from Kodi, repairing the entry." % showid, 1)
|
|
|
|
# Force re-add episodes after the show is re-created.
|
|
|
|
force_episodes = True
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
if viewtag is None or viewid is None:
|
|
|
|
# Get view tag from emby
|
2016-05-31 16:06:42 +10:00
|
|
|
viewtag, viewid, mediatype = embyserver.getView_plexid(itemid)
|
2015-12-25 06:51:47 +11:00
|
|
|
self.logMsg("View tag found: %s" % viewtag, 2)
|
|
|
|
|
|
|
|
# fileId information
|
|
|
|
checksum = API.getChecksum()
|
|
|
|
|
|
|
|
# item details
|
2016-01-10 02:14:02 +11:00
|
|
|
genres = API.getGenres()
|
|
|
|
title, sorttitle = API.getTitle()
|
|
|
|
plot = API.getPlot()
|
|
|
|
rating = API.getAudienceRating()
|
2015-12-25 06:51:47 +11:00
|
|
|
premieredate = API.getPremiereDate()
|
2016-03-17 04:47:57 +11:00
|
|
|
tvdb = API.getProvider('tvdb')
|
2015-12-25 06:51:47 +11:00
|
|
|
mpaa = API.getMpaa()
|
2016-01-10 02:14:02 +11:00
|
|
|
genre = API.joinList(genres)
|
2015-12-25 06:51:47 +11:00
|
|
|
studios = API.getStudios()
|
2016-03-02 02:28:48 +11:00
|
|
|
collections = API.getCollections()
|
2016-01-10 02:14:02 +11:00
|
|
|
try:
|
|
|
|
studio = studios[0]
|
|
|
|
except IndexError:
|
|
|
|
studio = None
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# GET THE FILE AND PATH #####
|
2016-03-16 06:26:45 +11:00
|
|
|
doIndirect = not self.directpath
|
2015-12-25 06:51:47 +11:00
|
|
|
if self.directpath:
|
|
|
|
# Direct paths is set the Kodi way
|
2016-03-16 06:26:45 +11:00
|
|
|
playurl = API.getTVShowPath()
|
|
|
|
if playurl is None:
|
|
|
|
# Something went wrong, trying to use non-direct paths
|
|
|
|
doIndirect = True
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
2016-04-13 22:15:31 +10:00
|
|
|
playurl = API.validatePlayurl(playurl,
|
|
|
|
API.getType(),
|
|
|
|
folder=True)
|
2016-04-13 01:18:32 +10:00
|
|
|
if playurl is None:
|
2016-03-16 22:26:31 +11:00
|
|
|
return False
|
2016-03-16 06:26:45 +11:00
|
|
|
if "\\" in playurl:
|
|
|
|
# Local path
|
|
|
|
path = "%s\\" % playurl
|
|
|
|
toplevelpath = "%s\\" % dirname(dirname(path))
|
|
|
|
else:
|
|
|
|
# Network path
|
|
|
|
path = "%s/" % playurl
|
|
|
|
toplevelpath = "%s/" % dirname(dirname(path))
|
|
|
|
if doIndirect:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Set plugin path
|
2015-12-27 21:49:14 +11:00
|
|
|
toplevelpath = "plugin://plugin.video.plexkodiconnect.tvshows/"
|
2015-12-25 06:51:47 +11:00
|
|
|
path = "%s%s/" % (toplevelpath, itemid)
|
2016-03-16 06:26:45 +11:00
|
|
|
|
2016-04-22 22:46:08 +10:00
|
|
|
# Add top path
|
2016-04-26 22:41:58 +10:00
|
|
|
toppathid = self.kodi_db.addPath(toplevelpath)
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE TVSHOW #####
|
2015-12-25 06:51:47 +11:00
|
|
|
if update_item:
|
|
|
|
self.logMsg("UPDATE tvshow itemid: %s - Title: %s" % (itemid, title), 1)
|
|
|
|
|
|
|
|
# Update the tvshow entry
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE tvshow",
|
|
|
|
"SET c00 = ?, c01 = ?, c04 = ?, c05 = ?, c08 = ?, c09 = ?,",
|
|
|
|
"c12 = ?, c13 = ?, c14 = ?, c15 = ?",
|
|
|
|
"WHERE idShow = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (title, plot, rating, premieredate, genre, title,
|
|
|
|
tvdb, mpaa, studio, sorttitle, showid))
|
|
|
|
|
|
|
|
# Update the checksum in emby table
|
|
|
|
emby_db.updateReference(itemid, checksum)
|
|
|
|
|
|
|
|
##### OR ADD THE TVSHOW #####
|
|
|
|
else:
|
|
|
|
self.logMsg("ADD tvshow itemid: %s - Title: %s" % (itemid, title), 1)
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE path",
|
|
|
|
"SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ?",
|
|
|
|
"WHERE idPath = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (toplevelpath, "tvshows", "metadata.local", 1, toppathid))
|
|
|
|
|
|
|
|
# Add path
|
2016-04-26 21:53:19 +10:00
|
|
|
pathid = self.kodi_db.addPath(path)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Create the tvshow entry
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO tvshow(
|
|
|
|
idShow, c00, c01, c04, c05, c08, c09, c12, c13, c14, c15)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (showid, title, plot, rating, premieredate, genre,
|
|
|
|
title, tvdb, mpaa, studio, sorttitle))
|
|
|
|
|
|
|
|
# Link the path
|
|
|
|
query = "INSERT INTO tvshowlinkpath(idShow, idPath) values(?, ?)"
|
|
|
|
kodicursor.execute(query, (showid, pathid))
|
|
|
|
|
|
|
|
# Create the reference in emby table
|
|
|
|
emby_db.addReference(itemid, showid, "Series", "tvshow", pathid=pathid,
|
|
|
|
checksum=checksum, mediafolderid=viewid)
|
|
|
|
# Update the path
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE path",
|
2016-04-22 22:46:08 +10:00
|
|
|
"SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ?, ",
|
|
|
|
"idParentPath = ?"
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idPath = ?"
|
|
|
|
))
|
2016-04-22 22:46:08 +10:00
|
|
|
kodicursor.execute(query, (path, None, None, 1, toppathid, pathid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Process cast
|
2016-01-10 02:14:02 +11:00
|
|
|
people = API.getPeopleList()
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addPeople(showid, people, "tvshow")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process genres
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addGenres(showid, genres, "tvshow")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process artwork
|
2016-01-10 02:14:02 +11:00
|
|
|
allartworks = API.getAllArtwork()
|
|
|
|
artwork.addArtwork(allartworks, showid, "tvshow", kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process studios
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addStudios(showid, studios, "tvshow")
|
2016-03-02 02:28:48 +11:00
|
|
|
# Process tags: view, PMS collection tags
|
2016-01-12 01:56:56 +11:00
|
|
|
tags = [viewtag]
|
2016-03-02 02:28:48 +11:00
|
|
|
tags.extend(collections)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addTags(showid, tags, "tvshow")
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-14 15:42:16 +11:00
|
|
|
if force_episodes:
|
|
|
|
# We needed to recreate the show entry. Re-add episodes now.
|
|
|
|
self.logMsg("Repairing episodes for showid: %s %s" % (showid, title), 1)
|
2016-02-20 06:03:06 +11:00
|
|
|
all_episodes = embyserver.getEpisodesbyShow(itemid)
|
2016-02-14 15:42:16 +11:00
|
|
|
self.added_episode(all_episodes['Items'], None)
|
2016-04-20 19:09:01 +10:00
|
|
|
self.kodiconn.commit()
|
|
|
|
self.embyconn.commit()
|
2016-02-14 15:42:16 +11:00
|
|
|
|
2016-03-10 04:36:36 +11:00
|
|
|
def add_updateSeason(self, item, viewtag=None, viewid=None):
|
|
|
|
try:
|
|
|
|
self.run_add_updateSeason(item, viewtag, viewid)
|
|
|
|
except Exception as e:
|
|
|
|
self.logMsg('itemtypes.py for tv seasons has crashed for item %s. '
|
|
|
|
'Error:' % item.attrib.get('ratingKey', None), -1)
|
|
|
|
self.logMsg(e, -1)
|
2016-03-10 19:58:26 +11:00
|
|
|
import traceback
|
|
|
|
self.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
|
2016-03-10 19:30:20 +11:00
|
|
|
utils.window('plex_scancrashed', value='true')
|
2016-03-10 04:36:36 +11:00
|
|
|
# skip this item for now
|
|
|
|
return
|
|
|
|
|
2016-03-14 02:06:54 +11:00
|
|
|
def run_add_updateSeason(self, item, viewtag=None, viewid=None):
|
2016-01-11 01:16:59 +11:00
|
|
|
API = PlexAPI.API(item)
|
2016-01-30 06:07:21 +11:00
|
|
|
itemid = API.getRatingKey()
|
2016-02-13 02:53:49 +11:00
|
|
|
if not itemid:
|
|
|
|
self.logMsg('Error getting itemid for season, skipping', -1)
|
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor = self.kodicursor
|
|
|
|
emby_db = self.emby_db
|
|
|
|
artwork = self.artwork
|
2016-01-10 02:14:02 +11:00
|
|
|
seasonnum = API.getIndex()
|
2016-03-14 02:06:54 +11:00
|
|
|
# Get parent tv show Plex id
|
|
|
|
plexshowid = item.attrib.get('parentRatingKey')
|
|
|
|
# Get Kodi showid
|
|
|
|
emby_dbitem = emby_db.getItem_byId(plexshowid)
|
|
|
|
try:
|
|
|
|
showid = emby_dbitem[0]
|
|
|
|
except:
|
|
|
|
self.logMsg('Could not find parent tv show for season %s. '
|
|
|
|
'Skipping season for now.'
|
|
|
|
% (itemid), -1)
|
|
|
|
return
|
|
|
|
|
2016-04-26 22:41:58 +10:00
|
|
|
seasonid = self.kodi_db.addSeason(showid, seasonnum)
|
2016-01-12 03:37:01 +11:00
|
|
|
checksum = API.getChecksum()
|
|
|
|
# Check whether Season already exists
|
|
|
|
update_item = True
|
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
embyDbItemId = emby_dbitem[0]
|
|
|
|
except TypeError:
|
|
|
|
update_item = False
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process artwork
|
2016-01-10 02:14:02 +11:00
|
|
|
allartworks = API.getAllArtwork()
|
|
|
|
artwork.addArtwork(allartworks, seasonid, "season", kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-01-12 03:37:01 +11:00
|
|
|
if update_item:
|
|
|
|
# Update a reference: checksum in emby table
|
|
|
|
emby_db.updateReference(itemid, checksum)
|
|
|
|
else:
|
|
|
|
# Create the reference in emby table
|
2016-03-14 02:06:54 +11:00
|
|
|
emby_db.addReference(itemid, seasonid, "Season", "season", parentid=viewid, checksum=checksum)
|
2016-04-20 19:09:01 +10:00
|
|
|
self.kodiconn.commit()
|
|
|
|
self.embyconn.commit()
|
2016-01-12 03:37:01 +11:00
|
|
|
|
2016-01-10 02:14:02 +11:00
|
|
|
def add_updateEpisode(self, item, viewtag=None, viewid=None):
|
2016-03-10 04:36:36 +11:00
|
|
|
try:
|
|
|
|
self.run_add_updateEpisode(item, viewtag, viewid)
|
|
|
|
except Exception as e:
|
|
|
|
self.logMsg('itemtypes.py for tv episode has crashed for item %s. '
|
|
|
|
'Error:' % item.attrib.get('ratingKey', None), -1)
|
|
|
|
self.logMsg(e, -1)
|
2016-03-10 19:58:26 +11:00
|
|
|
import traceback
|
|
|
|
self.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
|
2016-03-10 19:30:20 +11:00
|
|
|
utils.window('plex_scancrashed', value='true')
|
2016-03-10 04:36:36 +11:00
|
|
|
# skip this item for now
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_add_updateEpisode(self, item, viewtag=None, viewid=None):
|
2016-01-10 02:14:02 +11:00
|
|
|
"""
|
|
|
|
viewtag and viewid are irrelevant!
|
|
|
|
"""
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process single episode
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
emby_db = self.emby_db
|
|
|
|
artwork = self.artwork
|
2016-01-10 02:14:02 +11:00
|
|
|
API = PlexAPI.API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# If the item already exist in the local Kodi DB we'll perform a full
|
|
|
|
# item update
|
2015-12-25 06:51:47 +11:00
|
|
|
# If the item doesn't exist, we'll add it to the database
|
|
|
|
update_item = True
|
2016-01-30 06:07:21 +11:00
|
|
|
itemid = API.getRatingKey()
|
2016-02-13 02:53:49 +11:00
|
|
|
if not itemid:
|
|
|
|
self.logMsg('Error getting itemid for episode, skipping', -1)
|
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
episodeid = emby_dbitem[0]
|
|
|
|
fileid = emby_dbitem[1]
|
|
|
|
pathid = emby_dbitem[2]
|
|
|
|
except TypeError:
|
|
|
|
update_item = False
|
|
|
|
# episodeid
|
|
|
|
kodicursor.execute("select coalesce(max(idEpisode),0) from episode")
|
|
|
|
episodeid = kodicursor.fetchone()[0] + 1
|
|
|
|
|
2016-02-14 15:42:16 +11:00
|
|
|
else:
|
|
|
|
# Verification the item is still in Kodi
|
|
|
|
query = "SELECT * FROM episode WHERE idEpisode = ?"
|
|
|
|
kodicursor.execute(query, (episodeid,))
|
|
|
|
try:
|
|
|
|
kodicursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
# item is not found, let's recreate it.
|
|
|
|
update_item = False
|
|
|
|
self.logMsg("episodeid: %s missing from Kodi, repairing the entry." % episodeid, 1)
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# fileId information
|
|
|
|
checksum = API.getChecksum()
|
|
|
|
dateadded = API.getDateCreated()
|
|
|
|
userdata = API.getUserData()
|
|
|
|
playcount = userdata['PlayCount']
|
|
|
|
dateplayed = userdata['LastPlayedDate']
|
|
|
|
|
|
|
|
# item details
|
2016-01-11 01:16:59 +11:00
|
|
|
peoples = API.getPeople()
|
|
|
|
director = API.joinList(peoples['Director'])
|
|
|
|
writer = API.joinList(peoples['Writer'])
|
|
|
|
cast = API.joinList(peoples['Cast'])
|
|
|
|
producer = API.joinList(peoples['Producer'])
|
2016-01-10 02:14:02 +11:00
|
|
|
title, sorttitle = API.getTitle()
|
|
|
|
plot = API.getPlot()
|
2016-04-02 19:31:21 +11:00
|
|
|
rating = userdata['Rating']
|
2016-01-11 01:16:59 +11:00
|
|
|
resume, runtime = API.getRuntime()
|
2015-12-25 06:51:47 +11:00
|
|
|
premieredate = API.getPremiereDate()
|
|
|
|
|
|
|
|
# episode details
|
2016-01-10 02:14:02 +11:00
|
|
|
seriesId, seriesName, season, episode = API.getEpisodeDetails()
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
if season is None:
|
2016-03-12 00:47:41 +11:00
|
|
|
season = -1
|
2016-04-20 19:09:01 +10:00
|
|
|
if episode is None:
|
|
|
|
episode = -1
|
2016-03-12 00:47:41 +11:00
|
|
|
# if item.get('AbsoluteEpisodeNumber'):
|
|
|
|
# # Anime scenario
|
|
|
|
# season = 1
|
|
|
|
# episode = item['AbsoluteEpisodeNumber']
|
|
|
|
# else:
|
|
|
|
# season = -1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Specials ordering within season
|
2015-12-30 20:21:49 +11:00
|
|
|
if item.get('AirsAfterSeasonNumber'):
|
|
|
|
airsBeforeSeason = item['AirsAfterSeasonNumber']
|
|
|
|
airsBeforeEpisode = 4096 # Kodi default number for afterseason ordering
|
|
|
|
else:
|
2016-02-22 10:43:46 +11:00
|
|
|
airsBeforeSeason = item.get('AirsBeforeSeasonNumber')
|
|
|
|
airsBeforeEpisode = item.get('AirsBeforeEpisodeNumber')
|
2016-01-10 02:14:02 +11:00
|
|
|
|
|
|
|
airsBeforeSeason = "-1"
|
|
|
|
airsBeforeEpisode = "-1"
|
2015-12-25 06:51:47 +11:00
|
|
|
# Append multi episodes to title
|
2016-02-13 02:53:49 +11:00
|
|
|
# if item.get('IndexNumberEnd'):
|
2016-01-10 02:14:02 +11:00
|
|
|
# title = "| %02d | %s" % (item['IndexNumberEnd'], title)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Get season id
|
|
|
|
show = emby_db.getItem_byId(seriesId)
|
|
|
|
try:
|
|
|
|
showid = show[0]
|
|
|
|
except TypeError:
|
2016-01-30 06:07:21 +11:00
|
|
|
# self.logMsg("Show is missing from database, trying to add", 2)
|
|
|
|
# show = self.emby.getItem(seriesId)
|
|
|
|
# self.logMsg("Show now: %s. Trying to add new show" % show, 2)
|
|
|
|
# self.add_update(show)
|
|
|
|
# show = emby_db.getItem_byId(seriesId)
|
|
|
|
# try:
|
|
|
|
# showid = show[0]
|
|
|
|
# except TypeError:
|
|
|
|
# self.logMsg("Skipping: %s. Unable to add series: %s." % (itemid, seriesId), -1)
|
|
|
|
self.logMsg("Parent tvshow now found, skip item", 2)
|
|
|
|
return False
|
2016-04-26 21:53:19 +10:00
|
|
|
seasonid = self.kodi_db.addSeason(showid, season)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# GET THE FILE AND PATH #####
|
2016-03-16 06:26:45 +11:00
|
|
|
doIndirect = not self.directpath
|
2016-04-20 16:46:51 +10:00
|
|
|
playurl = API.getFilePath()
|
2015-12-25 06:51:47 +11:00
|
|
|
if self.directpath:
|
|
|
|
# Direct paths is set the Kodi way
|
2016-03-16 06:26:45 +11:00
|
|
|
if playurl is None:
|
|
|
|
# Something went wrong, trying to use non-direct paths
|
|
|
|
doIndirect = True
|
|
|
|
else:
|
2016-04-13 01:18:32 +10:00
|
|
|
playurl = API.validatePlayurl(playurl, API.getType())
|
|
|
|
if playurl is None:
|
2016-03-16 22:26:31 +11:00
|
|
|
return False
|
2016-03-16 06:26:45 +11:00
|
|
|
if "\\" in playurl:
|
|
|
|
# Local path
|
|
|
|
filename = playurl.rsplit("\\", 1)[1]
|
|
|
|
else:
|
|
|
|
# Network share
|
|
|
|
filename = playurl.rsplit("/", 1)[1]
|
|
|
|
path = playurl.replace(filename, "")
|
2016-04-26 22:41:58 +10:00
|
|
|
parentPathId = self.kodi_db.getParentPathId(path)
|
2016-03-16 06:26:45 +11:00
|
|
|
if doIndirect:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Set plugin path and media flags using real filename
|
2016-04-20 16:46:51 +10:00
|
|
|
if playurl is not None:
|
|
|
|
if '\\' in playurl:
|
|
|
|
filename = playurl.rsplit('\\', 1)[1]
|
|
|
|
else:
|
|
|
|
filename = playurl.rsplit('/', 1)[1]
|
|
|
|
else:
|
2016-05-08 06:17:12 +10:00
|
|
|
filename = 'file_not_found.mkv'
|
2016-04-20 16:46:51 +10:00
|
|
|
path = "plugin://plugin.video.plexkodiconnect.tvshows/%s/" % seriesId
|
2015-12-25 06:51:47 +11:00
|
|
|
params = {
|
2016-05-07 21:15:02 +10:00
|
|
|
'filename': utils.tryEncode(filename),
|
2015-12-25 06:51:47 +11:00
|
|
|
'id': itemid,
|
|
|
|
'dbid': episodeid,
|
|
|
|
'mode': "play"
|
|
|
|
}
|
2016-05-08 06:17:12 +10:00
|
|
|
filename = "%s?%s" % (path,
|
|
|
|
utils.tryDecode(urllib.urlencode(params)))
|
2016-04-23 19:48:21 +10:00
|
|
|
playurl = filename
|
2016-04-26 22:41:58 +10:00
|
|
|
parentPathId = self.kodi_db.addPath(
|
2016-04-22 22:46:08 +10:00
|
|
|
'plugin://plugin.video.plexkodiconnect.tvshows/')
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-04-23 19:48:21 +10:00
|
|
|
# Even if the item is only updated, the file may have been moved or updated.
|
|
|
|
# In the worst case we get exactly the same values as we had before.
|
2016-04-26 22:41:58 +10:00
|
|
|
pathid = self.kodi_db.addPath(path)
|
|
|
|
fileid = self.kodi_db.addFile(filename, pathid)
|
2016-04-23 19:48:21 +10:00
|
|
|
|
|
|
|
# episodes table:
|
|
|
|
# c18 - playurl
|
|
|
|
# c19 - pathid
|
|
|
|
# This information is used later by file browser.
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE EPISODE #####
|
2015-12-25 06:51:47 +11:00
|
|
|
if update_item:
|
2016-01-30 06:07:21 +11:00
|
|
|
self.logMsg("UPDATE episode itemid: %s" % (itemid), 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Update the movie entry
|
2016-04-26 21:53:19 +10:00
|
|
|
if self.kodiversion in (16, 17):
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE episode",
|
|
|
|
"SET c00 = ?, c01 = ?, c03 = ?, c04 = ?, c05 = ?, c09 = ?, c10 = ?,",
|
2016-04-23 19:48:21 +10:00
|
|
|
"c12 = ?, c13 = ?, c14 = ?, c15 = ?, c16 = ?, c18 = ?, c19 = ?,",
|
|
|
|
"idSeason = ?",
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idEpisode = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (title, plot, rating, writer, premieredate,
|
|
|
|
runtime, director, season, episode, title, airsBeforeSeason,
|
2016-04-23 19:48:21 +10:00
|
|
|
airsBeforeEpisode, playurl, pathid, seasonid, episodeid))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE episode",
|
|
|
|
"SET c00 = ?, c01 = ?, c03 = ?, c04 = ?, c05 = ?, c09 = ?, c10 = ?,",
|
2016-04-23 19:48:21 +10:00
|
|
|
"c12 = ?, c13 = ?, c14 = ?, c15 = ?, c16 = ?, c18 = ?, c19 = ?",
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idEpisode = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (title, plot, rating, writer, premieredate,
|
|
|
|
runtime, director, season, episode, title, airsBeforeSeason,
|
2016-04-23 19:48:21 +10:00
|
|
|
airsBeforeEpisode, playurl, pathid, episodeid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Update the checksum in emby table
|
|
|
|
emby_db.updateReference(itemid, checksum)
|
|
|
|
# Update parentid reference
|
|
|
|
emby_db.updateParentId(itemid, seasonid)
|
|
|
|
|
|
|
|
##### OR ADD THE EPISODE #####
|
|
|
|
else:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.logMsg("ADD episode itemid: %s - Title: %s" % (itemid, title), 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Create the episode entry
|
2016-04-26 21:53:19 +10:00
|
|
|
if self.kodiversion in (16, 17):
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO episode(
|
|
|
|
idEpisode, idFile, c00, c01, c03, c04, c05, c09, c10, c12, c13, c14,
|
2016-04-23 19:48:21 +10:00
|
|
|
idShow, c15, c16, c18, c19, idSeason)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-04-23 19:48:21 +10:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2015-12-25 06:51:47 +11:00
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (episodeid, fileid, title, plot, rating, writer,
|
|
|
|
premieredate, runtime, director, season, episode, title, showid,
|
2016-04-23 19:48:21 +10:00
|
|
|
airsBeforeSeason, airsBeforeEpisode, playurl, pathid, seasonid))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO episode(
|
|
|
|
idEpisode, idFile, c00, c01, c03, c04, c05, c09, c10, c12, c13, c14,
|
2016-04-23 19:48:21 +10:00
|
|
|
idShow, c15, c16, c18, c19)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-04-23 19:48:21 +10:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2015-12-25 06:51:47 +11:00
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (episodeid, fileid, title, plot, rating, writer,
|
|
|
|
premieredate, runtime, director, season, episode, title, showid,
|
2016-04-23 19:48:21 +10:00
|
|
|
airsBeforeSeason, airsBeforeEpisode, playurl, pathid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Create the reference in emby table
|
|
|
|
emby_db.addReference(itemid, episodeid, "Episode", "episode", fileid, pathid,
|
|
|
|
seasonid, checksum)
|
|
|
|
|
|
|
|
# Update the path
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE path",
|
2016-04-22 22:46:08 +10:00
|
|
|
"SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ?, ",
|
|
|
|
"idParentPath = ?"
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idPath = ?"
|
|
|
|
))
|
2016-04-22 22:46:08 +10:00
|
|
|
kodicursor.execute(query, (path, None, None, 1, parentPathId, pathid))
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update the file
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE files",
|
|
|
|
"SET idPath = ?, strFilename = ?, dateAdded = ?",
|
|
|
|
"WHERE idFile = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (pathid, filename, dateadded, fileid))
|
|
|
|
# Process cast
|
2016-01-10 02:14:02 +11:00
|
|
|
people = API.getPeopleList()
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addPeople(episodeid, people, "episode")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process artwork
|
2016-03-14 21:47:14 +11:00
|
|
|
# Wide "screenshot" of particular episode
|
|
|
|
poster = item.attrib.get('thumb')
|
|
|
|
if poster:
|
|
|
|
poster = API.addPlexCredentialsToUrl(
|
|
|
|
"%s%s" % (self.server, poster))
|
|
|
|
artwork.addOrUpdateArt(
|
|
|
|
poster, episodeid, "episode", "thumb", kodicursor)
|
|
|
|
# poster of TV show itself
|
|
|
|
# poster = item.attrib.get('grandparentThumb')
|
|
|
|
# if poster:
|
|
|
|
# poster = API.addPlexCredentialsToUrl(
|
|
|
|
# "%s%s" % (self.server, poster))
|
|
|
|
# artwork.addOrUpdateArt(
|
|
|
|
# poster, episodeid, "episode", "poster", kodicursor)
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process stream details
|
|
|
|
streams = API.getMediaStreams()
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addStreams(fileid, streams, runtime)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process playstates
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addPlaystate(fileid, resume, runtime, playcount, dateplayed)
|
2015-12-25 06:51:47 +11:00
|
|
|
if not self.directpath and resume:
|
|
|
|
# Create additional entry for widgets. This is only required for plugin/episode.
|
2016-04-26 21:53:19 +10:00
|
|
|
temppathid = self.kodi_db.getPath("plugin://plugin.video.plexkodiconnect.tvshows/")
|
|
|
|
tempfileid = self.kodi_db.addFile(filename, temppathid)
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE files",
|
|
|
|
"SET idPath = ?, strFilename = ?, dateAdded = ?",
|
|
|
|
"WHERE idFile = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (temppathid, filename, dateadded, tempfileid))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addPlaystate(tempfileid, resume, runtime, playcount, dateplayed)
|
2016-04-20 19:09:01 +10:00
|
|
|
self.kodiconn.commit()
|
|
|
|
self.embyconn.commit()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def remove(self, itemid):
|
|
|
|
# Remove showid, fileid, pathid, emby reference
|
|
|
|
emby_db = self.emby_db
|
|
|
|
embycursor = self.embycursor
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
artwork = self.artwork
|
|
|
|
|
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
kodiid = emby_dbitem[0]
|
|
|
|
fileid = emby_dbitem[1]
|
|
|
|
pathid = emby_dbitem[2]
|
|
|
|
parentid = emby_dbitem[3]
|
|
|
|
mediatype = emby_dbitem[4]
|
|
|
|
self.logMsg("Removing %s kodiid: %s fileid: %s" % (mediatype, kodiid, fileid), 1)
|
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
|
|
|
|
##### PROCESS ITEM #####
|
|
|
|
|
|
|
|
# Remove the emby reference
|
|
|
|
emby_db.removeItem(itemid)
|
|
|
|
|
|
|
|
|
|
|
|
##### IF EPISODE #####
|
|
|
|
|
|
|
|
if mediatype == "episode":
|
|
|
|
# Delete kodi episode and file, verify season and tvshow
|
|
|
|
self.removeEpisode(kodiid, fileid)
|
|
|
|
|
|
|
|
# Season verification
|
|
|
|
season = emby_db.getItem_byKodiId(parentid, "season")
|
|
|
|
try:
|
|
|
|
showid = season[1]
|
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
|
|
|
|
season_episodes = emby_db.getItem_byParentId(parentid, "episode")
|
|
|
|
if not season_episodes:
|
|
|
|
self.removeSeason(parentid)
|
|
|
|
emby_db.removeItem(season[0])
|
|
|
|
|
|
|
|
# Show verification
|
|
|
|
show = emby_db.getItem_byKodiId(showid, "tvshow")
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT totalCount",
|
|
|
|
"FROM tvshowcounts",
|
|
|
|
"WHERE idShow = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (showid,))
|
|
|
|
result = kodicursor.fetchone()
|
|
|
|
if result and result[0] is None:
|
|
|
|
# There's no episodes left, delete show and any possible remaining seasons
|
|
|
|
seasons = emby_db.getItem_byParentId(showid, "season")
|
|
|
|
for season in seasons:
|
|
|
|
self.removeSeason(season[1])
|
|
|
|
else:
|
|
|
|
# Delete emby season entries
|
|
|
|
emby_db.removeItems_byParentId(showid, "season")
|
|
|
|
self.removeShow(showid)
|
|
|
|
emby_db.removeItem(show[0])
|
|
|
|
|
|
|
|
##### IF TVSHOW #####
|
|
|
|
|
|
|
|
elif mediatype == "tvshow":
|
|
|
|
# Remove episodes, seasons, tvshow
|
|
|
|
seasons = emby_db.getItem_byParentId(kodiid, "season")
|
|
|
|
for season in seasons:
|
|
|
|
seasonid = season[1]
|
|
|
|
season_episodes = emby_db.getItem_byParentId(seasonid, "episode")
|
|
|
|
for episode in season_episodes:
|
|
|
|
self.removeEpisode(episode[1], episode[2])
|
|
|
|
else:
|
|
|
|
# Remove emby episodes
|
|
|
|
emby_db.removeItems_byParentId(seasonid, "episode")
|
|
|
|
else:
|
|
|
|
# Remove emby seasons
|
|
|
|
emby_db.removeItems_byParentId(kodiid, "season")
|
|
|
|
|
|
|
|
# Remove tvshow
|
|
|
|
self.removeShow(kodiid)
|
|
|
|
|
|
|
|
##### IF SEASON #####
|
|
|
|
|
|
|
|
elif mediatype == "season":
|
|
|
|
# Remove episodes, season, verify tvshow
|
|
|
|
season_episodes = emby_db.getItem_byParentId(kodiid, "episode")
|
|
|
|
for episode in season_episodes:
|
|
|
|
self.removeEpisode(episode[1], episode[2])
|
|
|
|
else:
|
|
|
|
# Remove emby episodes
|
|
|
|
emby_db.removeItems_byParentId(kodiid, "episode")
|
|
|
|
|
|
|
|
# Remove season
|
|
|
|
self.removeSeason(kodiid)
|
|
|
|
|
|
|
|
# Show verification
|
|
|
|
seasons = emby_db.getItem_byParentId(parentid, "season")
|
|
|
|
if not seasons:
|
|
|
|
# There's no seasons, delete the show
|
|
|
|
self.removeShow(parentid)
|
|
|
|
emby_db.removeItem_byKodiId(parentid, "tvshow")
|
|
|
|
|
|
|
|
self.logMsg("Deleted %s: %s from kodi database" % (mediatype, itemid), 1)
|
|
|
|
|
|
|
|
def removeShow(self, kodiid):
|
|
|
|
|
|
|
|
kodicursor = self.kodicursor
|
2016-04-26 21:53:19 +10:00
|
|
|
self.artwork.deleteArtwork(kodiid, "tvshow", kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor.execute("DELETE FROM tvshow WHERE idShow = ?", (kodiid,))
|
|
|
|
self.logMsg("Removed tvshow: %s." % kodiid, 2)
|
|
|
|
|
|
|
|
def removeSeason(self, kodiid):
|
|
|
|
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.artwork.deleteArtwork(kodiid, "season", kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor.execute("DELETE FROM seasons WHERE idSeason = ?", (kodiid,))
|
|
|
|
self.logMsg("Removed season: %s." % kodiid, 2)
|
|
|
|
|
|
|
|
def removeEpisode(self, kodiid, fileid):
|
|
|
|
|
|
|
|
kodicursor = self.kodicursor
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.artwork.deleteArtwork(kodiid, "episode", kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor.execute("DELETE FROM episode WHERE idEpisode = ?", (kodiid,))
|
|
|
|
kodicursor.execute("DELETE FROM files WHERE idFile = ?", (fileid,))
|
|
|
|
self.logMsg("Removed episode: %s." % kodiid, 2)
|
|
|
|
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
class Music(Items):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
def __init__(self):
|
|
|
|
Items.__init__(self)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
self.directstream = utils.settings('streamMusic') == "true"
|
2016-01-21 06:21:56 +11:00
|
|
|
self.enableimportsongrating = utils.settings('enableImportSongRating') == "true"
|
|
|
|
self.enableexportsongrating = utils.settings('enableExportSongRating') == "true"
|
|
|
|
self.enableupdatesongrating = utils.settings('enableUpdateSongRating') == "true"
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
def __enter__(self):
|
|
|
|
"""
|
|
|
|
OVERWRITE this method, because we need to open another DB.
|
|
|
|
Open DB connections and cursors
|
|
|
|
"""
|
|
|
|
self.embyconn = utils.kodiSQL('emby')
|
|
|
|
self.embycursor = self.embyconn.cursor()
|
|
|
|
# Here it is, not 'video' but 'music'
|
|
|
|
self.kodiconn = utils.kodiSQL('music')
|
|
|
|
self.kodicursor = self.kodiconn.cursor()
|
|
|
|
self.emby_db = embydb.Embydb_Functions(self.embycursor)
|
|
|
|
self.kodi_db = kodidb.Kodidb_Functions(self.kodicursor)
|
|
|
|
return self
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def added(self, items, pdialog):
|
|
|
|
|
2015-12-30 23:42:37 +11:00
|
|
|
total = len(items)
|
|
|
|
count = 0
|
2015-12-25 06:51:47 +11:00
|
|
|
for artist in items:
|
|
|
|
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
2016-04-26 21:53:19 +10:00
|
|
|
pdialog.update(percentage, message=artist['Name'])
|
2015-12-25 06:51:47 +11:00
|
|
|
count += 1
|
|
|
|
self.add_updateArtist(artist)
|
|
|
|
# Add albums
|
|
|
|
all_albums = self.emby.getAlbumsbyArtist(artist['Id'])
|
|
|
|
self.added_album(all_albums['Items'], pdialog)
|
|
|
|
|
|
|
|
def added_album(self, items, pdialog):
|
|
|
|
|
2015-12-30 23:42:37 +11:00
|
|
|
total = len(items)
|
|
|
|
count = 0
|
2015-12-25 06:51:47 +11:00
|
|
|
for album in items:
|
|
|
|
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
2016-04-26 21:53:19 +10:00
|
|
|
pdialog.update(percentage, message=album['Name'])
|
2015-12-25 06:51:47 +11:00
|
|
|
count += 1
|
|
|
|
self.add_updateAlbum(album)
|
|
|
|
# Add songs
|
|
|
|
all_songs = self.emby.getSongsbyAlbum(album['Id'])
|
|
|
|
self.added_song(all_songs['Items'], pdialog)
|
|
|
|
|
|
|
|
def added_song(self, items, pdialog):
|
|
|
|
|
2015-12-30 23:42:37 +11:00
|
|
|
total = len(items)
|
|
|
|
count = 0
|
2015-12-25 06:51:47 +11:00
|
|
|
for song in items:
|
|
|
|
|
|
|
|
if pdialog:
|
|
|
|
percentage = int((float(count) / float(total))*100)
|
2016-04-26 21:53:19 +10:00
|
|
|
pdialog.update(percentage, message=song['Name'])
|
2015-12-25 06:51:47 +11:00
|
|
|
count += 1
|
|
|
|
self.add_updateSong(song)
|
|
|
|
if not pdialog and self.contentmsg:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.contentPop(song['Name'], self.newmusic_time)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-10 04:36:36 +11:00
|
|
|
def add_updateArtist(self, item, viewtag=None, viewid=None, artisttype="MusicArtist"):
|
|
|
|
try:
|
|
|
|
self.run_add_updateArtist(item, viewtag, viewid, artisttype)
|
|
|
|
except Exception as e:
|
|
|
|
self.logMsg('itemtypes.py for music artist has crashed for '
|
|
|
|
'item %s. Error:'
|
|
|
|
% item.attrib.get('ratingKey', None), -1)
|
|
|
|
self.logMsg(e, -1)
|
2016-03-10 19:58:26 +11:00
|
|
|
import traceback
|
|
|
|
self.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
|
2016-03-10 19:30:20 +11:00
|
|
|
utils.window('plex_scancrashed', value='true')
|
2016-03-10 04:36:36 +11:00
|
|
|
# skip this item for now
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_add_updateArtist(self, item, viewtag=None, viewid=None,
|
2016-02-13 02:53:49 +11:00
|
|
|
artisttype="MusicArtist"):
|
2016-01-09 22:13:17 +11:00
|
|
|
kodicursor = self.kodicursor
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_db = self.emby_db
|
|
|
|
artwork = self.artwork
|
2016-02-13 02:53:49 +11:00
|
|
|
API = PlexAPI.API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
update_item = True
|
2016-02-13 02:53:49 +11:00
|
|
|
itemid = API.getRatingKey()
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
artistid = emby_dbitem[0]
|
|
|
|
except TypeError:
|
|
|
|
update_item = False
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# The artist details #####
|
2015-12-25 06:51:47 +11:00
|
|
|
lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
dateadded = API.getDateCreated()
|
|
|
|
checksum = API.getChecksum()
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
name, sortname = API.getTitle()
|
|
|
|
# musicBrainzId = API.getProvider('MusicBrainzArtist')
|
|
|
|
musicBrainzId = None
|
|
|
|
genres = API.joinList(API.getGenres())
|
|
|
|
bio = API.getPlot()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Associate artwork
|
2016-02-13 02:53:49 +11:00
|
|
|
artworks = API.getAllArtwork(parentInfo=True)
|
2015-12-25 06:51:47 +11:00
|
|
|
thumb = artworks['Primary']
|
2016-02-13 02:53:49 +11:00
|
|
|
backdrops = artworks['Backdrop'] # List
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
if thumb:
|
|
|
|
thumb = "<thumb>%s</thumb>" % thumb
|
|
|
|
if backdrops:
|
|
|
|
fanart = "<fanart>%s</fanart>" % backdrops[0]
|
|
|
|
else:
|
|
|
|
fanart = ""
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE ARTIST #####
|
2015-12-25 06:51:47 +11:00
|
|
|
if update_item:
|
2016-02-13 02:53:49 +11:00
|
|
|
self.logMsg("UPDATE artist itemid: %s - Name: %s"
|
|
|
|
% (itemid, name), 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update the checksum in emby table
|
|
|
|
emby_db.updateReference(itemid, checksum)
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# OR ADD THE ARTIST #####
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
self.logMsg("ADD artist itemid: %s - Name: %s" % (itemid, name), 1)
|
2016-02-13 02:53:49 +11:00
|
|
|
# safety checks: It looks like Emby supports the same artist
|
|
|
|
# multiple times.
|
|
|
|
# Kodi doesn't allow that. In case that happens we just merge the
|
|
|
|
# artist entries.
|
2016-04-26 21:53:19 +10:00
|
|
|
artistid = self.kodi_db.addArtist(name, musicBrainzId)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Create the reference in emby table
|
2016-02-13 02:53:49 +11:00
|
|
|
emby_db.addReference(
|
|
|
|
itemid, artistid, artisttype, "artist", checksum=checksum)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Process the artist
|
2016-01-24 02:45:31 +11:00
|
|
|
if self.kodiversion in (16, 17):
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE artist",
|
|
|
|
"SET strGenres = ?, strBiography = ?, strImage = ?, strFanart = ?,",
|
|
|
|
"lastScraped = ?",
|
|
|
|
"WHERE idArtist = ?"
|
|
|
|
))
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute(query, (genres, bio, thumb, fanart,
|
|
|
|
lastScraped, artistid))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE artist",
|
|
|
|
"SET strGenres = ?, strBiography = ?, strImage = ?, strFanart = ?,",
|
|
|
|
"lastScraped = ?, dateAdded = ?",
|
|
|
|
"WHERE idArtist = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (genres, bio, thumb, fanart, lastScraped,
|
2016-02-13 02:53:49 +11:00
|
|
|
dateadded, artistid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Update artwork
|
|
|
|
artwork.addArtwork(artworks, artistid, "artist", kodicursor)
|
2016-03-18 01:06:04 +11:00
|
|
|
self.embyconn.commit()
|
|
|
|
self.kodiconn.commit()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
def add_updateAlbum(self, item, viewtag=None, viewid=None):
|
2016-03-10 04:36:36 +11:00
|
|
|
try:
|
|
|
|
self.run_add_updateAlbum(item, viewtag, viewid)
|
|
|
|
except Exception as e:
|
|
|
|
self.logMsg('itemtypes.py for music album has crashed for '
|
|
|
|
'item %s. Error:'
|
|
|
|
% item.attrib.get('ratingKey', None), -1)
|
|
|
|
self.logMsg(e, -1)
|
2016-03-10 19:58:26 +11:00
|
|
|
import traceback
|
|
|
|
self.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
|
2016-03-10 19:30:20 +11:00
|
|
|
utils.window('plex_scancrashed', value='true')
|
2016-03-10 04:36:36 +11:00
|
|
|
# skip this item for now
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_add_updateAlbum(self, item, viewtag=None, viewid=None):
|
2016-01-09 22:13:17 +11:00
|
|
|
kodicursor = self.kodicursor
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_db = self.emby_db
|
|
|
|
artwork = self.artwork
|
2016-02-13 02:53:49 +11:00
|
|
|
API = PlexAPI.API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
update_item = True
|
2016-02-13 02:53:49 +11:00
|
|
|
itemid = API.getRatingKey()
|
|
|
|
if not itemid:
|
|
|
|
self.logMsg('Error processing Album, skipping', -1)
|
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
albumid = emby_dbitem[0]
|
|
|
|
except TypeError:
|
2016-02-13 02:53:49 +11:00
|
|
|
# Albumid not found
|
2015-12-25 06:51:47 +11:00
|
|
|
update_item = False
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# The album details #####
|
2015-12-25 06:51:47 +11:00
|
|
|
lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
dateadded = API.getDateCreated()
|
|
|
|
userdata = API.getUserData()
|
|
|
|
checksum = API.getChecksum()
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
name, sorttitle = API.getTitle()
|
|
|
|
# musicBrainzId = API.getProvider('MusicBrainzAlbum')
|
|
|
|
musicBrainzId = None
|
|
|
|
year = API.getYear()
|
|
|
|
genres = API.getGenres()
|
|
|
|
genre = API.joinList(genres)
|
|
|
|
bio = API.getPlot()
|
2016-01-12 02:53:41 +11:00
|
|
|
rating = userdata['UserRating']
|
2016-03-28 18:56:22 +11:00
|
|
|
studio = API.getMusicStudio()
|
2016-02-13 02:53:49 +11:00
|
|
|
# artists = item['AlbumArtists']
|
|
|
|
# if not artists:
|
|
|
|
# artists = item['ArtistItems']
|
|
|
|
# artistname = []
|
|
|
|
# for artist in artists:
|
|
|
|
# artistname.append(artist['Name'])
|
|
|
|
artistname = item.attrib.get('parentTitle')
|
|
|
|
if not artistname:
|
|
|
|
artistname = item.attrib.get('originalTitle')
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Associate artwork
|
2016-02-13 02:53:49 +11:00
|
|
|
artworks = API.getAllArtwork(parentInfo=True)
|
2015-12-25 06:51:47 +11:00
|
|
|
thumb = artworks['Primary']
|
|
|
|
if thumb:
|
|
|
|
thumb = "<thumb>%s</thumb>" % thumb
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE ALBUM #####
|
2015-12-25 06:51:47 +11:00
|
|
|
if update_item:
|
2016-02-13 02:53:49 +11:00
|
|
|
self.logMsg("UPDATE album itemid: %s - Name: %s"
|
|
|
|
% (itemid, name), 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update the checksum in emby table
|
|
|
|
emby_db.updateReference(itemid, checksum)
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# OR ADD THE ALBUM #####
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
self.logMsg("ADD album itemid: %s - Name: %s" % (itemid, name), 1)
|
2016-02-13 02:53:49 +11:00
|
|
|
# safety checks: It looks like Emby supports the same artist
|
|
|
|
# multiple times.
|
|
|
|
# Kodi doesn't allow that. In case that happens we just merge the
|
|
|
|
# artist entries.
|
2016-04-26 21:53:19 +10:00
|
|
|
albumid = self.kodi_db.addAlbum(name, musicBrainzId)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Create the reference in emby table
|
2016-02-13 02:53:49 +11:00
|
|
|
emby_db.addReference(
|
|
|
|
itemid, albumid, "MusicAlbum", "album", checksum=checksum)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Process the album info
|
2016-04-26 21:53:19 +10:00
|
|
|
if self.kodiversion == 17:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Krypton
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE album",
|
|
|
|
"SET strArtists = ?, iYear = ?, strGenres = ?, strReview = ?, strImage = ?,",
|
2016-03-28 18:56:22 +11:00
|
|
|
"iUserrating = ?, lastScraped = ?, strReleaseType = ?, "
|
|
|
|
"strLabel = ? ",
|
2016-01-24 02:45:31 +11:00
|
|
|
"WHERE idAlbum = ?"
|
|
|
|
))
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute(query, (artistname, year, genre, bio, thumb,
|
2016-03-28 18:56:22 +11:00
|
|
|
rating, lastScraped, "album", studio,
|
|
|
|
albumid))
|
2016-04-26 21:53:19 +10:00
|
|
|
elif self.kodiversion == 16:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Kodi Jarvis
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE album",
|
|
|
|
"SET strArtists = ?, iYear = ?, strGenres = ?, strReview = ?, strImage = ?,",
|
2016-03-28 18:56:22 +11:00
|
|
|
"iRating = ?, lastScraped = ?, strReleaseType = ?, "
|
|
|
|
"strLabel = ? ",
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idAlbum = ?"
|
|
|
|
))
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute(query, (artistname, year, genre, bio, thumb,
|
2016-03-28 18:56:22 +11:00
|
|
|
rating, lastScraped, "album", studio,
|
|
|
|
albumid))
|
2016-04-26 21:53:19 +10:00
|
|
|
elif self.kodiversion == 15:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Kodi Isengard
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE album",
|
|
|
|
"SET strArtists = ?, iYear = ?, strGenres = ?, strReview = ?, strImage = ?,",
|
2016-03-28 18:56:22 +11:00
|
|
|
"iRating = ?, lastScraped = ?, dateAdded = ?, "
|
|
|
|
"strReleaseType = ?, strLabel = ? ",
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idAlbum = ?"
|
|
|
|
))
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute(query, (artistname, year, genre, bio, thumb,
|
|
|
|
rating, lastScraped, dateadded,
|
2016-03-28 18:56:22 +11:00
|
|
|
"album", studio, albumid))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE album",
|
|
|
|
"SET strArtists = ?, iYear = ?, strGenres = ?, strReview = ?, strImage = ?,",
|
2016-03-28 18:56:22 +11:00
|
|
|
"iRating = ?, lastScraped = ?, dateAdded = ?, "
|
|
|
|
"strLabel = ? ",
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idAlbum = ?"
|
|
|
|
))
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute(query, (artistname, year, genre, bio, thumb,
|
2016-03-28 18:56:22 +11:00
|
|
|
rating, lastScraped, dateadded, studio,
|
2016-02-13 02:53:49 +11:00
|
|
|
albumid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Associate the parentid for emby reference
|
2016-02-13 02:53:49 +11:00
|
|
|
parentId = item.attrib.get('parentRatingKey')
|
2015-12-25 06:51:47 +11:00
|
|
|
if parentId is not None:
|
|
|
|
emby_dbartist = emby_db.getItem_byId(parentId)
|
|
|
|
try:
|
|
|
|
artistid = emby_dbartist[0]
|
|
|
|
except TypeError:
|
2016-02-13 02:53:49 +11:00
|
|
|
self.logMsg('Artist %s does not exist in emby database'
|
|
|
|
% parentId, 1)
|
|
|
|
artist = GetPlexMetadata(parentId)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Item may not be an artist, verification necessary.
|
2016-04-08 02:29:23 +10:00
|
|
|
if artist is not None and artist != 401:
|
2016-02-13 02:53:49 +11:00
|
|
|
if artist[0].attrib.get('type') == "artist":
|
|
|
|
# Update with the parentId, for remove reference
|
|
|
|
emby_db.addReference(
|
|
|
|
parentId, parentId, "MusicArtist", "artist")
|
|
|
|
emby_db.updateParentId(itemid, parentId)
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Update emby reference with the artistid
|
|
|
|
emby_db.updateParentId(itemid, artistid)
|
|
|
|
|
|
|
|
# Assign main artists to album
|
2016-02-13 02:53:49 +11:00
|
|
|
# Plex unfortunately only supports 1 artist :-(
|
|
|
|
artistId = parentId
|
|
|
|
emby_dbartist = emby_db.getItem_byId(artistId)
|
|
|
|
try:
|
|
|
|
artistid = emby_dbartist[0]
|
|
|
|
except TypeError:
|
|
|
|
# Artist does not exist in emby database, create the reference
|
|
|
|
self.logMsg('Artist %s does not exist in emby database'
|
|
|
|
% artistId, 1)
|
|
|
|
artist = GetPlexMetadata(artistId)
|
2016-04-08 02:29:23 +10:00
|
|
|
if artist is not None and artist != 401:
|
2016-02-13 02:53:49 +11:00
|
|
|
self.add_updateArtist(artist[0], artisttype="AlbumArtist")
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_dbartist = emby_db.getItem_byId(artistId)
|
|
|
|
artistid = emby_dbartist[0]
|
2016-02-13 02:53:49 +11:00
|
|
|
else:
|
|
|
|
# Best take this name over anything else.
|
|
|
|
query = "UPDATE artist SET strArtist = ? WHERE idArtist = ?"
|
|
|
|
kodicursor.execute(query, (artistname, artistid,))
|
|
|
|
self.logMsg("UPDATE artist: strArtist: %s, idArtist: %s"
|
|
|
|
% (artistname, artistid), 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# Add artist to album
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (artistid, albumid, artistname))
|
|
|
|
# Update discography
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO discography(idArtist, strAlbum, strYear)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (artistid, name, year))
|
|
|
|
# Update emby reference with parentid
|
|
|
|
emby_db.updateParentId(artistId, albumid)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Add genres
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addMusicGenres(albumid, genres, "album")
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update artwork
|
|
|
|
artwork.addArtwork(artworks, albumid, "album", kodicursor)
|
2016-03-18 01:06:04 +11:00
|
|
|
self.embyconn.commit()
|
|
|
|
self.kodiconn.commit()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
def add_updateSong(self, item, viewtag=None, viewid=None):
|
2016-03-10 04:36:36 +11:00
|
|
|
try:
|
|
|
|
self.run_add_updateSong(item, viewtag, viewid)
|
|
|
|
except Exception as e:
|
|
|
|
self.logMsg('itemtypes.py for music song has crashed for '
|
|
|
|
'item %s. Error:'
|
|
|
|
% item.attrib.get('ratingKey', None), -1)
|
|
|
|
self.logMsg(e, -1)
|
2016-03-10 19:58:26 +11:00
|
|
|
import traceback
|
|
|
|
self.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
|
2016-03-10 19:30:20 +11:00
|
|
|
utils.window('plex_scancrashed', value='true')
|
2016-03-10 04:36:36 +11:00
|
|
|
# skip this item for now
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_add_updateSong(self, item, viewtag=None, viewid=None):
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process single song
|
2016-01-09 22:13:17 +11:00
|
|
|
kodicursor = self.kodicursor
|
2016-03-16 22:13:47 +11:00
|
|
|
emby = self.emby
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_db = self.emby_db
|
|
|
|
artwork = self.artwork
|
2016-02-13 02:53:49 +11:00
|
|
|
API = PlexAPI.API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
update_item = True
|
2016-02-13 02:53:49 +11:00
|
|
|
itemid = API.getRatingKey()
|
|
|
|
if not itemid:
|
|
|
|
self.logMsg('Error processing Song; skipping', -1)
|
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
songid = emby_dbitem[0]
|
|
|
|
pathid = emby_dbitem[2]
|
|
|
|
albumid = emby_dbitem[3]
|
|
|
|
except TypeError:
|
2016-02-13 02:53:49 +11:00
|
|
|
# Songid not found
|
2015-12-25 06:51:47 +11:00
|
|
|
update_item = False
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute("select coalesce(max(idSong),0) from song")
|
|
|
|
songid = kodicursor.fetchone()[0] + 1
|
|
|
|
|
|
|
|
# The song details #####
|
2015-12-25 06:51:47 +11:00
|
|
|
checksum = API.getChecksum()
|
|
|
|
dateadded = API.getDateCreated()
|
|
|
|
userdata = API.getUserData()
|
|
|
|
playcount = userdata['PlayCount']
|
2016-04-08 22:48:47 +10:00
|
|
|
if playcount is None:
|
|
|
|
# This is different to Video DB!
|
|
|
|
playcount = 0
|
2015-12-25 06:51:47 +11:00
|
|
|
dateplayed = userdata['LastPlayedDate']
|
|
|
|
|
|
|
|
# item details
|
2016-02-13 02:53:49 +11:00
|
|
|
title, sorttitle = API.getTitle()
|
|
|
|
# musicBrainzId = API.getProvider('MusicBrainzTrackId')
|
|
|
|
musicBrainzId = None
|
|
|
|
genres = API.getGenres()
|
|
|
|
genre = API.joinList(genres)
|
|
|
|
artists = item.attrib.get('grandparentTitle')
|
|
|
|
tracknumber = int(item.attrib.get('index', 0))
|
|
|
|
disc = int(item.attrib.get('parentIndex', 1))
|
2015-12-25 06:51:47 +11:00
|
|
|
if disc == 1:
|
|
|
|
track = tracknumber
|
|
|
|
else:
|
|
|
|
track = disc*2**16 + tracknumber
|
2016-02-13 02:53:49 +11:00
|
|
|
year = API.getYear()
|
|
|
|
resume, duration = API.getRuntime()
|
2016-04-08 22:48:47 +10:00
|
|
|
rating = int(userdata['UserRating'])
|
2016-01-22 09:45:51 +11:00
|
|
|
|
2016-03-16 06:26:45 +11:00
|
|
|
#if enabled, try to get the rating from file and/or emby
|
|
|
|
# if not self.directstream:
|
|
|
|
# rating, comment, hasEmbeddedCover = musicutils.getAdditionalSongTags(itemid, rating, API, kodicursor, emby_db, self.enableimportsongrating, self.enableexportsongrating, self.enableupdatesongrating)
|
|
|
|
# else:
|
|
|
|
# hasEmbeddedCover = False
|
|
|
|
# comment = API.getOverview()
|
|
|
|
|
|
|
|
hasEmbeddedCover = False
|
2016-02-13 02:53:49 +11:00
|
|
|
comment = None
|
|
|
|
|
2016-03-16 06:26:45 +11:00
|
|
|
# GET THE FILE AND PATH #####
|
|
|
|
doIndirect = not self.directpath
|
|
|
|
if self.directpath:
|
|
|
|
# Direct paths is set the Kodi way
|
|
|
|
playurl = API.getFilePath()
|
|
|
|
if playurl is None:
|
|
|
|
# Something went wrong, trying to use non-direct paths
|
|
|
|
doIndirect = True
|
|
|
|
else:
|
2016-04-13 01:18:32 +10:00
|
|
|
playurl = API.validatePlayurl(playurl, API.getType())
|
|
|
|
if playurl is None:
|
2016-03-16 22:26:31 +11:00
|
|
|
return False
|
2016-03-16 06:26:45 +11:00
|
|
|
if "\\" in playurl:
|
|
|
|
# Local path
|
|
|
|
filename = playurl.rsplit("\\", 1)[1]
|
|
|
|
else:
|
|
|
|
# Network share
|
|
|
|
filename = playurl.rsplit("/", 1)[1]
|
|
|
|
path = playurl.replace(filename, "")
|
|
|
|
if doIndirect:
|
|
|
|
# Plex works a bit differently
|
|
|
|
path = "%s%s" % (self.server, item[0][0].attrib.get('key'))
|
2016-03-22 23:25:30 +11:00
|
|
|
path = API.addPlexCredentialsToUrl(path)
|
|
|
|
filename = path.rsplit('/', 1)[1]
|
|
|
|
path = path.replace(filename, '')
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE SONG #####
|
2015-12-25 06:51:47 +11:00
|
|
|
if update_item:
|
2016-03-18 01:06:04 +11:00
|
|
|
self.logMsg("UPDATE song itemid: %s - Title: %s with path: %s"
|
|
|
|
% (itemid, title, path), 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update path
|
2016-03-22 23:25:30 +11:00
|
|
|
# Use dummy strHash '123' for Kodi
|
|
|
|
query = "UPDATE path SET strPath = ?, strHash = ? WHERE idPath = ?"
|
|
|
|
kodicursor.execute(query, (path, '123', pathid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Update the song entry
|
|
|
|
query = ' '.join((
|
|
|
|
"UPDATE song",
|
|
|
|
"SET idAlbum = ?, strArtists = ?, strGenres = ?, strTitle = ?, iTrack = ?,",
|
|
|
|
"iDuration = ?, iYear = ?, strFilename = ?, iTimesPlayed = ?, lastplayed = ?,",
|
2016-01-12 02:53:41 +11:00
|
|
|
"rating = ?, comment = ?",
|
2015-12-25 06:51:47 +11:00
|
|
|
"WHERE idSong = ?"
|
|
|
|
))
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute(query, (albumid, artists, genre, title, track,
|
|
|
|
duration, year, filename, playcount,
|
|
|
|
dateplayed, rating, comment, songid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Update the checksum in emby table
|
|
|
|
emby_db.updateReference(itemid, checksum)
|
2016-02-13 02:53:49 +11:00
|
|
|
|
|
|
|
# OR ADD THE SONG #####
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
self.logMsg("ADD song itemid: %s - Title: %s" % (itemid, title), 1)
|
2016-02-13 02:53:49 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Add path
|
2016-04-26 21:53:19 +10:00
|
|
|
pathid = self.kodi_db.addPath(path, strHash="123")
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
try:
|
2016-01-01 15:19:36 +11:00
|
|
|
# Get the album
|
2016-02-13 02:53:49 +11:00
|
|
|
emby_dbalbum = emby_db.getItem_byId(
|
|
|
|
item.attrib.get('parentRatingKey'))
|
2015-12-25 06:51:47 +11:00
|
|
|
albumid = emby_dbalbum[0]
|
2016-01-01 15:19:36 +11:00
|
|
|
except KeyError:
|
2016-03-16 22:13:47 +11:00
|
|
|
# Verify if there's an album associated.
|
2016-03-17 21:17:30 +11:00
|
|
|
album_name = item.get('parentTitle')
|
2016-03-16 22:13:47 +11:00
|
|
|
if album_name:
|
2016-03-17 14:19:52 +11:00
|
|
|
self.logMsg("Creating virtual music album for song: %s." % itemid, 1)
|
2016-04-26 21:53:19 +10:00
|
|
|
albumid = self.kodi_db.addAlbum(album_name, API.getProvider('MusicBrainzAlbum'))
|
2016-03-16 22:13:47 +11:00
|
|
|
emby_db.addReference("%salbum%s" % (itemid, albumid), albumid, "MusicAlbum_", "album")
|
|
|
|
else:
|
|
|
|
# No album Id associated to the song.
|
|
|
|
self.logMsg("Song itemid: %s has no albumId associated." % itemid, 1)
|
|
|
|
return False
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2016-01-30 13:40:23 +11:00
|
|
|
# No album found. Let's create it
|
2016-01-31 13:49:10 +11:00
|
|
|
self.logMsg("Album database entry missing.", 1)
|
2016-02-13 02:53:49 +11:00
|
|
|
emby_albumId = item.attrib.get('parentRatingKey')
|
2016-03-28 04:06:36 +11:00
|
|
|
album = GetPlexMetadata(emby_albumId)
|
2016-04-08 02:29:23 +10:00
|
|
|
if album is None or album == 401:
|
|
|
|
self.logMsg('Could not download album, abort', -1)
|
|
|
|
return
|
2016-03-28 04:06:36 +11:00
|
|
|
self.add_updateAlbum(album[0])
|
2016-01-30 13:40:23 +11:00
|
|
|
emby_dbalbum = emby_db.getItem_byId(emby_albumId)
|
|
|
|
try:
|
|
|
|
albumid = emby_dbalbum[0]
|
2016-01-31 13:49:10 +11:00
|
|
|
self.logMsg("Found albumid: %s" % albumid, 1)
|
2016-01-30 13:40:23 +11:00
|
|
|
except TypeError:
|
|
|
|
# No album found, create a single's album
|
2016-01-31 13:49:10 +11:00
|
|
|
self.logMsg("Failed to add album. Creating singles.", 1)
|
2016-01-30 13:40:23 +11:00
|
|
|
kodicursor.execute("select coalesce(max(idAlbum),0) from album")
|
|
|
|
albumid = kodicursor.fetchone()[0] + 1
|
2016-04-26 21:53:19 +10:00
|
|
|
if self.kodiversion == 16:
|
2016-01-30 13:40:23 +11:00
|
|
|
# Kodi Jarvis
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO album(idAlbum, strGenres, iYear, strReleaseType)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (albumid, genre, year, "single"))
|
2016-04-26 21:53:19 +10:00
|
|
|
elif self.kodiversion == 15:
|
2016-01-30 13:40:23 +11:00
|
|
|
# Kodi Isengard
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO album(idAlbum, strGenres, iYear, dateAdded, strReleaseType)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (albumid, genre, year, dateadded, "single"))
|
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO album(idAlbum, strGenres, iYear, dateAdded)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (albumid, genre, year, dateadded))
|
2016-02-13 02:53:49 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Create the song entry
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO song(
|
|
|
|
idSong, idAlbum, idPath, strArtists, strGenres, strTitle, iTrack,
|
|
|
|
iDuration, iYear, strFileName, strMusicBrainzTrackID, iTimesPlayed, lastplayed,
|
2016-04-08 22:48:47 +10:00
|
|
|
rating, iStartOffset, iEndOffset)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-04-08 22:48:47 +10:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2015-12-25 06:51:47 +11:00
|
|
|
'''
|
|
|
|
)
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute(
|
|
|
|
query, (songid, albumid, pathid, artists, genre, title, track,
|
|
|
|
duration, year, filename, musicBrainzId, playcount,
|
2016-04-08 22:48:47 +10:00
|
|
|
dateplayed, rating, 0, 0))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Create the reference in emby table
|
2016-02-13 02:53:49 +11:00
|
|
|
emby_db.addReference(
|
2016-02-20 02:02:58 +11:00
|
|
|
itemid, songid, "Audio", "song",
|
|
|
|
pathid=pathid,
|
2016-02-13 02:53:49 +11:00
|
|
|
parentid=albumid,
|
2015-12-25 06:51:47 +11:00
|
|
|
checksum=checksum)
|
|
|
|
|
|
|
|
# Link song to album
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO albuminfosong(
|
|
|
|
idAlbumInfoSong, idAlbumInfo, iTrack, strTitle, iDuration)
|
2016-02-13 02:53:49 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (songid, albumid, track, title, duration))
|
2016-03-17 21:17:30 +11:00
|
|
|
|
2016-03-16 22:13:47 +11:00
|
|
|
# Link song to artists
|
2016-03-17 21:17:30 +11:00
|
|
|
artistLoop = [{
|
|
|
|
'Name': item.attrib.get('grandparentTitle'),
|
|
|
|
'Id': item.attrib.get('grandparentRatingKey')
|
|
|
|
}]
|
|
|
|
# for index, artist in enumerate(item['ArtistItems']):
|
|
|
|
for index, artist in enumerate(artistLoop):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-16 22:13:47 +11:00
|
|
|
artist_name = artist['Name']
|
|
|
|
artist_eid = artist['Id']
|
|
|
|
artist_edb = emby_db.getItem_byId(artist_eid)
|
|
|
|
try:
|
|
|
|
artistid = artist_edb[0]
|
|
|
|
except TypeError:
|
|
|
|
# Artist is missing from emby database, add it.
|
|
|
|
artist_full = emby.getItem(artist_eid)
|
2016-04-08 02:29:23 +10:00
|
|
|
artistXml = GetPlexMetadata(artist_eid)
|
|
|
|
if artistXml is None or artistXml == 401:
|
|
|
|
self.logMsg('Error getting artist, abort', -1)
|
|
|
|
return
|
|
|
|
self.add_updateArtist(artistXml[0])
|
2016-03-16 22:13:47 +11:00
|
|
|
artist_edb = emby_db.getItem_byId(artist_eid)
|
|
|
|
artistid = artist_edb[0]
|
|
|
|
finally:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO song_artist(idArtist, idSong, iOrder, strArtist)
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
kodicursor.execute(query, (artistid, songid, index, artist_name))
|
2016-02-13 02:53:49 +11:00
|
|
|
|
2016-03-16 22:13:47 +11:00
|
|
|
# Verify if album artist exists
|
|
|
|
album_artists = []
|
2016-03-17 21:17:30 +11:00
|
|
|
# for artist in item['AlbumArtists']:
|
|
|
|
if False:
|
2015-12-25 06:51:47 +11:00
|
|
|
artist_name = artist['Name']
|
2016-03-16 22:13:47 +11:00
|
|
|
album_artists.append(artist_name)
|
|
|
|
artist_eid = artist['Id']
|
|
|
|
artist_edb = emby_db.getItem_byId(artist_eid)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-03-16 22:13:47 +11:00
|
|
|
artistid = artist_edb[0]
|
|
|
|
except TypeError:
|
|
|
|
# Artist is missing from emby database, add it.
|
|
|
|
artist_full = emby.getItem(artist_eid)
|
|
|
|
self.add_updateArtist(artist_full)
|
|
|
|
artist_edb = emby_db.getItem_byId(artist_eid)
|
|
|
|
artistid = artist_edb[0]
|
|
|
|
finally:
|
2015-12-25 06:51:47 +11:00
|
|
|
query = (
|
|
|
|
'''
|
2016-02-13 02:53:49 +11:00
|
|
|
INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist)
|
2015-12-25 06:51:47 +11:00
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-02-13 02:53:49 +11:00
|
|
|
kodicursor.execute(query, (artistid, albumid, artist_name))
|
2016-03-16 22:13:47 +11:00
|
|
|
# Update discography
|
|
|
|
if item.get('Album'):
|
2015-12-25 06:51:47 +11:00
|
|
|
query = (
|
|
|
|
'''
|
2016-03-16 22:13:47 +11:00
|
|
|
INSERT OR REPLACE INTO discography(idArtist, strAlbum, strYear)
|
2015-12-25 06:51:47 +11:00
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-03-16 22:13:47 +11:00
|
|
|
kodicursor.execute(query, (artistid, item['Album'], 0))
|
2016-03-17 21:17:30 +11:00
|
|
|
# else:
|
|
|
|
if False:
|
2016-03-16 22:13:47 +11:00
|
|
|
album_artists = " / ".join(album_artists)
|
|
|
|
query = ' '.join((
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-16 22:13:47 +11:00
|
|
|
"SELECT strArtists",
|
|
|
|
"FROM album",
|
|
|
|
"WHERE idAlbum = ?"
|
|
|
|
))
|
|
|
|
kodicursor.execute(query, (albumid,))
|
|
|
|
result = kodicursor.fetchone()
|
|
|
|
if result and result[0] != album_artists:
|
|
|
|
# Field is empty
|
2016-04-26 21:53:19 +10:00
|
|
|
if self.kodiversion in (16, 17):
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?"
|
2016-03-16 22:13:47 +11:00
|
|
|
kodicursor.execute(query, (album_artists, albumid))
|
2016-04-26 21:53:19 +10:00
|
|
|
elif self.kodiversion == 15:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Kodi Isengard
|
|
|
|
query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?"
|
2016-03-16 22:13:47 +11:00
|
|
|
kodicursor.execute(query, (album_artists, albumid))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?"
|
2016-03-16 22:13:47 +11:00
|
|
|
kodicursor.execute(query, (album_artists, albumid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Add genres
|
2016-04-26 21:53:19 +10:00
|
|
|
self.kodi_db.addMusicGenres(songid, genres, "song")
|
2016-03-16 22:13:47 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update artwork
|
2016-02-13 02:53:49 +11:00
|
|
|
allart = API.getAllArtwork(parentInfo=True)
|
2016-03-17 21:17:30 +11:00
|
|
|
if hasEmbeddedCover:
|
|
|
|
allart["Primary"] = "image://music@" + artwork.single_urlencode( playurl )
|
2016-01-19 05:38:53 +11:00
|
|
|
artwork.addArtwork(allart, songid, "song", kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-17 21:17:30 +11:00
|
|
|
# if item.get('AlbumId') is None:
|
|
|
|
if item.get('parentKey') is None:
|
2016-03-16 22:13:47 +11:00
|
|
|
# Update album artwork
|
|
|
|
artwork.addArtwork(allart, albumid, "album", kodicursor)
|
2016-03-18 01:06:04 +11:00
|
|
|
self.embyconn.commit()
|
|
|
|
self.kodiconn.commit()
|
2016-03-16 22:13:47 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def remove(self, itemid):
|
|
|
|
# Remove kodiid, fileid, pathid, emby reference
|
|
|
|
emby_db = self.emby_db
|
2016-01-09 22:13:17 +11:00
|
|
|
kodicursor = self.kodicursor
|
2015-12-25 06:51:47 +11:00
|
|
|
artwork = self.artwork
|
|
|
|
|
|
|
|
emby_dbitem = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
kodiid = emby_dbitem[0]
|
|
|
|
mediatype = emby_dbitem[4]
|
|
|
|
self.logMsg("Removing %s kodiid: %s" % (mediatype, kodiid), 1)
|
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
|
|
|
|
##### PROCESS ITEM #####
|
|
|
|
|
|
|
|
# Remove the emby reference
|
|
|
|
emby_db.removeItem(itemid)
|
|
|
|
|
|
|
|
|
|
|
|
##### IF SONG #####
|
|
|
|
|
|
|
|
if mediatype == "song":
|
|
|
|
# Delete song
|
|
|
|
self.removeSong(kodiid)
|
2016-03-16 22:13:47 +11:00
|
|
|
# This should only address single song scenario, where server doesn't actually
|
|
|
|
# create an album for the song.
|
|
|
|
emby_db.removeWildItem(itemid)
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
for item in emby_db.getItem_byWildId(itemid):
|
2016-03-16 22:13:47 +11:00
|
|
|
|
|
|
|
item_kid = item[0]
|
|
|
|
item_mediatype = item[1]
|
|
|
|
|
|
|
|
if item_mediatype == "album":
|
|
|
|
childs = emby_db.getItem_byParentId(item_kid, "song")
|
|
|
|
if not childs:
|
|
|
|
# Delete album
|
|
|
|
self.removeAlbum(item_kid)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
##### IF ALBUM #####
|
|
|
|
|
|
|
|
elif mediatype == "album":
|
|
|
|
# Delete songs, album
|
|
|
|
album_songs = emby_db.getItem_byParentId(kodiid, "song")
|
|
|
|
for song in album_songs:
|
|
|
|
self.removeSong(song[1])
|
|
|
|
else:
|
|
|
|
# Remove emby songs
|
|
|
|
emby_db.removeItems_byParentId(kodiid, "song")
|
|
|
|
|
|
|
|
# Remove the album
|
|
|
|
self.removeAlbum(kodiid)
|
|
|
|
|
|
|
|
##### IF ARTIST #####
|
|
|
|
|
|
|
|
elif mediatype == "artist":
|
|
|
|
# Delete songs, album, artist
|
|
|
|
albums = emby_db.getItem_byParentId(kodiid, "album")
|
|
|
|
for album in albums:
|
|
|
|
albumid = album[1]
|
|
|
|
album_songs = emby_db.getItem_byParentId(albumid, "song")
|
|
|
|
for song in album_songs:
|
|
|
|
self.removeSong(song[1])
|
|
|
|
else:
|
|
|
|
# Remove emby song
|
|
|
|
emby_db.removeItems_byParentId(albumid, "song")
|
|
|
|
# Remove emby artist
|
|
|
|
emby_db.removeItems_byParentId(albumid, "artist")
|
|
|
|
# Remove kodi album
|
|
|
|
self.removeAlbum(albumid)
|
|
|
|
else:
|
|
|
|
# Remove emby albums
|
|
|
|
emby_db.removeItems_byParentId(kodiid, "album")
|
|
|
|
|
|
|
|
# Remove artist
|
|
|
|
self.removeArtist(kodiid)
|
|
|
|
|
|
|
|
self.logMsg("Deleted %s: %s from kodi database" % (mediatype, itemid), 1)
|
2016-03-18 01:06:04 +11:00
|
|
|
self.embyconn.commit()
|
|
|
|
self.kodiconn.commit()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def removeSong(self, kodiid):
|
|
|
|
|
2016-01-09 22:13:17 +11:00
|
|
|
kodicursor = self.kodicursor
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.artwork.deleteArtwork(kodiid, "song", self.kodicursor)
|
|
|
|
self.kodicursor.execute("DELETE FROM song WHERE idSong = ?", (kodiid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def removeAlbum(self, kodiid):
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.artwork.deleteArtwork(kodiid, "album", self.kodicursor)
|
|
|
|
self.kodicursor.execute("DELETE FROM album WHERE idAlbum = ?", (kodiid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def removeArtist(self, kodiid):
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.artwork.deleteArtwork(kodiid, "artist", self.kodicursor)
|
|
|
|
self.kodicursor.execute("DELETE FROM artist WHERE idArtist = ?", (kodiid,))
|