#-------------------------------------------------------------------------------
# Name:        Scan Music Videos
# Purpose:     I need it
#
# Author:      xtermin8r
#
# Created:     21/05/2014
# Copyright:   (c) xtermin8r 2014
# Licence:
#-------------------------------------------------------------------------------
import re
import os
from re import findall
from xml.etree import ElementTree as ET
import urllib, urllib2

# Set the directories
rootDir = "E:\Videos\Music Vids"
rootDir2 = "F:\Videos\Music Vids"
xmlpath = "E:\\Appz\\Vox\\payloads\\musicvideos.xml"
txtfile = open("E:\\Appz\\Vox\\payloads\\music_videos_list.txt", "wb")

#function: get rid of unwanted characters
def cfix(zx):
    global x
    f2 = re.sub( '\s+', ' ', zx).strip()
    f2 = f2.replace(" -", " ")
    f2 = f2.replace("- ", " ")
    f2 = f2.replace(" - ", " ")
    f2 = f2.replace("-", " ")
    f2 = f2.replace(":", " ")
    f2 = f2.replace("_", " ")
    f2 = f2.replace("5.1", "")
    f2 = f2.replace(".", " ")
    f2 = f2.replace("!", " ")
    f2 = f2.replace("1080p", " ")
    f2 = f2.replace("720p", " ")
    f2 = f2.replace("x264", " ")
    f2 = f2.replace("ac3", " ")
    f2 = f2.replace("AC3", " ")
    f2 = f2.replace("DTS", " ")
    f2 = f2.replace("DVD", " ")
    f2 = f2.replace("DvD", " ")
    f2 = f2.replace("VOB", " ")
    f2 = f2.replace("rip", " ")
    f2 = f2.replace("aac", " ")
    f2 = f2.replace("Rip", " ")
    f2 = f2.replace("hd", " ")
    f2 = f2.replace("HD", " ")
    f2 = f2.replace("Hd", " ")
    f2 = f2.replace("Hq", " ")
    f2 = f2.replace("BluRay", " ")
    f2 = f2.replace("Blu Ray", " ")
    f2 = f2.replace("1080i", " ")
    f2 = f2.replace("H264", " ")
    f2 = f2.replace("h264", " ")
    f2 = f2.replace("(", " ")
    f2 = f2.replace(")", " ")
    f2 = f2.replace("]", " ")
    f2 = f2.replace("[", " ")
    f2 = f2.replace("AlbumArtSmall", " ")
    f2 = f2.replace("folder", " ")
    f2 = f2.replace("AlbumArt", " ")
    f2 = re.sub( '\s+', ' ', f2).strip()
    x = f2
    return x

#create the root </PayloadsRoot> <PayloadsRoot>
root_element = ET.Element("PayloadsRoot")


def Scan(rootDir):
    for dirName, subdirList, fileList in os.walk(rootDir):
        for fname in fileList:
            if not fname.endswith (".jpg") :
                #create the first subelemet <payload> </payload>
                pay_element = ET.SubElement(root_element, "payload")
                #create the first child <value> </value>
                child = ET.SubElement(pay_element, "value")
                child.text = dirName +"\\" + fname
                #create the second child <phrase> </phrase>
                child = ET.Element("phrase")
                #get the filename without the extension
                fname = ('.').join(fname.split('.')[:-1])
                #Call function get rid of unwanted characters
                cfix(fname)
                fileName = x

                #Debug : Display file names
                #print(fileName)

                #Write filename to text file
                txtfile.write(str(fileName) + "\n")

                #now append
                child.text = fileName
                pay_element.append(child)

                child = ET.Element("subsetmatching")
                child.text = "true"

                pay_element.append(child)

def indent(elem, level=0):
    i = "\n" + level*'\t'
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + '\t'
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

#Main
#Call function Scan directory
Scan(rootDir)
#Another directory to scan
#Scan(rootDir2)
txtfile.close()

indent(root_element)
#Debug : View xml tree
#print ( ET.tostring(root_element) )

output_file = open(xmlpath, 'w' )
output_file.write( '<?xml version="1.0" encoding="utf-8"?>'+'\n' )
output_file.write( '<!--A VoxCommando Payload file-->' +'\n' )
output_file.write( ET.tostring(root_element) )
output_file.close()

#Send tts command to vox over http
url = urllib.URLopener()
url.open('http://127.0.0.1:8081/api/tts.speak&&scan has completed,sir')