Guest User

Untitled

a guest
Jan 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3.  
  4. #folder settings
  5.  
  6.  
  7. OUTPUT = "output"
  8.  
  9. DELIMITER = "-"
  10.  
  11. MAIN = "%(author)s " + DELIMITER + " %(album)s"
  12. CD = MAIN + " CD%(cd)d"
  13.  
  14. #file name
  15. FILE = "%(track)02.d " + DELIMITER + " %(name)s.%(ext)s"
  16.  
  17. #extra tags
  18.  
  19. COMMENT = ""
  20. URL = ""
  21.  
  22. from sys import argv
  23.  
  24. from os import listdir, makedirs
  25. from os.path import isfile, isdir, basename, join, exists
  26.  
  27. from shutil import copy
  28. from subprocess import Popen, PIPE
  29. from operator import or_
  30. from getopt import getopt, GetoptError
  31. import re
  32.  
  33. #globals
  34.  
  35. album = ""
  36. author = ""
  37. year = 0
  38. genre = ""
  39. maxcd = 0
  40.  
  41.  
  42. #reg exp
  43.  
  44. album_re = re.compile(r"(?P<author>[^\\" + DELIMITER + r"]+)" + DELIMITER + "\s*(?P<album>.+)", re.I)
  45. cd_re = re.compile(r"cd\s*(?P<cd>\d+)", re.I)
  46. title_re = re.compile(r"(?P<title>\d+)[^"+DELIMITER+"]+" + DELIMITER + "(?P<name>.+)", re.I)
  47. file_ext = re.compile(r"\.(mp3|ogg)", re.I)
  48.  
  49. #helper
  50.  
  51. def idv3v2(path, args=[]):
  52. if type(args) is not list:
  53. args = [args]
  54.  
  55. cmd = ["id3v2"] + args + [path]
  56. proc = Popen(cmd, bufsize=-1, stdout=PIPE, stderr=PIPE)
  57.  
  58. stdout, stderr = proc.communicate()
  59. if stderr.strip():
  60. print "ERROR: %s" % stderr.strip()
  61.  
  62. return stdout.strip()
  63.  
  64. def hasDir(path):
  65. return reduce(or_, [isdir(join(path,x)) for x in listdir(path)])
  66.  
  67. def getDirs(path):
  68. dirs = []
  69. for x in listdir(path):
  70. if isdir(join(path, x)):
  71. dirs.append(x)
  72.  
  73. return dirs
  74.  
  75.  
  76. def print_help():
  77. print "id3retag v0.1"
  78. print "Usage: [args] <directory>"
  79. print "Args:"
  80. print "--author=, --album=, --nocd, --year=, --genre=, --force"
  81. print "When no args are given info will be parsed from filenames"
  82. exit()
  83.  
  84. #main
  85.  
  86. def retagFolder(path, author, album, output, cd=0):
  87.  
  88. track = 1
  89.  
  90. for file in listdir(path):
  91. if not isfile(join(path, file)): continue
  92. m = file_ext.search(file)
  93. if m:
  94. ext = m.group(1)
  95. else:
  96. ext = None
  97.  
  98. if not ext:
  99. copy(join(path, file), join(output, file))
  100. continue
  101.  
  102.  
  103. data = title_re.search(file).groupdict()
  104. name = data["name"].strip()
  105. track = int(data["title"])
  106.  
  107. filename = FILE % {"track": track, "name": name, "ext": ext}
  108. print "\t" + filename
  109. dest = join(output, filename)
  110. copy(join(path, file), dest)
  111.  
  112. idv3v2(dest, "-D")
  113. cmd = ["-a", author, "-A", album, "-t", name, "-T", str(track)]
  114. if COMMENT:
  115. cmd += ["-c", COMMENT]
  116. if URL:
  117. cmd += ["--WXXX", URL]
  118. if genre is not None:
  119. number = None
  120. try:
  121. number = int(genre)
  122. except :
  123. pass
  124.  
  125. if number is not None:
  126. tag = number
  127. else:
  128. tag = genre
  129.  
  130. cmd += ["-g", tag]
  131. if year:
  132. cmd += ["-y", year]
  133. if cd:
  134. cmd += ["--TPOS", "%s/%s" % (cd, maxcd)]
  135.  
  136. idv3v2(dest, cmd)
  137.  
  138.  
  139. if __name__ == "__main__":
  140.  
  141. argv = argv[1:]
  142. try:
  143. options, args = getopt(argv, 'a:g:nfy:',
  144. ["author=", "album=", "nocd", "year=", "genre=", "force"])
  145.  
  146. for option, params in options:
  147. if option in ("-a", "--author"):
  148. author = params
  149. elif option in ("--album",):
  150. album = params
  151. elif option in ("--year", "-y"):
  152. year = params
  153. elif option in ("--genre", "-g"):
  154. genre = params
  155. elif option in ("-n","nocd"):
  156. maxcd = None
  157.  
  158. if not len(args):
  159. print_help()
  160.  
  161. except GetoptError, e:
  162. print e
  163. print_help()
  164.  
  165. path = args[0]
  166. folder = basename(path)
  167.  
  168. if not album or not author:
  169. data = album_re.search(path).groupdict()
  170. if not album: album = data["album"].strip()
  171. if not author: author = data["author"].strip()
  172.  
  173.  
  174. print "Album:", album
  175. print "Author:", author
  176.  
  177. cds = {}
  178.  
  179. if hasDir(path):
  180. folders = getDirs(path)
  181. for cd in folders:
  182. m = cd_re.search(cd).groupdict()
  183. cds[int(m["cd"])] = cd
  184. else:
  185. cds[0] = folder
  186.  
  187. maxcd = len(cds)
  188. print "CDs:", maxcd
  189.  
  190. numbers = sorted(cds.keys())
  191. for cd in numbers:
  192. if cd:
  193. output = join(OUTPUT, MAIN % {"author" : author, "album": album}, CD % {"author":author,"album": album, "cd": cd})
  194. input = join(path, cds[cd])
  195. else:
  196. output = join(OUTPUT, MAIN % {"author" : author, "album": album})
  197. input = path
  198.  
  199. if not exists(output):
  200. makedirs(output)
  201.  
  202. print "Writing CD:", output
  203.  
  204. retagFolder(input, author, album, output, cd=cd)
Add Comment
Please, Sign In to add comment