#!/usr/bin/env python # # Martin Tournoij # # # import getopt import random import subprocess import sys import time # You can modify the values here or use the commandline arguments musicdir = '/data/music' playlist = '%s/playlist' % musicdir def Usage(): print " Usage: %s [-hr] [-m musicdir] [-p playlist]" % sys.argv[0] print "" print "\t-h\tPrint this help message and exit." print "\t-m\tMusic directory." print "\t-p\tPlaylist file." print "\t-r\tRecreate playlist." print "" def MakePlaylist(musicdir, playlist): fp = open(playlist, 'w') playlist = subprocess.Popen(['find', musicdir, '-type', 'f', '-and', '-name', '*\.mp3'], stdout=fp).communicate()[0] fp.close() sys.exit(0) def MainLoop(musiclist, lastC): maxn = len(musiclist) null = open('/dev/null') # Loop & make the noise while True: try: r = random.randrange(0, maxn) mp3 = musiclist[r].replace('\n', '') print 'Playing %s' % mp3 o = subprocess.Popen(['/usr/pkg/bin/mpg123', mp3], stdout=null, stderr=null).communicate() except KeyboardInterrupt: now = int(time.strftime('%s')) # Hit ^C in the same second: We exit. Else we continue to the next song if (now - lastC) == 0: sys.exit(0) break MainLoop(musiclist, now) if __name__ == '__main__': # Get commandline options try: options, arguments = getopt.getopt(sys.argv[1:], 'm:p:hr') except getopt.GetoptError: msg, opt = sys.exc_info()[1] print "%s\n" % msg Usage() sys.exit(1) makePlaylist = False for opt, arg in options: if opt == '-h': Usage() sys.exit(0) elif opt == '-m': musicdir = arg elif opt == '-p': playlist = arg elif opt == '-r': makePlaylist = True if makePlaylist: MakePlaylist(musicdir, playlist) # Read playlist fp = open('playlist') musiclist = fp.readlines() fp.close() MainLoop(musiclist, 10)