#-------------------------------------------------------------------------------
# Name:        Scan Music VidcArtists to m3u
# Purpose:
#
# Author:      xtermin8r
#
# Created:     18/07/2014
# Copyright:   (c) xtermin8r 2014
# Licence:     To Kill
#-------------------------------------------------------------------------------
from itertools import groupby
import urllib, urllib2
import re
import os
from re import findall
from xml.etree import ElementTree as ET
import sys
import subprocess
import urllib

pathmpc = "E:\Appz\Media Players\MPC-HC.1.7.3.x86\mpc-hc.exe"
pathvc = "E:\Appz\Vox\VoxCommando_1.951\VoxCommando.exe"
m3ufile = open("E:\\Appz\\Vox\\payloads\\Artist_list.m3u", "wb")

xml = "E:\Appz\Vox\Payloads\musicvideos.xml"

#-------------------------------------------------------------------------------
# get comamnd line arguments
#-------------------------------------------------------------------------------
if(len(sys.argv) > 1):
    string1 = (sys.argv)
    # join list as a string
    string1 = ' '.join(string1)
    # regex commandline argument from vc
    string1 = (re.findall('.py (.*)',string1))
    #store match 0
    string1 = string1[0]
#-------------------------------------------------------------------------------
# vc stuff
#-------------------------------------------------------------------------------
#DEBUG
#string1 = 'andain'
url = urllib.URLopener()
url.open('http://127.0.0.1:8081/api/tts.speak&&playing music videos by  ' + string1)
#-------------------------------------------------------------------------------

value = []
phrase = []

def ReadXml():
    for child in root:
        global nodup
        c1 = (child.find('value').text)
        if c1 is not None:
            if string1.lower() in c1.lower():
                value = (c1.encode("UTF-8"))
                m3ufile.write(str(value) + "\n")
                #print value
                phrase.append(value)
    return

#MAIN
tree = ET.parse(xml)
root = tree.getroot()
m3ufile.write("#EXTM3U" + "\n")
ReadXml()
m3ufile.close()
subprocess.call([pathmpc, "E:\Appz\Vox\Payloads\Artist_list.m3u"])


