Class: Sh::LastFM

Inherits:
Object show all
Defined in:
lib/sh_lastfm.rb

Class Method Summary collapse

Class Method Details

.get_album_info(album) ⇒ Object



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
# File 'lib/sh_lastfm.rb', line 10

def self.get_album_info album
  artist = album.artist.name
  title = album.title

  if artist and title
    image_path = File.join(Global::PATHS[:cover_dir], "#{artist.to_md5}_#{title.to_md5}")
    doc = Hpricot(@album_getInfo[:artist => artist, :album => title])

    if File.exists? image_path and File.size?(image_path) and File.size?(image_path) > 0
      album.image_path = image_path
    else
      begin
        img_url = doc.at('/lfm/album/image[@size="extralarge"]').inner_html.strip
        input = open(img_url).read
        if input and not input.empty?
          open(image_path, 'w') {|output| output << input}
          album.image_path = image_path
        end
      rescue
        album.image_path = nil
      end
    end

    (album.info ||= (doc/'/lfm/album/wiki/content').inner_text.strip.unescape_html) rescue Exception
    (album.mbid ||= (doc/'/lfm/album/mbid').inner_html.strip) rescue Exception
  end
  album.save
end

.get_artist_info(artist) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sh_lastfm.rb', line 39

def self.get_artist_info artist
  name = artist.name

  if name
    image_path = File.join(Global::PATHS[:cover_dir], name.to_md5)
    doc = Hpricot(@artist_getInfo[:artist => name])

    size = File.size?(image_path)
    if File.exists? image_path and size and size > 0
      artist.image_path = image_path
    else
      begin
        img_url = doc.at('/lfm/artist/image[@size="extralarge"]').inner_html.strip
        open(image_path, 'w') {|output| output << open(img_url).read}
        artist.image_path = image_path
      rescue
        artist.image_path = nil
      end
    end
  end

  (artist.info ||= (doc/'/lfm/artist/bio/content').inner_text.strip.unescape_html) rescue Exception
  (artist.mbid ||= (doc/'/lfm/artist/mbid').inner_html.strip) rescue Exception
end