Skip to content

Instantly share code, notes, and snippets.

@crofty
Created January 4, 2009 10:34
Show Gist options
  • Save crofty/43036 to your computer and use it in GitHub Desktop.
Save crofty/43036 to your computer and use it in GitHub Desktop.
Script to tag mp3 releases using the discogs webservice.
require 'rubygems'
require 'id3lib'
require File.join(File.dirname(__FILE__),'discogs/lib/discogs.rb')
# Script to tag mp3 releases using the discogs webservice.
# It requires the discogs gem: git://github.com/crofty/discogs.git
# Usage:
# ruby tag release_folder
# Then, when prompted, give the discogs release number
if ARGV.empty?
puts "usage: ruby tag release_folder"
exit
end
unless File.exist?(ARGV[0])
puts "Invalid folder"
exit
end
Dir.chdir(ARGV[0])
starting_dir = Dir.pwd
# The directory to move the tagged files to:
ending_dir = '/Users/jamescroft/Tagged'
# Change to the directory
Dir.chdir(starting_dir)
# Get a list of the mp3's
mp3s = Dir.new(".").find_all{|file| file =~ /.mp3/}
mp3s.each{|m| puts m}
# Ask for the discogs number
puts "Enter the discogs release number"
discogs_no = $stdin.gets.chomp
# Grab the discogs release
discogs_release = Discogs::Release.new(discogs_no)
if discogs_release.tracks.length != mp3s.length
puts "The number of files is different"
puts "Continue? (Y/N)"
ans = $stdin.gets.chomp
if ans != "Y"
puts "Exiting"
exit
end
end
# Grab the image if there is one:
image_path = discogs_release.images.first.uri150 if discogs_release.images.first
`wget #{image_path} -O ./folder.jpg` if image_path
discogs_release.tracks.each_with_index do |track, i|
tag = ID3Lib::Tag.new(mp3s[i])
tag.set_frame_text(:TIT2, track.title)
tag.set_frame_text(:TRCK, track.position)
tag.set_frame_text(:TPE1, track.artist)
tag.set_frame_text(:TCON, discogs_release.genres)
tag.set_frame_text(:TYER, discogs_release.released)
tag.set_frame_text(:TALB, discogs_release.title)
tag.set_frame_text(:TPUB, discogs_release.labels)
comment = "Styles: #{discogs_release.styles.join(',')}\n"
comment << "Country: #{discogs_release.country}\n"
comment << "Formats: #{discogs_release.formats}\n"
comment << "Label: #{discogs_release.labels}\n"
comment << "Cat No: #{discogs_release.catno.to_s}\n"
comment << "Discogs ID: #{discogs_release.discog_id}\n"
tag.comment = comment
if image_path
cover = {
:id => :APIC,
:mimetype => 'image/jpeg',
:picturetype => 3,
:description => 'A',
:textenc => 0,
:data => File.read('folder.jpg')
}
tag << cover
end
tag.update!
puts "Title:" + tag.title
puts "Artist:" + tag.artist
end
# Create the label folder
label_dir = File.join(ending_dir,"#{discogs_release.labels}")
Dir.mkdir( label_dir ) unless File.exists?(label_dir)
# Move the release to the label folder
File.rename(starting_dir , File.join(label_dir,"[#{discogs_release.catno.to_s}] #{discogs_release.artists.join(", ")} - #{discogs_release.title.gsub('/','-')}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment