Class: GrokITunes

Inherits:
Object
  • Object
show all
Defined in:
lib/grok_itunes.rb

Constant Summary collapse

VERSION =
'0.1.1'
BENCHMARK =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ GrokITunes

Returns a new instance of GrokITunes.



21
22
23
24
25
26
27
28
29
# File 'lib/grok_itunes.rb', line 21

def initialize(file)
  @db = nil
  @file = file
  @top = 10
  @tracks = []
  @playlists = []
  @now = Time.now
  valid_file?
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



19
20
21
# File 'lib/grok_itunes.rb', line 19

def db
  @db
end

#fileObject

Returns the value of attribute file.



19
20
21
# File 'lib/grok_itunes.rb', line 19

def file
  @file
end

#nowObject

Returns the value of attribute now.



19
20
21
# File 'lib/grok_itunes.rb', line 19

def now
  @now
end

#playlistsObject

Returns the value of attribute playlists.



19
20
21
# File 'lib/grok_itunes.rb', line 19

def playlists
  @playlists
end

#tracksObject

Returns the value of attribute tracks.



19
20
21
# File 'lib/grok_itunes.rb', line 19

def tracks
  @tracks
end

Instance Method Details

#bad_track_names(suffix) ⇒ Object



451
452
453
454
455
456
# File 'lib/grok_itunes.rb', line 451

