Skip to content

Instantly share code, notes, and snippets.

@dasjestyr
Created July 23, 2013 23:32
Show Gist options
  • Save dasjestyr/6067037 to your computer and use it in GitHub Desktop.
Save dasjestyr/6067037 to your computer and use it in GitHub Desktop.
Movie randomizer. This will check your xbmc for unwatched movies and then pick one at random and start playing it. My first attempt at python
################################################################
# Script: Play random unwatched movie
# Description: Plays a random movie from your library that
# has not yet been played completely
# Written By: Jeremy Stafford
# Date: 11/15/2012
# Tested On: XBMC Eden
################################################################
import urllib2
import urllib
import base64
import xml.etree.ElementTree as ET
from xml.dom.minidom import parse
from random import randint
from urllib import quote_plus
##XBMC INFOS
xboxHost = "192.168.100.21"
USERNAME="dasjestyr"
PASSWORD="nomicon"
#DEFINE SOME FUNCTIONS
base64string = base64.encodestring('%s:%s' % (USERNAME, PASSWORD)) [:-1]
authHeader = "Basic %s" % base64string
def getApiUri():
return "http://" + xboxHost + "/xbmcCmds/xbmcHttp?command="
def getQueryCommandString(query):
command = "queryvideodatabase(" + query + ")"
return getApiUri() + command
def getPlayCommandString(path):
command = "PlayFile(" + path + ")"
return getApiUri() + command
def getQueryRequest(query):
requestString = getQueryCommandString(query)
request = urllib2.Request(requestString)
request.add_header("Authorization", authHeader)
return request
def playFile(filePath, fileName):
fullPath = quote_plus(filePath + fileName)
command = getPlayCommandString(fullPath)
request = urllib2.Request(command)
request.add_header("Authorization", authHeader)
response = urllib2.urlopen(request)
print response.read()
####SPIN THE WHEEL LOGIC####
#Parse reponse to get list of unwatched videos
qry = "select+f.idFile%0d%0afrom+movieview+m%0d%0aleft+join+files+f+on+m.idFile+%3d+f.idFile%0d%0awhere+f.playCount+is+null"
req = getQueryRequest(qry)
response = urllib2.urlopen(req)
unwatchedVideosRaw = response.read()
root = ET.fromstring(unwatchedVideosRaw)
ids = []
for child in root:
ids.append(child.text)
#randomly select a movie
arrayLength = len(ids)
i = randint(0, arrayLength - 1)
selectedMovie = ids[i]
#get file path
qry = "select+strPath%0d%0afrom+path%0d%0awhere+idPath+%3d%0d%0a(select+idPath%0d%0afrom+files%0d%0awhere+idFile+%3d+00" + selectedMovie + ")"
req = getQueryRequest(qry)
response = urllib2.urlopen(req)
filePathRaw = response.read()
root = ET.fromstring(filePathRaw)
filePath = ""
for child in root:
filePath = child.text
#get file name
qry = "select+strFilename%0d%0afrom+files%0d%0awhere+idFile+%3d+" + selectedMovie
req = getQueryRequest(qry)
response = urllib2.urlopen(req)
fileNameRaw = response.read()
root = ET.fromstring(fileNameRaw)
fileName = ""
for child in root:
fileName = child.text
#play the file
playFile(filePath, fileName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment