2017-02-02 04:47:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os, pytest
|
2017-02-08 07:00:43 +00:00
|
|
|
from plexapi import CONFIG
|
2017-01-31 00:02:22 +00:00
|
|
|
from plexapi.exceptions import BadRequest, NotFound
|
2017-01-09 14:21:54 +00:00
|
|
|
from plexapi.utils import download
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_attr(pms):
|
2017-02-07 07:14:49 +00:00
|
|
|
assert pms._baseurl == 'http://138.68.157.5:32400'
|
2017-01-09 14:21:54 +00:00
|
|
|
assert pms.friendlyName == 'PMS_API_TEST_SERVER'
|
|
|
|
assert pms.machineIdentifier == 'e42470b5c527c7e5ebbdc017b5a32c8c683f6f8b'
|
|
|
|
assert pms.myPlex is True
|
|
|
|
assert pms.myPlexMappingState == 'mapped'
|
|
|
|
assert pms.myPlexSigninState == 'ok'
|
|
|
|
assert pms.myPlexSubscription == '0'
|
|
|
|
assert pms.myPlexUsername == 'testplexapi@gmail.com'
|
|
|
|
assert pms.platform == 'Linux'
|
|
|
|
assert pms.platformVersion == '4.4.0-59-generic (#80-Ubuntu SMP Fri Jan 6 17:47:47 UTC 2017)'
|
|
|
|
#assert pms.session == <requests.sessions.Session object at 0x029A5E10>
|
2017-02-08 07:00:43 +00:00
|
|
|
assert pms._token == os.environ.get('PLEX_TEST_TOKEN') or CONFIG.get('authentication.server_token')
|
2017-01-09 14:21:54 +00:00
|
|
|
assert pms.transcoderActiveVideoSessions == 0
|
2017-02-11 10:09:35 +00:00
|
|
|
#assert str(pms.updatedAt.date()) == '2017-01-20'
|
2017-01-09 14:21:54 +00:00
|
|
|
assert pms.version == '1.3.3.3148-b38628e'
|
|
|
|
|
|
|
|
|
2017-02-11 04:08:36 +00:00
|
|
|
def test_server_notifier(pms, a_movie_section):
|
|
|
|
import time
|
|
|
|
messages = []
|
|
|
|
notifier = pms.startNotifier(messages.append)
|
|
|
|
a_movie_section.refresh()
|
|
|
|
time.sleep(5)
|
|
|
|
notifier.stop()
|
|
|
|
assert len(messages) >= 3
|
|
|
|
|
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
@pytest.mark.req_client
|
|
|
|
def test_server_session():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_library(pms):
|
|
|
|
assert pms.library
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_url(pms):
|
2017-02-09 04:29:17 +00:00
|
|
|
assert 'ohno' in pms.url('ohno')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_server_transcodeImage(tmpdir, pms, a_show):
|
|
|
|
# Ideally we should also test the black white but this has to do for now.
|
2017-02-02 04:47:22 +00:00
|
|
|
from PIL import Image
|
|
|
|
width, height = 500, 500
|
2017-01-09 14:21:54 +00:00
|
|
|
img_url_resize = pms.transcodeImage(a_show.banner, height, width)
|
|
|
|
gray = img_url_resize = pms.transcodeImage(a_show.banner, height, width, saturation=0)
|
|
|
|
resized_image = download(img_url_resize, savepath=str(tmpdir), filename='resize_image')
|
2017-02-09 04:29:17 +00:00
|
|
|
org_image = download(a_show._server.url(a_show.banner), savepath=str(tmpdir), filename='org_image')
|
2017-01-09 14:21:54 +00:00
|
|
|
gray_image = download(gray, savepath=str(tmpdir), filename='gray_image')
|
|
|
|
with Image.open(resized_image) as im:
|
|
|
|
assert width, height == im.size
|
|
|
|
with Image.open(org_image) as im:
|
|
|
|
assert width, height != im.size
|
2017-02-02 04:47:22 +00:00
|
|
|
assert _detect_color_image(gray_image, thumb_size=150) == 'grayscale'
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
2017-02-02 04:47:22 +00:00
|
|
|
def _detect_color_image(file, thumb_size=150, MSE_cutoff=22, adjust_color_bias=True):
|
|
|
|
# from http://stackoverflow.com/questions/20068945/detect-if-image-is-color-grayscale-or-black-and-white-with-python-pil
|
|
|
|
from PIL import Image, ImageStat
|
|
|
|
pil_img = Image.open(file)
|
|
|
|
bands = pil_img.getbands()
|
|
|
|
if bands == ('R', 'G', 'B') or bands == ('R', 'G', 'B', 'A'):
|
|
|
|
thumb = pil_img.resize((thumb_size, thumb_size))
|
|
|
|
SSE, bias = 0, [0, 0, 0]
|
|
|
|
if adjust_color_bias:
|
|
|
|
bias = ImageStat.Stat(thumb).mean[:3]
|
|
|
|
bias = [b - sum(bias) / 3 for b in bias]
|
|
|
|
for pixel in thumb.getdata():
|
|
|
|
mu = sum(pixel) / 3
|
|
|
|
SSE += sum((pixel[i] - mu - bias[i]) * (pixel[i] - mu - bias[i]) for i in [0, 1, 2])
|
|
|
|
MSE = float(SSE) / (thumb_size * thumb_size)
|
|
|
|
if MSE <= MSE_cutoff:
|
|
|
|
return 'grayscale'
|
|
|
|
else:
|
|
|
|
return 'color'
|
|
|
|
elif len(bands) == 1:
|
|
|
|
return 'blackandwhite'
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_server_search(pms):
|
|
|
|
# basic search. see test_search.py
|
|
|
|
assert pms.search('16 Blocks')
|
2017-01-31 00:02:22 +00:00
|
|
|
assert pms.search('16 blocks', mediatype='movie')
|
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
def test_server_playlist(pms):
|
|
|
|
pl = pms.playlist('some_playlist')
|
|
|
|
assert pl.title == 'some_playlist'
|
2017-01-31 00:02:22 +00:00
|
|
|
with pytest.raises(NotFound):
|
|
|
|
pms.playlist('124xxx11y')
|
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
def test_server_playlists(pms):
|
|
|
|
playlists = pms.playlists()
|
|
|
|
assert len(playlists)
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_history(pms):
|
|
|
|
history = pms.history()
|
|
|
|
assert len(history)
|
|
|
|
|
|
|
|
|
2017-01-31 00:02:22 +00:00
|
|
|
def test_server_Server_query(pms):
|
2017-02-09 04:29:17 +00:00
|
|
|
assert pms.query('/')
|
2017-01-31 00:02:22 +00:00
|
|
|
from plexapi.server import PlexServer
|
|
|
|
with pytest.raises(BadRequest):
|
2017-02-09 04:29:17 +00:00
|
|
|
assert pms.query('/asdasdsada/12123127/aaaa', headers={'random_headers': '1337'})
|
2017-02-08 07:00:43 +00:00
|
|
|
with pytest.raises(BadRequest):
|
2017-01-31 00:02:22 +00:00
|
|
|
# This is really requests.exceptions.HTTPError:
|
|
|
|
# 401 Client Error: Unauthorized for url:
|
|
|
|
PlexServer('http://138.68.157.5:32400', '1234')
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_Server_session():
|
|
|
|
from requests import Session
|
|
|
|
from plexapi.server import PlexServer
|
|
|
|
|
|
|
|
class MySession(Session):
|
|
|
|
def __init__(self):
|
|
|
|
super(self.__class__, self).__init__()
|
|
|
|
self.plexapi_session_test = True
|
|
|
|
|
|
|
|
plex = PlexServer('http://138.68.157.5:32400',
|
2017-02-14 21:12:56 +00:00
|
|
|
os.environ.get('PLEX_TEST_TOKEN'),
|
|
|
|
session=MySession())
|
2017-02-08 07:00:43 +00:00
|
|
|
assert hasattr(plex._session, 'plexapi_session_test')
|
2017-01-31 00:02:22 +00:00
|
|
|
pl = plex.playlists()
|
2017-02-09 04:29:17 +00:00
|
|
|
assert hasattr(pl[0]._server._session, 'plexapi_session_test')
|
2017-02-02 04:47:22 +00:00
|
|
|
# TODO: Check client in test_server_Server_session.
|
|
|
|
# TODO: Check myplex in test_server_Server_session.
|
2017-01-31 00:02:22 +00:00
|
|
|
|
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
def test_server_token_in_headers(pms):
|
2017-02-08 07:00:43 +00:00
|
|
|
h = pms._headers()
|
2017-01-09 14:21:54 +00:00
|
|
|
assert 'X-Plex-Token' in h and len(h['X-Plex-Token'])
|
2017-01-31 00:02:22 +00:00
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2017-02-14 21:12:56 +00:00
|
|
|
def test_server_createPlayQueue(pms, a_movie):
|
|
|
|
pq = pms.createPlayQueue(a_movie, **dict(shuffle=1, repeat=1))
|
|
|
|
assert 'shuffle=1' and 'repeat=1' in pq._initpath
|
|
|
|
assert pq.playQueueShuffled is True
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2017-02-02 04:47:22 +00:00
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
def _test_server_createPlaylist():
|
|
|
|
# see test_playlists.py
|
|
|
|
pass
|
|
|
|
|
2017-01-31 00:02:22 +00:00
|
|
|
|
|
|
|
def test_server_client_not_found(pms):
|
|
|
|
with pytest.raises(NotFound):
|
2017-02-02 04:47:22 +00:00
|
|
|
pms.client('<This-client-should-not-be-found>')
|
2017-01-31 00:02:22 +00:00
|
|
|
|
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
@pytest.mark.req_client
|
|
|
|
def test_server_client(pms):
|
|
|
|
assert pms.client('Plex Web (Chrome)')
|
|
|
|
|
2017-02-02 04:47:22 +00:00
|
|
|
|
2017-01-31 00:02:22 +00:00
|
|
|
def test_server_Server_sessions(pms):
|
|
|
|
assert len(pms.sessions()) == 0
|
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
@pytest.mark.req_client
|
|
|
|
def test_server_clients(pms):
|
|
|
|
assert len(pms.clients())
|
|
|
|
m = pms.clients()[0]
|
2017-02-07 07:14:49 +00:00
|
|
|
assert m._baseurl == 'http://127.0.0.1:32400'
|
2017-01-09 14:21:54 +00:00
|
|
|
assert m.device is None
|
|
|
|
assert m.deviceClass == 'pc'
|
|
|
|
assert m.machineIdentifier == '89hgkrbqxaxmf45o1q2949ru'
|
|
|
|
assert m.model is None
|
|
|
|
assert m.platform is None
|
|
|
|
assert m.platformVersion is None
|
|
|
|
assert m.product == 'Plex Web'
|
|
|
|
assert m.protocol == 'plex'
|
|
|
|
assert m.protocolCapabilities == ['timeline', 'playback', 'navigation', 'mirror', 'playqueues']
|
|
|
|
assert m.protocolVersion == '1'
|
2017-02-09 04:29:17 +00:00
|
|
|
assert m._server._baseurl == 'http://138.68.157.5:32400'
|
2017-01-09 14:21:54 +00:00
|
|
|
assert m.state is None
|
|
|
|
assert m.title == 'Plex Web (Chrome)'
|
|
|
|
assert m.token is None
|
|
|
|
assert m.vendor is None
|
|
|
|
assert m.version == '2.12.5'
|
|
|
|
|
|
|
|
|
|
|
|
def test_server_account(pms):
|
|
|
|
acc = pms.account()
|
|
|
|
assert acc.authToken
|
2017-02-02 04:47:22 +00:00
|
|
|
# TODO: Figure out why this is missing from time to time.
|
|
|
|
#assert acc.mappingError == 'publisherror'
|
2017-01-09 14:21:54 +00:00
|
|
|
assert acc.mappingErrorMessage is None
|
|
|
|
assert acc.mappingState == 'mapped'
|
|
|
|
assert acc.privateAddress == '138.68.157.5'
|
|
|
|
assert acc.privatePort == '32400'
|
|
|
|
assert acc.publicAddress == '138.68.157.5'
|
|
|
|
assert acc.publicPort == '32400'
|
|
|
|
assert acc.signInState == 'ok'
|
2017-02-03 16:39:46 +00:00
|
|
|
assert acc.subscriptionActive == False
|
|
|
|
assert acc.subscriptionFeatures == []
|
2017-01-09 14:21:54 +00:00
|
|
|
assert acc.subscriptionState == 'Unknown'
|
|
|
|
assert acc.username == 'testplexapi@gmail.com'
|