Module: IMP3::Commands::Genres

Included in:
IMP3::Commands
Defined in:
lib/imp3/commands/genres.rb

Instance Method Summary collapse

Instance Method Details

#genres_fetch_cache_command(options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/imp3/commands/genres.rb', line 49

def genres_fetch_cache_command(options)
  genres = {}
  (cache.artist_genres.map{|artist, tags| tags}.flatten - config.ignore_genres).each do |tag|
    genres[tag] ||= 0
    genres[tag] += 1
  end

  genres = genres.sort{|a, b| b[1] <=> a[1]}

  if genres.any?
    genre_table = table do |t|
      t.headings = 'Genre', 'Artist count'
      genres.to_a[0..[genres.size, options.limit].min].each do |g, c|
        t << [g, c]
      end
    end
    puts genre_table
  else
    puts "No genres cached yet."
  end

  summary
end

#genres_fetch_command(options) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/imp3/commands/genres.rb', line 2

def genres_fetch_command (options)
  if options.flush_cache
    cache.artist_genres.clear
    puts "Flushing artist genres cache."
  end

  tagged, requests, errors = 0, 0, 0

  progress_bar tracks do |track, bar|
    normalized_artist_name = track.artist.normalize
    begin
      unless cache.artist_genres.has_key?(normalized_artist_name)
        raise "Track has no artist name set" if track.artist.empty?
        requests += 1
        bar.refresh(:title => "Quering last.fm for '#{track.artist}'")
        response = Nokogiri(open("http://ws.audioscrobbler.com/1.0/artist/#{URI.escape(CGI.escape(track.artist))}/toptags.xml"))
        cache.artist_genres[normalized_artist_name] = response.search("//toptags/tag/name").map{|t| t.text.normalize }.uniq
        cache.save
      end
    rescue => e
      errors += 1
      bar.refresh(:title => "Error: #{e}")
      cache.artist_genres[normalized_artist_name] = nil
    end

    if (cache.artist_genres.has_key?(normalized_artist_name) and not cache.artist_genres[normalized_artist_name].nil?)
      genre = (cache.artist_genres[normalized_artist_name] - config.ignore_genres).first

      if track.genre.eql?(genre)
        bar.refresh(:title => "Skipping track #{track.object_id}. Genre is already '#{genre}'")
      else
        bar.refresh(:title => "Tagging track #{track.object_id} with genre '#{genre}'")
        tagged += 1
        begin
          track.genre = genre
        rescue => e
          errors += 1
          bar.refresh(:title => "Error: #{e}")
        end
      end
    end

    bar.increment
  end
  summary(:tracks_tagged => tagged, :requests => requests, :errors => errors)
end

#genres_ignore_add_command(genre) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/imp3/commands/genres.rb', line 91

def genres_ignore_add_command (genre)
  raise "Please specify a genre" unless genre
  genre.strip!
  config.ignore_genres << genre unless config.ignore_genres.include?(genre)
  config.save
  puts "Genre '#{genre}' added to ignore list"
end

#genres_ignore_del_command(genre) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/imp3/commands/genres.rb', line 99

def genres_ignore_del_command (genre)
  raise "Please specify a genre" unless genre
  genre.strip!
  config.ignore_genres.delete(genre) if config.ignore_genres.include?(genre)
  config.save
  puts "Genre '#{genre}' deleted from ignore list"
end

#genres_ignore_list_commandObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/imp3/commands/genres.rb', line 107

def genres_ignore_list_command
  if config.ignore_genres.any?
    genre_table = table do |t|
      t.headings = 'Genre'
      config.ignore_genres.each do |g|
        t << [g]
      end
    end
    puts genre_table
  else
    puts "No ignored genres."
  end
end

#genres_list_commandObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/imp3/commands/genres.rb', line 73

def genres_list_command
  genres = {}
  tracks.each do |track|
    genres[track.genre] ||= 0
    genres[track.genre] += 1
  end

  genre_table = table do |t|
    t.headings = 'Genre', 'Tracks'
    genres.sort{|a, b| b[1] <=> a[1]}.each do |g, c|
      t << [g, c]
    end
  end

  puts genre_table
  summary
end