Last active
August 29, 2015 13:56
-
-
Save surskitt/8925882 to your computer and use it in GitHub Desktop.
Get randomised list of films from plex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
from xml.etree import ElementTree | |
from optparse import OptionParser | |
from random import sample | |
# Plex host | |
host = "127.0.0.1" | |
# Plex port | |
port = "32400" | |
# Library section containing films | |
section = "1" | |
# Command line options | |
parser = OptionParser() | |
parser.add_option( | |
'-M', '--maximum', action='store', type='int', dest='maximum', | |
help='Limit selection to those below given duration') | |
parser.add_option( | |
'-m', '--minimum', action='store', type='int', dest='minimum', | |
help='Limit selection to those above given duration') | |
parser.add_option( | |
'-c', '--count', action='store', type='int', dest='count', | |
help='The amount of films returned') | |
(options, args) = parser.parse_args() | |
# Parse command line options, using defaults if not set | |
maxDuration = (options.maximum or 1000) * 60000 | |
minDuration = (options.minimum or 0) * 60000 | |
returnCount = (options.count or 5) | |
# Pull library XML and create XML element tree | |
url= 'http://{}:{}/library/sections/{}/unwatched'.format(host, port, section) | |
response = requests.get(url) | |
tree = ElementTree.fromstring(response.content) | |
# Create list of films, filtering on max and min duration | |
c = [i.attrib for i in tree | |
if int(i.attrib['duration']) <= maxDuration | |
and int(i.attrib['duration']) >= minDuration] | |
# If the number of films returned by filter is more than 0... | |
if len(c) > 0: | |
# Get a random selection determined by returnCount | |
# If filtered list is less, return that amount | |
selection = sample(c, returnCount * (len(c) > returnCount) or len(c)) | |
for n, i in enumerate(selection, 1): | |
film_tuple = n, i['title'], int(i['duration']) / 60000 | |
print u'{}: {} ({} minutes)'.format(*film_tuple) | |
else: | |
print 'No films matching given criteria' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment