Skip to content

Commit

Permalink
Merge pull request #3619 from trolley/topic/plex-secure-connections
Browse files Browse the repository at this point in the history
feat: support secure Plex connections
  • Loading branch information
sampsyo authored Jun 9, 2020
2 parents ee25439 + 6d41f31 commit 9b47e4f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
39 changes: 30 additions & 9 deletions beetsplug/plexupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
from beets.plugins import BeetsPlugin


def get_music_section(host, port, token, library_name):
def get_music_section(host, port, token, library_name, secure,
ignore_cert_errors):
"""Getting the section key for the music library in Plex.
"""
api_endpoint = append_token('library/sections', token)
url = urljoin('http://{0}:{1}'.format(host, port), api_endpoint)
url = urljoin('{0}://{1}:{2}'.format(get_protocol(secure), host,
port), api_endpoint)

# Sends request.
r = requests.get(url)
r = requests.get(url, verify=not ignore_cert_errors)

# Parse xml tree and extract music section key.
tree = ElementTree.fromstring(r.content)
Expand All @@ -34,17 +36,25 @@ def get_music_section(host, port, token, library_name):
return child.get('key')


def update_plex(host, port, token, library_name):
def update_plex(host, port, token, library_name, secure,
ignore_cert_errors):
"""Ignore certificate errors if configured to.
"""
if ignore_cert_errors:
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
"""Sends request to the Plex api to start a library refresh.
"""
# Getting section key and build url.
section_key = get_music_section(host, port, token, library_name)
section_key = get_music_section(host, port, token, library_name,
secure, ignore_cert_errors)
api_endpoint = 'library/sections/{0}/refresh'.format(section_key)
api_endpoint = append_token(api_endpoint, token)
url = urljoin('http://{0}:{1}'.format(host, port), api_endpoint)
url = urljoin('{0}://{1}:{2}'.format(get_protocol(secure), host,
port), api_endpoint)

# Sends request and returns requests object.
r = requests.get(url)
r = requests.get(url, verify=not ignore_cert_errors)
return r


Expand All @@ -56,6 +66,13 @@ def append_token(url, token):
return url


def get_protocol(secure):
if secure:
return 'https'
else:
return 'http'


class PlexUpdate(BeetsPlugin):
def __init__(self):
super(PlexUpdate, self).__init__()
Expand All @@ -65,7 +82,9 @@ def __init__(self):
u'host': u'localhost',
u'port': 32400,
u'token': u'',
u'library_name': u'Music'})
u'library_name': u'Music',
u'secure': False,
u'ignore_cert_errors': False})

config['plex']['token'].redact = True
self.register_listener('database_change', self.listen_for_db_change)
Expand All @@ -85,7 +104,9 @@ def update(self, lib):
config['plex']['host'].get(),
config['plex']['port'].get(),
config['plex']['token'].get(),
config['plex']['library_name'].get())
config['plex']['library_name'].get(),
config['plex']['secure'].get(bool),
config['plex']['ignore_cert_errors'].get(bool))
self._log.info(u'... started.')

except requests.exceptions.RequestException:
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ New features:
:bug:`3567`
* ``beet import`` now handles tar archives with bzip2 or gzip compression.
:bug:`3606`
* :doc:`/plugins/plexupdate`: Add option to use secure connection to Plex
server, and to ignore certificate validation errors if necessary.
:bug:`2871`

Fixes:

Expand Down
4 changes: 4 additions & 0 deletions docs/plugins/plexupdate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ The available options under the ``plex:`` section are:
Default: Empty.
- **library_name**: The name of the Plex library to update.
Default: ``Music``
- **secure**: Use secure connections to the Plex server.
Default: ``False``
- **ignore_cert_errors**: Ignore TLS certificate errors when using secure connections.
Default: ``False``
12 changes: 9 additions & 3 deletions test/test_plexupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def test_get_music_section(self):
self.config['plex']['host'],
self.config['plex']['port'],
self.config['plex']['token'],
self.config['plex']['library_name'].get()), '2')
self.config['plex']['library_name'].get(),
self.config['plex']['secure'],
self.config['plex']['ignore_cert_errors']), '2')

@responses.activate
def test_get_named_music_section(self):
Expand All @@ -104,7 +106,9 @@ def test_get_named_music_section(self):
self.config['plex']['host'],
self.config['plex']['port'],
self.config['plex']['token'],
'My Music Library'), '2')
'My Music Library',
self.config['plex']['secure'],
self.config['plex']['ignore_cert_errors']), '2')

@responses.activate
def test_update_plex(self):
Expand All @@ -117,7 +121,9 @@ def test_update_plex(self):
self.config['plex']['host'],
self.config['plex']['port'],
self.config['plex']['token'],
self.config['plex']['library_name'].get()).status_code, 200)
self.config['plex']['library_name'].get(),
self.config['plex']['secure'],
self.config['plex']['ignore_cert_errors']).status_code, 200)


def suite():
Expand Down

0 comments on commit 9b47e4f

Please sign in to comment.