Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>
- # Modifies AirDC++ XML files
- import os
- import sys
- import optparse
- import re
- from xml.dom.minidom import parse
- def keep_sample_info(xmlf, output_dir):
- ofile = os.path.join(output_dir, os.path.basename(xmlf))
- dom = parse(xmlf)
- main = dom.getElementsByTagName("Bundle")[0]
- dls = dom.getElementsByTagName("Download")
- finished = dom.getElementsByTagName("Finished")
- for dl in dls:
- if not re.match(".*(.avi|.mkv|.mp4)$|.*subs?.*\.sfv$", dl.getAttribute("Target")):
- main.removeChild(dl)
- # remove Finished
- for f in finished:
- main.removeChild(f)
- with open(ofile, 'wb') as out:
- print("Writing %s" % ofile)
- out.write('''<?xml version="1.0" encoding="utf-8" standalone="yes"?>\n''')
- d = dom.toxml()
- d = d[22:]
- e = ""
- for line in d.split('\n'):
- if line.strip() != '':
- e += line + "\n"
- out.write(e.encode('utf-8'))
- def main(options, args):
- xmldir = args[0]
- if os.path.isdir(xmldir):
- # create output dir
- odir = os.path.join(xmldir, options.output_dir)
- try:
- os.makedirs(odir)
- except WindowsError:
- pass
- for xfile in os.listdir(xmldir):
- if xfile[-4:] == ".xml":
- keep_sample_info(os.path.join(xmldir, xfile), odir)
- else:
- print("Only directories are accepted.")
- if __name__ == '__main__':
- parser = optparse.OptionParser(
- usage="Usage: %prog directory\n"
- "This tool will strip all non sample related data from the xml.\n",
- version="%prog 0.1 (2012-09-12)") # --help, --version
- parser.add_option("-o", dest="output_dir", metavar="DIRECTORY",
- default='output',
- help="default folder is 'output' in the xml dir")
- # no arguments given
- if len(sys.argv) < 2:
- print(parser.format_help())
- else:
- (options, args) = parser.parse_args()
- main(options, args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement