2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
2017-05-07 01:04:09 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2017-05-07 01:04:09 +10:00
|
|
|
from logging import getLogger
|
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
from .plex_api import API
|
2019-01-09 04:00:54 +11:00
|
|
|
from . import utils
|
2018-06-22 03:24:37 +10:00
|
|
|
from . import variables as v
|
2017-05-07 01:04:09 +10:00
|
|
|
|
|
|
|
###############################################################################
|
2019-03-28 23:47:35 +11:00
|
|
|
LOG = getLogger('PLEX.music.py')
|
2017-05-07 01:04:09 +10:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2019-01-09 04:00:54 +11:00
|
|
|
def excludefromscan_music_folders(sections):
|
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-06-03 21:32:25 +10:00
|
|
|
xml: etree XML PMS answer containing all library sections
|
2017-05-07 01:04:09 +10:00
|
|
|
|
2018-01-08 01:16:53 +11:00
|
|
|
Reboots Kodi if new library detected
|
2017-05-07 01:04:09 +10:00
|
|
|
"""
|
|
|
|
paths = []
|
2018-06-02 04:36:24 +10:00
|
|
|
reboot = False
|
2017-05-07 01:04:09 +10:00
|
|
|
api = API(item=None)
|
2019-01-09 04:00:54 +11:00
|
|
|
for section in sections:
|
2019-03-28 23:47:35 +11:00
|
|
|
if section.section_type != v.PLEX_TYPE_ARTIST:
|
2017-05-07 01:04:09 +10:00
|
|
|
# Only look at music libraries
|
|
|
|
continue
|
2019-01-09 04:00:54 +11:00
|
|
|
if not section.sync_to_kodi:
|
|
|
|
continue
|
|
|
|
for location in section.xml.findall('Location'):
|
|
|
|
path = api.validate_playurl(location.attrib['path'],
|
|
|
|
typus=v.PLEX_TYPE_ARTIST,
|
|
|
|
omit_check=True)
|
|
|
|
paths.append(_turn_to_regex(path))
|
2018-01-08 01:16:53 +11:00
|
|
|
try:
|
2018-06-22 03:24:37 +10:00
|
|
|
with utils.XmlKodiSetting(
|
|
|
|
'advancedsettings.xml',
|
|
|
|
force_create=True,
|
|
|
|
top_element='advancedsettings') as xml_file:
|
2018-06-03 21:32:25 +10:00
|
|
|
parent = xml_file.set_setting(['audio', 'excludefromscan'])
|
2018-01-08 01:16:53 +11:00
|
|
|
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)
|
2018-06-03 21:32:25 +10:00
|
|
|
xml_file.set_setting(['audio', 'excludefromscan', 'regexp'],
|
|
|
|
value=path,
|
|
|
|
append=True)
|
2018-06-02 04:36:24 +10:00
|
|
|
if paths:
|
|
|
|
# We only need to reboot if we ADD new paths!
|
2018-06-03 21:32:25 +10:00
|
|
|
reboot = xml_file.write_xml
|
2018-01-08 01:16:53 +11:00
|
|
|
# Delete obsolete entries
|
2018-06-02 04:36:24 +10:00
|
|
|
# Make sure we're not saving an empty audio-excludefromscan
|
2018-06-03 21:32:25 +10:00
|
|
|
xml_file.write_xml = reboot
|
2018-01-08 01:16:53 +11:00
|
|
|
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)
|
2018-06-03 21:32:25 +10:00
|
|
|
xml_file.write_xml = True
|
2018-10-14 21:15:09 +11:00
|
|
|
except (utils.ParseError, IOError):
|
2018-01-08 01:16:53 +11:00
|
|
|
LOG.error('Could not adjust advancedsettings.xml')
|
|
|
|
if reboot is True:
|
|
|
|
# 'New Plex music library detected. Sorry, but we need to
|
|
|
|
# restart Kodi now due to the changes made.'
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.reboot_kodi(utils.lang(39711))
|
2017-05-07 01:04:09 +10:00
|
|
|
|
|
|
|
|
2019-01-09 04:00:54 +11:00
|
|
|
def _turn_to_regex(path):
|
2017-05-07 01:04:09 +10:00
|
|
|
"""
|
|
|
|
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
|