Guest User

tags-maker.py

a guest
Mar 6th, 2021
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os, argparse
  2.  
  3.  
  4. class TagsMaker(object):
  5.     FILENAME_OUT = '!tags.m3u'
  6.     EXCLUDED_EXTS = ['.py','.txt','.exe','.dll', '.bat', '.sh', '.m3u', '.7z', '.zip', '.rar']
  7.  
  8.     def __init__(self):
  9.         self._args = None
  10.  
  11.         self._files = []
  12.  
  13.  
  14.     def _parse(self):
  15.         description = (
  16.             "tags.m3u maker"
  17.         )
  18.         epilog = (
  19.             "Creates a semi-empty !tags.m3u file\n"
  20.             "Reverse FNV IDs if fnv.txt is provided instead\n"
  21.             "Examples:\n"
  22.             "  %(prog)s\n"
  23.         )
  24.  
  25.         parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter)
  26.         parser.add_argument('-a',  '--tag-artist',   help="Add %%ARTIST tag per file")
  27.         parser.add_argument('-T',  '--notag-title',  help="Don't add %%TITLE tag per file", action='store_true')
  28.         return parser.parse_args()
  29.  
  30.  
  31.     def _process(self):
  32.         paths = os.listdir()
  33.         paths.sort() #just in case
  34.  
  35.         for path in paths:
  36.             if not os.path.isfile(path):
  37.                 continue
  38.  
  39.             name = os.path.basename(path)
  40.             if name.startswith('.'):
  41.                 continue
  42.  
  43.             __, extension = os.path.splitext(name)
  44.             if extension in self.EXCLUDED_EXTS:
  45.                 continue
  46.  
  47.             self._files.append(name)
  48.  
  49.  
  50.     def _write(self):
  51.         with open(self.FILENAME_OUT, 'w', newline="\r\n") as outfile:
  52.             outfile.write("# @ALBUM    \n")
  53.             if not self._args.tag_artist:
  54.                 outfile.write("# @ARTIST   \n")
  55.             outfile.write("# $AUTOTRACK\n")
  56.             outfile.write("\n")
  57.  
  58.             for file in self._files:
  59.                 if self._args.tag_artist is not None: #allow empty
  60.                     if self._args.tag_artist.strip():
  61.                         outfile.write("# %%ARTIST   %s\n" % (self._args.tag_artist))
  62.                     else:
  63.                         outfile.write("# %ARTIST   \n")
  64.  
  65.                 if not self._args.notag_title:
  66.                     outfile.write("# %TITLE    \n")
  67.  
  68.                 outfile.write('%s\n' % (file))
  69.  
  70.         print("done")
  71.  
  72.  
  73.     def start(self):
  74.         self._args = self._parse()
  75.         self._process()
  76.         self._write()
  77.  
  78.  
  79. # #####################################
  80.  
  81. if __name__ == "__main__":
  82.     TagsMaker().start()
  83.  
RAW Paste Data