PlexKodiConnect/resources/lib/itemtypes.py

1861 lines
79 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2016-02-20 06:03:06 +11:00
###############################################################################
2017-12-10 00:35:08 +11:00
from logging import getLogger
from ntpath import dirname
from datetime import datetime
2018-06-22 03:24:37 +10:00
from . import artwork
from . import utils
from . import plexdb_functions as plexdb
from . import kodidb_functions as kodidb
from .plex_api import API
from . import plex_functions as PF
from . import variables as v
from . import state
2016-02-20 06:03:06 +11:00
###############################################################################
2018-06-22 03:24:37 +10:00
LOG = getLogger('PLEX.itemtypes')
2016-08-31 00:40:11 +10:00
# Note: always use same order of URL arguments, NOT urlencode:
# plex_id=<plex_id>&plex_type=<plex_type>&mode=play
2016-08-31 00:40:11 +10:00
###############################################################################
class Items(object):
2016-01-11 01:16:59 +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)
Input:
kodiType: optional argument; e.g. 'video' or 'music'
2016-01-11 01:16:59 +11:00
"""
def __init__(self):
2018-06-22 03:24:37 +10:00
self.artwork = artwork.Artwork()
self.server = utils.window('pms_server')
2018-02-11 22:06:04 +11:00
self.plexconn = None
self.plexcursor = None
self.kodiconn = None
self.kodicursor = None
self.plex_db = None
self.kodi_db = None
def __enter__(self):
2016-01-11 01:16:59 +11:00
"""
Open DB connections and cursors
"""
2018-06-22 03:24:37 +10:00
self.plexconn = utils.kodi_sql('plex')
self.plexcursor = self.plexconn.cursor()
2018-06-22 03:24:37 +10:00
self.kodiconn = utils.kodi_sql('video')
self.kodicursor = self.kodiconn.cursor()
self.plex_db = plexdb.Plex_DB_Functions(self.plexcursor)
2018-02-25 23:42:20 +11:00
self.kodi_db = kodidb.KodiDBMethods(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.plexconn.commit()
self.kodiconn.commit()
self.plexconn.close()
self.kodiconn.close()
return self
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
2017-02-02 22:27:21 +11:00
def getfanart(self, plex_id, refresh=False):
"""
2017-02-02 22:27:21 +11:00
Tries to get additional fanart for movies (+sets) and TV shows.
Returns True if successful, False otherwise
"""
2017-02-02 22:27:21 +11:00
with plexdb.Get_Plex_DB() as plex_db:
db_item = plex_db.getItem_byId(plex_id)
try:
kodi_id = db_item[0]
kodi_type = db_item[4]
except TypeError:
2018-02-11 22:06:04 +11:00
LOG.error('Could not get Kodi id for plex id %s, abort getfanart',
plex_id)
2017-02-02 22:27:21 +11:00
return False
if refresh is True:
# Leave the Plex art untouched
allartworks = None
else:
with kodidb.GetKodiDB('video') as kodi_db:
2018-03-04 23:39:18 +11:00
allartworks = kodi_db.get_art(kodi_id, kodi_type)
2017-02-02 22:27:21 +11:00
# Check if we even need to get additional art
needsupdate = False
2018-03-04 23:39:18 +11:00
for key in v.ALL_KODI_ARTWORK:
if key not in allartworks:
2017-02-02 22:27:21 +11:00
needsupdate = True
break
if needsupdate is False:
2018-02-11 22:06:04 +11:00
LOG.debug('Already got all fanart for Plex id %s', plex_id)
2017-02-02 22:27:21 +11:00
return True
2018-06-22 03:24:37 +10:00
xml = PF.GetPlexMetadata(plex_id)
2017-02-02 22:27:21 +11:00
if xml is None:
# Did not receive a valid XML - skip that item for now
2018-02-11 22:06:04 +11:00
LOG.error("Could not get metadata for %s. Skipping that item "
"for now", plex_id)
2017-02-02 22:27:21 +11:00
return False
elif xml == 401:
2018-02-11 22:06:04 +11:00
LOG.error('HTTP 401 returned by PMS. Too much strain? '
2017-02-02 22:27:21 +11:00
'Cancelling sync for now')
# Kill remaining items in queue (for main thread to cont.)
return False
2018-02-11 22:06:04 +11:00
api = API(xml[0])
if allartworks is None:
2018-02-12 00:42:49 +11:00
allartworks = api.artwork()
2018-03-04 23:39:18 +11:00
self.artwork.modify_artwork(api.fanart_artwork(allartworks),
kodi_id,
kodi_type,
self.kodicursor)
# Also get artwork for collections/movie sets
2017-02-02 22:27:21 +11:00
if kodi_type == v.KODI_TYPE_MOVIE:
2018-02-12 00:42:49 +11:00
for setname in api.collection_list():
2018-02-11 22:06:04 +11:00
LOG.debug('Getting artwork for movie set %s', setname)
2018-06-15 23:15:35 +10:00
setid = self.kodi_db.create_collection(setname)
2018-03-04 23:39:18 +11:00
self.artwork.modify_artwork(api.set_artwork(),
setid,
v.KODI_TYPE_SET,
self.kodicursor)
2018-06-15 23:15:35 +10:00
self.kodi_db.assign_collection(setid, kodi_id)
2017-02-02 22:27:21 +11:00
return True
2018-02-11 22:06:04 +11:00
def updateUserdata(self, xml):
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:
2018-02-11 22:06:04 +11:00
api = API(mediaitem)
2016-02-04 00:44:11 +11:00
# Get key and db entry on the Kodi db side
2018-02-12 00:42:49 +11:00
db_item = self.plex_db.getItem_byId(api.plex_id())
2016-02-11 22:44:12 +11:00
try:
fileid = db_item[1]
except TypeError:
2016-02-11 22:44:12 +11:00
continue
2016-02-04 00:44:11 +11:00
# Grab the user's viewcount, resume points etc. from PMS' answer
2018-02-12 00:42:49 +11:00
userdata = api.userdata()
2016-02-04 00:44:11 +11:00
# Write to Kodi DB
2018-06-15 23:15:35 +10:00
self.kodi_db.set_resume(fileid,
userdata['Resume'],
userdata['Runtime'],
userdata['PlayCount'],
userdata['LastPlayedDate'],
api.plex_type())
if v.KODIVERSION >= 17:
self.kodi_db.update_userrating(db_item[0],
db_item[4],
userdata['UserRating'])
2016-02-04 00:44:11 +11:00
def updatePlaystate(self, mark_played, view_count, resume, duration,
file_id, lastViewedAt, plex_type):
2016-03-28 01:57:35 +11:00
"""
Use with websockets, not xml
2016-03-28 01:57:35 +11:00
"""
# If the playback was stopped, check whether we need to increment the
# playcount. PMS won't tell us the playcount via websockets
LOG.debug('Playstate file_id %s: viewcount: %s, resume: %s, type: %s',
file_id, view_count, resume, plex_type)
if mark_played:
2018-02-11 22:06:04 +11:00
LOG.info('Marking as completely watched in Kodi')
try:
view_count += 1
except TypeError:
view_count = 1
resume = 0
2016-03-28 01:57:35 +11:00
# Do the actual update
2018-06-15 23:15:35 +10:00
self.kodi_db.set_resume(file_id,
resume,
duration,
view_count,
lastViewedAt,
plex_type)
2016-03-25 04:52:02 +11:00
class Movies(Items):
2018-02-11 22:06:04 +11:00
"""
Used for plex library-type movies
"""
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
def add_update(self, item, viewtag=None, viewid=None):
2018-02-11 22:06:04 +11:00
"""
Process single movie
"""
kodicursor = self.kodicursor
plex_db = self.plex_db
artwork = self.artwork
2018-02-11 22:06:04 +11:00
api = API(item)
# 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
2018-02-12 00:42:49 +11:00
itemid = api.plex_id()
# Cannot parse XML, abort
if not itemid:
2018-02-11 22:06:04 +11:00
LOG.error("Cannot parse XML data for movie")
return
plex_dbitem = plex_db.getItem_byId(itemid)
try:
movieid = plex_dbitem[0]
2018-03-11 03:09:21 +11:00
old_fileid = plex_dbitem[1]
pathid = plex_dbitem[2]
2016-02-05 06:23:04 +11:00
except TypeError:
# movieid
update_item = False
kodicursor.execute("select coalesce(max(idMovie),0) from movie")
movieid = kodicursor.fetchone()[0] + 1
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
2018-02-11 22:06:04 +11:00
LOG.info("movieid: %s missing from Kodi, repairing the entry.",
movieid)
# fileId information
2018-02-12 00:42:49 +11:00
checksum = api.checksum()
dateadded = api.date_created()
userdata = api.userdata()
playcount = userdata['PlayCount']
dateplayed = userdata['LastPlayedDate']
resume = userdata['Resume']
runtime = userdata['Runtime']
# item details
2018-02-12 00:42:49 +11:00
people = api.people()
writer = api.list_to_string(people['Writer'])
director = api.list_to_string(people['Director'])
genres = api.genre_list()
genre = api.list_to_string(genres)
title, sorttitle = api.titles()
plot = api.plot()
2015-12-29 04:47:16 +11:00
shortplot = None
2018-02-12 00:42:49 +11:00
tagline = api.tagline()
2015-12-30 23:25:37 +11:00
votecount = None
2018-02-12 00:42:49 +11:00
collections = api.collection_list()
2016-01-11 19:57:45 +11:00
2016-04-02 19:31:21 +11:00
rating = userdata['Rating']
2018-02-12 00:42:49 +11:00
year = api.year()
premieredate = api.premiere_date()
imdb = api.provider('imdb')
mpaa = api.content_rating()
countries = api.country_list()
country = api.list_to_string(countries)
studios = api.music_studio_list()
try:
studio = studios[0]
except IndexError:
studio = None
2018-02-12 18:10:39 +11:00
trailer = api.trailer_id()
if trailer:
trailer = ('plugin://%s.movies/?plex_id=%s&plex_type=%s&mode=play'
2018-02-12 18:10:39 +11:00
% (v.ADDON_ID, trailer, v.PLEX_TYPE_CLIP))
# GET THE FILE AND PATH #####
2018-02-11 22:06:04 +11:00
do_indirect = not state.DIRECT_PATHS
2017-05-17 23:42:12 +10:00
if state.DIRECT_PATHS:
# Direct paths is set the Kodi way
2018-02-12 00:42:49 +11:00
playurl = api.file_path(force_first_media=True)
if playurl is None:
# Something went wrong, trying to use non-direct paths
2018-02-11 22:06:04 +11:00
do_indirect = True
else:
2018-02-12 00:42:49 +11:00
playurl = api.validate_playurl(playurl, api.plex_type())
if playurl is None:
return False
if "\\" in playurl:
# Local path
filename = playurl.rsplit("\\", 1)[1]
else:
# Network share
filename = playurl.rsplit("/", 1)[1]
path = playurl.replace(filename, "")
2018-03-11 03:09:21 +11:00
pathid = self.kodi_db.add_video_path(path,
content='movies',
scraper='metadata.local')
2018-02-11 22:06:04 +11:00
if do_indirect:
# Set plugin path and media flags using real filename
filename = api.file_name(force_first_media=True)
path = 'plugin://%s.movies/' % v.ADDON_ID
filename = ('%s?plex_id=%s&plex_type=%s&mode=play&filename=%s'
% (path, itemid, v.PLEX_TYPE_MOVIE, filename))
playurl = filename
2018-03-11 03:09:21 +11:00
pathid = self.kodi_db.get_path(path)
# movie table:
# c22 - playurl
# c23 - pathid
# This information is used later by file browser.
# add/retrieve pathid and fileid
# if the path or file already exists, the calls return current value
2018-03-11 03:09:21 +11:00
fileid = self.kodi_db.add_file(filename, pathid, dateadded)
# UPDATE THE MOVIE #####
if update_item:
2018-02-11 22:06:04 +11:00
LOG.info("UPDATE movie itemid: %s - Title: %s", itemid, title)
2018-03-11 03:09:21 +11:00
if fileid != old_fileid:
LOG.debug('Removing old file entry: %s', old_fileid)
2018-03-11 03:09:21 +11:00
self.kodi_db.remove_file(old_fileid)
# Update the movie entry
2017-01-25 02:54:51 +11:00
if v.KODIVERSION >= 17:
# update new ratings Kodi 17
2017-02-20 01:57:31 +11:00
rating_id = self.kodi_db.get_ratingid(movieid,
v.KODI_TYPE_MOVIE)
self.kodi_db.update_ratings(movieid,
v.KODI_TYPE_MOVIE,
"default",
rating,
votecount,
2017-02-20 01:57:31 +11:00
rating_id)
# update new uniqueid Kodi 17
if imdb is not None:
uniqueid = self.kodi_db.get_uniqueid(movieid,
v.KODI_TYPE_MOVIE)
self.kodi_db.update_uniqueid(movieid,
v.KODI_TYPE_MOVIE,
imdb,
"imdb",
uniqueid)
else:
self.kodi_db.remove_uniqueid(movieid, v.KODI_TYPE_MOVIE)
uniqueid = -1
2017-02-03 01:23:54 +11:00
query = '''
UPDATE movie
SET c00 = ?, c01 = ?, c02 = ?, c03 = ?, c04 = ?, c05 = ?,
c06 = ?, c07 = ?, c09 = ?, c10 = ?, c11 = ?, c12 = ?,
c14 = ?, c15 = ?, c16 = ?, c18 = ?, c19 = ?, c21 = ?,
c22 = ?, c23 = ?, idFile=?, premiered = ?,
userrating = ?
WHERE idMovie = ?
'''
kodicursor.execute(query, (title, plot, shortplot, tagline,
2017-05-01 04:41:28 +10:00
votecount, rating_id, writer, year, uniqueid, sorttitle,
2017-02-20 01:57:31 +11:00
runtime, mpaa, genre, director, title, studio, trailer,
2017-09-23 23:56:26 +10:00
country, playurl, pathid, fileid, premieredate,
2017-02-20 01:57:31 +11:00
userdata['UserRating'], movieid))
else:
2017-02-03 01:23:54 +11:00
query = '''
UPDATE movie
SET c00 = ?, c01 = ?, c02 = ?, c03 = ?, c04 = ?, c05 = ?,
c06 = ?, c07 = ?, c09 = ?, c10 = ?, c11 = ?, c12 = ?,
c14 = ?, c15 = ?, c16 = ?, c18 = ?, c19 = ?, c21 = ?,
c22 = ?, c23 = ?, idFile=?
WHERE idMovie = ?
'''
kodicursor.execute(query, (title, plot, shortplot, tagline,
votecount, rating, writer, year, imdb, sorttitle, runtime,
mpaa, genre, director, title, studio, trailer, country,
playurl, pathid, fileid, movieid))
2017-02-03 01:23:54 +11:00
# OR ADD THE MOVIE #####
else:
2018-02-11 22:06:04 +11:00
LOG.info("ADD movie itemid: %s - Title: %s", itemid, title)
2017-01-25 02:54:51 +11:00
if v.KODIVERSION >= 17:
# add new ratings Kodi 17
rating_id = self.kodi_db.get_ratingid(movieid,
v.KODI_TYPE_MOVIE)
2017-02-20 01:57:31 +11:00
self.kodi_db.add_ratings(rating_id,
movieid,
v.KODI_TYPE_MOVIE,
"default",
rating,
votecount)
# add new uniqueid Kodi 17
if imdb is not None:
uniqueid = self.kodi_db.get_uniqueid(movieid,
v.KODI_TYPE_MOVIE)
self.kodi_db.add_uniqueid(uniqueid,
movieid,
v.KODI_TYPE_MOVIE,
imdb,
"imdb")
else:
uniqueid = -1
2017-02-03 01:23:54 +11:00
query = '''
INSERT INTO movie(idMovie, idFile, c00, c01, c02, c03,
c04, c05, c06, c07, c09, c10, c11, c12, c14, c15, c16,
2017-02-03 01:23:54 +11:00
c18, c19, c21, c22, c23, premiered, userrating)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
2017-02-03 01:23:54 +11:00
?, ?, ?, ?, ?, ?, ?)
'''
kodicursor.execute(query, (movieid, fileid, title, plot,
2017-02-20 01:57:31 +11:00
shortplot, tagline, votecount, rating_id, writer, year,
2017-05-01 04:41:28 +10:00
uniqueid, sorttitle, runtime, mpaa, genre, director,
2017-09-23 23:56:26 +10:00
title, studio, trailer, country, playurl, pathid, premieredate,
2017-02-03 01:23:54 +11:00
userdata['UserRating']))
else:
2017-02-03 01:23:54 +11:00
query = '''
INSERT INTO movie(idMovie, idFile, c00, c01, c02, c03,
c04, c05, c06, c07, c09, c10, c11, c12, c14, c15, c16,
c18, c19, c21, c22, c23)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
2017-02-03 01:23:54 +11:00
?, ?, ?, ?, ?)
'''
kodicursor.execute(query, (movieid, fileid, title, plot,
shortplot, tagline, votecount, rating, writer, year, imdb,
sorttitle, runtime, mpaa, genre, director, title, studio,
trailer, country, playurl, pathid))
# Create or update the reference in plex table Add reference is
2016-10-27 06:06:16 +11:00
# idempotent; the call here updates also fileid and pathid when item is
# moved or renamed
plex_db.addReference(itemid,
v.PLEX_TYPE_MOVIE,
movieid,
v.KODI_TYPE_MOVIE,
kodi_fileid=fileid,
kodi_pathid=pathid,
parent_id=None,
checksum=checksum,
view_id=viewid)
# Process countries
2018-02-28 07:14:42 +11:00
self.kodi_db.modify_countries(movieid, v.KODI_TYPE_MOVIE, countries)
# Process cast
self.kodi_db.modify_people(movieid,
v.KODI_TYPE_MOVIE,
api.people_list())
# Process genres
2018-02-28 07:14:42 +11:00
self.kodi_db.modify_genres(movieid, v.KODI_TYPE_MOVIE, genres)
# Process artwork
2018-03-04 23:39:18 +11:00
artwork.modify_artwork(api.artwork(),
movieid,
v.KODI_TYPE_MOVIE,
kodicursor)
# Process stream details
2018-02-26 19:18:44 +11:00
self.kodi_db.modify_streams(fileid, api.mediastreams(), runtime)
# Process studios
2018-02-28 07:14:42 +11:00
self.kodi_db.modify_studios(movieid, v.KODI_TYPE_MOVIE, studios)
2016-03-02 02:18:12 +11:00
# Process tags: view, Plex collection tags
2016-01-12 01:56:56 +11:00
tags = [viewtag]
tags.extend(collections)
2016-03-02 02:18:12 +11:00
if userdata['Favorite']:
tags.append("Favorite movies")
2018-02-28 07:14:42 +11:00
self.kodi_db.modify_tags(movieid, v.KODI_TYPE_MOVIE, tags)
2016-05-17 01:56:48 +10:00
# Add any sets from Plex collection tags
2018-06-15 23:15:35 +10:00
self.kodi_db.add_sets(movieid, collections)
# Process playstates
2018-06-15 23:15:35 +10:00
self.kodi_db.set_resume(fileid,
resume,
runtime,
playcount,
dateplayed,
v.PLEX_TYPE_MOVIE)
def remove(self, plex_id):
2018-02-11 22:06:04 +11:00
"""
Remove a movie with all references and all orphaned associated entries
from the Kodi DB
2018-02-11 22:06:04 +11:00
"""
plex_dbitem = self.plex_db.getItem_byId(plex_id)
try:
kodi_id = plex_dbitem[0]
file_id = plex_dbitem[1]
kodi_type = plex_dbitem[4]
LOG.debug('Removing %sid: %s file_id: %s',
kodi_type, kodi_id, file_id)
except TypeError:
LOG.error('Movie with plex_id %s not found in DB - cannot delete',
plex_id)
return
# Remove the plex reference
self.plex_db.removeItem(plex_id)
# Remove artwork
self.artwork.delete_artwork(kodi_id, kodi_type, self.kodicursor)
if kodi_type == v.KODI_TYPE_MOVIE:
2018-02-26 19:06:35 +11:00
set_id = self.kodi_db.get_set_id(kodi_id)
self.kodi_db.modify_countries(kodi_id, kodi_type)
self.kodi_db.modify_people(kodi_id, kodi_type)
self.kodi_db.modify_genres(kodi_id, kodi_type)
self.kodi_db.modify_studios(kodi_id, kodi_type)
self.kodi_db.modify_tags(kodi_id, kodi_type)
# Delete kodi movie and file
2018-03-11 22:00:28 +11:00
self.kodi_db.remove_file(file_id)
self.kodicursor.execute("DELETE FROM movie WHERE idMovie = ?",
(kodi_id,))
2018-02-26 19:06:35 +11:00
if set_id:
self.kodi_db.delete_possibly_empty_set(set_id)
if v.KODIVERSION >= 17:
2017-02-03 03:09:20 +11:00
self.kodi_db.remove_uniqueid(kodi_id, kodi_type)
self.kodi_db.remove_ratings(kodi_id, kodi_type)
elif kodi_type == v.KODI_TYPE_SET:
# Delete kodi boxset
boxset_movies = self.plex_db.getItem_byParentId(kodi_id,
v.KODI_TYPE_MOVIE)
for movie in boxset_movies:
2016-05-31 16:06:42 +10:00
plexid = movie[0]
movieid = movie[1]
2018-03-11 22:08:59 +11:00
self.kodi_db.remove_from_set(movieid)
# Update plex reference
self.plex_db.updateParentId(plexid, None)
self.kodicursor.execute("DELETE FROM sets WHERE idSet = ?",
(kodi_id,))
LOG.debug("Deleted %s %s from kodi database", kodi_type, plex_id)
2016-01-10 02:14:02 +11:00
class TVShows(Items):
2018-02-11 22:06:04 +11:00
"""
For Plex library-type TV shows
"""
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
def add_update(self, item, viewtag=None, viewid=None):
2018-02-11 22:06:04 +11:00
"""
Process a single show
"""
kodicursor = self.kodicursor
plex_db = self.plex_db
artwork = self.artwork
2018-02-11 22:06:04 +11:00
api = API(item)
update_item = True
2018-02-12 00:42:49 +11:00
itemid = api.plex_id()
2016-01-10 02:14:02 +11:00
if not itemid:
2018-02-11 22:06:04 +11:00
LOG.error("Cannot parse XML data for TV show")
2016-01-10 02:14:02 +11:00
return
update_item = True
plex_dbitem = plex_db.getItem_byId(itemid)
try:
showid = plex_dbitem[0]
pathid = plex_dbitem[2]
except TypeError:
update_item = False
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
2018-02-11 22:06:04 +11:00
LOG.info("showid: %s missing from Kodi, repairing the entry.",
showid)
# fileId information
2018-02-12 00:42:49 +11:00
checksum = api.checksum()
# item details
2018-02-12 00:42:49 +11:00
genres = api.genre_list()
title, sorttitle = api.titles()
plot = api.plot()
rating = api.audience_rating()
2017-02-19 02:30:06 +11:00
votecount = None
2018-02-12 00:42:49 +11:00
premieredate = api.premiere_date()
tvdb = api.provider('tvdb')
mpaa = api.content_rating()
genre = api.list_to_string(genres)
studios = api.music_studio_list()
collections = api.collection_list()
2016-01-10 02:14:02 +11:00
try:
studio = studios[0]
except IndexError:
studio = None
# GET THE FILE AND PATH #####
2017-05-17 23:42:12 +10:00
if state.DIRECT_PATHS:
# Direct paths is set the Kodi way
2018-03-11 00:51:00 +11:00
playurl = api.validate_playurl(api.tv_show_path(),
api.plex_type(),
folder=True)
if playurl is None:
2018-03-11 00:51:00 +11:00
return
if "\\" in playurl:
# Local path
path = "%s\\" % playurl
toplevelpath = "%s\\" % dirname(dirname(path))
else:
2018-03-11 00:51:00 +11:00
# Network path
path = "%s/" % playurl
toplevelpath = "%s/" % dirname(dirname(path))
toppathid = self.kodi_db.add_video_path(
toplevelpath,
content='tvshows',
scraper='metadata.local')
else:
# Set plugin path
toplevelpath = "plugin://%s.tvshows/" % v.ADDON_ID
path = "%s%s/" % (toplevelpath, itemid)
# Do NOT set a parent id because addon-path cannot be "stacked"
toppathid = None
pathid = self.kodi_db.add_video_path(path,
date_added=api.date_created(),
id_parent_path=toppathid)
# UPDATE THE TVSHOW #####
if update_item:
2018-02-11 22:06:04 +11:00
LOG.info("UPDATE tvshow itemid: %s - Title: %s", itemid, title)
2017-02-19 02:30:06 +11:00
# Add reference is idempotent; the call here updates also fileid
# and pathid when item is moved or renamed
plex_db.addReference(itemid,
v.PLEX_TYPE_SHOW,
showid,
v.KODI_TYPE_SHOW,
kodi_pathid=pathid,
checksum=checksum,
view_id=viewid)
2017-01-25 02:54:51 +11:00
if v.KODIVERSION >= 17:
# update new ratings Kodi 17
2017-02-19 02:30:06 +11:00
rating_id = self.kodi_db.get_ratingid(showid, v.KODI_TYPE_SHOW)
self.kodi_db.update_ratings(showid,
v.KODI_TYPE_SHOW,
"default",
rating,
2017-02-19 02:30:06 +11:00
votecount,
rating_id)
# update new uniqueid Kodi 17
2017-05-01 18:08:23 +10:00
if tvdb is not None:
uniqueid = self.kodi_db.get_uniqueid(showid,
v.KODI_TYPE_SHOW)
self.kodi_db.update_uniqueid(showid,
v.KODI_TYPE_SHOW,
tvdb,
"unknown",
uniqueid)
else:
self.kodi_db.remove_uniqueid(showid, v.KODI_TYPE_SHOW)
2017-05-01 18:08:23 +10:00
uniqueid = -1
2017-02-19 02:30:06 +11:00
# Update the tvshow entry
query = '''
UPDATE tvshow
SET c00 = ?, c01 = ?, c04 = ?, c05 = ?, c08 = ?, c09 = ?,
c12 = ?, c13 = ?, c14 = ?, c15 = ?
WHERE idShow = ?
'''
kodicursor.execute(query, (title, plot, rating_id,
premieredate, genre, title,
uniqueid, mpaa, studio, sorttitle,
showid))
2017-02-19 02:30:06 +11:00
else:
# Update the tvshow entry
query = '''
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))
2017-02-19 02:30:06 +11:00
# OR ADD THE TVSHOW #####
else:
2018-02-11 22:06:04 +11:00
LOG.info("ADD tvshow itemid: %s - Title: %s", itemid, title)
2017-02-19 02:30:06 +11:00
# Link the path
query = "INSERT INTO tvshowlinkpath(idShow, idPath) values (?, ?)"
kodicursor.execute(query, (showid, pathid))
# Create the reference in plex table
plex_db.addReference(itemid,
v.PLEX_TYPE_SHOW,
2016-10-27 05:53:20 +11:00
showid,
v.KODI_TYPE_SHOW,
kodi_pathid=pathid,
2016-10-27 05:53:20 +11:00
checksum=checksum,
view_id=viewid)
2017-01-25 02:54:51 +11:00
if v.KODIVERSION >= 17:
# add new ratings Kodi 17
rating_id = self.kodi_db.get_ratingid(showid, v.KODI_TYPE_SHOW)
2017-02-19 02:30:06 +11:00
self.kodi_db.add_ratings(rating_id,
showid,
v.KODI_TYPE_SHOW,
"default",
rating,
2017-02-19 02:30:06 +11:00
votecount)
# add new uniqueid Kodi 17
2017-05-01 18:08:23 +10:00
if tvdb is not None:
uniqueid = self.kodi_db.get_uniqueid(showid,
v.KODI_TYPE_SHOW)
2017-05-01 18:08:23 +10:00
self.kodi_db.add_uniqueid(uniqueid,
showid,
v.KODI_TYPE_SHOW,
tvdb,
"unknown")
else:
uniqueid = -1
2017-02-19 02:30:06 +11:00
# Create the tvshow entry
query = '''
INSERT INTO tvshow(
idShow, c00, c01, c04, c05, c08, c09, c12, c13, c14,
c15)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
2017-02-19 02:30:06 +11:00
kodicursor.execute(query, (showid, title, plot, rating_id,
premieredate, genre, title,
uniqueid, mpaa, studio, sorttitle))
2017-02-19 02:30:06 +11:00
else:
# Create the tvshow entry
query = '''
INSERT INTO tvshow(
idShow, c00, c01, c04, c05, c08, c09, c12, c13, c14,
c15)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
2017-02-19 02:30:06 +11:00
kodicursor.execute(query, (showid, title, plot, rating,
premieredate, genre, title, tvdb,
mpaa, studio, sorttitle))
self.kodi_db.modify_people(showid, v.KODI_TYPE_SHOW, api.people_list())
self.kodi_db.modify_genres(showid, v.KODI_TYPE_SHOW, genres)
2018-03-04 23:39:18 +11:00
artwork.modify_artwork(api.artwork(),
showid,
v.KODI_TYPE_SHOW,
kodicursor)
# Process studios
self.kodi_db.modify_studios(showid, v.KODI_TYPE_SHOW, studios)
2016-03-02 02:28:48 +11:00
# Process tags: view, PMS collection tags
2016-01-12 01:56:56 +11:00
tags = [viewtag]
tags.extend(collections)
self.kodi_db.modify_tags(showid, v.KODI_TYPE_SHOW, tags)
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
def add_updateSeason(self, item, viewtag=None, viewid=None):
2018-02-11 22:06:04 +11:00
"""
Process a single season of a certain tv show
"""
api = API(item)
2018-02-12 00:42:49 +11:00
plex_id = api.plex_id()
if not plex_id:
2018-02-11 22:06:04 +11:00
LOG.error('Error getting plex_id for season, skipping')
return
kodicursor = self.kodicursor
plex_db = self.plex_db
artwork = self.artwork
2018-02-12 00:42:49 +11:00
seasonnum = api.season_number()
2016-03-14 02:06:54 +11:00
# Get parent tv show Plex id
2018-03-11 00:53:39 +11:00
plexshowid = api.parent_plex_id()
2016-03-14 02:06:54 +11:00
# Get Kodi showid
plex_dbitem = plex_db.getItem_byId(plexshowid)
2016-03-14 02:06:54 +11:00
try:
showid = plex_dbitem[0]
2018-02-11 22:06:04 +11:00
except TypeError:
LOG.error('Could not find parent tv show for season %s. '
'Skipping season for now.', plex_id)
2016-03-14 02:06:54 +11:00
return
2018-03-11 01:02:06 +11:00
seasonid = self.kodi_db.add_season(showid, seasonnum)
2018-02-12 00:42:49 +11:00
checksum = api.checksum()
2016-01-12 03:37:01 +11:00
# Check whether Season already exists
plex_dbitem = plex_db.getItem_byId(plex_id)
2018-02-11 22:06:04 +11:00
update_item = False if plex_dbitem is None else True
2018-03-11 01:03:31 +11:00
artwork.modify_artwork(api.artwork(),
2018-03-04 23:39:18 +11:00
seasonid,
v.KODI_TYPE_SEASON,
kodicursor)
2016-01-12 03:37:01 +11:00
if update_item:
# Update a reference: checksum in plex table
plex_db.updateReference(plex_id, checksum)
2016-01-12 03:37:01 +11:00
else:
# Create the reference in plex table
plex_db.addReference(plex_id,
v.PLEX_TYPE_SEASON,
2016-08-31 00:40:11 +10:00
seasonid,
v.KODI_TYPE_SEASON,
parent_id=showid,
view_id=viewid,
2016-08-31 00:40:11 +10:00
checksum=checksum)
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
2016-01-10 02:14:02 +11:00
def add_updateEpisode(self, item, viewtag=None, viewid=None):
"""
2018-02-11 22:06:04 +11:00
Process single episode
2016-01-10 02:14:02 +11:00
"""
kodicursor = self.kodicursor
plex_db = self.plex_db
artwork = self.artwork
2018-02-11 22:06:04 +11:00
api = API(item)
update_item = True
2018-02-12 00:42:49 +11:00
itemid = api.plex_id()
if not itemid:
2018-02-11 22:06:04 +11:00
LOG.error('Error getting itemid for episode, skipping')
return
plex_dbitem = plex_db.getItem_byId(itemid)
try:
episodeid = plex_dbitem[0]
2018-03-11 03:09:21 +11:00
old_fileid = plex_dbitem[1]
pathid = plex_dbitem[2]
except TypeError:
update_item = False
# episodeid
2018-03-11 01:44:08 +11:00
kodicursor.execute('SELECT COALESCE(MAX(idEpisode),0) FROM episode')
episodeid = kodicursor.fetchone()[0] + 1
else:
# Verification the item is still in Kodi
2018-03-11 01:44:08 +11:00
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
2018-03-11 01:44:08 +11:00
LOG.info('episodeid: %s missing from Kodi, repairing entry.',
2018-02-11 22:06:04 +11:00
episodeid)
# fileId information
2018-02-12 00:42:49 +11:00
checksum = api.checksum()
dateadded = api.date_created()
userdata = api.userdata()
playcount = userdata['PlayCount']
dateplayed = userdata['LastPlayedDate']
2018-02-12 00:42:49 +11:00
tvdb = api.provider('tvdb')
votecount = None
# item details
2018-02-12 00:42:49 +11:00
peoples = api.people()
director = api.list_to_string(peoples['Director'])
writer = api.list_to_string(peoples['Writer'])
title, _ = api.titles()
plot = api.plot()
2016-04-02 19:31:21 +11:00
rating = userdata['Rating']
2018-02-12 00:42:49 +11:00
resume, runtime = api.resume_runtime()
premieredate = api.premiere_date()
# episode details
2018-02-12 00:42:49 +11:00
series_id, _, season, episode = api.episode_data()
2016-01-10 02:14:02 +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
2018-02-11 22:06:04 +11:00
airs_before_season = "-1"
airs_before_episode = "-1"
# Get season id
2018-02-11 22:06:04 +11:00
show = plex_db.getItem_byId(series_id)
try:
showid = show[0]
except TypeError:
2018-02-11 22:06:04 +11:00
LOG.error("Parent tvshow now found, skip item")
2016-01-30 06:07:21 +11:00
return False
2018-03-11 01:02:06 +11:00
seasonid = self.kodi_db.add_season(showid, season)
# GET THE FILE AND PATH #####
2017-05-17 23:42:12 +10:00
if state.DIRECT_PATHS:
2018-03-11 01:44:08 +11:00
playurl = api.file_path(force_first_media=True)
playurl = api.validate_playurl(playurl, v.PLEX_TYPE_EPISODE)
2018-03-11 01:44:08 +11:00
if "\\" in playurl:
# Local path
filename = playurl.rsplit("\\", 1)[1]
else:
2018-03-11 01:44:08 +11:00
# Network share
filename = playurl.rsplit("/", 1)[1]
path = playurl.replace(filename, "")
parent_path_id = self.kodi_db.parent_path_id(path)
pathid = self.kodi_db.add_video_path(path,
id_parent_path=parent_path_id)
2018-03-11 01:44:08 +11:00
else:
# Set plugin path - do NOT use "intermediate" paths for the show
# as with direct paths!
filename = api.file_name(force_first_media=True)
path = 'plugin://%s.tvshows/%s/' % (v.ADDON_ID, series_id)
filename = ('%s?plex_id=%s&plex_type=%s&mode=play&filename=%s'
% (path, itemid, v.PLEX_TYPE_EPISODE, filename))
playurl = filename
# Root path tvshows/ already saved in Kodi DB
pathid = self.kodi_db.add_video_path(path)
# add/retrieve pathid and fileid
# if the path or file already exists, the calls return current value
2018-03-11 03:09:21 +11:00
fileid = self.kodi_db.add_file(filename, pathid, dateadded)
# UPDATE THE EPISODE #####
if update_item:
2018-02-11 22:06:04 +11:00
LOG.info("UPDATE episode itemid: %s", itemid)
2018-03-11 03:09:21 +11:00
if fileid != old_fileid:
LOG.debug('Removing old file entry: %s', old_fileid)
2018-03-11 03:09:21 +11:00
self.kodi_db.remove_file(old_fileid)
# Update the movie entry
2017-02-03 01:23:54 +11:00
if v.KODIVERSION >= 17:
# update new ratings Kodi 17
2017-02-14 06:23:02 +11:00
ratingid = self.kodi_db.get_ratingid(episodeid,
v.KODI_TYPE_EPISODE)
self.kodi_db.update_ratings(episodeid,
v.KODI_TYPE_EPISODE,
"default",
rating,
votecount,
ratingid)
# update new uniqueid Kodi 17
2017-02-14 06:26:30 +11:00
uniqueid = self.kodi_db.get_uniqueid(episodeid,
v.KODI_TYPE_EPISODE)
self.kodi_db.update_uniqueid(episodeid,
v.KODI_TYPE_EPISODE,
tvdb,
"tvdb",
uniqueid)
2017-02-03 01:23:54 +11:00
query = '''
UPDATE episode
SET c00 = ?, c01 = ?, c03 = ?, c04 = ?, c05 = ?, c09 = ?,
c10 = ?, c12 = ?, c13 = ?, c14 = ?, c15 = ?, c16 = ?,
c18 = ?, c19 = ?, idFile=?, idSeason = ?,
userrating = ?
2017-02-03 22:34:14 +11:00
WHERE idEpisode = ?
2017-02-03 01:23:54 +11:00
'''
2018-03-11 23:02:04 +11:00
kodicursor.execute(query, (title, plot, ratingid, writer,
2017-02-03 01:23:54 +11:00
premieredate, runtime, director, season, episode, title,
airs_before_season, airs_before_episode, playurl, pathid,
fileid, seasonid, userdata['UserRating'], episodeid))
2018-03-11 03:09:21 +11:00
else:
2017-02-03 01:23:54 +11:00
# Kodi Jarvis
query = '''
UPDATE episode
SET c00 = ?, c01 = ?, c03 = ?, c04 = ?, c05 = ?, c09 = ?,
c10 = ?, c12 = ?, c13 = ?, c14 = ?, c15 = ?, c16 = ?,
c18 = ?, c19 = ?, idFile=?, idSeason = ?
WHERE idEpisode = ?
'''
kodicursor.execute(query, (title, plot, rating, writer,
premieredate, runtime, director, season, episode, title,
airs_before_season, airs_before_episode, playurl, pathid,
fileid, seasonid, episodeid))
# Update parentid reference
plex_db.updateParentId(itemid, seasonid)
2017-02-03 01:23:54 +11:00
# OR ADD THE EPISODE #####
else:
2018-02-11 22:06:04 +11:00
LOG.info("ADD episode itemid: %s - Title: %s", itemid, title)
# Create the episode entry
2017-02-03 01:23:54 +11:00
if v.KODIVERSION >= 17:
# add new ratings Kodi 17
rating_id = self.kodi_db.get_ratingid(episodeid,
v.KODI_TYPE_EPISODE)
2017-02-18 05:35:51 +11:00
self.kodi_db.add_ratings(rating_id,
episodeid,
v.KODI_TYPE_EPISODE,
"default",
rating,
votecount)
# add new uniqueid Kodi 17
uniqueid = self.kodi_db.get_uniqueid(episodeid,
v.KODI_TYPE_EPISODE)
self.kodi_db.add_uniqueid(uniqueid,
episodeid,
v.KODI_TYPE_EPISODE,
tvdb,
"tvdb")
2017-02-03 01:23:54 +11:00
query = '''
INSERT INTO episode( idEpisode, idFile, c00, c01, c03, c04,
c05, c09, c10, c12, c13, c14, idShow, c15, c16, c18,
c19, idSeason, userrating)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?)
2017-02-03 01:23:54 +11:00
'''
kodicursor.execute(query, (episodeid, fileid, title, plot,
rating_id, writer, premieredate, runtime, director, season,
episode, title, showid, airs_before_season,
airs_before_episode, playurl, pathid, seasonid,
userdata['UserRating']))
else:
2017-02-03 01:23:54 +11:00
# Kodi Jarvis
query = '''
INSERT INTO episode( idEpisode, idFile, c00, c01, c03, c04,
c05, c09, c10, c12, c13, c14, idShow, c15, c16, c18,
c19, idSeason)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?)
'''
2017-02-03 01:23:54 +11:00
kodicursor.execute(query, (episodeid, fileid, title, plot,
rating, writer, premieredate, runtime, director, season,
episode, title, showid, airs_before_season,
airs_before_episode, playurl, pathid, seasonid))
# Create or update the reference in plex table Add reference is
# idempotent; the call here updates also fileid and pathid when item is
# moved or renamed
plex_db.addReference(itemid,
v.PLEX_TYPE_EPISODE,
episodeid,
v.KODI_TYPE_EPISODE,
kodi_fileid=fileid,
kodi_pathid=pathid,
parent_id=seasonid,
checksum=checksum,
view_id=viewid)
self.kodi_db.modify_people(episodeid,
v.KODI_TYPE_EPISODE,
api.people_list())
2018-03-19 05:18:44 +11:00
artwork.modify_artwork(api.artwork(),
episodeid,
v.KODI_TYPE_EPISODE,
kodicursor)
2018-02-12 00:42:49 +11:00
streams = api.mediastreams()
2018-02-26 19:18:44 +11:00
self.kodi_db.modify_streams(fileid, streams, runtime)
2018-06-15 23:15:35 +10:00
self.kodi_db.set_resume(fileid,
resume,
runtime,
playcount,
dateplayed,
2018-06-22 03:24:37 +10:00
None) # Do send None, we check here
if not state.DIRECT_PATHS:
# need to set a SECOND file entry for a path without plex show id
filename = api.file_name(force_first_media=True)
path = 'plugin://%s.tvshows/' % v.ADDON_ID
# Filename is exactly the same, WITH plex show id!
filename = ('%s%s/?plex_id=%s&plex_type=%s&mode=play&filename=%s'
% (path, series_id, itemid, v.PLEX_TYPE_EPISODE,
filename))
pathid = self.kodi_db.add_video_path(path)
fileid = self.kodi_db.add_file(filename, pathid, dateadded)
2018-06-15 23:15:35 +10:00
self.kodi_db.set_resume(fileid,
resume,
runtime,
playcount,
dateplayed,
2018-06-22 03:24:37 +10:00
None) # Do send None - 2nd entry
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
def remove(self, plex_id):
2018-02-11 22:06:04 +11:00
"""
Remove the entire TV shows object (show, season or episode) including
all associated entries from the Kodi DB.
2018-02-11 22:06:04 +11:00
"""
plex_dbitem = self.plex_db.getItem_byId(plex_id)
if plex_dbitem is None:
2018-04-07 22:34:27 +10:00
LOG.debug('Cannot delete plex_id %s - not found in DB', plex_id)
return
kodi_id = plex_dbitem[0]
file_id = plex_dbitem[1]
parent_id = plex_dbitem[3]
kodi_type = plex_dbitem[4]
LOG.info("Removing %s with kodi_id: %s file_id: %s parent_id: %s",
kodi_type, kodi_id, file_id, parent_id)
# Remove the plex reference
self.plex_db.removeItem(plex_id)
##### EPISODE #####
if kodi_type == v.KODI_TYPE_EPISODE:
# Delete episode, verify season and tvshow
self.remove_episode(kodi_id, file_id)
# Season verification
season = self.plex_db.getItem_byKodiId(parent_id,
v.KODI_TYPE_SEASON)
if not self.plex_db.getItem_byParentId(parent_id,
v.KODI_TYPE_EPISODE):
# No episode left for season - so delete the season
self.remove_season(parent_id)
self.plex_db.removeItem(season[0])
show = self.plex_db.getItem_byKodiId(season[1],
v.KODI_TYPE_SHOW)
if not self.plex_db.getItem_byParentId(season[1],
v.KODI_TYPE_SEASON):
# No seasons for show left - so delete entire show
self.remove_show(season[1])
self.plex_db.removeItem(show[0])
##### SEASON #####
elif kodi_type == v.KODI_TYPE_SEASON:
# Remove episodes, season, verify tvshow
for episode in self.plex_db.getItem_byParentId(
kodi_id, v.KODI_TYPE_EPISODE):
self.remove_episode(episode[1], episode[2])
self.plex_db.removeItem(episode[0])
# Remove season
self.remove_season(kodi_id)
# Show verification
if not self.plex_db.getItem_byParentId(parent_id,
v.KODI_TYPE_SEASON):
# There's no other season left, delete the show
self.remove_show(parent_id)
self.plex_db.removeItem_byKodiId(parent_id, v.KODI_TYPE_SHOW)
##### TVSHOW #####
elif kodi_type == v.KODI_TYPE_SHOW:
# Remove episodes, seasons and the tvshow itself
for season in self.plex_db.getItem_byParentId(kodi_id,
v.KODI_TYPE_SEASON):
for episode in self.plex_db.getItem_byParentId(
season[1], v.KODI_TYPE_EPISODE):
self.remove_episode(episode[1], episode[2])
self.plex_db.removeItem(episode[0])
self.remove_season(season[1])
self.plex_db.removeItem(season[0])
self.remove_show(kodi_id)
LOG.debug("Deleted %s %s from Kodi database", kodi_type, plex_id)
def remove_show(self, kodi_id):
2018-02-11 22:06:04 +11:00
"""
Remove a TV show, and only the show, no seasons or episodes
"""
self.kodi_db.modify_genres(kodi_id, v.KODI_TYPE_SHOW)
self.kodi_db.modify_studios(kodi_id, v.KODI_TYPE_SHOW)
self.kodi_db.modify_tags(kodi_id, v.KODI_TYPE_SHOW)
self.artwork.delete_artwork(kodi_id,
v.KODI_TYPE_SHOW,
self.kodicursor)
self.kodicursor.execute("DELETE FROM tvshow WHERE idShow = ?",
(kodi_id,))
if v.KODIVERSION >= 17:
2017-02-03 03:09:20 +11:00
self.kodi_db.remove_uniqueid(kodi_id, v.KODI_TYPE_SHOW)
self.kodi_db.remove_ratings(kodi_id, v.KODI_TYPE_SHOW)
2018-04-07 22:34:27 +10:00
LOG.debug("Removed tvshow: %s", kodi_id)
def remove_season(self, kodi_id):
2018-02-11 22:06:04 +11:00
"""
Remove a season, and only a season, not the show or episodes
"""
self.artwork.delete_artwork(kodi_id,
v.KODI_TYPE_SEASON,
self.kodicursor)
self.kodicursor.execute("DELETE FROM seasons WHERE idSeason = ?",
(kodi_id,))
2018-04-07 22:34:27 +10:00
LOG.debug("Removed season: %s", kodi_id)
def remove_episode(self, kodi_id, file_id):
2018-02-11 22:06:04 +11:00
"""
Remove an episode, and episode only from the Kodi DB (not Plex DB)
2018-02-11 22:06:04 +11:00
"""
self.kodi_db.modify_people(kodi_id, v.KODI_TYPE_EPISODE)
self.kodi_db.remove_file(file_id, plex_type=v.PLEX_TYPE_EPISODE)
self.artwork.delete_artwork(kodi_id,
v.KODI_TYPE_EPISODE,
self.kodicursor)
self.kodicursor.execute("DELETE FROM episode WHERE idEpisode = ?",
(kodi_id,))
if v.KODIVERSION >= 17:
self.kodi_db.remove_uniqueid(kodi_id, v.KODI_TYPE_EPISODE)
self.kodi_db.remove_ratings(kodi_id, v.KODI_TYPE_EPISODE)
2018-04-07 22:34:27 +10:00
LOG.debug("Removed episode: %s", kodi_id)
class Music(Items):
2018-02-11 22:06:04 +11:00
"""
For Plex library-type music. Also works for premium music libraries
"""
def __enter__(self):
"""
OVERWRITE this method, because we need to open another DB.
Open DB connections and cursors
"""
2018-06-22 03:24:37 +10:00
self.plexconn = utils.kodi_sql('plex')
self.plexcursor = self.plexconn.cursor()
# Here it is, not 'video' but 'music'
2018-06-22 03:24:37 +10:00
self.kodiconn = utils.kodi_sql('music')
self.kodicursor = self.kodiconn.cursor()
self.plex_db = plexdb.Plex_DB_Functions(self.plexcursor)
2018-02-25 23:42:20 +11:00
self.kodi_db = kodidb.KodiDBMethods(self.kodicursor)
return self
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
2018-02-11 22:06:04 +11:00
def add_updateArtist(self, item, viewtag=None, viewid=None):
"""
Adds a single artist
"""
kodicursor = self.kodicursor
plex_db = self.plex_db
artwork = self.artwork
2018-02-11 22:06:04 +11:00
api = API(item)
update_item = True
2018-02-12 00:42:49 +11:00
itemid = api.plex_id()
plex_dbitem = plex_db.getItem_byId(itemid)
try:
artistid = plex_dbitem[0]
except TypeError:
update_item = False
# The artist details #####
lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
2018-02-12 00:42:49 +11:00
dateadded = api.date_created()
checksum = api.checksum()
2018-02-12 00:42:49 +11:00
name, _ = api.titles()
# musicBrainzId = api.provider('MusicBrainzArtist')
musicBrainzId = None
2018-02-12 00:42:49 +11:00
genres = ' / '.join(api.genre_list())
bio = api.plot()
# Associate artwork
2018-03-04 00:40:12 +11:00
artworks = api.artwork()
if 'poster' in artworks:
thumb = "<thumb>%s</thumb>" % artworks['poster']
else:
2018-03-04 00:40:12 +11:00
thumb = None
2018-03-05 00:22:39 +11:00
if 'fanart' in artworks:
fanart = "<fanart>%s</fanart>" % artworks['fanart']
2018-03-04 00:40:12 +11:00
else:
fanart = None
# UPDATE THE ARTIST #####
if update_item:
2018-02-11 22:06:04 +11:00
LOG.info("UPDATE artist itemid: %s - Name: %s", itemid, name)
# Update the checksum in plex table
plex_db.updateReference(itemid, checksum)
# OR ADD THE ARTIST #####
else:
2018-02-11 22:06:04 +11:00
LOG.info("ADD artist itemid: %s - Name: %s", itemid, name)
# safety checks: It looks like plex supports the same artist
# multiple times.
# Kodi doesn't allow that. In case that happens we just merge the
# artist entries.
2018-06-15 23:15:35 +10:00
artistid = self.kodi_db.add_artist(name, musicBrainzId)
# Create the reference in plex table
plex_db.addReference(itemid,
v.PLEX_TYPE_ARTIST,
artistid,
v.KODI_TYPE_ARTIST,
view_id=viewid,
checksum=checksum)
# Process the artist
2017-01-25 02:54:51 +11:00
if v.KODIVERSION >= 16:
2017-04-03 01:02:41 +10:00
query = '''
UPDATE artist
SET strGenres = ?, strBiography = ?, strImage = ?,
strFanart = ?, lastScraped = ?
WHERE idArtist = ?
'''
kodicursor.execute(query, (genres, bio, thumb, fanart,
lastScraped, artistid))
else:
2017-04-03 01:02:41 +10:00
query = '''
UPDATE artist
SET strGenres = ?, strBiography = ?, strImage = ?,
strFanart = ?, lastScraped = ?, dateAdded = ?
WHERE idArtist = ?
'''
kodicursor.execute(query, (genres, bio, thumb, fanart, lastScraped,
dateadded, artistid))
# Update artwork
2018-03-04 23:39:18 +11:00
artwork.modify_artwork(artworks,
artistid,
v.KODI_TYPE_ARTIST,
kodicursor)
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
def add_updateAlbum(self, item, viewtag=None, viewid=None, children=None,
scan_children=True):
2017-04-03 01:02:41 +10:00
"""
2018-02-11 22:06:04 +11:00
Adds a single music album
children: list of child xml's, so in this case songs
scan_children: set to False if you don't want to add children
2017-04-03 01:02:41 +10:00
"""
kodicursor = self.kodicursor
plex_db = self.plex_db
artwork = self.artwork
2018-02-11 22:06:04 +11:00
api = API(item)
update_item = True
plex_id = api.plex_id()
if not plex_id:
2018-02-11 22:06:04 +11:00
LOG.error('Error processing Album, skipping')
return
plex_dbitem = plex_db.getItem_byId(plex_id)
try:
album_id = plex_dbitem[0]
except TypeError:
update_item = False
# The album details #####
lastScraped = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
2018-02-12 00:42:49 +11:00
userdata = api.userdata()
checksum = api.checksum()
2018-02-12 00:42:49 +11:00
name, _ = api.titles()
# musicBrainzId = api.provider('MusicBrainzAlbum')
musicBrainzId = None
2018-02-12 00:42:49 +11:00
year = api.year()
self.genres = api.genre_list()
2017-04-03 01:02:41 +10:00
self.genre = ' / '.join(self.genres)
2018-02-12 00:42:49 +11:00
bio = api.plot()
rating = userdata['UserRating']
2018-02-12 00:42:49 +11:00
studio = api.music_studio()
artistname = item.attrib.get('parentTitle')
if not artistname:
artistname = item.attrib.get('originalTitle')
2017-04-03 01:02:41 +10:00
# See if we have a compilation - Plex does NOT feature a compilation
# flag for albums
self.compilation = 0
for child in children:
if child.attrib.get('originalTitle') is not None:
self.compilation = 1
break
# Associate artwork
2018-03-04 00:40:12 +11:00
artworks = api.artwork()
if 'poster' in artworks:
thumb = "<thumb>%s</thumb>" % artworks['poster']
else:
thumb = None
# UPDATE THE ALBUM #####
if update_item:
LOG.info("UPDATE album plex_id: %s - Name: %s", plex_id, name)
# Update the checksum in plex table
plex_db.updateReference(plex_id, checksum)
# OR ADD THE ALBUM #####
else:
LOG.info("ADD album plex_id: %s - Name: %s", plex_id, name)
# safety checks: It looks like plex supports the same artist
# multiple times.
# Kodi doesn't allow that. In case that happens we just merge the
# artist entries.
2018-06-15 23:15:35 +10:00
album_id = self.kodi_db.add_album(name, musicBrainzId)
# Create the reference in plex table
plex_db.addReference(plex_id,
v.PLEX_TYPE_ALBUM,
album_id,
v.KODI_TYPE_ALBUM,
view_id=viewid,
checksum=checksum)
# Process the album info
if v.KODIVERSION >= 18:
# Kodi Leia
2017-04-03 01:02:41 +10:00
query = '''
UPDATE album
SET strArtistDisp = ?, iYear = ?, strGenres = ?, strReview = ?,
2017-04-03 01:02:41 +10:00
strImage = ?, iUserrating = ?, lastScraped = ?,
strReleaseType = ?, strLabel = ?, bCompilation = ?
WHERE idAlbum = ?
'''
kodicursor.execute(query, (artistname, year, self.genre, bio,
thumb, rating, lastScraped,
v.KODI_TYPE_ALBUM, studio,
self.compilation, album_id))
elif v.KODIVERSION == 17:
# Kodi Krypton
2017-04-03 01:02:41 +10:00
query = '''
UPDATE album
SET strArtists = ?, iYear = ?, strGenres = ?, strReview = ?,
strImage = ?, iUserrating = ?, lastScraped = ?,
2017-04-03 01:02:41 +10:00
strReleaseType = ?, strLabel = ?, bCompilation = ?
WHERE idAlbum = ?
'''
kodicursor.execute(query, (artistname, year, self.genre, bio,
thumb, rating, lastScraped,
v.KODI_TYPE_ALBUM, studio,
self.compilation, album_id))
elif v.KODIVERSION == 16:
# Kodi Jarvis
2017-04-03 01:02:41 +10:00
query = '''
UPDATE album
SET strArtists = ?, iYear = ?, strGenres = ?, strReview = ?,
strImage = ?, iRating = ?, lastScraped = ?,
strReleaseType = ?, strLabel = ?, bCompilation = ?
2017-04-03 01:02:41 +10:00
WHERE idAlbum = ?
'''
kodicursor.execute(query, (artistname, year, self.genre, bio,
thumb, rating, lastScraped,
v.KODI_TYPE_ALBUM, studio,
self.compilation, album_id))
# Associate the parentid for plex reference
2018-03-11 00:53:39 +11:00
parent_id = api.parent_plex_id()
artist_id = None
2018-02-11 22:06:04 +11:00
if parent_id is not None:
try:
artist_id = plex_db.getItem_byId(parent_id)[0]
except TypeError:
LOG.info('Artist %s does not yet exist in Plex DB', parent_id)
2018-06-22 03:24:37 +10:00
artist = PF.GetPlexMetadata(parent_id)
try:
artist[0].attrib
except (TypeError, IndexError, AttributeError):
LOG.error('Could not get artist xml for %s', parent_id)
else:
self.add_updateArtist(artist[0])
plex_dbartist = plex_db.getItem_byId(parent_id)
try:
artist_id = plex_dbartist[0]
except TypeError:
LOG.error('Adding artist failed for %s', parent_id)
# Update plex reference with the artist_id
plex_db.updateParentId(plex_id, artist_id)
# Add artist to album
2017-04-03 01:02:41 +10:00
query = '''
INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist)
VALUES (?, ?, ?)
2017-04-03 01:02:41 +10:00
'''
kodicursor.execute(query, (artist_id, album_id, artistname))
# Update discography
2017-04-03 01:02:41 +10:00
query = '''
INSERT OR REPLACE INTO discography(idArtist, strAlbum, strYear)
VALUES (?, ?, ?)
2017-04-03 01:02:41 +10:00
'''
kodicursor.execute(query, (artist_id, name, year))
if v.KODIVERSION < 18:
2018-06-15 23:15:35 +10:00
self.kodi_db.add_music_genres(album_id,
self.genres,
v.KODI_TYPE_ALBUM)
# Update artwork
2018-06-22 03:24:37 +10:00
artwork.modify_artwork(artworks,
album_id,
v.KODI_TYPE_ALBUM,
kodicursor)
2017-04-03 01:02:41 +10:00
# Add all children - all tracks
if scan_children:
for child in children:
self.add_updateSong(child, viewtag, viewid)
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
def add_updateSong(self, item, viewtag=None, viewid=None):
2018-02-11 22:06:04 +11:00
"""
Process single song
"""
kodicursor = self.kodicursor
plex_db = self.plex_db
artwork = self.artwork
2018-02-11 22:06:04 +11:00
api = API(item)
update_item = True
2018-02-12 00:42:49 +11:00
itemid = api.plex_id()
if not itemid:
2018-02-11 22:06:04 +11:00
LOG.error('Error processing Song; skipping')
return
plex_dbitem = plex_db.getItem_byId(itemid)
try:
songid = plex_dbitem[0]
pathid = plex_dbitem[2]
albumid = plex_dbitem[3]
except TypeError:
# Songid not found
update_item = False
kodicursor.execute("select coalesce(max(idSong),0) from song")
songid = kodicursor.fetchone()[0] + 1
# The song details #####
2018-02-12 00:42:49 +11:00
checksum = api.checksum()
dateadded = api.date_created()
userdata = api.userdata()
playcount = userdata['PlayCount']
2016-04-08 22:48:47 +10:00
if playcount is None:
# This is different to Video DB!
playcount = 0
dateplayed = userdata['LastPlayedDate']
# item details
2018-02-12 00:42:49 +11:00
title, _ = api.titles()
# musicBrainzId = api.provider('MusicBrainzTrackId')
musicBrainzId = None
2017-04-03 01:02:41 +10:00
try:
genres = self.genres
genre = self.genre
except AttributeError:
# No parent album - hence no genre information from Plex
genres = None
genre = None
try:
if self.compilation == 0:
2018-03-11 00:56:24 +11:00
artists = api.grandparent_title()
2017-04-03 01:02:41 +10:00
else:
artists = item.attrib.get('originalTitle')
except AttributeError:
# compilation not set
2018-03-11 00:56:24 +11:00
artists = item.attrib.get('originalTitle', api.grandparent_title())
tracknumber = int(item.attrib.get('index', 0))
disc = int(item.attrib.get('parentIndex', 1))
if disc == 1:
track = tracknumber
else:
2018-06-22 03:24:37 +10:00
track = disc * 2 ** 16 + tracknumber
2018-02-12 00:42:49 +11:00
year = api.year()
_, duration = api.resume_runtime()
2017-02-03 01:23:54 +11:00
rating = userdata['UserRating']
comment = None
2017-04-03 01:02:41 +10:00
# Moods
moods = []
for entry in item:
if entry.tag == 'Mood':
moods.append(entry.attrib['tag'])
mood = ' / '.join(moods)
# GET THE FILE AND PATH #####
2018-02-11 22:06:04 +11:00
do_indirect = not state.DIRECT_PATHS
2017-05-17 23:42:12 +10:00
if state.DIRECT_PATHS:
# Direct paths is set the Kodi way
2018-02-12 00:42:49 +11:00
playurl = api.file_path(force_first_media=True)
if playurl is None:
# Something went wrong, trying to use non-direct paths
2018-02-11 22:06:04 +11:00
do_indirect = True
else:
2018-02-12 00:42:49 +11:00
playurl = api.validate_playurl(playurl, api.plex_type())
if playurl is None:
return False
if "\\" in playurl:
# Local path
filename = playurl.rsplit("\\", 1)[1]
else:
# Network share
filename = playurl.rsplit("/", 1)[1]
path = playurl.replace(filename, "")
2018-02-11 22:06:04 +11:00
if do_indirect:
# Plex works a bit differently
path = "%s%s" % (self.server, item[0][0].attrib.get('key'))
2018-02-12 00:42:49 +11:00
path = api.attach_plex_token_to_url(path)
filename = path.rsplit('/', 1)[1]
path = path.replace(filename, '')
# UPDATE THE SONG #####
if update_item:
2018-02-11 22:06:04 +11:00
LOG.info("UPDATE song itemid: %s - Title: %s with path: %s",
itemid, title, path)
# Update path
# Use dummy strHash '123' for Kodi
query = "UPDATE path SET strPath = ?, strHash = ? WHERE idPath = ?"
kodicursor.execute(query, (path, '123', pathid))
# Update the song entry
if v.KODIVERSION >= 18:
# Kodi Leia
query = '''
UPDATE song
SET idAlbum = ?, strArtistDisp = ?, strGenres = ?,
strTitle = ?, iTrack = ?, iDuration = ?, iYear = ?,
strFilename = ?, iTimesPlayed = ?, lastplayed = ?,
rating = ?, comment = ?, mood = ?
WHERE idSong = ?
'''
kodicursor.execute(query, (albumid, artists, genre, title,
track, duration, year, filename,
playcount, dateplayed, rating,
comment, mood, songid))
else:
query = '''
UPDATE song
SET idAlbum = ?, strArtists = ?, strGenres = ?,
strTitle = ?, iTrack = ?, iDuration = ?, iYear = ?,
strFilename = ?, iTimesPlayed = ?, lastplayed = ?,
rating = ?, comment = ?, mood = ?
WHERE idSong = ?
'''
kodicursor.execute(query, (albumid, artists, genre, title,
track, duration, year, filename,
playcount, dateplayed, rating,
comment, mood, songid))
# Update the checksum in plex table
plex_db.updateReference(itemid, checksum)
# OR ADD THE SONG #####
else:
2018-02-11 22:06:04 +11:00
LOG.info("ADD song itemid: %s - Title: %s", itemid, title)
# Add path
2018-06-15 23:15:35 +10:00
pathid = self.kodi_db.add_music_path(path, hash_string="123")
try:
2016-01-01 15:19:36 +11:00
# Get the album
2018-03-11 00:53:39 +11:00
plex_dbalbum = plex_db.getItem_byId(api.parent_plex_id())
albumid = plex_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:
2018-02-11 22:06:04 +11:00
LOG.info("Creating virtual music album for song: %s.",
itemid)
2018-06-15 23:15:35 +10:00
albumid = self.kodi_db.add_album(
2017-04-03 01:02:41 +10:00
album_name,
2018-02-12 00:42:49 +11:00
api.provider('MusicBrainzAlbum'))
plex_db.addReference("%salbum%s" % (itemid, albumid),
v.PLEX_TYPE_ALBUM,
albumid,
v.KODI_TYPE_ALBUM,
view_id=viewid)
2016-03-16 22:13:47 +11:00
else:
# No album Id associated to the song.
2018-02-11 22:06:04 +11:00
LOG.error("Song itemid: %s has no albumId associated.",
itemid)
2016-03-16 22:13:47 +11:00
return False
except TypeError:
# No album found. Let's create it
2018-02-11 22:06:04 +11:00
LOG.info("Album database entry missing.")
2018-03-11 00:53:39 +11:00
plex_album_id = api.parent_plex_id()
2018-06-22 03:24:37 +10:00
album = PF.GetPlexMetadata(plex_album_id)
if album is None or album == 401:
2018-02-11 22:06:04 +11:00
LOG.error('Could not download album, abort')
return
self.add_updateAlbum(album[0],
children=[item],
scan_children=False)
2018-02-11 22:06:04 +11:00
plex_dbalbum = plex_db.getItem_byId(plex_album_id)
try:
albumid = plex_dbalbum[0]
2018-02-11 22:06:04 +11:00
LOG.debug("Found albumid: %s", albumid)
except TypeError:
# No album found, create a single's album
2018-02-11 22:06:04 +11:00
LOG.info("Failed to add album. Creating singles.")
2017-04-03 01:02:41 +10:00
kodicursor.execute(
"select coalesce(max(idAlbum),0) from album")
albumid = kodicursor.fetchone()[0] + 1
2017-01-25 02:54:51 +11:00
if v.KODIVERSION >= 16:
# Kodi Jarvis
2017-04-03 01:02:41 +10:00
query = '''
INSERT INTO album(
idAlbum, strGenres, iYear, strReleaseType)
VALUES (?, ?, ?, ?)
2017-04-03 01:02:41 +10:00
'''
kodicursor.execute(query,
(albumid, genre, year, "single"))
2017-01-25 02:54:51 +11:00
elif v.KODIVERSION == 15:
# Kodi Isengard
2017-04-03 01:02:41 +10:00
query = '''
INSERT INTO album(
idAlbum, strGenres, iYear, dateAdded,
strReleaseType)
VALUES (?, ?, ?, ?, ?)
2017-04-03 01:02:41 +10:00
'''
kodicursor.execute(query, (albumid, genre, year,
dateadded, "single"))
else:
# Kodi Helix
2017-04-03 01:02:41 +10:00
query = '''
INSERT INTO album(
idAlbum, strGenres, iYear, dateAdded)
VALUES (?, ?, ?, ?)
2017-04-03 01:02:41 +10:00
'''
kodicursor.execute(query, (albumid, genre, year,
dateadded))
# Create the song entry
if v.KODIVERSION >= 18:
# Kodi Leia
query = '''
INSERT INTO song(
idSong, idAlbum, idPath, strArtistDisp, strGenres,
strTitle, iTrack, iDuration, iYear, strFileName,
strMusicBrainzTrackID, iTimesPlayed, lastplayed,
rating, iStartOffset, iEndOffset, mood)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
kodicursor.execute(
query, (songid, albumid, pathid, artists, genre, title,
track, duration, year, filename, musicBrainzId,
playcount, dateplayed, rating, 0, 0, mood))
else:
query = '''
INSERT INTO song(
idSong, idAlbum, idPath, strArtists, strGenres,
strTitle, iTrack, iDuration, iYear, strFileName,
strMusicBrainzTrackID, iTimesPlayed, lastplayed,
rating, iStartOffset, iEndOffset, mood)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
kodicursor.execute(
query, (songid, albumid, pathid, artists, genre, title,
track, duration, year, filename, musicBrainzId,
playcount, dateplayed, rating, 0, 0, mood))
# Create the reference in plex table
plex_db.addReference(itemid,
v.PLEX_TYPE_SONG,
songid,
v.KODI_TYPE_SONG,
kodi_pathid=pathid,
parent_id=albumid,
checksum=checksum,
view_id=viewid)
if v.KODIVERSION < 18:
# Link song to album
query = '''
INSERT OR REPLACE INTO albuminfosong(
idAlbumInfoSong, idAlbumInfo, iTrack, strTitle, iDuration)
VALUES (?, ?, ?, ?, ?)
'''
2018-06-22 03:24:37 +10:00
kodicursor.execute(query,
(songid, albumid, track, title, duration))
2016-03-16 22:13:47 +11:00
# Link song to artists
2018-02-11 22:06:04 +11:00
artist_loop = [{
2018-03-11 00:56:24 +11:00
'Name': api.grandparent_title(),
'Id': api.grandparent_id()
2016-03-17 21:17:30 +11:00
}]
# for index, artist in enumerate(item['ArtistItems']):
2018-02-11 22:06:04 +11:00
for index, artist in enumerate(artist_loop):
2016-03-16 22:13:47 +11:00
artist_name = artist['Name']
artist_eid = artist['Id']
artist_edb = plex_db.getItem_byId(artist_eid)
2016-03-16 22:13:47 +11:00
try:
artistid = artist_edb[0]
except TypeError:
# Artist is missing from plex database, add it.
2018-06-22 03:24:37 +10:00
artist_xml = PF.GetPlexMetadata(artist_eid)
2018-02-11 22:06:04 +11:00
if artist_xml is None or artist_xml == 401:
LOG.error('Error getting artist, abort')
return
2018-02-11 22:06:04 +11:00
self.add_updateArtist(artist_xml[0])
artist_edb = plex_db.getItem_byId(artist_eid)
2016-03-16 22:13:47 +11:00
artistid = artist_edb[0]
finally:
2017-01-25 02:54:51 +11:00
if v.KODIVERSION >= 17:
# Kodi Krypton
2017-04-03 01:02:41 +10:00
query = '''
INSERT OR REPLACE INTO song_artist(
idArtist, idSong, idRole, iOrder, strArtist)
VALUES (?, ?, ?, ?, ?)
2017-04-03 01:02:41 +10:00
'''
kodicursor.execute(query, (artistid, songid, 1, index,
artist_name))
# May want to look into only doing this once?
2017-04-03 01:02:41 +10:00
query = '''
INSERT OR REPLACE INTO role(idRole, strRole)
VALUES (?, ?)
2017-04-03 01:02:41 +10:00
'''
kodicursor.execute(query, (1, 'Composer'))
else:
2017-04-03 01:02:41 +10:00
query = '''
INSERT OR REPLACE INTO song_artist(
idArtist, idSong, iOrder, strArtist)
VALUES (?, ?, ?, ?)
2017-04-03 01:02:41 +10:00
'''
kodicursor.execute(query, (artistid, songid, index,
artist_name))
# Add genres
2017-04-03 01:02:41 +10:00
if genres:
2018-06-15 23:15:35 +10:00
self.kodi_db.add_music_genres(songid, genres, v.KODI_TYPE_SONG)
2018-03-04 23:39:18 +11:00
artworks = api.artwork()
artwork.modify_artwork(artworks, songid, v.KODI_TYPE_SONG, kodicursor)
2016-03-17 21:17:30 +11:00
if item.get('parentKey') is None:
2016-03-16 22:13:47 +11:00
# Update album artwork
2018-06-22 03:24:37 +10:00
artwork.modify_artwork(artworks,
albumid,
v.KODI_TYPE_ALBUM,
kodicursor)
2016-03-16 22:13:47 +11:00
2018-06-22 03:24:37 +10:00
@utils.catch_exceptions(warnuser=True)
2018-04-07 22:30:17 +10:00
def remove(self, plex_id):
2018-02-11 22:06:04 +11:00
"""
2018-04-07 22:30:17 +10:00
Completely remove the item with plex_id from the Kodi and Plex DBs.
Orphaned entries will also be deleted.
2018-02-11 22:06:04 +11:00
"""
2018-04-07 22:30:17 +10:00
plex_dbitem = self.plex_db.getItem_byId(plex_id)
try:
2018-04-07 22:30:17 +10:00
kodi_id = plex_dbitem[0]
file_id = plex_dbitem[1]
path_id = plex_dbitem[2]
2018-04-07 22:30:17 +10:00
parent_id = plex_dbitem[3]
kodi_type = plex_dbitem[4]
2018-04-13 02:54:12 +10:00
LOG.debug('Removing plex_id %s with kodi_type %s, kodi_id %s, '
'parent_id %s, file_id %s, pathid %s',
plex_id, kodi_type, kodi_id, parent_id, file_id, path_id)
except TypeError:
2018-04-07 22:30:17 +10:00
LOG.debug('Cannot delete item with plex id %s from Kodi', plex_id)
return
# Remove the plex reference
2018-04-07 22:30:17 +10:00
self.plex_db.removeItem(plex_id)
##### SONG #####
if kodi_type == v.KODI_TYPE_SONG:
# Delete song and orphaned artists and albums
self._remove_song(kodi_id, path_id=path_id)
2018-04-07 22:30:17 +10:00
# Album verification
album = self.plex_db.getItem_byKodiId(parent_id,
v.KODI_TYPE_ALBUM)
if not self.plex_db.getItem_byParentId(parent_id,
v.KODI_TYPE_SONG):
# No song left for album - so delete the album
self.plex_db.removeItem(album[0])
self._remove_album(parent_id)
2018-04-07 22:30:17 +10:00
##### ALBUM #####
elif kodi_type == v.KODI_TYPE_ALBUM:
# Delete songs, album
songs = self.plex_db.getItem_byParentId(kodi_id,
v.KODI_TYPE_SONG)
for song in songs:
self._remove_song(song[1], path_id=song[2])
# Remove songs from Plex table
2018-04-07 22:30:17 +10:00
self.plex_db.removeItems_byParentId(kodi_id,
v.KODI_TYPE_SONG)
# Remove the album and associated orphaned entries
self._remove_album(kodi_id)
##### IF ARTIST #####
2018-04-07 22:30:17 +10:00
elif kodi_type == v.KODI_TYPE_ARTIST:
# Delete songs, album, artist
2018-06-22 03:24:37 +10:00
albums = self.plex_db.getItem_byParentId(kodi_id,
v.KODI_TYPE_ALBUM)
for album in albums:
songs = self.plex_db.getItem_byParentId(album[1],
v.KODI_TYPE_SONG)
for song in songs:
self._remove_song(song[1], path_id=song[2])
# Remove entries for the songs in the Plex db
self.plex_db.removeItems_byParentId(album[1], v.KODI_TYPE_SONG)
2018-02-11 22:06:04 +11:00
# Remove kodi album
self._remove_album(album[1])
# Remove album entries in the Plex db
2018-04-07 22:30:17 +10:00
self.plex_db.removeItems_byParentId(kodi_id, v.KODI_TYPE_ALBUM)
# Remove artist
self._remove_artist(kodi_id)
2018-04-07 22:30:17 +10:00
LOG.debug("Deleted plex_id %s from kodi database", plex_id)
def _remove_song(self, kodi_id, path_id=None):
2018-02-11 22:06:04 +11:00
"""
Remove song, orphaned artists and orphaned paths
2018-02-11 22:06:04 +11:00
"""
if not path_id:
query = 'SELECT idPath FROM song WHERE idSong = ? LIMIT 1'
self.kodicursor.execute(query, (kodi_id, ))
try:
path_id = self.kodicursor.fetchone()[0]
except TypeError:
pass
artist_to_delete = self.kodi_db.delete_song_from_song_artist(kodi_id)
if artist_to_delete:
# Delete the artist reference in the Plex table
artist = self.plex_db.getItem_byKodiId(artist_to_delete,
v.KODI_TYPE_ARTIST)
try:
plex_id = artist[0]
except TypeError:
pass
else:
self.plex_db.removeItem(plex_id)
self._remove_artist(artist_to_delete)
self.kodicursor.execute('DELETE FROM song WHERE idSong = ?',
(kodi_id, ))
# Check whether we have orphaned path entries
query = 'SELECT idPath FROM song WHERE idPath = ? LIMIT 1'
self.kodicursor.execute(query, (path_id, ))
if not self.kodicursor.fetchone():
self.kodicursor.execute('DELETE FROM path WHERE idPath = ?',
(path_id, ))
if v.KODIVERSION < 18:
self.kodi_db.delete_song_from_song_genre(kodi_id)
query = 'DELETE FROM albuminfosong WHERE idAlbumInfoSong = ?'
self.kodicursor.execute(query, (kodi_id, ))
2018-04-07 22:30:17 +10:00
self.artwork.delete_artwork(kodi_id, v.KODI_TYPE_SONG, self.kodicursor)
def _remove_album(self, kodi_id):
'''
Remove an album
'''
self.kodi_db.delete_album_from_discography(kodi_id)
if v.KODIVERSION < 18:
self.kodi_db.delete_album_from_album_genre(kodi_id)
query = 'DELETE FROM albuminfosong WHERE idAlbumInfo = ?'
self.kodicursor.execute(query, (kodi_id, ))
self.kodicursor.execute('DELETE FROM album_artist WHERE idAlbum = ?',
(kodi_id, ))
self.kodicursor.execute('DELETE FROM album WHERE idAlbum = ?',
(kodi_id, ))
2018-04-07 22:30:17 +10:00
self.artwork.delete_artwork(kodi_id, v.KODI_TYPE_ALBUM, self.kodicursor)
def _remove_artist(self, kodi_id):
'''
Remove an artist and associated songs and albums
'''
self.kodicursor.execute('DELETE FROM album_artist WHERE idArtist = ?',
(kodi_id, ))
self.kodicursor.execute('DELETE FROM artist WHERE idArtist = ?',
(kodi_id, ))
self.kodicursor.execute('DELETE FROM song_artist WHERE idArtist = ?',
(kodi_id, ))
self.kodicursor.execute('DELETE FROM discography WHERE idArtist = ?',
(kodi_id, ))
2018-04-07 22:30:17 +10:00
self.artwork.delete_artwork(kodi_id,
v.KODI_TYPE_ARTIST,
self.kodicursor)