Skip to content

Commit a7df2a9

Browse files
authored
adding createExistingUser
Add ability to create an Existing Plex User. If User already exists in Sharing/Friends, sections and settings carry over. If User does not already exist in Sharing/Friends, sections and settings can be assigned.
1 parent c719c90 commit a7df2a9

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

plexapi/myplex.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class MyPlexAccount(PlexObject):
6363
"""
6464
FRIENDINVITE = 'https://plex.tv/api/servers/{machineId}/shared_servers' # post with data
6565
HOMEUSERCREATE = 'https://plex.tv/api/home/users?title={title}' # post with data
66+
EXISTINGUSER = 'https://plex.tv/api/home/users?invitedEmail={username}' # post with data
6667
FRIENDSERVERS = 'https://plex.tv/api/servers/{machineId}/shared_servers/{serverId}' # put with data
6768
PLEXSERVERS = 'https://plex.tv/api/servers/{machineId}' # get
6869
FRIENDUPDATE = 'https://plex.tv/api/friends/{userId}' # put with args, delete
@@ -277,6 +278,56 @@ def createHomeUser(self, user, server, sections=None, allowSync=False, allowCame
277278
library_assignment = self.query(url, self._session.post, json=params, headers=headers)
278279
return user_creation, library_assignment
279280

281+
def createExistingUser(self, user, server, sections=None, allowSync=False, allowCameraUpload=False,
282+
allowChannels=False, filterMovies=None, filterTelevision=None, filterMusic=None):
283+
""" Share library content with the specified user.
284+
285+
Parameters:
286+
user (str): MyPlexUser, username, email of the user to be added.
287+
server (PlexServer): PlexServer object or machineIdentifier containing the library sections to share.
288+
sections ([Section]): Library sections, names or ids to be shared (default None shares all sections).
289+
allowSync (Bool): Set True to allow user to sync content.
290+
allowCameraUpload (Bool): Set True to allow user to upload photos.
291+
allowChannels (Bool): Set True to allow user to utilize installed channels.
292+
filterMovies (Dict): Dict containing key 'contentRating' and/or 'label' each set to a list of
293+
values to be filtered. ex: {'contentRating':['G'], 'label':['foo']}
294+
filterTelevision (Dict): Dict containing key 'contentRating' and/or 'label' each set to a list of
295+
values to be filtered. ex: {'contentRating':['G'], 'label':['foo']}
296+
filterMusic (Dict): Dict containing key 'label' set to a list of values to be filtered.
297+
ex: {'label':['foo']}
298+
"""
299+
headers = {'Content-Type': 'application/json'}
300+
# If user already exists, carry over sections and settings.
301+
if isinstance(user, MyPlexUser):
302+
username = user.username
303+
elif user in [_user.username for _user in self.users()]:
304+
username = self.user(user).username
305+
else:
306+
# If user does not already exists, treat request as new request and include sections and settings.
307+
newUser = user
308+
url = self.EXISTINGUSER.format(username=newUser)
309+
user_creation = self.query(url, self._session.post, headers=headers)
310+
machineId = server.machineIdentifier if isinstance(server, PlexServer) else server
311+
sectionIds = self._getSectionIds(server, sections)
312+
params = {
313+
'server_id': machineId,
314+
'shared_server': {'library_section_ids': sectionIds, 'invited_email': newUser},
315+
'sharing_settings': {
316+
'allowSync': ('1' if allowSync else '0'),
317+
'allowCameraUpload': ('1' if allowCameraUpload else '0'),
318+
'allowChannels': ('1' if allowChannels else '0'),
319+
'filterMovies': self._filterDictToStr(filterMovies or {}),
320+
'filterTelevision': self._filterDictToStr(filterTelevision or {}),
321+
'filterMusic': self._filterDictToStr(filterMusic or {}),
322+
},
323+
}
324+
url = self.FRIENDINVITE.format(machineId=machineId)
325+
library_assignment = self.query(url, self._session.post, json=params, headers=headers)
326+
return user_creation, library_assignment
327+
328+
url = self.EXISTINGUSER.format(username=username)
329+
return self.query(url, self._session.post, headers=headers)
330+
280331
def removeFriend(self, user):
281332
""" Remove the specified user from all sharing.
282333

0 commit comments

Comments
 (0)