VoxCommando Forum

Help and Support (Using VoxCommando) => Python Scripting => Topic started by: xtermin8r on May 20, 2014, 09:28:56 PM

Title: Python script to generate payload xml files for VC
Post by: xtermin8r on May 20, 2014, 09:28:56 PM
Python script to generate payload xml files for vc, scans directories and subdirectories also gets rid of unwanted characters.

#-------------------------------------------------------------------------------
# Name:        VC Payload Xml Generator
# 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

# Set the directories
mvpath = "E:\\Videos"
xmlpath = "E:\\Appz\\Vox\VC_187_Eden\\payloads\\musicvideos.xml"

#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(":", "")
   x = f2
   return x

#create the root </PayloadsRoot> <PayloadsRoot>
root_element = ET.Element("PayloadsRoot")

for dirName, subdirList, fileList in os.walk(mvpath):
   for fname in fileList:
       #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])
       #get rid of unwanted characters
       cfix(fname)
       fileName = x
       child.text = fileName
       #now append
       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

indent(root_element)
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()


Enjoy.
Title: Re: Python script to generate payload xml files for VC
Post by: xtermin8r on May 21, 2014, 01:16:41 PM
Latest python script for creating payload xml files for vc.

Changes:
more filtering of unwanted characters (not perfect but does a good job)
creates a text file of the phrases, this could be useful because we can command vc to show us a list of music videos for example and vc could open up an external application such as notepad (boring) and display us the phrases (voice commands).

#-------------------------------------------------------------------------------
# Name:        VC Payload Xml Generator 0.2
# Purpose:     I need it
#
# Author:      xtermin8r
#
# Created:     21/05/2014
# Copyright:   (c) xtermin8r 2014
# Licence:     to chill
#-------------------------------------------------------------------------------
import re
import os
from re import findall
from xml.etree import ElementTree as ET

# Set the directories and files
rootDir = "E:\\Videos"
xmlpath = "E:\\Appz\\Vox\\payloads\\videos.xml"
txtfile = open("E:\\Appz\\Vox\\payloads\\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("(", " ")
   f2 = f2.replace(")", " ")
   f2 = f2.replace("]", " ")
   f2 = f2.replace("[", " ")
   f2 = re.sub( '\s+', ' ', f2).strip()
   x = f2
   return x

#create the root </PayloadsRoot> <PayloadsRoot>
root_element = ET.Element("PayloadsRoot")

for dirName, subdirList, fileList in os.walk(rootDir):
   for fname in fileList:
       #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])
       #UnicodeEncodeError: 'ascii' codec can't encode character
       fname.encode('ascii', 'ignore')
       #get rid of unwanted characters
       cfix(fname)
       fileName = x
       #print(fileName)
       txtfile.write(str(fileName) + "\n")
       child.text = fileName
       #now append
       pay_element.append(child)
txtfile.close()

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

indent(root_element)
#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()



Enjoy !