Skip to content

Instantly share code, notes, and snippets.

@hryk
Created October 20, 2013 12:12
Show Gist options
  • Save hryk/7068715 to your computer and use it in GitHub Desktop.
Save hryk/7068715 to your computer and use it in GitHub Desktop.
Remove duplicate tracks from iTunes.
#!/usr/bin/env ruby
#
# Remove duplicate tracks from iTunes with
# (itunes-client)[https://github.com/katsuma/itunes-client/].
# ------------------------------------
#
require "itunes-client"
include Itunes
begin
tracks = Track.find_by(enabled: "true")
rescue JSON::ParserError => e
puts e
puts e.backtrace.join("\n")
end
# create map by name.
puts "Checking duplicate tracks..."
mapping = {}
tracks.each do |track|
if mapping.key? "#{track.artist}_#{track.album}_#{track.name}"
mapping["#{track.artist}_#{track.album}_#{track.name}"] << track
else
mapping["#{track.artist}_#{track.album}_#{track.name}"] = [track]
end
end
puts "Deleting duplicate tracks..."
mapping.each do |_, val|
if val.size > 1
val.last.delete!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment