Skip to content

Commit b938b47

Browse files
committed
Add switchHomeUser method to PlexServer
1 parent 7046546 commit b938b47

3 files changed

Lines changed: 49 additions & 14 deletions

File tree

plexapi/myplex.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,18 @@ def removeHomeUser(self, user):
497497
url = self.HOMEUSER.format(userId=user.id)
498498
return self.query(url, self._session.delete)
499499

500-
def switchHomeUser(self, user, pin=None):
501-
""" Returns a new :class:`~plexapi.myplex.MyPlexAccount` object switched to the given home user.
500+
def switchHomeUser(self, user, pin=None, session=None, timeout=None):
501+
""" Returns a new :class:`~plexapi.myplex.MyPlexAccount` object switched to the given Plex Home user.
502502
503503
Parameters:
504504
user (:class:`~plexapi.myplex.MyPlexUser` or str): :class:`~plexapi.myplex.MyPlexUser`,
505-
username, or email of the home user to switch to.
506-
pin (str): PIN for the home user (required if the home user has a PIN set).
505+
username, or email of the Plex Home user to switch to.
506+
pin (str): PIN for the Plex Home user (required if the Plex Home user has a PIN set).
507+
session (requests.Session, optional): Use your own session object if you want to
508+
cache the http responses from the server. This will default to the same
509+
session as the current account if no new session is provided.
510+
timeout (int, optional): Timeout in seconds on initial connection to the server.
511+
This will default to the same timeout as the current account if no new timeout is provided.
507512
508513
Example:
509514
@@ -523,7 +528,11 @@ def switchHomeUser(self, user, pin=None):
523528
params['pin'] = pin
524529
data = self.query(url, self._session.post, params=params)
525530
userToken = data.attrib.get('authenticationToken')
526-
return MyPlexAccount(token=userToken, session=self._session)
531+
if session is None:
532+
session = self._session
533+
if timeout is None:
534+
timeout = self._timeout
535+
return MyPlexAccount(token=userToken, session=session, timeout=timeout)
527536

528537
def setPin(self, newPin, currentPin=None):
529538
""" Set a new Plex Home PIN for the account.

plexapi/playlist.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,13 @@ def create(cls, server, title, section=None, items=None, smart=False, limit=None
443443
return cls._create(server, title, items)
444444

445445
def copyToUser(self, user):
446-
""" Copy playlist to another user account.
446+
""" Copy playlist to another Plex Home user account.
447447
448448
Parameters:
449449
user (:class:`~plexapi.myplex.MyPlexUser` or str): `MyPlexUser` object, username,
450-
email, or user id of the user to copy the playlist to.
450+
email, or user id of the Plex Home user to copy the playlist to.
451451
"""
452-
userServer = self._server.switchUser(user)
452+
userServer = self._server.switchHomeUser(user)
453453
return self.create(server=userServer, title=self.title, items=self.items())
454454

455455
def sync(self, videoQuality=None, photoResolution=None, audioBitrate=None, client=None, clientId=None, limit=None,

plexapi/server.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,10 @@ def createToken(self, type='delegation', scope='all'):
234234
q = self.query(f'/security/token?type={type}&scope={scope}')
235235
return q.attrib.get('token')
236236

237+
@utils.deprecated('switchUser is deprecated, use switchHomeUser instead')
237238
def switchUser(self, user, session=None, timeout=None):
238-
""" Returns a new :class:`~plexapi.server.PlexServer` object logged in as the given username.
239+
""" Deprecated. Use :meth:`~plexapi.server.PlexServer.switchHomeUser` instead.
240+
Returns a new :class:`~plexapi.server.PlexServer` object logged in as the given username.
239241
Note: Only the admin account can switch to other users.
240242
241243
Parameters:
@@ -258,14 +260,38 @@ def switchUser(self, user, session=None, timeout=None):
258260
# Login to the same Plex server using a different account
259261
userPlex = plex.switchUser("Username")
260262
263+
"""
264+
return self.switchHomeUser(user, session=session, timeout=timeout)
265+
266+
def switchHomeUser(self, user, pin=None, session=None, timeout=None):
267+
""" Returns a new :class:`~plexapi.server.PlexServer` object logged in as the given Plex Home user.
268+
Note: Only the admin account can switch to other users.
269+
270+
Parameters:
271+
user (:class:`~plexapi.myplex.MyPlexUser` or str): :class:`~plexapi.myplex.MyPlexUser`,
272+
username, or email of the Plex Home user to switch to.
273+
pin (str): PIN for the Plex Home user (required if the Plex Home user has a PIN set).
274+
session (requests.Session, optional): Use your own session object if you want to
275+
cache the http responses from the server. This will default to the same
276+
session as the current account if no new session is provided.
277+
timeout (int, optional): Timeout in seconds on initial connection to the server.
278+
This will default to the same timeout as the current account if no new timeout is provided.
279+
280+
Example:
281+
282+
.. code-block:: python
283+
284+
from plexapi.server import PlexServer
285+
# Login to the Plex server using the admin token
286+
plex = PlexServer('http://plexserver:32400', token='2ffLuB84dqLswk9skLos')
287+
# Login to the same Plex server using a different Plex Home user account
288+
userPlex = plex.switchHomeUser("Username")
289+
261290
"""
262291
from plexapi.myplex import MyPlexUser
263292
user = user if isinstance(user, MyPlexUser) else self.myPlexAccount().user(user)
264-
userToken = user.get_token(self.machineIdentifier)
265-
if session is None:
266-
session = self._session
267-
if timeout is None:
268-
timeout = self._timeout
293+
userAccount = self.myPlexAccount().switchHomeUser(user, pin=pin, session=session, timeout=timeout)
294+
userToken = userAccount.resource(self.machineIdentifier).accessToken
269295
return PlexServer(self._baseurl, token=userToken, session=session, timeout=timeout)
270296

271297
@cached_data_property

0 commit comments

Comments
 (0)