musicfix

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

commit 07223674143aa3b0a86f3703f53083d641760289
parent 3c7f3aaea59a1d29232add95fdf83c29542476d5
Author: lostd <lostd@2f30.org>
Date:   Fri,  8 Nov 2013 19:06:13 +0000

Remove discogs gem depend, use discogs json api and parse as yaml

Diffstat:
MREADME | 2+-
Mbin/musicfix | 69++++++++++++++++++++++++++++++++++++++-------------------------------
2 files changed, 39 insertions(+), 32 deletions(-)

diff --git a/README b/README @@ -13,7 +13,7 @@ to the program. Enjoy! # Dependencies * System packages: taglib - * Ruby gems: discogs-wrapper stringex taglib-ruby + * Ruby gems: stringex taglib-ruby # Usage diff --git a/bin/musicfix b/bin/musicfix @@ -6,16 +6,20 @@ require 'rubygems' require 'fileutils' require 'open-uri' -require 'discogs' require 'stringex' require 'taglib' require 'yaml' +# Headers +Ver = '0.1.3' +Headers = { + 'User-Agent' => "musicfix/#{Ver} +http://www.2f30.org/~lostd/musicfix/"} + # Concatenate artist list using '&' # Convert "Sound, The (2)" to "The Sound" # Convert "Unknown (21), The" to "The Unknown" def mkartist al - nl = al.collect {|a| a.name} + nl = al.collect {|a| a['name']} nl.each {|n| n.gsub! /\s\(\d+\)$/, ''} nl.each {|n| n.gsub! /(.*), The$/, 'The \1'} nl.each {|n| n.gsub! /\s\(\d+\)$/, ''} @@ -66,10 +70,10 @@ end # Get cover artwork def getimgurl rel - return nil unless rel.images - img = rel.images.select {|i| i.type == 'primary'}.first || - rel.images.first - img.uri + return nil unless rel['images'] + img = rel['images'].select {|i| i['type'] == 'primary'}.first || + rel['images'].first + img['uri'] end # Construct position list from tracks filter @@ -96,14 +100,14 @@ end def mkformat format f = [] # Ignore in favor of LP/EP/7"/12" descriptions - unless format.name == "Vinyl" - f << format.name + unless format['name'] == "Vinyl" + f << format['name'] end - if format.descriptions + if format['descriptions'] # Too general - format.descriptions.delete "Album" - unless format.descriptions.empty? - f << format.descriptions.first + format['descriptions'].delete "Album" + unless format['descriptions'].empty? + f << format['descriptions'].first end end f.join ' ' @@ -207,38 +211,41 @@ if cmd == 'load' then else # Get release data from Discogs puts "Geting release data..." - discogs = Discogs::Wrapper.new("musicfix") - r = discogs.get_release(relid) - mr = if r.master_id then discogs.get_master_release(r.master_id) end + r = YAML.load(open("http://api.discogs.com/release/#{relid}?f=json", + Headers))['resp']['release'] + mr = if r['master_id'] then + YAML.load(open("http://api.discogs.com/master/#{r['master_id']}?f=json", + Headers))['resp']['master'] + end # Tracklist can contain dummy header tracks, strip them - tl = r.tracklist.select {|t| t.position} + tl = r['tracklist'].select {|t| t['position']} # Apply tracks filter if given if tracks then pl = mkposlist tracks puts "Invalid tracks filter: #{tracks}" unless pl - tl = tl.select {|t| pl.include? t.position} + tl = tl.select {|t| pl.include? t['position']} end # Gather release-wide data rel = {} - rel['artist'] = mkartist r.artists - rel['album'] = r.title - rel['year'] = r.released - rel['masteryear'] = if mr then mr.year end || r.released + rel['artist'] = mkartist r['artists'] + rel['album'] = r['title'] + rel['year'] = r['released'] + rel['masteryear'] = if mr then mr['year'] end || r['released'] # Year can be full-date so keep only the year part - rel['year'] = rel['year'].slice(0..3) - rel['masteryear'] = rel['masteryear'].slice(0..3) - rel['genre'] = if r.styles then r.styles.first end || - if r.genres then r.genres.first end - rel['format'] = mkformat r.formats.first - rel['comment'] = "Discogs: #{r.id}" + rel['year'] = rel['year'].to_s.slice(0..3).to_i + rel['masteryear'] = rel['masteryear'].to_s.slice(0..3).to_i + rel['genre'] = if r['styles'] then r['styles'].first end || + if r['genres'] then r['genres'].first end + rel['format'] = mkformat r['formats'].first + rel['comment'] = "Discogs: #{r['id']}" rel['imgurl'] = getimgurl r rel['tracklist'] = [] # Populate tracklist tl.each do |s| trk = {} - trk['pos'] = s.position - trk['artist'] = mkartist s.artists if s.artists - trk['title'] = s.title + trk['pos'] = s['position'] + trk['artist'] = mkartist s['artists'] if s['artists'] + trk['title'] = s['title'] rel['tracklist'] << trk end end @@ -310,7 +317,7 @@ fl.each do |ofname| f.tag.album = b f.tag.title = t f.tag.track = tn - f.tag.year = y.to_i + f.tag.year = y f.tag.genre = g f.tag.comment = c f.save