Class: Sh::Song
Instance Attribute Summary collapse
- #album ⇒ Object
- #artist ⇒ Object
-
#db_id ⇒ Object
Returns the value of attribute db_id.
-
#lyrics ⇒ Object
Returns the value of attribute lyrics.
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#mbid ⇒ Object
Returns the value of attribute mbid.
-
#mime ⇒ Object
readonly
Returns the value of attribute mime.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#title ⇒ Object
Returns the value of attribute title.
- #track_num ⇒ Object
Instance Method Summary collapse
- #duration ⇒ Object
-
#initialize(path) ⇒ Song
constructor
A new instance of Song.
- #lookup! ⇒ Object
- #read_tags!(overwrite = false) ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#album ⇒ Object
24 25 26 |
# File 'lib/sh_song.rb', line 24 def album return @album end |
#artist ⇒ Object
28 29 30 |
# File 'lib/sh_song.rb', line 28 def artist return @artist end |
#db_id ⇒ Object
Returns the value of attribute db_id.
16 17 18 |
# File 'lib/sh_song.rb', line 16 def db_id @db_id end |
#lyrics ⇒ Object
Returns the value of attribute lyrics.
15 16 17 |
# File 'lib/sh_song.rb', line 15 def lyrics @lyrics end |
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
13 14 15 |
# File 'lib/sh_song.rb', line 13 def matches @matches end |
#mbid ⇒ Object
Returns the value of attribute mbid.
15 16 17 |
# File 'lib/sh_song.rb', line 15 def mbid @mbid end |
#mime ⇒ Object (readonly)
Returns the value of attribute mime.
13 14 15 |
# File 'lib/sh_song.rb', line 13 def mime @mime end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/sh_song.rb', line 13 def path @path end |
#title ⇒ Object
Returns the value of attribute title.
15 16 17 |
# File 'lib/sh_song.rb', line 15 def title @title end |
#track_num ⇒ Object
32 33 34 |
# File 'lib/sh_song.rb', line 32 def track_num return (@track_num || 0).to_i end |
Instance Method Details
#duration ⇒ Object
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 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_html ⇒ Object
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_s ⇒ Object
118 119 120 |
# File 'lib/sh_song.rb', line 118 def to_s sprintf("%s, %s, %.2d - %s", artist, album, track_num, title) end |