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
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 |
#artist ⇒ Object
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_id ⇒ Object
Returns the value of attribute db_id.
15 16 17 |
# File 'lib/sh_song.rb', line 15 def db_id @db_id end |
#lyrics ⇒ Object
Returns the value of attribute lyrics.
14 15 16 |
# File 'lib/sh_song.rb', line 14 def lyrics @lyrics end |
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
12 13 14 |
# File 'lib/sh_song.rb', line 12 def matches @matches end |
#mbid ⇒ Object
Returns the value of attribute mbid.
14 15 16 |
# File 'lib/sh_song.rb', line 14 def mbid @mbid end |
#mime ⇒ Object (readonly)
Returns the value of attribute mime.
12 13 14 |
# File 'lib/sh_song.rb', line 12 def mime @mime end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/sh_song.rb', line 12 def path @path end |
#title ⇒ Object
Returns the value of attribute title.
14 15 16 |
# File 'lib/sh_song.rb', line 14 def title @title end |
#track_num ⇒ Object
33 34 35 |
# File 'lib/sh_song.rb', line 33 def track_num return (@track_num || 0).to_i end |
Instance Method Details
#duration ⇒ Object
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 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
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_s ⇒ Object
115 116 117 |
# File 'lib/sh_song.rb', line 115 def to_s sprintf("%s, %s, %.2d - %s", artist, album, track_num, title) end |