Advertisement
Gfy

keep_samples.py

Gfy
Sep 12th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program.  If not, see <http://www.gnu.org/licenses/>
  16.  
  17. # Modifies AirDC++ XML files
  18.  
  19. import os
  20. import sys
  21. import optparse
  22. import re
  23. from xml.dom.minidom import parse
  24.    
  25. def keep_sample_info(xmlf, output_dir):
  26.     ofile = os.path.join(output_dir, os.path.basename(xmlf))
  27.    
  28.     dom = parse(xmlf)
  29.     main = dom.getElementsByTagName("Bundle")[0]
  30.     dls = dom.getElementsByTagName("Download")
  31.     finished = dom.getElementsByTagName("Finished")
  32.    
  33.     for dl in dls:
  34.         if not re.match(".*(.avi|.mkv|.mp4)$|.*subs?.*\.sfv$", dl.getAttribute("Target")):
  35.             main.removeChild(dl)
  36.            
  37.     # remove Finished
  38.     for f in finished:
  39.         main.removeChild(f)
  40.  
  41.     with open(ofile, 'wb') as out:
  42.         print("Writing %s" % ofile)
  43.         out.write('''<?xml version="1.0" encoding="utf-8" standalone="yes"?>\n''')
  44.         d = dom.toxml()
  45.         d = d[22:]
  46.         e = ""
  47.         for line in d.split('\n'):
  48.             if line.strip() != '':
  49.                 e += line + "\n"
  50.         out.write(e.encode('utf-8'))
  51.  
  52. def main(options, args):
  53.     xmldir = args[0]
  54.     if os.path.isdir(xmldir):
  55.         # create output dir
  56.         odir = os.path.join(xmldir, options.output_dir)
  57.         try:
  58.             os.makedirs(odir)
  59.         except WindowsError:
  60.             pass
  61.        
  62.         for xfile in os.listdir(xmldir):
  63.             if xfile[-4:] == ".xml":
  64.                 keep_sample_info(os.path.join(xmldir, xfile), odir)
  65.     else:
  66.         print("Only directories are accepted.")
  67.            
  68. if __name__ == '__main__':
  69.     parser = optparse.OptionParser(
  70.         usage="Usage: %prog directory\n"
  71.         "This tool will strip all non sample related data from the xml.\n",
  72.         version="%prog 0.1 (2012-09-12)") # --help, --version
  73.  
  74.     parser.add_option("-o", dest="output_dir", metavar="DIRECTORY",
  75.                     default='output',
  76.                     help="default folder is 'output' in the xml dir")
  77.    
  78.     # no arguments given
  79.     if len(sys.argv) < 2:
  80.         print(parser.format_help())
  81.     else:
  82.         (options, args) = parser.parse_args()
  83.         main(options, args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement