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.



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

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

Instance Attribute Details

#albumObject



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

def album
  #@album = $db.albums(:id => @album.db_id).first if @album and @album.db_id
  return @album
end

#artistObject



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

def artist
  #@artist = $db.artists(:id => @artist.db_id).first if @artist and @artist.db_id
  return @artist
end

#db_idObject

Returns the value of attribute db_id.



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

def db_id
  @db_id
end

#lyricsObject

Returns the value of attribute lyrics.



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

def lyrics
  @lyrics
end

#matchesObject (readonly)

Returns the value of attribute matches.



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

def matches
  @matches
end

#mbidObject

Returns the value of attribute mbid.



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

def mbid
  @mbid
end

#mimeObject (readonly)

Returns the value of attribute mime.



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

def mime
  @mime
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#track_numObject



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

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

Instance Method Details

#durationObject



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

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

#lookup!Object



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

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|
      self.track_num = i + 1 if t.title == track.title and t.duration == track.duration
    end
  rescue Exception
    return false
  end
  
  return true
end

#read_tags!(overwrite = false) ⇒ Object



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

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



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sh_song.rb', line 119

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



115
116
117
# File 'lib/sh_song.rb', line 115

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