Skip to content

Instantly share code, notes, and snippets.

@ledbettj
Created December 16, 2012 15:11
Show Gist options
  • Select an option

  • Save ledbettj/4308320 to your computer and use it in GitHub Desktop.

Select an option

Save ledbettj/4308320 to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem 'hidat-ruby-echonest', git: 'git://github.com/hidat/ruby-echonest.git'
gem 'ruby-graphviz'
gem 'ruby-mp3info'
#!/usr/bin/env ruby
require 'mp3info'
require 'echonest'
require 'graphviz'
API_KEY = '' # Echonest API Key
ARTIST_THRESHOLD = 5 # Minimum number of songs required to include an artist.
MUSIC_DIR = '/home/john/Music'
e = Echonest(API_KEY)
artists = {}
nodes = {}
g = GraphViz.new(:G, :type => :digraph, :ratio => 'compress')
# this might take a while...
Dir.glob(File.join(MUSIC_DIR, '**', '*')) do |file|
next unless (file =~ /\.mp3$/i rescue nil)
Mp3Info.open(file) do |mp3|
if mp3.tag.artist
a = mp3.tag.artist.strip.downcase
artists[a] ||= 0
artists[a] += 1
end
end
end
total = artists.values.reduce(0){ |a,b| a + b }
artists.each do |name, count|
next if count < ARTIST_THRESHOLD
puts "#{name.ljust(32)} #{count}"
nodes[name] = g.add_nodes(name, {
:shape => 'circle',
:size => (count.to_f / total) * 100,
:style => 'filled',
:fillcolor => '#3366cc',
:fontcolor => '#f0f0f0',
:fontname => 'Ubuntu'
})
e.artist(name).similar['artists'].each do |row|
a = row['name'].downcase.strip
g.add_edges(nodes[name], nodes[a]) if nodes.has_key?(a) # only include artists in our collection.
end
end
g.output(:png => 'output-graph.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment