Add a test for share playlist.

This commit is contained in:
Hellowlol 2017-07-17 16:11:03 +02:00
parent 97b8c5e64a
commit a0f030e353
5 changed files with 39 additions and 5 deletions

View file

@ -277,7 +277,7 @@ class PlexPartialObject(PlexObject):
clsname = self.__class__.__name__
title = self.__dict__.get('title', self.__dict__.get('name'))
objname = "%s '%s'" % (clsname, title) if title else clsname
log.warn("Reloading %s for attr '%s'" % (objname, attr))
log.warning("Reloading %s for attr '%s'" % (objname, attr))
# Reload and return the value
self.reload()
return utils.getattributeOrNone(PlexPartialObject, self, attr)

View file

@ -280,7 +280,6 @@ class MyPlexAccount(PlexObject):
requested = [MyPlexUser(self, elem, self.REQUESTED) for elem in self.query(self.REQUESTED)]
return friends + requested
def _getSectionIds(self, server, sections):
""" Converts a list of section objects or names to sectionIds needed for library sharing. """
if not sections: return []
@ -393,9 +392,8 @@ class MyPlexUser(PlexObject):
print(item.attrib.get('userID'), self.id)
if utils.cast(int, item.attrib.get('userID')) == self.id:
return item.attrib.get('accessToken')
except Exception as e: # fix me
print(e)
except Exception:
log.exception('Failed to get access token for %s' % self.title)
class MyPlexServerShare(PlexObject):

View file

@ -85,6 +85,7 @@ class Playlist(PlexPartialObject, Playable):
""" Create a playlist. """
if not isinstance(items, (list, tuple)):
items = [items]
ratingKeys = []
for item in items:
if item.listType != items[0].listType:
@ -100,3 +101,15 @@ class Playlist(PlexPartialObject, Playable):
})
data = server.query(key, method=server._session.post)[0]
return cls(server, data, initpath=key)
def share(self, user):
"""Share this playlist with another user."""
from plexapi.server import PlexServer
myplex = self._server.myPlexAccount()
user = myplex.user(user)
# Get the token for your machine.
token = user.get_token(self._server.machineIdentifier)
# Login to your server using your friends credentials.
user_server = PlexServer(self._server._baseurl, token)
return self.create(user_server, self.title, self.items())

View file

@ -70,6 +70,11 @@ def plex():
return PlexServer(SERVER_BASEURL, SERVER_TOKEN, session=session)
@pytest.fixture
def fresh_plex():
return PlexServer
@pytest.fixture()
def plex2():
return plex()

View file

@ -96,3 +96,21 @@ def test_playqueues(plex):
assert len(playqueue.items) == 1, 'No items in play queue.'
assert playqueue.items[0].title == episode.title, 'Wrong show queued.'
assert playqueue.playQueueID, 'Play queue ID not set.'
def test_share(plex, show, fresh_plex):
episodes = show.episodes()
playlist = plex.createPlaylist('shared_from_test_plexapi', episodes)
try:
playlist.share('hellowlol')
# Login to testserver as hellowlol
user = plex.myPlexAccount().user('hellowlol')
user_plex = fresh_plex(plex._baseurl, user.get_token(plex.machineIdentifier))
assert playlist.title in [p.title for p in user_plex.playlists()]
finally:
playlist.delete()