Class: Sh::Song

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Song

Returns a new instance of Song.



18
19
20
21
22
# File 'lib/sh_song.rb', line 18

def initialize path
  @path = File.expand_path path
  self.artist = Sh::Artist.new
  self.album = Sh::Album.new
end

Instance Attribute Details

#albumObject



24
25
26
# File 'lib/sh_song.rb', line 24

def album
  return @album
end

#artistObject



28
29
30
# File 'lib/sh_song.rb', line 28

def artist
  return @artist
end

#db_idObject

Returns the value of attribute db_id.



16
17
18
# File 'lib/sh_song.rb', line 16

def db_id
  @db_id
end

#lyricsObject

Returns the value of attribute lyrics.



15
16
17
# File 'lib/sh_song.rb', line 15

def lyrics
  @lyrics
end

#matchesObject (readonly)

Returns the value of attribute matches.



13
14
15
# File 'lib/sh_song.rb', line 13

def matches
  @matches
end

#mbidObject

Returns the value of attribute mbid.



15
16
17
# File 'lib/sh_song.rb', line 15

def mbid
  @mbid
end

#mimeObject (readonly)

Returns the value of attribute mime.



13
14
15
# File 'lib/sh_song.rb', line 13

def mime
  @mime
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/sh_song.rb', line 13

def path
  @path
end

#titleObject

Returns the value of attribute title.



15
16
17
# File 'lib/sh_song.rb', line 15

def title
  @title
end

#track_numObject



32
33
34
# File 'lib/sh_song.rb', line 32

def track_num
  return (@track_num || 0).to_i
end

Instance Method Details

#durationObject



36
37
38
# File 'lib/sh_song.rb', line 36

def duration
  return (@duration ||= Sh::TagReader.read(path)[:duration])
end

#lookup!Object



40
41
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
# File 'lib/sh_song.rb', line 40

def lookup!
  # Return if there is no internet connection
  return false unless Ping.pingecho("musicip.com", 5)
  
  begin
    track = lookup_multiple.first
    # Don't distinguish between bands with the same name by adding to the name
    track.artist.disambiguation = false
    # Pull data from response
    self.title = track.title
    artist.name = track.artist.to_s
    artist.mbid = track.artist.id.to_mbid.uuid
    rel = track.releases.to_a.first
    album.title = rel.title
    album.mbid = rel.id.to_mbid.uuid
    # Determine track number
    query = Webservice::Query.new
    filter = Webservice::TrackFilter.new(:artistid => artist.mbid, 
    	:releaseid => album.mbid)
    tracks = query.get_tracks(filter).entities
    tracks.to_a.each_with_index do |t, i|
    	if t.title == track.title and t.duration == track.duration
      	self.track_num = i + 1
      end
    end
  rescue Exception
    return false
  end
  
  return true
end

#read_tags!(overwrite = false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sh_song.rb', line 97

def read_tags! overwrite=false
  Sh::TagReader.read(path) do |data|
    if overwrite
      @title = data[:title]
      artist.name = data[:artist]
      album.title = data[:album]
      album.date = data[:year]
      @track_num = data[:track_num]
      @duration = data[:duration]
    else
      @title ||= data[:title]
      artist.name ||= data[:artist]
      album.title ||= data[:album]
      album.date ||= data[:year]
      @track_num ||= data[:track_num]
      @duration ||= data[:duration]
    end
  end
end

#to_htmlObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sh_song.rb', line 122

def to_html
  t = title
  t = "Unknown" if not t or t.strip == ""
  t = CGI.escapeHTML t
  ar = artist.name
  ar = "Unknown" if not ar or ar.strip == ""
  ar = CGI.escapeHTML ar
  al = album.title
  al = "Unknown" if not al or al.strip == ""
  al = CGI.escapeHTML al
  return "<b>#{t}</b> by <i>#{ar}</i> from <i>#{al}</i>"
end

#to_sObject



118
119
120
# File 'lib/sh_song.rb', line 118

def to_s
  sprintf("%s, %s, %.2d - %s", artist, album, track_num, title)
end