Skip to content

Instantly share code, notes, and snippets.

@wrboyce
Created March 13, 2017 17:58
Show Gist options
  • Save wrboyce/af228df2d92396e944f409fda99edd23 to your computer and use it in GitHub Desktop.
Save wrboyce/af228df2d92396e944f409fda99edd23 to your computer and use it in GitHub Desktop.
Plex Random Pre-Rolls
#!/usr/bin/env python
import random
import sys
import urllib
import requests
from lxml import etree
PLEX_URL = 'http://...:32400'
PLEX_SERVER_ID = '...'
PLEX_PREROLLS_SECTION_ID = '...'
BASE_URL = 'https://app.plex.tv/web/app#!/server/{server_id}/details?key={key}'
try:
media_type = sys.argv[1]
except:
exit("USAGE: random_preroll.py <media_type>")
if media_type != 'movie':
exit()
prerolls = etree.fromstring(requests.get('{}/library/sections/{}/all'.format(PLEX_URL, PLEX_PREROLLS_SECTION_ID)).content)
preroll_key = random.choice(prerolls.xpath('//Video')).attrib['key']
preroll_url = BASE_URL.format(**dict(server_id=PLEX_SERVER_ID, key=preroll_key))
requests.put('{}/:/prefs?{}'.format(PLEX_URL, urllib.urlencode({'CinemaTrailersPrerollID': preroll_url})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment