#!/usr/bin/python

# This script takes a set of music directories and creates playlists from them
import os
import shutil
from mutagen.mp3 import MP3
import random

def musicListGen(playlistdir, workingdir, duration, playlistname, linersdir=None, weatherfile=None, legalidfile=None, newsfile=None):
	playlistfile = open(os.path.join(playlistdir, playlistname), "w")
	playlistLength = 0
	songlist = os.listdir(workingdir)
	random.shuffle(songlist)
	songcount = 0
	if legalidfile:
		try:
			print "repeaterfile="+legalidfile
			playlistLength += getLength(legalidfile)
			playlistfile.write(legalidfile+'\n')
		except:
			print "problem with legal ID"
	if weatherfile:
		try:
			print "weatherfile="+weatherfile
			playlistLength += 25
			playlistfile.write(weatherfile+'\n')
		except:
			print "problem with Weather"
	if newsfile:
		try:
			print "newsfile="+newsfile
			playlistLength += getLength(newsfile)
			playlistfile.write(newsfile+'\n')
		except:
			print "problem with news"
	for song in songlist:
		try:
		# iterate through files while the playlist is as long as the intended duration
			if playlistLength <=duration:
				# check to see if you want random liners, and insert them here
				if linersdir:
					linerslist = os.listdir(linersdir)
					random.shuffle(linerslist)
					if songcount == 3:
						linersfile = os.path.join(linersdir, linerslist[0])
						# print "linersfile="+linersfile
						linersduration = getLength(linersfile)
						playlistLength+=linersduration
						playlistfile.write(linersfile+'\n')
						songcount = 0
				songfile = os.path.join(workingdir, song)
				songduration = getLength(songfile)
				playlistLength+=songduration
				playlistfile.write(songfile+'\n')
				songcount+=1
		except:
			print "problem with file"+song
			
	playlistfile.close()


def getLength(self):
	"""
	Get the length of the audio/video stream in seconds.
	"""
	audio = MP3(self)
	if audio:
		return audio.info.length
	return None
	
playlistdir = "/mnt/musicMount/playlists"
linersdir = "/mnt/musicMount/watch/liners/LINERROTATION"
weatherfile = "/mnt/musicMount/watch/WEATHER/wntxaccuwx.mp3"
topofhourfile = "/mnt/musicMount/watch/liners/TOHID/fredfmtopofhourid.mp3"
repeaterfile = "/mnt/musicMount/watch/liners/REPEATERID/fredfmlegalrepeater.mp3"
bluemonday = "/mnt/musicMount/watch/BLUES/"
honkytonktuesday = "/mnt/musicMount/watch/COUNTRY/"
bigbandwednesday = "/mnt/musicMount/watch/NOSTALGIA/"
bluegrassthursday = "/mnt/musicMount/watch/BLUEGRASS/"
soulfriday = "/mnt/musicMount/watch/JAMMIN/"
jazzsaturday = "/mnt/musicMount/watch/JAZZ_NEW_AGE/"
gospelsunday = "/mnt/musicMount/watch/GOSPEL/"
soundsofpraise = "/mnt/musicMount/watch/CHRISTIAN/"
timeless = "/mnt/musicMount/watch/timeless/"

# create an hour-long playlist for jazzsaturday
musicListGen(playlistdir, soundsofpraise, 3600, "soundsofpraise.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for jazzsaturday
musicListGen(playlistdir, gospelsunday, 3600, "gospelsunday.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for jazzsaturday
musicListGen(playlistdir, jazzsaturday, 3600, "jazzsaturday.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for bluegrassthursday
musicListGen(playlistdir, soulfriday, 3600, "soulfriday.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for bluegrassthursday
musicListGen(playlistdir, bluegrassthursday, 3600, "bluegrassthursday.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for blue monday
musicListGen(playlistdir, bluemonday, 3600, "bluemonday.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for honky tonk tuesday
musicListGen(playlistdir, honkytonktuesday, 3600, "honkytonktuesday.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for bigbandwednesday
musicListGen(playlistdir, bigbandwednesday, 3600, "bigbandwednesday.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for jazzsaturday
musicListGen(playlistdir, soundsofpraise, 3600, "soundsofpraise2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for jazzsaturday
musicListGen(playlistdir, gospelsunday, 3600, "gospelsunday2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for jazzsaturday
musicListGen(playlistdir, jazzsaturday, 3600, "jazzsaturday2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for bluegrassthursday
musicListGen(playlistdir, soulfriday, 3600, "soulfriday2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for bluegrassthursday
musicListGen(playlistdir, bluegrassthursday, 3600, "bluegrassthursday2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for blue monday
musicListGen(playlistdir, bluemonday, 3600, "bluemonday2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for honky tonk tuesday
musicListGen(playlistdir, honkytonktuesday, 3600, "honkytonktuesday2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for bigbandwednesday
musicListGen(playlistdir, bigbandwednesday, 3600, "bigbandwednesday2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# musicListGen(playlistdir, workingdir, duration, playlistname, linersdir=None, weatherfile=None, legalidfile=None, newsfile=None)

# create an hour-long playlist for mornings
musicListGen(playlistdir, timeless, 3600, "timeless.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create an hour-long playlist for mornings
musicListGen(playlistdir, timeless, 3600, "timeless2.m3u", linersdir=linersdir, legalidfile=topofhourfile)

# create a 30m top of the hour playlist for mornings with ID and news
musicListGen(playlistdir, timeless, 1800, "timelessmorningsTOH.m3u", linersdir=linersdir, weatherfile=weatherfile, legalidfile=topofhourfile)

# create a 30m bottom of the hour playlist for mornings with news and weather
musicListGen(playlistdir, timeless, 1800, "timelessmorningsBOH.m3u", linersdir=linersdir, weatherfile=weatherfile)

# create a 30m top of the hour LEGAL ID playlist for mornings with news and weather
musicListGen(playlistdir, timeless, 1800, "timelessmorningsLEGALID700.m3u", linersdir=linersdir, weatherfile=weatherfile, legalidfile=repeaterfile)

# create an hour-long playlist for legal repeater at 12:55
musicListGen(playlistdir, timeless, 3600, "timelessLEGALID1255.m3u", linersdir=linersdir, legalidfile=repeaterfile)
# create an hour-long playlist for legal repeater at 12:55
musicListGen(playlistdir, timeless, 3600, "timelessLEGALID1255_2.m3u", linersdir=linersdir, legalidfile=repeaterfile)

# create an hour-long playlist for afternoon drive
musicListGen(playlistdir, timeless, 3600, "timelessdrive.m3u", linersdir=linersdir, weatherfile=weatherfile, legalidfile=repeaterfile)

# create an hour-long playlist for afternoon drive
musicListGen(playlistdir, timeless, 3600, "timelessdrive2.m3u", linersdir=linersdir, weatherfile=weatherfile, legalidfile=repeaterfile)
