2017-01-09 14:21:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-10-25 19:53:52 +00:00
|
|
|
import time
|
2020-04-29 21:23:22 +00:00
|
|
|
|
2017-10-25 19:53:52 +00:00
|
|
|
import pytest
|
2021-05-27 07:16:01 +00:00
|
|
|
from plexapi.exceptions import BadRequest, NotFound, Unsupported
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2021-05-30 02:00:59 +00:00
|
|
|
from . import conftest as utils
|
|
|
|
|
|
|
|
|
|
|
|
def test_Playlist_attrs(playlist):
|
|
|
|
assert utils.is_datetime(playlist.addedAt)
|
|
|
|
assert playlist.allowSync is True
|
|
|
|
assert utils.is_composite(playlist.composite, prefix="/playlists")
|
|
|
|
assert playlist.content is None
|
|
|
|
assert utils.is_int(playlist.duration)
|
|
|
|
assert playlist.durationInSeconds is None
|
|
|
|
assert playlist.icon is None
|
|
|
|
assert playlist.guid.startswith("com.plexapp.agents.none://")
|
|
|
|
assert playlist.key.startswith("/playlists/")
|
|
|
|
assert playlist.leafCount == 3
|
|
|
|
assert playlist.playlistType == "video"
|
|
|
|
assert utils.is_int(playlist.ratingKey)
|
|
|
|
assert playlist.smart is False
|
|
|
|
assert playlist.summary == ""
|
|
|
|
assert playlist.title == "Test Playlist"
|
|
|
|
assert playlist.type == "playlist"
|
|
|
|
assert utils.is_datetime(playlist.updatedAt)
|
|
|
|
assert playlist.thumb == playlist.composite
|
|
|
|
assert playlist.metadataType == "movie"
|
|
|
|
assert playlist.isVideo is True
|
|
|
|
assert playlist.isAudio is False
|
|
|
|
assert playlist.isPhoto is False
|
|
|
|
|
|
|
|
|
|
|
|
def test_Playlist_create(plex, show):
|
2017-01-09 14:21:54 +00:00
|
|
|
# create the playlist
|
2017-04-16 15:58:21 +00:00
|
|
|
title = 'test_create_playlist_show'
|
|
|
|
episodes = show.episodes()
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist(title, items=episodes[:3])
|
2017-01-09 14:21:54 +00:00
|
|
|
try:
|
|
|
|
items = playlist.items()
|
2017-04-16 15:58:21 +00:00
|
|
|
# Test create playlist
|
2017-01-09 14:21:54 +00:00
|
|
|
assert playlist.title == title, 'Playlist not created successfully.'
|
|
|
|
assert len(items) == 3, 'Playlist does not contain 3 items.'
|
|
|
|
assert items[0].ratingKey == episodes[0].ratingKey, 'Items not in proper order [0a].'
|
|
|
|
assert items[1].ratingKey == episodes[1].ratingKey, 'Items not in proper order [1a].'
|
|
|
|
assert items[2].ratingKey == episodes[2].ratingKey, 'Items not in proper order [2a].'
|
2017-04-16 15:58:21 +00:00
|
|
|
# Test move items around (b)
|
2017-01-09 14:21:54 +00:00
|
|
|
playlist.moveItem(items[1])
|
2021-05-27 07:16:01 +00:00
|
|
|
items = playlist.reload().items()
|
2017-01-09 14:21:54 +00:00
|
|
|
assert items[0].ratingKey == episodes[1].ratingKey, 'Items not in proper order [0b].'
|
|
|
|
assert items[1].ratingKey == episodes[0].ratingKey, 'Items not in proper order [1b].'
|
|
|
|
assert items[2].ratingKey == episodes[2].ratingKey, 'Items not in proper order [2b].'
|
2017-04-16 15:58:21 +00:00
|
|
|
# Test move items around (c)
|
2017-01-09 14:21:54 +00:00
|
|
|
playlist.moveItem(items[0], items[1])
|
2021-05-27 07:16:01 +00:00
|
|
|
items = playlist.reload().items()
|
2017-01-09 14:21:54 +00:00
|
|
|
assert items[0].ratingKey == episodes[0].ratingKey, 'Items not in proper order [0c].'
|
|
|
|
assert items[1].ratingKey == episodes[1].ratingKey, 'Items not in proper order [1c].'
|
|
|
|
assert items[2].ratingKey == episodes[2].ratingKey, 'Items not in proper order [2c].'
|
2017-04-16 15:58:21 +00:00
|
|
|
# Test add item
|
2017-01-09 14:21:54 +00:00
|
|
|
playlist.addItems(episodes[3])
|
2021-05-27 07:16:01 +00:00
|
|
|
items = playlist.reload().items()
|
2017-01-09 14:21:54 +00:00
|
|
|
assert items[3].ratingKey == episodes[3].ratingKey, 'Missing added item: %s' % episodes[3]
|
2017-04-16 15:58:21 +00:00
|
|
|
# Test add two items
|
2017-01-09 14:21:54 +00:00
|
|
|
playlist.addItems(episodes[4:6])
|
2021-05-27 07:16:01 +00:00
|
|
|
items = playlist.reload().items()
|
2017-01-09 14:21:54 +00:00
|
|
|
assert items[4].ratingKey == episodes[4].ratingKey, 'Missing added item: %s' % episodes[4]
|
|
|
|
assert items[5].ratingKey == episodes[5].ratingKey, 'Missing added item: %s' % episodes[5]
|
|
|
|
assert len(items) == 6, 'Playlist should have 6 items, %s found' % len(items)
|
2017-04-16 15:58:21 +00:00
|
|
|
# Test remove item
|
2021-05-27 07:16:01 +00:00
|
|
|
toremove = items[5]
|
|
|
|
playlist.removeItems(toremove)
|
|
|
|
items = playlist.reload().items()
|
|
|
|
assert toremove not in items, 'Removed item still in playlist: %s' % items[5]
|
2017-01-09 14:21:54 +00:00
|
|
|
assert len(items) == 5, 'Playlist should have 5 items, %s found' % len(items)
|
2021-05-27 07:16:01 +00:00
|
|
|
# Test remove two item
|
|
|
|
toremove = items[3:5]
|
|
|
|
playlist.removeItems(toremove)
|
|
|
|
items = playlist.reload().items()
|
|
|
|
assert toremove[0] not in items, 'Removed item still in playlist: %s' % items[3]
|
|
|
|
assert toremove[1] not in items, 'Removed item still in playlist: %s' % items[4]
|
|
|
|
assert len(items) == 3, 'Playlist should have 5 items, %s found' % len(items)
|
|
|
|
finally:
|
|
|
|
playlist.delete()
|
|
|
|
|
|
|
|
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Playlist_edit(plex, movie):
|
2021-05-27 07:16:01 +00:00
|
|
|
title = 'test_playlist_edit'
|
|
|
|
new_title = 'test_playlist_edit_new_title'
|
|
|
|
new_summary = 'test_playlist_edit_summary'
|
|
|
|
try:
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist(title, items=movie)
|
2021-05-27 07:16:01 +00:00
|
|
|
assert playlist.title == title
|
|
|
|
assert playlist.summary == ''
|
|
|
|
playlist.edit(title=new_title, summary=new_summary)
|
|
|
|
playlist.reload()
|
|
|
|
assert playlist.title == new_title
|
|
|
|
assert playlist.summary == new_summary
|
2017-01-09 14:21:54 +00:00
|
|
|
finally:
|
|
|
|
playlist.delete()
|
|
|
|
|
|
|
|
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Playlist_item(plex, show):
|
2020-12-24 17:21:29 +00:00
|
|
|
title = 'test_playlist_item'
|
2020-12-24 07:03:08 +00:00
|
|
|
episodes = show.episodes()
|
|
|
|
try:
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist(title, items=episodes[:3])
|
2020-12-24 07:03:08 +00:00
|
|
|
item1 = playlist.item("Winter Is Coming")
|
|
|
|
assert item1 in playlist.items()
|
|
|
|
item2 = playlist.get("Winter Is Coming")
|
|
|
|
assert item2 in playlist.items()
|
|
|
|
assert item1 == item2
|
2020-12-24 17:21:29 +00:00
|
|
|
with pytest.raises(NotFound):
|
|
|
|
playlist.item("Does not exist")
|
2020-12-24 07:03:08 +00:00
|
|
|
finally:
|
|
|
|
playlist.delete()
|
|
|
|
|
|
|
|
|
2017-04-29 05:47:21 +00:00
|
|
|
@pytest.mark.client
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Playlist_play(plex, client, artist, album):
|
2017-01-09 14:21:54 +00:00
|
|
|
try:
|
2017-04-16 15:58:21 +00:00
|
|
|
playlist_name = 'test_play_playlist'
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist(playlist_name, items=album)
|
2017-01-09 14:21:54 +00:00
|
|
|
client.playMedia(playlist); time.sleep(5)
|
|
|
|
client.stop('music'); time.sleep(1)
|
|
|
|
finally:
|
|
|
|
playlist.delete()
|
2017-04-16 15:58:21 +00:00
|
|
|
assert playlist_name not in [i.title for i in plex.playlists()]
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Playlist_photos(plex, photoalbum):
|
2017-04-16 15:58:21 +00:00
|
|
|
album = photoalbum
|
2017-01-09 14:21:54 +00:00
|
|
|
photos = album.photos()
|
|
|
|
try:
|
2017-04-16 15:58:21 +00:00
|
|
|
playlist_name = 'test_playlist_photos'
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist(playlist_name, items=photos)
|
2017-04-16 15:58:21 +00:00
|
|
|
assert len(playlist.items()) >= 1
|
2017-01-09 14:21:54 +00:00
|
|
|
finally:
|
|
|
|
playlist.delete()
|
2017-04-16 15:58:21 +00:00
|
|
|
assert playlist_name not in [i.title for i in plex.playlists()]
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
2017-04-29 05:47:21 +00:00
|
|
|
@pytest.mark.client
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Play_photos(plex, client, photoalbum):
|
2017-04-29 05:47:21 +00:00
|
|
|
photos = photoalbum.photos()
|
2017-01-09 14:21:54 +00:00
|
|
|
for photo in photos[:4]:
|
|
|
|
client.playMedia(photo)
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Playlist_copyToUser(plex, show, fresh_plex, shared_username):
|
2017-07-17 14:11:03 +00:00
|
|
|
episodes = show.episodes()
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist('shared_from_test_plexapi', items=episodes)
|
2017-07-17 14:11:03 +00:00
|
|
|
try:
|
2018-09-14 18:03:23 +00:00
|
|
|
playlist.copyToUser(shared_username)
|
|
|
|
user = plex.myPlexAccount().user(shared_username)
|
2017-07-17 14:11:03 +00:00
|
|
|
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()
|
2018-11-16 23:20:21 +00:00
|
|
|
|
|
|
|
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Playlist_createSmart(plex, movies, movie):
|
2021-05-27 07:16:01 +00:00
|
|
|
try:
|
|
|
|
playlist = plex.createPlaylist(
|
|
|
|
title='smart_playlist',
|
|
|
|
smart=True,
|
|
|
|
limit=2,
|
|
|
|
section=movies,
|
|
|
|
sort='titleSort:desc',
|
|
|
|
**{'year>>': 2007}
|
|
|
|
)
|
|
|
|
items = playlist.items()
|
|
|
|
assert playlist.smart
|
|
|
|
assert len(items) == 2
|
|
|
|
assert items == sorted(items, key=lambda i: i.titleSort, reverse=True)
|
|
|
|
playlist.updateFilters(limit=1, year=movie.year)
|
|
|
|
playlist.reload()
|
|
|
|
assert len(playlist.items()) == 1
|
|
|
|
assert movie in playlist
|
|
|
|
finally:
|
|
|
|
playlist.delete()
|
|
|
|
|
|
|
|
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Playlist_section(plex, movies, movie):
|
2021-05-27 07:16:01 +00:00
|
|
|
title = 'test_playlist_section'
|
2020-12-04 04:56:08 +00:00
|
|
|
try:
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist(title, items=movie)
|
2021-05-27 07:16:01 +00:00
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
playlist.section()
|
2020-12-04 04:56:08 +00:00
|
|
|
finally:
|
2021-05-27 07:16:01 +00:00
|
|
|
playlist.delete()
|
|
|
|
|
|
|
|
try:
|
|
|
|
playlist = plex.createPlaylist(title, smart=True, section=movies, **{'year>>': 2000})
|
|
|
|
assert playlist.section() == movies
|
|
|
|
playlist.content = ''
|
|
|
|
assert playlist.section() == movies
|
|
|
|
playlist.updateFilters(year=1990)
|
|
|
|
playlist.reload()
|
|
|
|
playlist.content = ''
|
|
|
|
with pytest.raises(Unsupported):
|
|
|
|
playlist.section()
|
|
|
|
finally:
|
|
|
|
playlist.delete()
|
|
|
|
|
|
|
|
|
2021-05-30 02:00:59 +00:00
|
|
|
def test_Playlist_exceptions(plex, movies, movie, artist):
|
2021-05-27 07:16:01 +00:00
|
|
|
title = 'test_playlist_exceptions'
|
|
|
|
try:
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist(title, items=movie)
|
2021-05-27 07:16:01 +00:00
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
playlist.updateFilters()
|
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
playlist.addItems(artist)
|
2021-06-02 16:27:35 +00:00
|
|
|
with pytest.raises(NotFound):
|
|
|
|
playlist.removeItems(artist)
|
|
|
|
with pytest.raises(NotFound):
|
|
|
|
playlist.moveItem(artist)
|
|
|
|
with pytest.raises(NotFound):
|
|
|
|
playlist.moveItem(item=movie, after=artist)
|
2021-05-27 07:16:01 +00:00
|
|
|
finally:
|
|
|
|
playlist.delete()
|
|
|
|
|
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
plex.createPlaylist(title, items=[])
|
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
plex.createPlaylist(title, items=[movie, artist])
|
|
|
|
|
|
|
|
try:
|
2021-05-30 02:26:12 +00:00
|
|
|
playlist = plex.createPlaylist(title, smart=True, section=movies.title, **{'year>>': 2000})
|
2021-05-27 07:16:01 +00:00
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
playlist.addItems(movie)
|
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
playlist.removeItems(movie)
|
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
playlist.moveItem(movie)
|
|
|
|
finally:
|
|
|
|
playlist.delete()
|