2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2017-12-10 00:35:08 +11:00
|
|
|
from logging import getLogger
|
2015-12-25 06:51:47 +11:00
|
|
|
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
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
LOG = getLogger('PLEX.itemtypes')
|
2016-08-31 00:40:11 +10:00
|
|
|
|
2018-03-07 18:40:18 +11: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
|
|
|
###############################################################################
|
|
|
|
|
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
|
|
|
"""
|
2016-01-08 03:27:48 +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
|
2016-01-08 03:27:48 +11:00
|
|
|
|
|
|
|
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')
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plexcursor = self.plexconn.cursor()
|
2018-06-22 03:24:37 +10:00
|
|
|
self.kodiconn = utils.kodi_sql('video')
|
2016-01-08 03:27:48 +11:00
|
|
|
self.kodicursor = self.kodiconn.cursor()
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plex_db = plexdb.Plex_DB_Functions(self.plexcursor)
|
2018-02-25 23:42:20 +11:00
|
|
|
self.kodi_db = kodidb.KodiDBMethods(self.kodicursor)
|
2016-01-08 03:27:48 +11:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
"""
|
|
|
|
Make sure DB changes are committed and connection to DB is closed.
|
|
|
|
"""
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plexconn.commit()
|
2016-01-08 03:27:48 +11:00
|
|
|
self.kodiconn.commit()
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plexconn.close()
|
2016-01-08 03:27:48 +11:00
|
|
|
self.kodiconn.close()
|
|
|
|
return self
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-07-27 21:38:41 +10:00
|
|
|
def set_fanart(self, artworks, kodi_id, kodi_type):
|
2016-09-11 03:49:03 +10:00
|
|
|
"""
|
2018-07-27 21:38:41 +10:00
|
|
|
Writes artworks [dict containing only set artworks] to the Kodi art DB
|
2016-09-11 03:49:03 +10:00
|
|
|
"""
|
2018-07-27 21:38:41 +10:00
|
|
|
self.artwork.modify_artwork(artworks,
|
2018-03-04 23:39:18 +11:00
|
|
|
kodi_id,
|
|
|
|
kodi_type,
|
|
|
|
self.kodicursor)
|
2016-09-11 03:49:03 +10:00
|
|
|
|
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:
|
2018-05-21 02:54:31 +10:00
|
|
|
fileid = db_item[1]
|
2017-02-03 02:21:01 +11:00
|
|
|
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())
|
2017-02-03 02:21:01 +11:00
|
|
|
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
|
|
|
|
2018-02-06 03:48:50 +11:00
|
|
|
def updatePlaystate(self, mark_played, view_count, resume, duration,
|
2018-05-27 02:54:20 +10:00
|
|
|
file_id, lastViewedAt, plex_type):
|
2016-03-28 01:57:35 +11:00
|
|
|
"""
|
2018-05-21 02:54:31 +10: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
|
2018-05-27 02:54:20 +10:00
|
|
|
LOG.debug('Playstate file_id %s: viewcount: %s, resume: %s, type: %s',
|
|
|
|
file_id, view_count, resume, plex_type)
|
2018-02-06 03:48:50 +11:00
|
|
|
if mark_played:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.info('Marking as completely watched in Kodi')
|
2018-02-06 03:48:50 +11:00
|
|
|
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
|
|
|
|
2015-12-25 06:51:47 +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)
|
2015-12-25 06:51:47 +11:00
|
|
|
def add_update(self, item, viewtag=None, viewid=None):
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
|
|
|
Process single movie
|
|
|
|
"""
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor = self.kodicursor
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db = self.plex_db
|
2016-09-11 23:49:50 +10:00
|
|
|
artwork = self.artwork
|
2018-02-11 22:06:04 +11:00
|
|
|
api = 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
|
2018-02-12 00:42:49 +11:00
|
|
|
itemid = api.plex_id()
|
2016-01-03 23:36:00 +11:00
|
|
|
# Cannot parse XML, abort
|
|
|
|
if not itemid:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.error("Cannot parse XML data for movie")
|
2016-01-03 23:36:00 +11:00
|
|
|
return
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_dbitem = plex_db.getItem_byId(itemid)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
movieid = plex_dbitem[0]
|
2018-03-11 03:09:21 +11:00
|
|
|
old_fileid = plex_dbitem[1]
|
2017-01-05 06:57:16 +11:00
|
|
|
pathid = plex_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
|
2018-03-04 23:39:40 +11:00
|
|
|
kodicursor.execute("select coalesce(max(idMovie),0) from movie")
|
2015-12-25 06:51:47 +11:00
|
|
|
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
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.info("movieid: %s missing from Kodi, repairing the entry.",
|
|
|
|
movieid)
|
2016-02-14 15:42:16 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# fileId information
|
2018-02-12 00:42:49 +11:00
|
|
|
checksum = api.checksum()
|
|
|
|
dateadded = api.date_created()
|
|
|
|
userdata = api.userdata()
|
2015-12-25 06:51:47 +11:00
|
|
|
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
|
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()
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
|
|
|
studio = studios[0]
|
|
|
|
except IndexError:
|
|
|
|
studio = None
|
2018-08-04 23:11:21 +10:00
|
|
|
trailer = api.trailers()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-16 06:26:45 +11:00
|
|
|
# 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:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Direct paths is set the Kodi way
|
2018-02-12 00:42:49 +11:00
|
|
|
playurl = api.file_path(force_first_media=True)
|
2016-03-16 06:26:45 +11:00
|
|
|
if playurl is None:
|
|
|
|
# Something went wrong, trying to use non-direct paths
|
2018-02-11 22:06:04 +11:00
|
|
|
do_indirect = True
|
2016-03-16 06:26:45 +11:00
|
|
|
else:
|
2018-02-12 00:42:49 +11:00
|
|
|
playurl = api.validate_playurl(playurl, api.plex_type())
|
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
|
|
|
|
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:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Set plugin path and media flags using real filename
|
2018-03-27 17:01:24 +11:00
|
|
|
filename = api.file_name(force_first_media=True)
|
2018-02-22 06:24:31 +11:00
|
|
|
path = 'plugin://%s.movies/' % v.ADDON_ID
|
2018-03-19 00:50:37 +11:00
|
|
|
filename = ('%s?plex_id=%s&plex_type=%s&mode=play&filename=%s'
|
|
|
|
% (path, itemid, v.PLEX_TYPE_MOVIE, filename))
|
2016-04-23 19:35:37 +10:00
|
|
|
playurl = filename
|
2018-03-11 03:09:21 +11:00
|
|
|
pathid = self.kodi_db.get_path(path)
|
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
|
|
|
|
2016-10-25 08:37:29 +11:00
|
|
|
# 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)
|
2016-10-25 08:37:29 +11:00
|
|
|
|
2016-03-16 06:26:45 +11:00
|
|
|
# UPDATE THE MOVIE #####
|
2015-12-25 06:51:47 +11:00
|
|
|
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:
|
2018-03-11 21:47:04 +11:00
|
|
|
LOG.debug('Removing old file entry: %s', old_fileid)
|
2018-03-11 03:09:21 +11:00
|
|
|
self.kodi_db.remove_file(old_fileid)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update the movie entry
|
2017-01-25 02:54:51 +11:00
|
|
|
if v.KODIVERSION >= 17:
|
2017-01-10 06:33:52 +11:00
|
|
|
# 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)
|
2017-01-10 06:33:52 +11:00
|
|
|
self.kodi_db.update_ratings(movieid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_MOVIE,
|
2017-01-10 06:33:52 +11:00
|
|
|
"default",
|
|
|
|
rating,
|
|
|
|
votecount,
|
2017-02-20 01:57:31 +11:00
|
|
|
rating_id)
|
2017-01-10 06:33:52 +11:00
|
|
|
# update new uniqueid Kodi 17
|
2017-05-01 18:32:04 +10:00
|
|
|
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:
|
2018-02-26 21:20:11 +11:00
|
|
|
self.kodi_db.remove_uniqueid(movieid, v.KODI_TYPE_MOVIE)
|
2017-05-01 18:32:04 +10:00
|
|
|
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 = ?
|
|
|
|
'''
|
2016-10-19 05:47:45 +11:00
|
|
|
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))
|
2016-10-19 05:47:45 +11:00
|
|
|
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 = ?
|
|
|
|
'''
|
2016-10-19 05:47:45 +11:00
|
|
|
kodicursor.execute(query, (title, plot, shortplot, tagline,
|
|
|
|
votecount, rating, writer, year, imdb, sorttitle, runtime,
|
|
|
|
mpaa, genre, director, title, studio, trailer, country,
|
2016-10-25 08:37:29 +11:00
|
|
|
playurl, pathid, fileid, movieid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-02-03 01:23:54 +11:00
|
|
|
# OR ADD THE MOVIE #####
|
2015-12-25 06:51:47 +11:00
|
|
|
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:
|
2017-01-10 06:33:52 +11:00
|
|
|
# add new ratings Kodi 17
|
2018-02-26 20:58:27 +11:00
|
|
|
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,
|
2017-01-10 06:33:52 +11:00
|
|
|
movieid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_MOVIE,
|
2017-01-10 06:33:52 +11:00
|
|
|
"default",
|
|
|
|
rating,
|
|
|
|
votecount)
|
|
|
|
# add new uniqueid Kodi 17
|
2017-05-01 18:32:04 +10:00
|
|
|
if imdb is not None:
|
2018-02-26 20:58:27 +11:00
|
|
|
uniqueid = self.kodi_db.get_uniqueid(movieid,
|
|
|
|
v.KODI_TYPE_MOVIE)
|
2017-05-01 18:32:04 +10:00
|
|
|
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,
|
2016-11-13 03:22:45 +11:00
|
|
|
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)
|
2016-11-13 03:22:45 +11:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
|
2017-02-03 01:23:54 +11:00
|
|
|
?, ?, ?, ?, ?, ?, ?)
|
|
|
|
'''
|
2016-11-13 03:22:45 +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']))
|
2016-11-13 03:22:45 +11:00
|
|
|
else:
|
2017-02-03 01:23:54 +11:00
|
|
|
query = '''
|
|
|
|
INSERT INTO movie(idMovie, idFile, c00, c01, c02, c03,
|
2016-11-13 03:22:45 +11:00
|
|
|
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
|
|
|
?, ?, ?, ?, ?)
|
|
|
|
'''
|
2016-11-13 03:22:45 +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))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# 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
|
2017-01-08 06:11:48 +11:00
|
|
|
plex_db.addReference(itemid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.PLEX_TYPE_MOVIE,
|
2017-01-08 06:11:48 +11:00
|
|
|
movieid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_MOVIE,
|
2017-01-08 06:11:48 +11:00
|
|
|
kodi_fileid=fileid,
|
|
|
|
kodi_pathid=pathid,
|
|
|
|
parent_id=None,
|
|
|
|
checksum=checksum,
|
|
|
|
view_id=viewid)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process countries
|
2018-02-28 07:14:42 +11:00
|
|
|
self.kodi_db.modify_countries(movieid, v.KODI_TYPE_MOVIE, countries)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process cast
|
2018-03-01 03:24:32 +11:00
|
|
|
self.kodi_db.modify_people(movieid,
|
|
|
|
v.KODI_TYPE_MOVIE,
|
|
|
|
api.people_list())
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process genres
|
2018-02-28 07:14:42 +11:00
|
|
|
self.kodi_db.modify_genres(movieid, v.KODI_TYPE_MOVIE, genres)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process artwork
|
2018-03-04 23:39:18 +11:00
|
|
|
artwork.modify_artwork(api.artwork(),
|
|
|
|
movieid,
|
|
|
|
v.KODI_TYPE_MOVIE,
|
|
|
|
kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process stream details
|
2018-02-26 19:18:44 +11:00
|
|
|
self.kodi_db.modify_streams(fileid, api.mediastreams(), runtime)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process studios
|
2018-02-28 07:14:42 +11:00
|
|
|
self.kodi_db.modify_studios(movieid, v.KODI_TYPE_MOVIE, studios)
|
2016-01-12 01:56:56 +11:00
|
|
|
tags = [viewtag]
|
2016-03-02 02:18:12 +11:00
|
|
|
if userdata['Favorite']:
|
|
|
|
tags.append("Favorite movies")
|
2018-07-05 20:46:40 +10:00
|
|
|
if collections:
|
|
|
|
collections_match = api.collections_match()
|
|
|
|
for plex_set_id, set_name in collections:
|
|
|
|
tags.append(set_name)
|
|
|
|
# Add any sets from Plex collection tags
|
|
|
|
kodi_set_id = self.kodi_db.create_collection(set_name)
|
|
|
|
self.kodi_db.assign_collection(kodi_set_id, movieid)
|
|
|
|
for index, plex_id in collections_match:
|
|
|
|
# Get Plex artwork for collections - a pain
|
|
|
|
if index == plex_set_id:
|
|
|
|
set_xml = PF.GetPlexMetadata(plex_id)
|
|
|
|
try:
|
|
|
|
set_xml.attrib
|
|
|
|
except AttributeError:
|
|
|
|
LOG.error('Could not get set metadata %s', plex_id)
|
|
|
|
continue
|
|
|
|
set_api = API(set_xml[0])
|
|
|
|
artwork.modify_artwork(set_api.artwork(),
|
|
|
|
kodi_set_id,
|
|
|
|
v.KODI_TYPE_SET,
|
|
|
|
kodicursor)
|
|
|
|
break
|
2018-02-28 07:14:42 +11:00
|
|
|
self.kodi_db.modify_tags(movieid, v.KODI_TYPE_MOVIE, tags)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process playstates
|
2018-06-15 23:15:35 +10:00
|
|
|
self.kodi_db.set_resume(fileid,
|
|
|
|
resume,
|
|
|
|
runtime,
|
|
|
|
playcount,
|
|
|
|
dateplayed,
|
|
|
|
v.PLEX_TYPE_MOVIE)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-03-11 22:08:27 +11:00
|
|
|
def remove(self, plex_id):
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
2018-03-11 22:08:27 +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
|
|
|
"""
|
2018-03-11 22:08:27 +11:00
|
|
|
plex_dbitem = self.plex_db.getItem_byId(plex_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2017-02-03 02:53:50 +11:00
|
|
|
kodi_id = plex_dbitem[0]
|
|
|
|
file_id = plex_dbitem[1]
|
|
|
|
kodi_type = plex_dbitem[4]
|
2018-03-01 03:24:32 +11:00
|
|
|
LOG.debug('Removing %sid: %s file_id: %s',
|
|
|
|
kodi_type, kodi_id, file_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2018-03-11 22:08:27 +11:00
|
|
|
LOG.error('Movie with plex_id %s not found in DB - cannot delete',
|
|
|
|
plex_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
return
|
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# Remove the plex reference
|
2018-03-11 22:08:27 +11:00
|
|
|
self.plex_db.removeItem(plex_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Remove artwork
|
2018-03-11 22:08:27 +11:00
|
|
|
self.artwork.delete_artwork(kodi_id, kodi_type, self.kodicursor)
|
2017-02-03 02:53:50 +11:00
|
|
|
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)
|
2018-03-01 03:24:32 +11:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Delete kodi movie and file
|
2018-03-11 22:00:28 +11:00
|
|
|
self.kodi_db.remove_file(file_id)
|
2018-03-11 22:08:27 +11:00
|
|
|
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)
|
2017-02-03 02:53:50 +11:00
|
|
|
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)
|
2017-02-03 02:53:50 +11:00
|
|
|
elif kodi_type == v.KODI_TYPE_SET:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Delete kodi boxset
|
2018-03-11 22:08:27 +11:00
|
|
|
boxset_movies = self.plex_db.getItem_byParentId(kodi_id,
|
|
|
|
v.KODI_TYPE_MOVIE)
|
2015-12-25 06:51:47 +11:00
|
|
|
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]
|
2018-03-11 22:08:59 +11:00
|
|
|
self.kodi_db.remove_from_set(movieid)
|
2017-01-05 06:57:16 +11:00
|
|
|
# Update plex reference
|
2018-03-11 22:08:27 +11:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
def add_update(self, item, viewtag=None, viewid=None):
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
|
|
|
Process a single show
|
|
|
|
"""
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor = self.kodicursor
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db = self.plex_db
|
2016-09-11 23:49:50 +10:00
|
|
|
artwork = self.artwork
|
2018-02-11 22:06:04 +11:00
|
|
|
api = API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
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
|
2015-12-25 06:51:47 +11:00
|
|
|
update_item = True
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_dbitem = plex_db.getItem_byId(itemid)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
showid = plex_dbitem[0]
|
|
|
|
pathid = plex_dbitem[2]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
update_item = False
|
2018-03-04 23:39:40 +11:00
|
|
|
kodicursor.execute("select coalesce(max(idShow),0) from tvshow")
|
2016-02-14 15:42:16 +11:00
|
|
|
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)
|
2016-02-14 15:42:16 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# fileId information
|
2018-02-12 00:42:49 +11:00
|
|
|
checksum = api.checksum()
|
2015-12-25 06:51:47 +11:00
|
|
|
# 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
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# GET THE FILE AND PATH #####
|
2017-05-17 23:42:12 +10:00
|
|
|
if state.DIRECT_PATHS:
|
2015-12-25 06:51:47 +11:00
|
|
|
# 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)
|
2016-03-16 06:26:45 +11:00
|
|
|
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))
|
2015-12-25 06:51:47 +11:00
|
|
|
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:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Set plugin path
|
2018-02-22 06:24:31 +11:00
|
|
|
toplevelpath = "plugin://%s.tvshows/" % v.ADDON_ID
|
2015-12-25 06:51:47 +11:00
|
|
|
path = "%s%s/" % (toplevelpath, itemid)
|
2018-05-27 02:54:20 +10:00
|
|
|
# Do NOT set a parent id because addon-path cannot be "stacked"
|
|
|
|
toppathid = None
|
2016-03-16 06:26:45 +11:00
|
|
|
|
2018-05-25 03:36:14 +10:00
|
|
|
pathid = self.kodi_db.add_video_path(path,
|
2018-05-27 01:28:17 +10:00
|
|
|
date_added=api.date_created(),
|
2018-05-25 03:36:14 +10:00
|
|
|
id_parent_path=toppathid)
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE TVSHOW #####
|
2015-12-25 06:51:47 +11:00
|
|
|
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:
|
2017-01-10 06:47:32 +11:00
|
|
|
# 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)
|
2017-01-10 06:47:32 +11:00
|
|
|
self.kodi_db.update_ratings(showid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_SHOW,
|
2017-01-10 06:47:32 +11:00
|
|
|
"default",
|
|
|
|
rating,
|
2017-02-19 02:30:06 +11:00
|
|
|
votecount,
|
|
|
|
rating_id)
|
2017-01-10 06:47:32 +11:00
|
|
|
# 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:
|
2018-02-26 21:22:18 +11:00
|
|
|
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,
|
2017-05-01 17:53:40 +10:00
|
|
|
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))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
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
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db.addReference(itemid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.PLEX_TYPE_SHOW,
|
2016-10-27 05:53:20 +11:00
|
|
|
showid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_SHOW,
|
2017-01-08 06:11:48 +11:00
|
|
|
kodi_pathid=pathid,
|
2016-10-27 05:53:20 +11:00
|
|
|
checksum=checksum,
|
2017-01-08 06:11:48 +11:00
|
|
|
view_id=viewid)
|
2017-01-25 02:54:51 +11:00
|
|
|
if v.KODIVERSION >= 17:
|
2017-01-10 06:47:32 +11:00
|
|
|
# add new ratings Kodi 17
|
2018-02-26 20:58:27 +11:00
|
|
|
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,
|
2017-01-10 06:47:32 +11:00
|
|
|
showid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_SHOW,
|
2017-01-10 06:47:32 +11:00
|
|
|
"default",
|
|
|
|
rating,
|
2017-02-19 02:30:06 +11:00
|
|
|
votecount)
|
2017-01-10 06:47:32 +11:00
|
|
|
# add new uniqueid Kodi 17
|
2017-05-01 18:08:23 +10:00
|
|
|
if tvdb is not None:
|
2018-02-26 20:58:27 +11:00
|
|
|
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2015-12-25 06:51:47 +11:00
|
|
|
'''
|
2017-02-19 02:30:06 +11:00
|
|
|
kodicursor.execute(query, (showid, title, plot, rating_id,
|
2017-05-01 17:57:24 +10:00
|
|
|
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2015-12-25 06:51:47 +11:00
|
|
|
'''
|
2017-02-19 02:30:06 +11:00
|
|
|
kodicursor.execute(query, (showid, title, plot, rating,
|
|
|
|
premieredate, genre, title, tvdb,
|
|
|
|
mpaa, studio, sorttitle))
|
|
|
|
|
2018-03-01 03:24:32 +11:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Process studios
|
2018-03-01 03:24:32 +11:00
|
|
|
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]
|
2018-07-05 20:46:40 +10:00
|
|
|
tags.extend([i for _, i in collections])
|
2018-03-01 03:24:32 +11:00
|
|
|
self.kodi_db.modify_tags(showid, v.KODI_TYPE_SHOW, tags)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
@utils.catch_exceptions(warnuser=True)
|
2016-03-10 04:36:36 +11:00
|
|
|
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()
|
2017-01-08 06:11:48 +11:00
|
|
|
if not plex_id:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.error('Error getting plex_id for season, skipping')
|
2016-02-13 02:53:49 +11:00
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor = self.kodicursor
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db = self.plex_db
|
2016-09-11 23:49:50 +10:00
|
|
|
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
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_dbitem = plex_db.getItem_byId(plexshowid)
|
2016-03-14 02:06:54 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
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
|
2017-01-08 06:11:48 +11:00
|
|
|
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:
|
2017-01-05 06:57:16 +11:00
|
|
|
# Update a reference: checksum in plex table
|
2017-01-08 06:11:48 +11:00
|
|
|
plex_db.updateReference(plex_id, checksum)
|
2016-01-12 03:37:01 +11:00
|
|
|
else:
|
2017-01-05 06:57:16 +11:00
|
|
|
# Create the reference in plex table
|
2017-01-08 06:11:48 +11:00
|
|
|
plex_db.addReference(plex_id,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.PLEX_TYPE_SEASON,
|
2016-08-31 00:40:11 +10:00
|
|
|
seasonid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_SEASON,
|
2017-01-08 06:11:48 +11:00
|
|
|
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
|
|
|
"""
|
2015-12-25 06:51:47 +11:00
|
|
|
kodicursor = self.kodicursor
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db = self.plex_db
|
2016-09-11 23:49:50 +10:00
|
|
|
artwork = self.artwork
|
2018-02-11 22:06:04 +11:00
|
|
|
api = API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
update_item = True
|
2018-02-12 00:42:49 +11:00
|
|
|
itemid = api.plex_id()
|
2016-02-13 02:53:49 +11:00
|
|
|
if not itemid:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.error('Error getting itemid for episode, skipping')
|
2016-02-13 02:53:49 +11:00
|
|
|
return
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_dbitem = plex_db.getItem_byId(itemid)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
episodeid = plex_dbitem[0]
|
2018-03-11 03:09:21 +11:00
|
|
|
old_fileid = plex_dbitem[1]
|
2017-01-05 06:57:16 +11:00
|
|
|
pathid = plex_dbitem[2]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
update_item = False
|
|
|
|
# episodeid
|
2018-03-11 01:44:08 +11:00
|
|
|
kodicursor.execute('SELECT COALESCE(MAX(idEpisode),0) FROM episode')
|
2015-12-25 06:51:47 +11:00
|
|
|
episodeid = kodicursor.fetchone()[0] + 1
|
2016-02-14 15:42:16 +11:00
|
|
|
else:
|
|
|
|
# Verification the item is still in Kodi
|
2018-03-11 01:44:08 +11:00
|
|
|
query = 'SELECT * FROM episode WHERE idEpisode = ?'
|
2016-02-14 15:42:16 +11:00
|
|
|
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)
|
2016-02-14 15:42:16 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# fileId information
|
2018-02-12 00:42:49 +11:00
|
|
|
checksum = api.checksum()
|
|
|
|
dateadded = api.date_created()
|
|
|
|
userdata = api.userdata()
|
2015-12-25 06:51:47 +11:00
|
|
|
playcount = userdata['PlayCount']
|
|
|
|
dateplayed = userdata['LastPlayedDate']
|
2018-02-12 00:42:49 +11:00
|
|
|
tvdb = api.provider('tvdb')
|
2017-02-14 06:19:22 +11:00
|
|
|
votecount = None
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# 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()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# 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
|
|
|
|
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
|
2018-02-11 22:06:04 +11:00
|
|
|
airs_before_season = "-1"
|
|
|
|
airs_before_episode = "-1"
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Get season id
|
2018-02-11 22:06:04 +11:00
|
|
|
show = plex_db.getItem_byId(series_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# GET THE FILE AND PATH #####
|
2018-09-03 04:34:44 +10:00
|
|
|
do_indirect = not state.DIRECT_PATHS
|
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)
|
2018-09-03 04:34:44 +10:00
|
|
|
if playurl is None:
|
|
|
|
do_indirect = True
|
2016-03-16 06:26:45 +11:00
|
|
|
else:
|
2018-09-03 04:34:44 +10:00
|
|
|
playurl = api.validate_playurl(playurl, v.PLEX_TYPE_EPISODE)
|
|
|
|
if "\\" in playurl:
|
|
|
|
# Local path
|
|
|
|
filename = playurl.rsplit("\\", 1)[1]
|
|
|
|
else:
|
|
|
|
# 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)
|
|
|
|
if do_indirect:
|
2018-05-21 02:54:31 +10:00
|
|
|
# Set plugin path - do NOT use "intermediate" paths for the show
|
|
|
|
# as with direct paths!
|
2018-03-27 17:01:24 +11:00
|
|
|
filename = api.file_name(force_first_media=True)
|
2018-05-25 03:36:14 +10:00
|
|
|
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))
|
2016-04-23 19:48:21 +10:00
|
|
|
playurl = filename
|
2018-03-18 23:48:29 +11:00
|
|
|
# Root path tvshows/ already saved in Kodi DB
|
|
|
|
pathid = self.kodi_db.add_video_path(path)
|
2016-04-23 19:48:21 +10:00
|
|
|
|
2016-10-27 06:04:46 +11:00
|
|
|
# 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)
|
2016-10-27 06:04:46 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE EPISODE #####
|
2015-12-25 06:51:47 +11:00
|
|
|
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:
|
2018-05-21 02:54:31 +10:00
|
|
|
LOG.debug('Removing old file entry: %s', old_fileid)
|
2018-03-11 03:09:21 +11:00
|
|
|
self.kodi_db.remove_file(old_fileid)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update the movie entry
|
2017-02-03 01:23:54 +11:00
|
|
|
if v.KODIVERSION >= 17:
|
2017-02-14 06:19:22 +11:00
|
|
|
# update new ratings Kodi 17
|
2017-02-14 06:23:02 +11:00
|
|
|
ratingid = self.kodi_db.get_ratingid(episodeid,
|
|
|
|
v.KODI_TYPE_EPISODE)
|
2017-02-14 06:19:22 +11:00
|
|
|
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)
|
2017-02-14 06:19:22 +11:00
|
|
|
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 = ?,
|
2018-05-25 03:36:14 +10:00
|
|
|
c10 = ?, c12 = ?, c13 = ?, c14 = ?, c15 = ?, c16 = ?,
|
2018-05-27 01:28:17 +10:00
|
|
|
c18 = ?, c19 = ?, idFile=?, idSeason = ?,
|
2018-05-25 03:36:14 +10:00
|
|
|
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,
|
2018-05-27 01:28:17 +10:00
|
|
|
airs_before_season, airs_before_episode, playurl, pathid,
|
2018-05-25 03:36:14 +10:00
|
|
|
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 = ?
|
|
|
|
'''
|
2016-10-27 06:04:46 +11:00
|
|
|
kodicursor.execute(query, (title, plot, rating, writer,
|
|
|
|
premieredate, runtime, director, season, episode, title,
|
2018-05-27 01:28:17 +10:00
|
|
|
airs_before_season, airs_before_episode, playurl, pathid,
|
2016-10-27 06:04:46 +11:00
|
|
|
fileid, seasonid, episodeid))
|
2015-12-25 06:51:47 +11:00
|
|
|
# Update parentid reference
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db.updateParentId(itemid, seasonid)
|
2017-02-03 01:23:54 +11:00
|
|
|
|
|
|
|
# OR ADD THE EPISODE #####
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.info("ADD episode itemid: %s - Title: %s", itemid, title)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Create the episode entry
|
2017-02-03 01:23:54 +11:00
|
|
|
if v.KODIVERSION >= 17:
|
2017-02-14 06:19:22 +11:00
|
|
|
# add new ratings Kodi 17
|
2018-02-26 20:58:27 +11:00
|
|
|
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,
|
2017-02-14 06:19:22 +11:00
|
|
|
episodeid,
|
|
|
|
v.KODI_TYPE_EPISODE,
|
|
|
|
"default",
|
|
|
|
rating,
|
|
|
|
votecount)
|
|
|
|
# add new uniqueid Kodi 17
|
2018-02-26 20:58:27 +11:00
|
|
|
uniqueid = self.kodi_db.get_uniqueid(episodeid,
|
|
|
|
v.KODI_TYPE_EPISODE)
|
|
|
|
self.kodi_db.add_uniqueid(uniqueid,
|
2017-02-14 06:19:22 +11:00
|
|
|
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,
|
2018-05-27 01:28:17 +10:00
|
|
|
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,
|
2018-05-27 01:28:17 +10:00
|
|
|
rating_id, writer, premieredate, runtime, director, season,
|
|
|
|
episode, title, showid, airs_before_season,
|
|
|
|
airs_before_episode, playurl, pathid, seasonid,
|
|
|
|
userdata['UserRating']))
|
2018-03-12 01:23:32 +11:00
|
|
|
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
|
|
|
|
?)
|
2015-12-25 06:51:47 +11:00
|
|
|
'''
|
2017-02-03 01:23:54 +11:00
|
|
|
kodicursor.execute(query, (episodeid, fileid, title, plot,
|
|
|
|
rating, writer, premieredate, runtime, director, season,
|
2018-05-27 01:28:17 +10:00
|
|
|
episode, title, showid, airs_before_season,
|
|
|
|
airs_before_episode, playurl, pathid, seasonid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# Create or update the reference in plex table Add reference is
|
2016-10-27 06:04:46 +11:00
|
|
|
# idempotent; the call here updates also fileid and pathid when item is
|
|
|
|
# moved or renamed
|
2017-01-08 06:11:48 +11:00
|
|
|
plex_db.addReference(itemid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.PLEX_TYPE_EPISODE,
|
2017-01-08 06:11:48 +11:00
|
|
|
episodeid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_EPISODE,
|
2017-01-08 06:11:48 +11:00
|
|
|
kodi_fileid=fileid,
|
|
|
|
kodi_pathid=pathid,
|
|
|
|
parent_id=seasonid,
|
|
|
|
checksum=checksum,
|
|
|
|
view_id=viewid)
|
2018-03-01 03:24:32 +11:00
|
|
|
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
|
2018-05-27 02:54:20 +10:00
|
|
|
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
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
@utils.catch_exceptions(warnuser=True)
|
2018-03-12 01:23:32 +11:00
|
|
|
def remove(self, plex_id):
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
2018-03-12 01:23:32 +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
|
|
|
"""
|
2018-03-12 01:23:32 +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)
|
2015-12-25 06:51:47 +11:00
|
|
|
return
|
2018-03-12 01:23:32 +11:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# Remove the plex reference
|
2018-03-12 01:23:32 +11:00
|
|
|
self.plex_db.removeItem(plex_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-03-12 01:23:32 +11:00
|
|
|
##### EPISODE #####
|
|
|
|
if kodi_type == v.KODI_TYPE_EPISODE:
|
|
|
|
# Delete episode, verify season and tvshow
|
|
|
|
self.remove_episode(kodi_id, file_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Season verification
|
2018-03-12 01:23:32 +11:00
|
|
|
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:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Remove episodes, season, verify tvshow
|
2018-03-12 01:23:32 +11:00
|
|
|
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])
|
2015-12-25 06:51:47 +11:00
|
|
|
# Remove season
|
2018-03-12 01:23:32 +11:00
|
|
|
self.remove_season(kodi_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Show verification
|
2018-03-12 01:23:32 +11:00
|
|
|
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
|
|
|
|
"""
|
2018-03-01 03:24:32 +11:00
|
|
|
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)
|
2018-03-12 01:23:32 +11:00
|
|
|
self.artwork.delete_artwork(kodi_id,
|
|
|
|
v.KODI_TYPE_SHOW,
|
|
|
|
self.kodicursor)
|
|
|
|
self.kodicursor.execute("DELETE FROM tvshow WHERE idShow = ?",
|
|
|
|
(kodi_id,))
|
2017-02-03 02:53:50 +11:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-03-12 01:23:32 +11:00
|
|
|
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
|
|
|
|
"""
|
2018-03-12 01:23:32 +11:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-03-12 01:23:32 +11:00
|
|
|
def remove_episode(self, kodi_id, file_id):
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
2018-03-12 01:23:32 +11:00
|
|
|
Remove an episode, and episode only from the Kodi DB (not Plex DB)
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
2018-03-01 03:24:32 +11:00
|
|
|
self.kodi_db.modify_people(kodi_id, v.KODI_TYPE_EPISODE)
|
2018-05-27 02:54:20 +10:00
|
|
|
self.kodi_db.remove_file(file_id, plex_type=v.PLEX_TYPE_EPISODE)
|
2018-03-12 01:23:32 +11:00
|
|
|
self.artwork.delete_artwork(kodi_id,
|
|
|
|
v.KODI_TYPE_EPISODE,
|
|
|
|
self.kodicursor)
|
|
|
|
self.kodicursor.execute("DELETE FROM episode WHERE idEpisode = ?",
|
|
|
|
(kodi_id,))
|
2017-02-14 06:19:22 +11:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
class Music(Items):
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
|
|
|
For Plex library-type music. Also works for premium music libraries
|
|
|
|
"""
|
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
|
|
|
|
"""
|
2018-06-22 03:24:37 +10:00
|
|
|
self.plexconn = utils.kodi_sql('plex')
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plexcursor = self.plexconn.cursor()
|
2016-02-13 02:53:49 +11:00
|
|
|
# Here it is, not 'video' but 'music'
|
2018-06-22 03:24:37 +10:00
|
|
|
self.kodiconn = utils.kodi_sql('music')
|
2016-02-13 02:53:49 +11:00
|
|
|
self.kodicursor = self.kodiconn.cursor()
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plex_db = plexdb.Plex_DB_Functions(self.plexcursor)
|
2018-02-25 23:42:20 +11:00
|
|
|
self.kodi_db = kodidb.KodiDBMethods(self.kodicursor)
|
2016-02-13 02:53:49 +11:00
|
|
|
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
|
|
|
|
"""
|
2016-01-09 22:13:17 +11:00
|
|
|
kodicursor = self.kodicursor
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db = self.plex_db
|
2016-09-11 23:49:50 +10:00
|
|
|
artwork = self.artwork
|
2018-02-11 22:06:04 +11:00
|
|
|
api = API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
update_item = True
|
2018-02-12 00:42:49 +11:00
|
|
|
itemid = api.plex_id()
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_dbitem = plex_db.getItem_byId(itemid)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
artistid = plex_dbitem[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
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')
|
2018-02-12 00:42:49 +11:00
|
|
|
dateadded = api.date_created()
|
|
|
|
checksum = api.checksum()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-02-12 00:42:49 +11:00
|
|
|
name, _ = api.titles()
|
|
|
|
# musicBrainzId = api.provider('MusicBrainzArtist')
|
2016-02-13 02:53:49 +11:00
|
|
|
musicBrainzId = None
|
2018-02-12 00:42:49 +11:00
|
|
|
genres = ' / '.join(api.genre_list())
|
|
|
|
bio = api.plot()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Associate artwork
|
2018-03-04 00:40:12 +11:00
|
|
|
artworks = api.artwork()
|
|
|
|
if 'poster' in artworks:
|
|
|
|
thumb = "<thumb>%s</thumb>" % artworks['poster']
|
2015-12-25 06:51:47 +11:00
|
|
|
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
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE ARTIST #####
|
2015-12-25 06:51:47 +11:00
|
|
|
if update_item:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.info("UPDATE artist itemid: %s - Name: %s", itemid, name)
|
2017-01-05 06:57:16 +11:00
|
|
|
# Update the checksum in plex table
|
|
|
|
plex_db.updateReference(itemid, checksum)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# OR ADD THE ARTIST #####
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.info("ADD artist itemid: %s - Name: %s", itemid, name)
|
2017-01-05 06:57:16 +11:00
|
|
|
# safety checks: It looks like plex supports the same artist
|
2016-02-13 02:53:49 +11:00
|
|
|
# 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)
|
2017-01-05 06:57:16 +11:00
|
|
|
# Create the reference in plex table
|
2017-01-08 06:11:48 +11:00
|
|
|
plex_db.addReference(itemid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.PLEX_TYPE_ARTIST,
|
2017-01-08 06:11:48 +11:00
|
|
|
artistid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_ARTIST,
|
2017-02-01 22:33:37 +11:00
|
|
|
view_id=viewid,
|
2017-01-08 06:11:48 +11:00
|
|
|
checksum=checksum)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# 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 = ?
|
|
|
|
'''
|
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:
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
|
|
|
UPDATE artist
|
|
|
|
SET strGenres = ?, strBiography = ?, strImage = ?,
|
|
|
|
strFanart = ?, lastScraped = ?, dateAdded = ?
|
|
|
|
WHERE idArtist = ?
|
|
|
|
'''
|
2015-12-25 06:51:47 +11:00
|
|
|
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
|
2018-03-04 23:39:18 +11:00
|
|
|
artwork.modify_artwork(artworks,
|
|
|
|
artistid,
|
|
|
|
v.KODI_TYPE_ARTIST,
|
|
|
|
kodicursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
@utils.catch_exceptions(warnuser=True)
|
2018-02-06 04:20:33 +11:00
|
|
|
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
|
2018-04-26 16:01:27 +10:00
|
|
|
scan_children: set to False if you don't want to add children
|
2017-04-03 01:02:41 +10:00
|
|
|
"""
|
2016-01-09 22:13:17 +11:00
|
|
|
kodicursor = self.kodicursor
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db = self.plex_db
|
2016-09-11 23:49:50 +10:00
|
|
|
artwork = self.artwork
|
2018-02-11 22:06:04 +11:00
|
|
|
api = API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
update_item = True
|
2018-04-26 16:01:27 +10:00
|
|
|
plex_id = api.plex_id()
|
|
|
|
if not plex_id:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.error('Error processing Album, skipping')
|
2016-02-13 02:53:49 +11:00
|
|
|
return
|
2018-04-26 16:01:27 +10:00
|
|
|
plex_dbitem = plex_db.getItem_byId(plex_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2018-04-26 16:01:27 +10:00
|
|
|
album_id = plex_dbitem[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
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')
|
2018-02-12 00:42:49 +11:00
|
|
|
userdata = api.userdata()
|
|
|
|
checksum = api.checksum()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-02-12 00:42:49 +11:00
|
|
|
name, _ = api.titles()
|
|
|
|
# musicBrainzId = api.provider('MusicBrainzAlbum')
|
2016-02-13 02:53:49 +11:00
|
|
|
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()
|
2016-01-12 02:53:41 +11:00
|
|
|
rating = userdata['UserRating']
|
2018-02-12 00:42:49 +11:00
|
|
|
studio = api.music_studio()
|
2016-02-13 02:53:49 +11:00
|
|
|
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
|
2015-12-25 06:51:47 +11:00
|
|
|
# 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
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE ALBUM #####
|
2015-12-25 06:51:47 +11:00
|
|
|
if update_item:
|
2018-04-26 16:01:27 +10:00
|
|
|
LOG.info("UPDATE album plex_id: %s - Name: %s", plex_id, name)
|
2017-01-05 06:57:16 +11:00
|
|
|
# Update the checksum in plex table
|
2018-04-26 16:01:27 +10:00
|
|
|
plex_db.updateReference(plex_id, checksum)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# OR ADD THE ALBUM #####
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
2018-04-26 16:01:27 +10:00
|
|
|
LOG.info("ADD album plex_id: %s - Name: %s", plex_id, name)
|
2017-01-05 06:57:16 +11:00
|
|
|
# safety checks: It looks like plex supports the same artist
|
2016-02-13 02:53:49 +11:00
|
|
|
# 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)
|
2017-01-05 06:57:16 +11:00
|
|
|
# Create the reference in plex table
|
2018-04-26 16:01:27 +10:00
|
|
|
plex_db.addReference(plex_id,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.PLEX_TYPE_ALBUM,
|
2018-04-26 16:01:27 +10:00
|
|
|
album_id,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_ALBUM,
|
2017-02-01 22:33:37 +11:00
|
|
|
view_id=viewid,
|
2017-01-08 06:11:48 +11:00
|
|
|
checksum=checksum)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Process the album info
|
2018-04-05 16:06:48 +10:00
|
|
|
if v.KODIVERSION >= 18:
|
|
|
|
# Kodi Leia
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
|
|
|
UPDATE album
|
2018-04-05 16:06:48 +10:00
|
|
|
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,
|
2018-04-26 16:01:27 +10:00
|
|
|
self.compilation, album_id))
|
2018-04-05 16:06:48 +10:00
|
|
|
elif v.KODIVERSION == 17:
|
|
|
|
# Kodi Krypton
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
|
|
|
UPDATE album
|
|
|
|
SET strArtists = ?, iYear = ?, strGenres = ?, strReview = ?,
|
2018-04-05 16:06:48 +10:00
|
|
|
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,
|
2018-04-26 16:01:27 +10:00
|
|
|
self.compilation, album_id))
|
2018-04-05 16:06:48 +10:00
|
|
|
elif v.KODIVERSION == 16:
|
|
|
|
# Kodi Jarvis
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
|
|
|
UPDATE album
|
|
|
|
SET strArtists = ?, iYear = ?, strGenres = ?, strReview = ?,
|
2018-04-05 16:06:48 +10:00
|
|
|
strImage = ?, iRating = ?, lastScraped = ?,
|
|
|
|
strReleaseType = ?, strLabel = ?, bCompilation = ?
|
2017-04-03 01:02:41 +10:00
|
|
|
WHERE idAlbum = ?
|
|
|
|
'''
|
|
|
|
kodicursor.execute(query, (artistname, year, self.genre, bio,
|
2018-04-05 16:06:48 +10:00
|
|
|
thumb, rating, lastScraped,
|
|
|
|
v.KODI_TYPE_ALBUM, studio,
|
2018-04-26 16:01:27 +10:00
|
|
|
self.compilation, album_id))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# Associate the parentid for plex reference
|
2018-03-11 00:53:39 +11:00
|
|
|
parent_id = api.parent_plex_id()
|
2018-04-26 16:01:27 +10:00
|
|
|
artist_id = None
|
2018-02-11 22:06:04 +11:00
|
|
|
if parent_id is not None:
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2018-04-26 16:01:27 +10:00
|
|
|
artist_id = plex_db.getItem_byId(parent_id)[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2018-04-26 16:01:27 +10:00
|
|
|
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)
|
2018-04-26 16:01:27 +10:00
|
|
|
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)
|
2016-02-13 02:53:49 +11:00
|
|
|
# Add artist to album
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
2016-02-13 02:53:49 +11:00
|
|
|
INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist)
|
|
|
|
VALUES (?, ?, ?)
|
2017-04-03 01:02:41 +10:00
|
|
|
'''
|
2018-04-26 16:01:27 +10:00
|
|
|
kodicursor.execute(query, (artist_id, album_id, artistname))
|
2016-02-13 02:53:49 +11:00
|
|
|
# Update discography
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
2016-02-13 02:53:49 +11:00
|
|
|
INSERT OR REPLACE INTO discography(idArtist, strAlbum, strYear)
|
|
|
|
VALUES (?, ?, ?)
|
2017-04-03 01:02:41 +10:00
|
|
|
'''
|
2018-04-26 16:01:27 +10:00
|
|
|
kodicursor.execute(query, (artist_id, name, year))
|
2018-04-05 16:06:48 +10:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
# 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
|
2018-02-06 04:20:33 +11:00
|
|
|
if scan_children:
|
|
|
|
for child in children:
|
2018-07-27 22:49:30 +10:00
|
|
|
self.add_updateSong(child, viewtag, viewid, item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
@utils.catch_exceptions(warnuser=True)
|
2018-07-27 22:49:30 +10:00
|
|
|
def add_updateSong(self, item, viewtag=None, viewid=None, album_xml=None):
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
|
|
|
Process single song
|
|
|
|
"""
|
2016-01-09 22:13:17 +11:00
|
|
|
kodicursor = self.kodicursor
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_db = self.plex_db
|
2016-09-11 23:49:50 +10:00
|
|
|
artwork = self.artwork
|
2018-02-11 22:06:04 +11:00
|
|
|
api = API(item)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
update_item = True
|
2018-02-12 00:42:49 +11:00
|
|
|
itemid = api.plex_id()
|
2016-02-13 02:53:49 +11:00
|
|
|
if not itemid:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.error('Error processing Song; skipping')
|
2016-02-13 02:53:49 +11:00
|
|
|
return
|
2017-01-05 06:57:16 +11:00
|
|
|
plex_dbitem = plex_db.getItem_byId(itemid)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
songid = plex_dbitem[0]
|
|
|
|
pathid = plex_dbitem[2]
|
|
|
|
albumid = plex_dbitem[3]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2016-02-13 02:53:49 +11:00
|
|
|
# Songid not found
|
2015-12-25 06:51:47 +11:00
|
|
|
update_item = False
|
2018-03-04 23:39:40 +11:00
|
|
|
kodicursor.execute("select coalesce(max(idSong),0) from song")
|
2016-02-13 02:53:49 +11:00
|
|
|
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()
|
2015-12-25 06:51:47 +11:00
|
|
|
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
|
2018-02-12 00:42:49 +11:00
|
|
|
title, _ = api.titles()
|
|
|
|
# musicBrainzId = api.provider('MusicBrainzTrackId')
|
2016-02-13 02:53:49 +11:00
|
|
|
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())
|
2016-02-13 02:53:49 +11:00
|
|
|
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:
|
2018-06-22 03:24:37 +10:00
|
|
|
track = disc * 2 ** 16 + tracknumber
|
2018-02-12 00:42:49 +11:00
|
|
|
year = api.year()
|
2018-07-27 22:49:30 +10:00
|
|
|
if not year and album_xml:
|
|
|
|
# Plex did not pass year info - get it from the parent album
|
|
|
|
album_api = API(album_xml)
|
|
|
|
year = album_api.year()
|
2018-02-12 00:42:49 +11:00
|
|
|
_, duration = api.resume_runtime()
|
2017-02-03 01:23:54 +11:00
|
|
|
rating = userdata['UserRating']
|
2016-02-13 02:53:49 +11:00
|
|
|
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)
|
2016-02-13 02:53:49 +11:00
|
|
|
|
2017-04-15 01:57:36 +10:00
|
|
|
# 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:
|
2016-03-16 06:26:45 +11:00
|
|
|
# Direct paths is set the Kodi way
|
2018-02-12 00:42:49 +11:00
|
|
|
playurl = api.file_path(force_first_media=True)
|
2016-03-16 06:26:45 +11:00
|
|
|
if playurl is None:
|
2017-04-15 01:57:36 +10:00
|
|
|
# Something went wrong, trying to use non-direct paths
|
2018-02-11 22:06:04 +11:00
|
|
|
do_indirect = True
|
2016-03-16 06:26:45 +11:00
|
|
|
else:
|
2018-02-12 00:42:49 +11:00
|
|
|
playurl = api.validate_playurl(playurl, api.plex_type())
|
2017-04-15 01:57:36 +10:00
|
|
|
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:
|
2017-04-15 01:57:36 +10:00
|
|
|
# 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)
|
2017-04-15 01:57:36 +10:00
|
|
|
filename = path.rsplit('/', 1)[1]
|
|
|
|
path = path.replace(filename, '')
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
# UPDATE THE SONG #####
|
2015-12-25 06:51:47 +11:00
|
|
|
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)
|
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
|
2018-04-05 16:06:48 +10:00
|
|
|
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))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# Update the checksum in plex table
|
|
|
|
plex_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:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.info("ADD song itemid: %s - Title: %s", itemid, title)
|
2016-02-13 02:53:49 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Add path
|
2018-06-15 23:15:35 +10:00
|
|
|
pathid = self.kodi_db.add_music_path(path, hash_string="123")
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
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())
|
2017-01-05 06:57:16 +11:00
|
|
|
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'))
|
2017-01-08 06:11:48 +11:00
|
|
|
plex_db.addReference("%salbum%s" % (itemid, albumid),
|
2017-01-25 02:04:42 +11:00
|
|
|
v.PLEX_TYPE_ALBUM,
|
2017-01-08 06:11:48 +11:00
|
|
|
albumid,
|
2017-02-01 22:33:37 +11:00
|
|
|
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
|
|
|
|
|
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
|
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)
|
2016-04-08 02:29:23 +10:00
|
|
|
if album is None or album == 401:
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.error('Could not download album, abort')
|
2016-04-08 02:29:23 +10:00
|
|
|
return
|
2018-02-06 04:20:33 +11:00
|
|
|
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)
|
2016-01-30 13:40:23 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
albumid = plex_dbalbum[0]
|
2018-02-11 22:06:04 +11:00
|
|
|
LOG.debug("Found albumid: %s", albumid)
|
2016-01-30 13:40:23 +11:00
|
|
|
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(
|
2018-03-04 23:39:40 +11:00
|
|
|
"select coalesce(max(idAlbum),0) from album")
|
2016-01-30 13:40:23 +11:00
|
|
|
albumid = kodicursor.fetchone()[0] + 1
|
2017-01-25 02:54:51 +11:00
|
|
|
if v.KODIVERSION >= 16:
|
2016-01-30 13:40:23 +11:00
|
|
|
# Kodi Jarvis
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
|
|
|
INSERT INTO album(
|
|
|
|
idAlbum, strGenres, iYear, strReleaseType)
|
2016-01-30 13:40:23 +11:00
|
|
|
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:
|
2016-01-30 13:40:23 +11:00
|
|
|
# Kodi Isengard
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
|
|
|
INSERT INTO album(
|
|
|
|
idAlbum, strGenres, iYear, dateAdded,
|
|
|
|
strReleaseType)
|
2016-01-30 13:40:23 +11:00
|
|
|
VALUES (?, ?, ?, ?, ?)
|
2017-04-03 01:02:41 +10:00
|
|
|
'''
|
|
|
|
kodicursor.execute(query, (albumid, genre, year,
|
|
|
|
dateadded, "single"))
|
2016-01-30 13:40:23 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
|
|
|
INSERT INTO album(
|
|
|
|
idAlbum, strGenres, iYear, dateAdded)
|
2016-01-30 13:40:23 +11:00
|
|
|
VALUES (?, ?, ?, ?)
|
2017-04-03 01:02:41 +10:00
|
|
|
'''
|
|
|
|
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
|
2018-04-05 16:06:48 +10:00
|
|
|
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))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# Create the reference in plex table
|
2017-01-08 06:11:48 +11:00
|
|
|
plex_db.addReference(itemid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.PLEX_TYPE_SONG,
|
2017-01-08 06:11:48 +11:00
|
|
|
songid,
|
2017-01-25 02:04:42 +11:00
|
|
|
v.KODI_TYPE_SONG,
|
2017-01-08 06:11:48 +11:00
|
|
|
kodi_pathid=pathid,
|
|
|
|
parent_id=albumid,
|
2017-02-01 22:33:37 +11:00
|
|
|
checksum=checksum,
|
|
|
|
view_id=viewid)
|
2018-04-05 16:06:48 +10:00
|
|
|
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):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-16 22:13:47 +11:00
|
|
|
artist_name = artist['Name']
|
|
|
|
artist_eid = artist['Id']
|
2017-01-05 06:57:16 +11:00
|
|
|
artist_edb = plex_db.getItem_byId(artist_eid)
|
2016-03-16 22:13:47 +11:00
|
|
|
try:
|
|
|
|
artistid = artist_edb[0]
|
|
|
|
except TypeError:
|
2017-01-05 06:57:16 +11:00
|
|
|
# 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')
|
2016-04-08 02:29:23 +10:00
|
|
|
return
|
2018-02-11 22:06:04 +11:00
|
|
|
self.add_updateArtist(artist_xml[0])
|
2017-01-05 06:57:16 +11:00
|
|
|
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:
|
2016-08-13 02:35:03 +10:00
|
|
|
# Kodi Krypton
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
|
|
|
INSERT OR REPLACE INTO song_artist(
|
|
|
|
idArtist, idSong, idRole, iOrder, strArtist)
|
2016-08-13 02:35:03 +10:00
|
|
|
VALUES (?, ?, ?, ?, ?)
|
2017-04-03 01:02:41 +10:00
|
|
|
'''
|
|
|
|
kodicursor.execute(query, (artistid, songid, 1, index,
|
|
|
|
artist_name))
|
2016-08-13 02:35:03 +10:00
|
|
|
# May want to look into only doing this once?
|
2017-04-03 01:02:41 +10:00
|
|
|
query = '''
|
2016-08-13 02:35:03 +10:00
|
|
|
INSERT OR REPLACE INTO role(idRole, strRole)
|
|
|
|
VALUES (?, ?)
|
2017-04-03 01:02:41 +10:00
|
|
|
'''
|
2016-08-13 02:35:03 +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)
|
2016-08-13 02:35:03 +10:00
|
|
|
VALUES (?, ?, ?, ?)
|
2017-04-03 01:02:41 +10:00
|
|
|
'''
|
|
|
|
kodicursor.execute(query, (artistid, songid, index,
|
|
|
|
artist_name))
|
2015-12-25 06:51:47 +11:00
|
|
|
# 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)
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2018-04-07 22:30:17 +10:00
|
|
|
kodi_id = plex_dbitem[0]
|
|
|
|
file_id = plex_dbitem[1]
|
2018-04-13 02:52:37 +10:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2018-04-07 22:30:17 +10:00
|
|
|
LOG.debug('Cannot delete item with plex id %s from Kodi', plex_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
return
|
2017-01-05 06:57:16 +11:00
|
|
|
# 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:
|
2018-04-13 02:52:37 +10:00
|
|
|
# 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
|
2018-04-13 02:52:37 +10:00
|
|
|
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:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Delete songs, album
|
2018-04-13 02:52:37 +10:00
|
|
|
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)
|
2018-04-13 02:52:37 +10:00
|
|
|
# Remove the album and associated orphaned entries
|
|
|
|
self._remove_album(kodi_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
##### IF ARTIST #####
|
2018-04-07 22:30:17 +10:00
|
|
|
elif kodi_type == v.KODI_TYPE_ARTIST:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Delete songs, album, artist
|
2018-06-22 03:24:37 +10:00
|
|
|
albums = self.plex_db.getItem_byParentId(kodi_id,
|
|
|
|
v.KODI_TYPE_ALBUM)
|
2015-12-25 06:51:47 +11:00
|
|
|
for album in albums:
|
2018-04-13 02:52:37 +10:00
|
|
|
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
|
2018-04-13 02:52:37 +10:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Remove artist
|
2018-04-13 02:52:37 +10:00
|
|
|
self._remove_artist(kodi_id)
|
2018-04-07 22:30:17 +10:00
|
|
|
LOG.debug("Deleted plex_id %s from kodi database", plex_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-04-13 02:52:37 +10:00
|
|
|
def _remove_song(self, kodi_id, path_id=None):
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
2018-04-13 02:52:37 +10:00
|
|
|
Remove song, orphaned artists and orphaned paths
|
2018-02-11 22:06:04 +11:00
|
|
|
"""
|
2018-04-13 02:52:37 +10: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)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-04-13 02:52:37 +10:00
|
|
|
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)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2018-04-13 02:52:37 +10:00
|
|
|
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)
|