diff --git a/plexapi/__init__.py b/plexapi/__init__.py index 2a7d39efe..579c33b98 100644 --- a/plexapi/__init__.py +++ b/plexapi/__init__.py @@ -2,8 +2,6 @@ import logging import os from logging.handlers import RotatingFileHandler -from platform import uname -from uuid import getnode from plexapi.config import PlexConfig, reset_base_headers import plexapi.const as const @@ -21,17 +19,6 @@ X_PLEX_CONTAINER_SIZE = CONFIG.get('plexapi.container_size', 100, int) X_PLEX_ENABLE_FAST_CONNECT = CONFIG.get('plexapi.enable_fast_connect', False, bool) -# Plex Header Configuration -X_PLEX_PROVIDES = CONFIG.get('header.provides', 'controller') -X_PLEX_PLATFORM = CONFIG.get('header.platform', CONFIG.get('header.platform', uname()[0])) -X_PLEX_PLATFORM_VERSION = CONFIG.get('header.platform_version', uname()[2]) -X_PLEX_PRODUCT = CONFIG.get('header.product', PROJECT) -X_PLEX_VERSION = CONFIG.get('header.version', VERSION) -X_PLEX_DEVICE = CONFIG.get('header.device', X_PLEX_PLATFORM) -X_PLEX_DEVICE_NAME = CONFIG.get('header.device_name', uname()[1]) -X_PLEX_IDENTIFIER = CONFIG.get('header.identifier', str(hex(getnode()))) -BASE_HEADERS = reset_base_headers() - # Logging Configuration log = logging.getLogger('plexapi') logfile = CONFIG.get('log.path') diff --git a/plexapi/client.py b/plexapi/client.py index b23518adb..f2f8c2487 100644 --- a/plexapi/client.py +++ b/plexapi/client.py @@ -3,7 +3,7 @@ from xml.etree import ElementTree import requests -from plexapi import BASE_HEADERS, CONFIG, TIMEOUT, log, logfilter, utils +from plexapi import CONFIG, TIMEOUT, log, logfilter, reset_base_headers, utils from plexapi.base import PlexObject from plexapi.exceptions import BadRequest, NotFound, Unauthorized, Unsupported from plexapi.playqueue import PlayQueue @@ -74,6 +74,8 @@ def __init__(self, server=None, data=None, initpath=None, baseurl=None, self._last_call = 0 self._timeline_cache = [] self._timeline_cache_timestamp = 0 + self._base_headers = reset_base_headers() + if not any([data is not None, initpath, baseurl, token]): self._baseurl = CONFIG.get('auth.client_baseurl', 'http://localhost:32433') self._token = logfilter.add_secret(CONFIG.get('auth.client_token')) @@ -149,7 +151,7 @@ def _loadData(self, data): def _headers(self, **kwargs): """ Returns a dict of all default headers for Client requests. """ - headers = BASE_HEADERS + headers = self._base_headers.copy() if self._token: headers['X-Plex-Token'] = self._token headers.update(kwargs) diff --git a/plexapi/config.py b/plexapi/config.py index 3b93f869f..6fe35a2ed 100644 --- a/plexapi/config.py +++ b/plexapi/config.py @@ -42,25 +42,47 @@ def get(self, key, default=None, cast=None): def _asDict(self): """ Returns all configuration values as a dictionary. """ config = defaultdict(dict) + config.update(self._defaults()) for section in self._sections: for name, value in self._sections[section].items(): if name != '__name__': config[section.lower()][name.lower()] = value return dict(config) + def _defaults(self): + from uuid import getnode + from platform import uname + from plexapi import PROJECT, VERSION + + platform_name, device_name, platform_version = uname()[0:3] + + return { + 'header': { + 'provides': 'controller', + 'platform': platform_name, + 'platform_version': platform_version, + 'product': PROJECT, + 'version': VERSION, + 'device': platform_name, + 'device_name': device_name, + 'identifier': str(hex(getnode())), + } + } + def reset_base_headers(): """ Convenience function returns a dict of all base X-Plex-* headers for session requests. """ - import plexapi + from plexapi import CONFIG + return { - 'X-Plex-Platform': plexapi.X_PLEX_PLATFORM, - 'X-Plex-Platform-Version': plexapi.X_PLEX_PLATFORM_VERSION, - 'X-Plex-Provides': plexapi.X_PLEX_PROVIDES, - 'X-Plex-Product': plexapi.X_PLEX_PRODUCT, - 'X-Plex-Version': plexapi.X_PLEX_VERSION, - 'X-Plex-Device': plexapi.X_PLEX_DEVICE, - 'X-Plex-Device-Name': plexapi.X_PLEX_DEVICE_NAME, - 'X-Plex-Client-Identifier': plexapi.X_PLEX_IDENTIFIER, + 'X-Plex-Platform': CONFIG.get('header.platorm', CONFIG.get('header.platform')), + 'X-Plex-Platform-Version': CONFIG.get('header.platform_version'), + 'X-Plex-Provides': CONFIG.get('header.provides'), + 'X-Plex-Product': CONFIG.get('header.product'), + 'X-Plex-Version': CONFIG.get('header.version'), + 'X-Plex-Device': CONFIG.get('header.device'), + 'X-Plex-Device-Name': CONFIG.get('header.device_name'), + 'X-Plex-Client-Identifier': CONFIG.get('header.identifier'), 'X-Plex-Sync-Version': '2', 'X-Plex-Features': 'external-media', } diff --git a/plexapi/myplex.py b/plexapi/myplex.py index ca3f026aa..263cd29a6 100644 --- a/plexapi/myplex.py +++ b/plexapi/myplex.py @@ -8,7 +8,8 @@ import requests from plexapi import (BASE_HEADERS, CONFIG, TIMEOUT, X_PLEX_CONTAINER_SIZE, - X_PLEX_ENABLE_FAST_CONNECT, X_PLEX_IDENTIFIER, log, logfilter, utils) + X_PLEX_ENABLE_FAST_CONNECT, X_PLEX_IDENTIFIER, log, + logfilter, reset_base_headers, utils) from plexapi.base import PlexObject from plexapi.client import PlexClient from plexapi.exceptions import BadRequest, NotFound, Unauthorized @@ -94,6 +95,7 @@ def __init__(self, username=None, password=None, token=None, session=None, timeo self._session = session or requests.Session() self._sonos_cache = [] self._sonos_cache_timestamp = 0 + self._base_headers = reset_base_headers() data, initpath = self._signin(username, password, code, timeout) super(MyPlexAccount, self).__init__(self, data, initpath) @@ -168,7 +170,7 @@ def devices(self): def _headers(self, **kwargs): """ Returns dict containing base headers for all requests to the server. """ - headers = BASE_HEADERS.copy() + headers = self._base_headers.copy() if self._token: headers['X-Plex-Token'] = self._token headers.update(kwargs) @@ -707,7 +709,7 @@ def syncItems(self, client=None, clientId=None): if client: clientId = client.clientIdentifier elif clientId is None: - clientId = X_PLEX_IDENTIFIER + clientId = CONFIG.get('header.identifier') data = self.query(SyncList.key.format(clientId=clientId)) @@ -733,7 +735,7 @@ def sync(self, sync_item, client=None, clientId=None): :exc:`~plexapi.exceptions.BadRequest`: Provided client doesn't provides `sync-target`. """ if not client and not clientId: - clientId = X_PLEX_IDENTIFIER + clientId = CONFIG.get('header.identifier') if not client: for device in self.devices(): @@ -1539,6 +1541,7 @@ def __init__(self, session=None, requestTimeout=None, headers=None, oauth=False) super(MyPlexPinLogin, self).__init__() self._session = session or requests.Session() self._requestTimeout = requestTimeout or TIMEOUT + self._base_headers = reset_base_headers() self.headers = headers self._oauth = oauth @@ -1706,7 +1709,7 @@ def _pollLogin(self): def _headers(self, **kwargs): """ Returns dict containing base headers for all requests for pin login. """ - headers = BASE_HEADERS.copy() + headers = self._base_headers.copy() if self.headers: headers.update(self.headers) headers.update(kwargs) diff --git a/plexapi/server.py b/plexapi/server.py index bec9fc083..3e02af061 100644 --- a/plexapi/server.py +++ b/plexapi/server.py @@ -5,7 +5,7 @@ import requests import os from plexapi import (BASE_HEADERS, CONFIG, TIMEOUT, X_PLEX_CONTAINER_SIZE, log, - logfilter) + logfilter, reset_base_headers) from plexapi import utils from plexapi.alert import AlertListener from plexapi.base import PlexObject @@ -112,6 +112,7 @@ def __init__(self, baseurl=None, token=None, session=None, timeout=None): self._myPlexAccount = None # cached myPlexAccount self._systemAccounts = None # cached list of SystemAccount self._systemDevices = None # cached list of SystemDevice + self._base_headers = reset_base_headers() data = self.query(self.key, timeout=self._timeout) super(PlexServer, self).__init__(self, data, self.key) @@ -162,7 +163,7 @@ def _loadData(self, data): def _headers(self, **kwargs): """ Returns dict containing base headers for all requests to the server. """ - headers = BASE_HEADERS.copy() + headers = self._base_headers.copy() if self._token: headers['X-Plex-Token'] = self._token headers.update(kwargs) diff --git a/plexapi/sonos.py b/plexapi/sonos.py index a7a57f4dd..ee5cf3c77 100644 --- a/plexapi/sonos.py +++ b/plexapi/sonos.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import requests -from plexapi import CONFIG, X_PLEX_IDENTIFIER +from plexapi import CONFIG from plexapi.client import PlexClient from plexapi.exceptions import BadRequest from plexapi.playqueue import PlayQueue @@ -105,7 +105,7 @@ def playMedia(self, media, offset=0, **params): "port": server_port, "token": media._server.createToken(), "commandID": self._nextCommandId(), - "X-Plex-Client-Identifier": X_PLEX_IDENTIFIER, + "X-Plex-Client-Identifier": CONFIG.get('header.identifier'), "X-Plex-Token": media._server._token, "X-Plex-Target-Client-Identifier": self.machineIdentifier, },