def bad_track_names(suffix)
  @db.execute("select name, location from tracks 
                where lower(name) like '%#{suffix}' 
                and podcast is null
                order by name")
end

#bitrate_histogramObject



500
501
502
503
504
505
# File 'lib/grok_itunes.rb', line 500

def bitrate_histogram
  @db.execute("select count(bit_rate) as count, bit_rate from tracks
                where bit_rate not null
                group by bit_rate
                order by bit_rate")
end

#clean_tracksObject



458
459
460
461
462
# File 'lib/grok_itunes.rb', line 458

def clean_tracks
  @db.execute("select name, artist, album, clean from tracks
                where clean = 'true'
                order by artist, album, name")
end

#count_albumsObject



333
334
335
336
# File 'lib/grok_itunes.rb', line 333

def count_albums
  @db.execute("select count(distinct album) as count from tracks 
                where album not null and album != ''")[0].first.to_i
end

#count_artistsObject



328
329
330
331
# File 'lib/grok_itunes.rb', line 328

def count_artists
  @db.execute("select count(distinct artist) as count from tracks
                where artist not null and artist != ''")[0].first.to_i
end

#count_tracksObject



338
339
340
# File 'lib/grok_itunes.rb', line 338

def count_tracks
  @db.execute("select count(*) from tracks")[0].first.to_i
end

#disk_pigsObject



526
527
528
529
530
# File 'lib/grok_itunes.rb', line 526

def disk_pigs
  @db.execute("select size,artist,album,name from tracks
                where size is not null
                order by size desc limit #{@top}")
end

#dj_itis_skip_happyObject



532
533
534
535
536
# File 'lib/grok_itunes.rb', line 532

def dj_itis_skip_happy
  @db.execute("select artist,album,name,skip_count,play_count from tracks
                where skip_count >= play_count
                order by skip_count,play_count,artist,album,name")
end

#explicit_tracksObject



464
465
466
467
468
# File 'lib/grok_itunes.rb', line 464

def explicit_tracks
  @db.execute("select name, artist, album, explicit from tracks
                where explicit = 'true'
                order by artist, album, name")
end

#find_buggy_itunes_filesObject



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/grok_itunes.rb', line 427

def find_buggy_itunes_files
  # iTune's has an evil bug that replicates leading numbers
  # for example: '01 01 01 somefile.mp3' then you click on it and iTunes transmutes it into
  # '01 01 01 01 somefile.mp3'. Every time you click! Bad!
  # But if you rename it back to '01 somefile.mp3' then the madness stops
  @db.execute("select artist,album,name,location from tracks
                where name like '01 01 %' 
                  or name like '02 02 %' 
                  or name like '03 03 %' 
                  or name like '04 04 %' 
                  or name like '05 05 %' 
                  or name like '06 06 %'
                  or name like '07 07 %' 
                  or name like '08 08 %'
                  or name like '09 09 %'
                order by artist, album, name")
end

#find_trailing_spaces(thing) ⇒ Object



445
446
447
448
449
# File 'lib/grok_itunes.rb', line 445

def find_trailing_spaces(thing)
  @db.execute("select #{thing} from tracks 
                where #{thing} like '% ' 
                order by #{thing}")
end

#fix_type(value, type) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/grok_itunes.rb', line 233

def fix_type(value, type)
  # Sanitize String data into proper Class before injection 
  if type =~ /date/ then
    # sqlite3 doesn't understand ISO 8601 ( YYYY/MM/DDTHH:MM:SSZ$ )
    value = value.sub(/Z/, '').to_s
    # Time.parse(value)
  elsif type =~ /integer/ then
    value.to_i
  elsif type =~ /string/ then
    value.to_s
  elsif type =~ /boolean/ then
    if value == "true"
      true
    elsif value == "false"
      false
    else
      raise NonBooleanValueException, "Really now? True or False?: #{value}"
    end
  else
    raise UnknownDataTypeException, "I don't know how to treat type: #{type}"
  end
end

#genre_histogramObject



507
508
509
510
511
512
# File 'lib/grok_itunes.rb', line 507

def genre_histogram
  @db.execute("select count(genre) as count, genre from tracks
                where genre is not null
                group by genre
                order by count desc")
end

#human_clock_time(seconds) ⇒ Object

Based on seconds_fraction_to_time snippets.dzone.com/posts/show/5958



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/grok_itunes.rb', line 214

def human_clock_time(seconds)
  days = hours = mins = 0
  if seconds >= 60 then
    mins = ( seconds / 60 ).to_i
    seconds = ( seconds % 60 ).to_i
    
    if mins >= 60 then
      hours = ( mins / 60 ).to_i
      mins = ( mins % 60 ).to_i
    end
    
    if hours >= 24 then
      days = ( hours / 24 ).to_i
      hours = ( hours % 24 ).to_i
    end
  end
  "#{days.to_s.rjust(3,"0")}:#{hours.to_s.rjust(2,"0")}:#{mins.to_s.rjust(2,"0")}:#{seconds.to_s.rjust(2,"0")}"
end

#init_dbObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/grok_itunes.rb', line 42

def init_db
  puts "init_db: start #{Time.now}" if BENCHMARK
  
  db_file = File.expand_path(@file) + ".db"

  @now = Time.now
  @db = SQLite3::Database.new("#{db_file}")
  @db.type_translation = true

  @db.execute( "drop table if exists tracks_playlists" )
  @db.execute( "drop table if exists playlists" )
  @db.execute( "drop table if exists tracks" )

  create_tables = <<SQL
    create table tracks (
      track_id                INTEGER PRIMARY KEY,
      name                    VARCHAR DEFAULT NULL,
      artist                  VARCHAR DEFAULT NULL,
      album_artist            VARCHAR DEFAULT NULL,
      composer                VARCHAR DEFAULT NULL,
      album                   VARCHAR DEFAULT NULL,
      grouping                VARCHAR DEFAULT NULL,
      genre                   VARCHAR DEFAULT NULL,
      kind                    VARCHAR DEFAULT NULL,
      size                    INTEGER DEFAULT NULL,
      total_time              INTEGER DEFAULT NULL,
      disc_number             INTEGER DEFAULT NULL,
      disc_count              INTEGER DEFAULT NULL,
      track_number            INTEGER DEFAULT NULL,
      track_count             INTEGER DEFAULT NULL,
      year                    INTEGER DEFAULT NULL,
      bpm                     INTEGER DEFAULT NULL,
      date_modified           DATE DEFAULT NULL,
      date_added              DATE DEFAULT NULL,
      bit_rate                INTEGER DEFAULT NULL,
      sample_rate             INTEGER DEFAULT NULL,
      volume_adjustment       INTEGER DEFAULT NULL,
      part_of_gapless_album   BOOLEAN DEFAULT NULL,
      equalizer               VARCHAR DEFAULT NULL,
      comments                VARCHAR DEFAULT NULL,
      play_count              INTEGER DEFAULT NULL,
      play_date               INTEGER DEFAULT NULL,
      play_date_utc           DATE DEFAULT NULL,
      skip_count              INTEGER DEFAULT NULL,
      skip_date               DATE DEFAULT NULL,
      release_date            DATE DEFAULT NULL,
      rating                  INTEGER DEFAULT NULL,
      album_rating            INTEGER DEFAULT NULL,
      album_rating_computed   BOOLEAN DEFAULT NULL,
      compilation             BOOLEAN DEFAULT NULL,
      series                  VARCHAR DEFAULT NULL,
      season                  INTEGER DEFAULT NULL,
      episode                 VARCHAR DEFAULT NULL,
      episode_order           INTEGER DEFAULT NULL,
      sort_album              VARCHAR DEFAULT NULL,
      sort_album_artist       VARCHAR DEFAULT NULL,
      sort_artist             VARCHAR DEFAULT NULL,
      sort_composer           VARCHAR DEFAULT NULL,
      sort_name               VARCHAR DEFAULT NULL,
      sort_series             VARCHAR DEFAULT NULL,
      artwork_count           INTEGER DEFAULT NULL,
      persistent_id           VARCHAR DEFAULT NULL,
      disabled                BOOLEAN DEFAULT NULL,
      clean                   BOOLEAN DEFAULT NULL,
      explicit                BOOLEAN DEFAULT NULL,
      track_type              VARCHAR DEFAULT NULL,
      podcast                 BOOLEAN DEFAULT NULL,
      has_video               BOOLEAN DEFAULT NULL,
      hd                      BOOLEAN DEFAULT NULL,
      video_width             INTEGER DEFAULT NULL,
      video_height            INTEGER DEFAULT NULL,
      movie                   BOOLEAN DEFAULT NULL,
      tv_show                 BOOLEAN DEFAULT NULL,
      protected               BOOLEAN DEFAULT NULL,
      purchased               BOOLEAN DEFAULT NULL,
      unplayed                BOOLEAN DEFAULT NULL,
      file_type               INTEGER DEFAULT NULL,
      file_creator            INTEGER DEFAULT NULL,
      location                VARCHAR DEFAULT NULL,
      file_folder_count       INTEGER DEFAULT NULL,
      library_folder_count    INTEGER DEFAULT NULL
    );

    create table playlists (
      playlist_id             INTEGER PRIMARY KEY,
      master                  BOOLEAN DEFAULT NULL,
      name                    VARCHAR DEFAULT NULL,
      playlist_persistent_id  VARCHAR DEFAULT NULL,
      distinguished_kind      INTEGER DEFAULT NULL,
      folder                  BOOLEAN DEFAULT NULL,
      visible                 BOOLEAN DEFAULT NULL,
      audiobooks              BOOLEAN DEFAULT NULL,
      movies                  BOOLEAN DEFAULT NULL,
      music                   BOOLEAN DEFAULT NULL,
      party_shuffle           BOOLEAN DEFAULT NULL,
      podcasts                BOOLEAN DEFAULT NULL,
      purchased_music         BOOLEAN DEFAULT NULL,
      tv_shows                BOOLEAN DEFAULT NULL,
      all_items               BOOLEAN DEFAULT NULL,
      genius_track_id         BOOLEAN DEFAULT NULL,
      smart_info              BLOB DEFAULT NULL,
      smart_criteria          BLOB DEFAULT NULL
    );

    create table tracks_playlists (
      track_id                INTEGER,
      playlist_id             INTEGER
    );
SQL

  @db.execute_batch( create_tables )
  puts "init_db: stop  #{Time.now}" if BENCHMARK
end

#missing_genreObject



520
521
522
523
524
# File 'lib/grok_itunes.rb', line 520

def missing_genre
  @db.execute("select artist,album,name from tracks
                where genre is null or genre = ''
                order by artist,album,name")
end

#missing_track_numbersObject



514
515
516
517
518
# File 'lib/grok_itunes.rb', line 514

def missing_track_numbers
  @db.execute("select artist,album,name from tracks
                where track_number is null and has_video is null
                order by artist,album,name")
end

#never_playedObject



493
494
495
496
497
498
# File 'lib/grok_itunes.rb', line 493

def never_played
  # QUESTION? How can you rate something if you never played it?
  @db.execute("select artist,album,name from tracks
                where play_count is null and skip_count is null
                order by artist,album,name")
end

#oldest_tracksObject



362
363
364
365
366
# File 'lib/grok_itunes.rb', line 362

def oldest_tracks
  @db.execute("select datetime(play_date_utc), name from tracks
                where play_date_utc not null and play_date_utc != ''
                order by play_date_utc").first(@top)
end

#parse_xmlObject

Based on work by zenspider @ snippets.dzone.com/posts/show/3700



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/grok_itunes.rb', line 258

def parse_xml
  puts "parse_xml: start #{Time.now}" if BENCHMARK
  
  is_tracks = is_playlists = false

  track = {}
  playlist = {}
  playitems = []

  IO.foreach(File.expand_path(@file)) do |line|
    if line =~ /^\t<key>Tracks<\/key>$/
      is_tracks = true
      next
    end

    if is_tracks then
      if line =~ /^\t\t\t<key>(.*)<\/key><(date|integer|string)>(.*)<\/.*>$/ then
        key = $1.downcase.split.join("_").intern
        track[key] = fix_type($3, $2)
        next
      elsif line =~ /^\t\t\t<key>(.*)<\/key><(true|false)\/>$/ then
        key = $1.downcase.split.join("_").intern
        track[key] = fix_type($2, "boolean")
        next
      elsif line =~ /^\t\t<\/dict>$/ then
        @tracks.push(track.dup)
        track.clear
        next
      elsif is_tracks && line =~ /^\t<\/dict>$/
        is_tracks = false
        next
      else
        next
      end
    end

    if line =~ /^\t<key>Playlists<\/key>$/
      is_playlists = true
      next
    end
    
    if is_playlists then
      if line =~ /^\t\t\t<key>(.*)<\/key><(date|integer|string)>(.*)<\/.*>$/ then
        key = $1.downcase.split.join("_").intern
        playlist[key] = fix_type($3, $2)
        next
      elsif line =~ /^\t\t\t<key>(.*)<\/key><(true|false)\/>$/ then
        key = $1.downcase.split.join("_").intern
        playlist[key] = fix_type($2, "boolean")
        next
      elsif line =~ /^\t\t\t\t\t<key>Track ID<\/key><integer>(.*)<\/integer>$/ then
        playitems.push($1.to_i)
        next
      elsif line =~ /^\t\t<\/dict>$/ then
        @playlists.push([playlist.dup, playitems.dup])
        playlist.clear
        playitems.clear
        next
      elsif is_playlists && line =~ /^\t<\/array>$/
        is_playlists = false
        next
      else
        next
      end
    end
  end

  puts "parse_xml: stop  #{Time.now}" if BENCHMARK
end

#podcast_without_genreObject



476
477
478
479
480
# File 'lib/grok_itunes.rb', line 476

def podcast_without_genre
  @db.execute("select name,artist from tracks
                where podcast = 'true' and genre is null or genre = ''
                order by name")
end

#populate_dbObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/grok_itunes.rb', line 156

def populate_db
  puts "populate_db: start #{Time.now}" if BENCHMARK
  @db.transaction do |db|
    db.prepare( 
      "insert into tracks values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 
        ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 
        ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 
        ?, ?, ?, ?, ?, ? )" 
    ) do |stmt|
      @tracks.each do |t|
        stmt.execute t[:track_id], "#{t[:name]}", "#{t[:artist]}", 
        "#{t[:album_artist]}", "#{t[:composer]}", "#{t[:album]}", 
        "#{t[:grouping]}", "#{t[:genre]}", "#{t[:kind]}", t[:size], 
        t[:total_time], t[:disc_number], t[:disc_count], t[:track_number], 
        t[:track_count], t[:year], t[:bpm], t[:date_modified], t[:date_added], 
        t[:bit_rate], t[:sample_rate], t[:volume_adjustment], 
        t[:part_of_gapless_album], "#{t[:equalizer]}", "#{t[:comments]}", 
        t[:play_count], t[:play_date], t[:play_date_utc], t[:skip_count], 
        t[:skip_date], t[:release_date], t[:rating], t[:album_rating], 
        t[:album_rating_computed], t[:compilation], t[:series], t[:season], 
        t[:episode], t[:episode_order], "#{t[:sort_album]}", 
        "#{t[:sort_album_artist]}", "#{t[:sort_artist]}", "#{t[:sort_composer]}", 
        "#{t[:sort_name]}", "#{t[:sort_series]}", t[:artwork_count], 
        "#{t[:persistent_id]}", t[:disabled], t[:clean], t[:explicit], 
        "#{t[:track_type]}", t[:podcast], t[:has_video], t[:hd], t[:video_width], 
        t[:video_height], t[:movie], t[:tv_show], t[:protected], t[:purchased], 
        t[:unplayed], t[:file_type], t[:file_creator], t[:location], 
        t[:file_folder_count], t[:library_folder_count]
      end
    end

    db.prepare(
      "insert into playlists values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, 
      ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    ) do |stmt|
      @playlists.each do |p|
        stmt.execute p[0][:playlist_id], p[0][:master], "#{p[0][:name]}", 
        "#{p[0][:playlist_persistent_id]}", p[0][:distinguished_kind], p[0][:folder], 
        p[0][:visible], p[0][:audiobooks], p[0][:movies], p[0][:music], p[0][:party_shuffle], 
        p[0][:podcasts], p[0][:purchased_music], p[0][:tv_shows], p[0][:all_items], 
        p[0][:genius_track_id], p[0][:smart_info], p[0][:smart_criteria]
      end
    end

    db.prepare(
      "insert into tracks_playlists values ( ?, ? )"
    ) do |stmt|
      @playlists.each do |p|
        p[1].each do |tracks|
          stmt.execute p[0][:playlist_id], tracks
        end
      end
    end
  end
  puts "populate_db: stop  #{Time.now}" if BENCHMARK
end

#rating_fanaticObject

Shame, scrobbler only does last.fm API Version 1.0 def related_tracks_to(track,artist) end



410
411
412
413
414
415
416
417
418
419
# File 'lib/grok_itunes.rb', line 410

def rating_fanatic
  base = @db.execute("select rating, count(rating), count(name) from tracks
                        group by rating")
  ratings = []
  base.each do |base|
    percent = "%.4f" % ( base[2].to_f / @tracks.size.to_f )
    ratings.push( [ base[0].to_i ||= 0, base[1].to_i, base[2].to_i, @tracks.size, percent.to_f ] )
  end
  ratings
end

I don’t trust this function yet since it always passes and Scrobbler kicks this error msg /Library/Ruby/Gems/1.8/gems/scrobbler-0.2.0/lib/scrobbler/base.rb:21: warning: instance variable @similar not initialized

Works from irb and is way cool!



397
398
399
400
401
402
403
404
# File 'lib/grok_itunes.rb', line 397

def related_artists_to(artist)
  artist = Scrobbler::Artist.new(artist)
  results = []
  artist.similar.each do |a| 
    results.push( [ "#{a.match.dup}", "#{a.name.dup}" ] )
  end
  results.first(@top)
end

#screaming_namesObject



538
539
540
541
542
543
544
545
546
547
548
# File 'lib/grok_itunes.rb', line 538

def screaming_names
  results = []
  @tracks.each do |track|
    if track[:name] then
      if track[:name] =~ /^[A-Z\s]*$/ then
        results.push( [ track[:name].dup, track[:artist].dup, track[:album].dup ] )
      end
    end
  end
  results
end

#small_files(size) ⇒ Object



421
422
423
424
425
# File 'lib/grok_itunes.rb', line 421

def small_files(size)
  @db.execute("select size,name,artist,location from tracks 
                where size <= #{size}
                order by size")
end

#spam_tunesObject



482
483
484
485
486
487
488
489
490
491
# File 'lib/grok_itunes.rb', line 482

def spam_tunes
  @db.execute("select artist,album,name from tracks
                where lower(name) like '%http:%'
                  or lower(name) like '%www.%'
                  or lower(artist) like '%http:%'
                  or lower(artist) like '%www.%'
                  or lower(album) like '%http:%'
                  or lower(album) like '%www.%'
                order by artist,album,name")
end

#top_artistsObject



349
350
351
352
353
354
# File 'lib/grok_itunes.rb', line 349

def top_artists
  @db.execute("select count(artist) as count, artist from tracks
                where artist not null
                group by artist
                order by count desc").first(@top)
end

#top_genresObject



357
358
359
360
# File 'lib/grok_itunes.rb', line 357

def top_genres
  @db.execute("select count(genre) as count, genre from tracks
                group by genre order by count desc, genre limit #{@top}")
end

#top_tracks_aging_wellObject

Based on work by zenspider @ snippets.dzone.com/posts/show/3700



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/grok_itunes.rb', line 375

def top_tracks_aging_well
  results = []
  now = Time.now
  rating = 0
  records = @db.execute("select rating, play_count, name, artist, 
      datetime(play_date_utc) from tracks
      where rating not null and play_count not null and name not null 
      and artist not null and play_date_utc not null
      order by rating, play_count")
  records.each do |rec|
    rating = rec[0].to_i * rec[1].to_i * Math.log( ((@now - Time.parse(rec[4])) / 86400.0).to_i )
    results.push([ rating.to_i, rec[2], rec[3], rec[4] ])
  end
  results.sort.reverse.first(@top)
end

#top_tracks_on_rating_times_playcountObject



368
369
370
371
372
# File 'lib/grok_itunes.rb', line 368

def top_tracks_on_rating_times_playcount
  @db.execute("select rating*play_count as count, name, artist, 
                datetime(play_date_utc) from tracks 
                order by count desc").first(@top)
end

#total_timeObject



342
343
344
345
346
347
# File 'lib/grok_itunes.rb', line 342

def total_time
  human_clock_time( 
    ( @db.execute("select sum(total_time) from tracks 
                  where total_time not null")[0].first.to_i )/1000.00
  )
end

#tracks_not_ratedObject



470
471
472
473
474
# File 'lib/grok_itunes.rb', line 470

def tracks_not_rated
  @db.execute("select album,artist,name from tracks
                where rating is null
                order by album,artist,name")
end

#treemap_bitrateObject



550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/grok_itunes.rb', line 550

def treemap_bitrate
  root = Treemap::Node.new
  self.bitrate_histogram.each do |bitrate|
    root.new_child(:size => bitrate[0].to_i, :label => "#{bitrate[1]}")
  end
  output = Treemap::HtmlOutput.new do |o|
    o.width = 1024
    o.height = 768
    o.center_labels_at_depth = 1
  end
  File.open('/tmp/bitrate_treemap.html', 'w') {|f| f.write(output.to_html(root)) }
end

#treemap_genreObject



563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/grok_itunes.rb', line 563

def treemap_genre
  root = Treemap::Node.new
  self.genre_histogram.each do |genre|
    root.new_child(:size => genre[0].to_i, :label => "#{genre[1]}")
  end
  output = Treemap::HtmlOutput.new do |o|
    o.width = 1024
    o.height = 768
    o.center_labels_at_depth = 1
  end
  File.open('/tmp/genre_treemap.html', 'w') {|f| f.write(output.to_html(root)) }
end

#valid_file?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
# File 'lib/grok_itunes.rb', line 31

def valid_file?
  unless File.exist?(File.expand_path(@file))
    raise FileNotFoundException, "File not found: #{@file}"
  end
  if File.size?(File.expand_path(@file)) == nil
    raise FileEmptyException, "File has no content: #{@file}"
  else
    true
  end
end