musicfix

music file renamer and tagger
git clone git://git.2f30.org/musicfix
Log | Files | Refs | README | LICENSE

commit 1a301155f38b0ad6c45ec20a2a9dc88682db0fce
parent 6d154a5e8b6af1e70895da4517ecb23cf73919b1
Author: lostd <lostd@2f30.org>
Date:   Tue,  1 Jan 2013 11:20:09 +0000

User configuration file support

Diffstat:
Mbin/musicfix | 51++++++++++++++++++++++++++++++++-------------------
1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/bin/musicfix b/bin/musicfix @@ -20,12 +20,6 @@ require 'stringex' require 'taglib' require 'yaml' -# Configuration -mdir = '/var/music/external' -mytrack = '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{d}#{fn}-#{fa}-#{ft}.#{x}"' -myimage = '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{zz}-#{fba}-#{fb}_cover.jpg"' -mycmd = '"mpc update external/#{fba}-#{my}-#{fb}-#{fv}"' - # Naming conventions # The 'f' prefix means "normalized for filename" # @@ -37,7 +31,7 @@ mycmd = '"mpc update external/#{fba}-#{my}-#{fb}-#{fv}"' # g: genre style # [f]v: release format # -# Only for mytrack: +# Only for track naming: # [f]a: track artist # [f]t: track title # [f]n: track number, may have letters (vinyls) @@ -45,7 +39,7 @@ mycmd = '"mpc update external/#{fba}-#{my}-#{fb}-#{fv}"' # tn: track number counter # x: file extension in lowercase # -# Only for myimage: +# Only for image naming: # zz: zeros that match d + n width # Concatenate artist list using '&' @@ -139,6 +133,24 @@ else tracks = ARGV[2] || nil end +# Default configuration +cfg = {} +cfg['mdir'] = '/var/music' +cfg['track'] = '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{d}#{fn}-#{fa}-#{ft}.#{x}"' +cfg['image'] = '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{zz}-#{fba}-#{fb}_cover.jpg"' +cfg['after'] = '"mpc update #{fba}-#{my}-#{fb}-#{fv}"' + +# User configuration overrides +cfgpath = File.expand_path('~/.musicfix') +if File.exists? cfgpath + new = YAML.load File.open(cfgpath, 'r') + cfg.merge! new +end + +# Print configuration +puts 'Configuration' +puts cfg.to_yaml + unless cmd == 'dump' then # Construct file list fl = Dir['*'].select {|f| File.extname(f).match /mp3|ogg|mpc|flac|wv/i}.sort @@ -147,8 +159,8 @@ unless cmd == 'dump' then exit end # Output file list - puts 'Files to process:' - puts fl + puts 'Files to process' + puts fl.to_yaml end # Initialize release info @@ -210,7 +222,8 @@ if cmd == 'dump' then exit end -# Use smaller variable names +# Variables for use in templates +mdir = cfg['mdir'] ba = rel['artist'] b = rel['album'] y = rel['year'] @@ -221,8 +234,8 @@ c = rel['comment'] fba = mkname ba fb = mkname b fv = mkname v +# Internal use only tl = rel['tracklist'] -imgurl = rel['imgurl'] # Sanity checks if tl.length != fl.length then @@ -253,7 +266,7 @@ fl.each do |ofname| ft = mkname t fn = mkname n x = File.extname(ofname).delete('.').downcase - nfname = eval mytrack + nfname = eval cfg['track'] puts "Copy track to #{nfname}" FileUtils.makedirs(File.dirname nfname) FileUtils.copy(ofname, nfname) @@ -271,18 +284,18 @@ end # Also save the first image of the artwork zz = '0' * (mkdiscnum tl.last['pos'].to_s).join.length -imgname = eval myimage -if imgurl then +imgname = eval cfg['image'] +if rel['imgurl'] then puts "Save cover to #{imgname}" - img = open(imgurl, {"User-Agent" => "Mozilla"}).read + img = open(rel['imgurl'], {"User-Agent" => "Mozilla"}).read File.open(imgname, 'wb').write img end # Execute command if provided -if mycmd then +if cfg['after'] then puts "Executing finishing command" - cmd = eval mycmd - `#{cmd}` + run = eval cfg['after'] + `#{run}` end # vim:set ts=4 sw=4 et: