Safe Plex library section uuid to plex.db instead of kodi tagid

This commit is contained in:
croneter 2019-04-27 10:41:07 +02:00
parent a603bd33ae
commit 6a292d29f6
3 changed files with 23 additions and 21 deletions

View file

@ -42,6 +42,8 @@ class Section(object):
def __init__(self, index=None, xml_element=None, section_db_element=None): def __init__(self, index=None, xml_element=None, section_db_element=None):
# Unique Plex id of this Plex library section # Unique Plex id of this Plex library section
self._section_id = None # int self._section_id = None # int
# Plex librarySectionUUID, unique for this section
self.uuid = None
# Building block for window variable # Building block for window variable
self._node = None # unicode self._node = None # unicode
# Index of this section (as section_id might not be subsequent) # Index of this section (as section_id might not be subsequent)
@ -58,9 +60,6 @@ class Section(object):
# Do we sync all items of this section to the Kodi DB? # Do we sync all items of this section to the Kodi DB?
# This will be set with section_type!! # This will be set with section_type!!
self.sync_to_kodi = None # bool self.sync_to_kodi = None # bool
# For sections to be synched, the section name will be recorded as a
# tag. This is the corresponding id for this tag
self.kodi_tagid = None # int
# When was this section last successfully/completely synched to the # When was this section last successfully/completely synched to the
# Kodi database? # Kodi database?
self.last_sync = None # int self.last_sync = None # int
@ -90,16 +89,20 @@ class Section(object):
elif section_db_element: elif section_db_element:
self.from_db_element(section_db_element) self.from_db_element(section_db_element)
def __repr__(self): def __unicode__(self):
return ("{{" return ("{{"
"'index': {self.index}, " "'index': {self.index}, "
"'name': '{self.name}', " "'name': '{self.name}', "
"'section_id': {self.section_id}, " "'section_id': {self.section_id}, "
"'section_type': '{self.section_type}', " "'section_type': '{self.section_type}', "
"'sync_to_kodi': {self.sync_to_kodi}, " "'sync_to_kodi': {self.sync_to_kodi}, "
"'last_sync': {self.last_sync}" "'last_sync': {self.last_sync}, "
"}}").format(self=self).encode('utf-8') "'uuid': {self.uuid}"
__str__ = __repr__ "}}").format(self=self)
def __str__(self):
return unicode(self).encode('utf-8')
__repr__ = __str__
def __nonzero__(self): def __nonzero__(self):
return (self.section_id is not None and return (self.section_id is not None and
@ -164,9 +167,9 @@ class Section(object):
def from_db_element(self, section_db_element): def from_db_element(self, section_db_element):
self.section_id = section_db_element['section_id'] self.section_id = section_db_element['section_id']
self.uuid = section_db_element['uuid']
self.name = section_db_element['section_name'] self.name = section_db_element['section_name']
self.section_type = section_db_element['plex_type'] self.section_type = section_db_element['plex_type']
self.kodi_tagid = section_db_element['kodi_tagid']
self.sync_to_kodi = section_db_element['sync_to_kodi'] self.sync_to_kodi = section_db_element['sync_to_kodi']
self.last_sync = section_db_element['last_sync'] self.last_sync = section_db_element['last_sync']
@ -176,6 +179,7 @@ class Section(object):
""" """
api = API(xml_element) api = API(xml_element)
self.section_id = utils.cast(int, xml_element.get('key')) self.section_id = utils.cast(int, xml_element.get('key'))
self.uuid = xml_element.get('uuid')
self.name = api.title() self.name = api.title()
self.section_type = api.plex_type() self.section_type = api.plex_type()
self.icon = api.one_artwork('composite') self.icon = api.one_artwork('composite')
@ -204,17 +208,17 @@ class Section(object):
raise RuntimeError('Section not clearly defined: %s' % self) raise RuntimeError('Section not clearly defined: %s' % self)
if plexdb: if plexdb:
plexdb.add_section(self.section_id, plexdb.add_section(self.section_id,
self.uuid,
self.name, self.name,
self.section_type, self.section_type,
self.kodi_tagid,
self.sync_to_kodi, self.sync_to_kodi,
self.last_sync) self.last_sync)
else: else:
with PlexDB(lock=False) as plexdb: with PlexDB(lock=False) as plexdb:
plexdb.add_section(self.section_id, plexdb.add_section(self.section_id,
self.uuid,
self.name, self.name,
self.section_type, self.section_type,
self.kodi_tagid,
self.sync_to_kodi, self.sync_to_kodi,
self.last_sync) self.last_sync)
@ -291,7 +295,7 @@ class Section(object):
path_ops.makedirs(self.path) path_ops.makedirs(self.path)
# Create a tag just like the section name in the Kodi DB # Create a tag just like the section name in the Kodi DB
with kodi_db.KodiVideoDB(lock=False) as kodidb: with kodi_db.KodiVideoDB(lock=False) as kodidb:
self.kodi_tagid = kodidb.create_tag(self.name) kodidb.create_tag(self.name)
# The xmls are numbered in order of appearance # The xmls are numbered in order of appearance
self.order = 0 self.order = 0
if not path_ops.exists(path_ops.path.join(self.path, 'index.xml')): if not path_ops.exists(path_ops.path.join(self.path, 'index.xml')):
@ -441,7 +445,6 @@ def _retrieve_old_settings(sections, old_sections):
Thus sets to the old values: Thus sets to the old values:
section.last_sync section.last_sync
section.kodi_tagid
section.sync_to_kodi section.sync_to_kodi
section.last_sync section.last_sync
""" """
@ -449,7 +452,6 @@ def _retrieve_old_settings(sections, old_sections):
for old_section in old_sections: for old_section in old_sections:
if section == old_section: if section == old_section:
section.last_sync = old_section.last_sync section.last_sync = old_section.last_sync
section.kodi_tagid = old_section.kodi_tagid
section.sync_to_kodi = old_section.sync_to_kodi section.sync_to_kodi = old_section.sync_to_kodi
section.last_sync = old_section.last_sync section.last_sync = old_section.last_sync

View file

@ -194,9 +194,9 @@ def initialize():
plexdb.cursor.execute(''' plexdb.cursor.execute('''
CREATE TABLE IF NOT EXISTS sections( CREATE TABLE IF NOT EXISTS sections(
section_id INTEGER PRIMARY KEY, section_id INTEGER PRIMARY KEY,
uuid TEXT,
section_name TEXT, section_name TEXT,
plex_type TEXT, plex_type TEXT,
kodi_tagid INTEGER,
sync_to_kodi INTEGER, sync_to_kodi INTEGER,
last_sync INTEGER) last_sync INTEGER)
''') ''')

View file

@ -15,9 +15,9 @@ class Sections(object):
""" """
For section_id, returns the dict For section_id, returns the dict
section_id INTEGER PRIMARY KEY, section_id INTEGER PRIMARY KEY,
uuid TEXT,
section_name TEXT, section_name TEXT,
plex_type TEXT, plex_type TEXT,
kodi_tagid INTEGER,
sync_to_kodi BOOL, sync_to_kodi BOOL,
last_sync INTEGER last_sync INTEGER
""" """
@ -31,9 +31,9 @@ class Sections(object):
return return
return { return {
'section_id': entry[0], 'section_id': entry[0],
'section_name': entry[1], 'uuid': entry[1],
'plex_type': entry[2], 'section_name': entry[2],
'kodi_tagid': entry[3], 'plex_type': entry[3],
'sync_to_kodi': entry[4] == 1, 'sync_to_kodi': entry[4] == 1,
'last_sync': entry[5] 'last_sync': entry[5]
} }
@ -49,7 +49,7 @@ class Sections(object):
except TypeError: except TypeError:
pass pass
def add_section(self, section_id, section_name, plex_type, kodi_tagid, def add_section(self, section_id, uuid, section_name, plex_type,
sync_to_kodi, last_sync): sync_to_kodi, last_sync):
""" """
Appends a Plex section to the Plex sections table Appends a Plex section to the Plex sections table
@ -58,18 +58,18 @@ class Sections(object):
query = ''' query = '''
INSERT OR REPLACE INTO sections( INSERT OR REPLACE INTO sections(
section_id, section_id,
uuid,
section_name, section_name,
plex_type, plex_type,
kodi_tagid,
sync_to_kodi, sync_to_kodi,
last_sync) last_sync)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
''' '''
self.cursor.execute(query, self.cursor.execute(query,
(section_id, (section_id,
uuid,
section_name, section_name,
plex_type, plex_type,
kodi_tagid,
sync_to_kodi, sync_to_kodi,
last_sync)) last_sync))