commit cf63307fe89a94bd34649d607eb3931ed132a2a6
parent 1dabbe8f0a04b6154e58ba9bf5940308b6508189
Author: lostd <lostd@2f30.org>
Date: Sun, 12 Jan 2014 16:53:16 +0000
Generate release files from tags
Diffstat:
M | bin/musicfix | | | 62 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
1 file changed, 59 insertions(+), 3 deletions(-)
diff --git a/bin/musicfix b/bin/musicfix
@@ -145,11 +145,18 @@ def mkshort n
n.gsub(ftre, ft)
end
+def flatten_if_one ary
+ if ary.uniq.length == 1 then
+ return ary.first
+ end
+end
+
# Parse command line
usage = ''
usage << "Usage: musicfix [fake] relid [tracks]\n"
usage << " musicfix [fake] dump relid [relfile]\n"
usage << " musicfix [fake] load [relfile]\n"
+usage << " musicfix [fake] tags [relfile]\n"
fake = ARGV[0] == 'fake'
ARGV.delete 'fake'
cmd = ARGV[0] || (puts usage; exit)
@@ -159,6 +166,8 @@ when 'load' then
when 'dump' then
relid = ARGV[1] || (puts usage; exit)
relfile = ARGV[2] || 'release.yaml'
+when 'tags' then
+ relfile = ARGV[1] || 'release.yaml'
else
relid = ARGV[0]
tracks = ARGV[1] || nil
@@ -187,12 +196,14 @@ puts 'Configuration'
puts cfg.to_yaml
# Early file checks
-if cmd == 'dump' then
+if cmd == 'dump' or cmd == 'tags' then
if File.exists? relfile then
puts "Release file #{relfile} exists!"
exit
end
-else
+end
+
+unless cmd == 'dump' then
# Supported formats
fmtre = /mp3|ogg|m4a|mpc|flac|wv/i
# Construct file list
@@ -231,6 +242,51 @@ if cmd == 'load' then
end
puts "Loading release data..."
rel = YAML.load File.open(relfile, 'r')
+elsif cmd == 'tags' then
+ # Generate release file from audio file tags
+ rel = {}
+ rel['artist'] = []
+ rel['album'] = []
+ rel['year'] = []
+ rel['masteryear'] = nil
+ rel['genre'] = []
+ rel['format'] = nil
+ rel['comment'] = []
+ rel['imgurl'] = nil
+ rel['tracklist'] = []
+ # Populate tracklist
+ fl.each do |fname|
+ TagLib::FileRef.open(fname) do |f|
+ trk = {}
+ trk['pos'] = f.tag.track
+ trk['artist'] = f.tag.artist
+ trk['title'] = f.tag.title
+ rel['tracklist'] << trk
+ # Make lists and flatten afterwards
+ rel['artist'] << f.tag.artist
+ rel['album'] << f.tag.album
+ rel['year'] << f.tag.year
+ rel['genre'] << f.tag.genre
+ rel['comment'] << f.tag.comment
+ end
+ end
+ if rel['artist'].uniq.length == 1 then
+ # Single-artist release
+ rel['artist'] = rel['artist'].first
+ rel['tracklist'].each do |trk|
+ trk.delete 'artist'
+ end
+ else
+ rel['artist'] = 'Various'
+ end
+ # These should be the same on all files
+ rel['album'] = flatten_if_one rel['album']
+ rel['year'] = flatten_if_one rel['year']
+ rel['genre'] = flatten_if_one rel['genre']
+ rel['comment'] = flatten_if_one rel['comment']
+ # Assumptions
+ rel['masteryear'] = rel['year']
+ rel['format'] = 'CD'
else
# Get release data from Discogs
puts "Getting release data..."
@@ -275,7 +331,7 @@ end
# Output release info
puts rel.to_yaml
-if cmd == 'dump' then
+if cmd == 'dump' or cmd == 'tags' then
unless fake
File.open(relfile, 'w') do |f|
f.puts rel.to_yaml