2017-05-07 01:04:09 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from logging import getLogger
|
|
|
|
from re import compile as re_compile
|
2018-01-08 01:16:53 +11:00
|
|
|
from xml.etree.ElementTree import ParseError
|
2017-05-07 01:04:09 +10:00
|
|
|
|
2018-01-08 01:16:53 +11:00
|
|
|
from utils import XmlKodiSetting, reboot_kodi, language as lang
|
2017-05-07 01:04:09 +10:00
|
|
|
from PlexFunctions import get_plex_sections
|
|
|
|
from PlexAPI import API
|
|
|
|
import variables as v
|
|
|
|
|
|
|
|
###############################################################################
|
2018-01-08 01:16:53 +11:00
|
|
|
LOG = getLogger("PLEX." + __name__)
|
2017-05-07 01:04:09 +10:00
|
|
|
|
|
|
|
REGEX_MUSICPATH = re_compile(r'''^\^(.+)\$$''')
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2018-01-08 01:16:53 +11:00
|
|
|
def excludefromscan_music_folders():
|
2017-05-07 01:04:09 +10:00
|
|
|
"""
|
|
|
|
Gets a complete list of paths for music libraries from the PMS. Sets them
|
|
|
|
to be excluded in the advancedsettings.xml from being scanned by Kodi.
|
|
|
|
Existing keys will be replaced
|
|
|
|
|
2018-01-08 01:16:53 +11:00
|
|
|
Reboots Kodi if new library detected
|
2017-05-07 01:04:09 +10:00
|
|
|
"""
|
|
|
|
xml = get_plex_sections()
|
|
|
|
try:
|
|
|
|
xml[0].attrib
|
|
|
|
except (TypeError, IndexError, AttributeError):
|
2018-01-08 01:16:53 +11:00
|
|
|
LOG.error('Could not get Plex sections')
|
2017-05-07 01:04:09 +10:00
|
|
|
return
|
|
|
|
# Build paths
|
|
|
|
paths = []
|
|
|
|
api = API(item=None)
|
|
|
|
for library in xml:
|
|
|
|
if library.attrib['type'] != v.PLEX_TYPE_ARTIST:
|
|
|
|
# Only look at music libraries
|
|
|
|
continue
|
|
|
|
for location in library:
|
|
|
|
if location.tag == 'Location':
|
2018-02-12 00:42:49 +11:00
|
|
|
path = api.validate_playurl(location.attrib['path'],
|
|
|
|
typus=v.PLEX_TYPE_ARTIST,
|
|
|
|
omit_check=True)
|
2017-05-07 01:04:09 +10:00
|
|
|
paths.append(__turn_to_regex(path))
|
2018-01-08 01:16:53 +11:00
|
|
|
try:
|
|
|
|
with XmlKodiSetting('advancedsettings.xml',
|
|
|
|
force_create=True,
|
|
|
|
top_element='advancedsettings') as xml:
|
|
|
|
parent = xml.set_setting(['audio', 'excludefromscan'])
|
|
|
|
for path in paths:
|
|
|
|
for element in parent:
|
|
|
|
if element.text == path:
|
|
|
|
# Path already excluded
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
LOG.info('New Plex music library detected: %s', path)
|
|
|
|
xml.set_setting(['audio', 'excludefromscan', 'regexp'],
|
|
|
|
value=path, check_existing=False)
|
|
|
|
# We only need to reboot if we ADD new paths!
|
|
|
|
reboot = xml.write_xml
|
|
|
|
# Delete obsolete entries
|
|
|
|
for element in parent:
|
|
|
|
for path in paths:
|
|
|
|
if element.text == path:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
LOG.info('Deleting music library from advancedsettings: %s',
|
|
|
|
element.text)
|
|
|
|
parent.remove(element)
|
|
|
|
except (ParseError, IOError):
|
|
|
|
LOG.error('Could not adjust advancedsettings.xml')
|
|
|
|
reboot = False
|
|
|
|
if reboot is True:
|
|
|
|
# 'New Plex music library detected. Sorry, but we need to
|
|
|
|
# restart Kodi now due to the changes made.'
|
|
|
|
reboot_kodi(lang(39711))
|
2017-05-07 01:04:09 +10:00
|
|
|
|
|
|
|
|
|
|
|
def __turn_to_regex(path):
|
|
|
|
"""
|
|
|
|
Turns a path into regex expression to be fed to Kodi's advancedsettings.xml
|
|
|
|
"""
|
|
|
|
# Make sure we have a slash or backslash at the end of the path
|
|
|
|
if '/' in path:
|
|
|
|
if not path.endswith('/'):
|
|
|
|
path = '%s/' % path
|
|
|
|
else:
|
|
|
|
if not path.endswith('\\'):
|
|
|
|
path = '%s\\' % path
|
|
|
|
# Need to escape backslashes
|
|
|
|
path = path.replace('\\', '\\\\')
|
|
|
|
# Beginning of path only needs to be similar
|
|
|
|
return '^%s' % path
|