python-plexapi/tests/examples.py

100 lines
3.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2014-12-29 03:21:58 +00:00
"""
PlexAPI Examples
2015-06-04 04:00:37 +00:00
As of Plex version 0.9.11 I noticed that you must be logged in
2014-12-29 03:21:58 +00:00
to browse even the plex server locatewd at localhost. You can
run this example suite with the following command:
>> python examples.py -u <USERNAME> -p <PASSWORD> -s <SERVERNAME>
"""
import argparse, sys
from os.path import dirname, abspath
sys.path.append(dirname(dirname(abspath(__file__))))
from utils import fetch_server, iter_tests, register
2014-12-29 03:21:58 +00:00
@register()
def list_all_unwatched_content(plex):
2014-12-29 03:21:58 +00:00
""" Example 1: List all unwatched content in library """
for section in plex.library.sections():
2015-02-17 20:35:17 +00:00
print('Unwatched content in %s:' % section.title)
2014-12-29 03:21:58 +00:00
for video in section.unwatched():
2015-02-17 20:35:17 +00:00
print(' %s' % video.title)
2014-12-29 03:21:58 +00:00
@register()
def mark_all_conan_episodes_watched(plex):
""" Example 2: Mark all Friends episodes watched. """
plex.library.section('TV Shows').get('Friends').markWatched()
2014-12-29 03:21:58 +00:00
@register()
def list_all_clients(plex):
2014-12-29 03:21:58 +00:00
""" Example 3: List all Clients connected to the Server. """
for client in plex.clients():
2015-02-17 20:35:17 +00:00
print(client.name)
else:
2015-09-27 08:13:06 +00:00
print('No clients')
2014-12-29 03:21:58 +00:00
@register()
def play_avatar_on_iphone(plex):
""" Example 4: Play the Movie Avatar on my iPhone.
Note: Client must be on same network as server.
"""
2014-12-29 03:21:58 +00:00
avatar = plex.library.section('Movies').get('Avatar')
client = plex.client("iphone-mike")
2014-12-29 03:21:58 +00:00
client.playMedia(avatar)
@register()
def search(plex):
2014-12-29 03:21:58 +00:00
""" Example 5: List all content with the word 'Game' in the title. """
for video in plex.search('Game'):
2015-02-17 20:35:17 +00:00
print('%s (%s)' % (video.title, video.TYPE))
2014-12-29 03:21:58 +00:00
@register()
def follow_the_talent(plex):
2014-12-29 03:21:58 +00:00
""" Example 6: List all movies directed by the same person as Jurassic Park. """
movies = plex.library.section('Movies')
jurassic_park = movies.get('Jurassic Park')
2014-12-29 03:21:58 +00:00
director = jurassic_park.directors[0]
for movie in movies.search(None, director=director):
2015-02-17 20:35:17 +00:00
print(movie.title)
2014-12-29 03:21:58 +00:00
@register()
def list_files(plex):
2014-12-29 03:21:58 +00:00
""" Example 7: List files for the latest episode of Friends. """
thelastone = plex.library.section('TV Shows').get('Friends').episodes()[-1]
for part in thelastone.iterParts():
2015-02-17 20:35:17 +00:00
print(part.file)
2014-12-29 03:21:58 +00:00
@register()
def get_stream_url(plex):
2015-06-04 04:00:37 +00:00
""" Example 8: Get a URL you can open in VLC, MPV, etc. """
jurassic_park = plex.library.section('Movies').get('Jurassic Park')
2015-09-27 08:13:06 +00:00
print('Try running the following command:')
print('vlc "%s"' % jurassic_park.getStreamUrl(videoResolution='800x600'))
2015-06-04 04:00:37 +00:00
2014-12-29 03:21:58 +00:00
if __name__ == '__main__':
# There are three ways to authenticate:
# 1. If the server is running on localhost, just run without any auth.
# 2. Pass in --username, --password, and --resource.
# 3. Pass in --baseuri, --token
2014-12-29 03:21:58 +00:00
parser = argparse.ArgumentParser(description='Run PlexAPI examples.')
parser.add_argument('--username', help='Username for the Plex server.')
parser.add_argument('--password', help='Password for the Plex server.')
parser.add_argument('--resource', help='Name of the Plex resource (requires user/pass).')
parser.add_argument('--baseuri', help='Baseuri needed for auth token authentication')
parser.add_argument('--token', help='Auth token (instead of user/pass)')
parser.add_argument('--example', help='Only run the specified example.')
parser.add_argument('-v', '--verbose', default=False, action='store_true', help='Print verbose logging.')
2014-12-29 03:21:58 +00:00
args = parser.parse_args()
2015-06-04 04:00:37 +00:00
plex, user = fetch_server(args)
for example in iter_tests(args.example):
example['func'](plex)