#! /usr/bin/env python # # Random Show 1.0 - http://nothingoutoftheordinary.com # Copyright James Wilson, # Released under GPL License # http://www.gnu.org/copyleft/gpl.html # # Settings. Explanation of each variable above it with extra info next to it. # The command to execute to play the video. Use %s to represent the filename. The %s will be in quotes, so you don't need to specify it. # After finishing playback, the player _needs_ to exit. VLC doesn't do this, or at least not by default. Mplayer works really well, but not when using the GUI (gmplayer). player = "mplayer -fs %s" # Default is just mplayer launched in full screen without the GUI. Use Esc to close it before the end of the show. 'f' to exit full screen # The location of the video files. The listing will be stored in a text file, specified below, until you rescan it. call randomshow.py rescan. You can make this an array to have multiple directories. It is recursive by default # location = "/media/My Book/downloads/tv/" # single location location = [ "/media/My Book/downloads/tv/" , "/media/hdb1/Documents and Settings/User/My Documents/My Videos/tv/Futurama" , "/media/hdb1/Documents and Settings/User/My Documents/Fastlane" ] # multiple locations # The type of files allowed. Seperate with | types = "avi|mkv|mpg|wmv|r00" # Special commands for certain filetypes. Such as extracting a rar and playing. # Lists in a list. [ [file, command] , ] # Make sure to include filetype in included filetypes above special = [ [ "r00" , # multipart rar files, usually scene. if you just do .rar, multipart files like part001.rar, part002.rar, ... will each get an entry in the filelist "unrar p -inul %s | mplayer -fs -" # just streams the rar file. instant playback without using up space. no seeking. # "unrar p -inul %s > /tmp/video.avi ; mplayer -fs /tmp/video.avi ; rm -f /tmp/video.avi" # completely extracts the archive to /tmp/video.avi then plays it. delayed playback and takes up space for video file (though immediately freed after playback). Allows seeking. ] , ] # The text file of the video collection (Just a text file with filenames. This is created on the first run, or by using the create command (see --usage), or updated when running rescan). This can be specified as a parameter when calling randomshow.py to use different collections text = "shows.txt" # Don't include if the filename includes any of these. Seperate with | disallow = "mythbusters|Mythbusters|top.gear|Sopranos|AVSEQ|sample|Sample" # Just a note. These are good shows, but I use this at night and don't want hour long shows. # End settings. Begin fun. import os, sys, random exts = types.split('|') dis = disallow.split('|') counter = 0 def checkdisallow(filename): global dis for d in dis: if filename.find(d) != -1: return 0 return 1 def scancollection(collection, loc=None): if not loc: try: first = open(collection, 'r').readline() loc = first.split(';')[-1] if ',' in loc: loc = loc.split(',') except IOError: global text loc = text print "From location(s)" print loc print "Clearing collection file" file = open(collection, 'w') if type(loc) == type([]): loctxt = '' for locs in loc: loctxt += "%s," % locs loctxt = loctxt[:-1] else: loctxt = loc file.write('Random-Show-Collection-1.0;%s;%s' % (collection, loctxt)) if type(loc) == type([]): for locs in loc: getdirectory(locs, file) else: getdirectory(loc, file) file.close() global counter print "Collection built - %d files" % counter def getdirectory(dir,f): global exts, counter try: files = os.listdir(dir) except OSError: ext = dir.split('.')[-1] (path, filename) = os.path.split(dir) if ext in exts and checkdisallow(filename): print "Added %s" % filename f.write('\n%s' % file) counter+=1 return 1 else: return 0 except IOError: return 0 for file in files: file = os.path.join(dir, file) try: os.listdir(file) getdirectory(file, f) # Pretty sure I could just do getdirectory and just make it recursive, since I think it should work that way. But whatever. This works, and fiddling with it could cause problems. The performance hit is minimum. except OSError: ext = file.split('.')[-1] (path, filename) = os.path.split(file) if ext in exts and checkdisallow(filename): print "Added %s" % filename f.write('\n%s' % file) counter+=1 return 1 def usage(): print """Random Show 0.1 - James Wilson randomshow.py [option] [count] For 1 random show randomshow.py For n random shows randomshow.py n Rescan your collection randomshow.py rescan Use different collection and play 1 show. If collection doesn't exist, it will use default. latenight.txt can be any collection text file. randomshow.py latenight.txt Use different collection and play n shows randomshow.py latenight.txt n Rescan different collection randomshow.py rescan latenight.txt Create new collection randomshow.py create latenight.txt "/media/tv/" """ sys.exit(2) # it would give me weird non-declared variables error first = None second = None third = None if len(sys.argv) > 1: first = sys.argv[1] if len(sys.argv) > 2: second = sys.argv[2] if len(sys.argv) > 3: third = sys.argv[3] if first == "--usage" or first == "-u" or first == "--help" or first == "-h": usage() elif first == "rescan": if second: scancollection(second) sys.exit(1) else: scancollection(text, location) sys.exit(1) elif first == "create": if second and third: scancollection(second, third) sys.exit(1) else: usage() cloc = text count = 1 if first: try: count = int(first) except ValueError: # unlike PHP, a non-number string can't be typecasted to an int cloc = first if second: try: count = int(second) except ValueError: pass try: collection = open(cloc, 'r').read() except IOError: scancollection(cloc, location) collection = open(cloc, 'r') vids = collection.split('\n') libinfo = vids[0] vids = vids[1:] numofvids = len(vids) - 1 if numofvids <= 0: print "Empty collection. Rescan or change directory(s)" sys.exit(2) selected = [] for i in xrange(count): selection = random.randint(0, numofvids) if selection in selected: if len(selected) > numofvids: selected.append(selection) else: selected.append(selection) print selected for video in selected: print "Playing %s" % vids[video] command = player.replace('%s', '"%s"' % vids[video]) for specialtypes in special: if specialtypes[0] == vids[video].split('.')[-1]: command = specialtypes[1].replace('%s', '"%s"' % vids[video]) print command os.system(command)