Class: XiamiRadio::Track
- Inherits:
-
Object
- Object
- XiamiRadio::Track
- Defined in:
- lib/xiami_radio/track.rb
Instance Attribute Summary collapse
-
#album_name ⇒ Object
readonly
Returns the value of attribute album_name.
-
#artist ⇒ Object
readonly
Returns the value of attribute artist.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#radio ⇒ Object
readonly
Returns the value of attribute radio.
-
#song_id ⇒ Object
readonly
Returns the value of attribute song_id.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #downloader ⇒ Object
- #duration ⇒ Object
- #duration=(duration) ⇒ Object
- #fav ⇒ Object
- #file_path ⇒ Object
- #grade? ⇒ Boolean
-
#initialize(track, radio:) ⇒ Track
constructor
A new instance of Track.
- #location(hd: false) ⇒ Object
- #reason ⇒ Object
- #record ⇒ Object
Constructor Details
#initialize(track, radio:) ⇒ Track
5 6 7 8 9 10 |
# File 'lib/xiami_radio/track.rb', line 5 def initialize(track, radio:) @info = track @title, @song_id, @album_name, @artist = track.values_at(:title, :song_id, :album_name, :artist) @radio = radio @client = radio.client end |
Instance Attribute Details
#album_name ⇒ Object (readonly)
Returns the value of attribute album_name.
3 4 5 |
# File 'lib/xiami_radio/track.rb', line 3 def album_name @album_name end |
#artist ⇒ Object (readonly)
Returns the value of attribute artist.
3 4 5 |
# File 'lib/xiami_radio/track.rb', line 3 def artist @artist end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/xiami_radio/track.rb', line 3 def client @client end |
#info ⇒ Object (readonly)
Returns the value of attribute info.
3 4 5 |
# File 'lib/xiami_radio/track.rb', line 3 def info @info end |
#radio ⇒ Object (readonly)
Returns the value of attribute radio.
3 4 5 |
# File 'lib/xiami_radio/track.rb', line 3 def radio @radio end |
#song_id ⇒ Object (readonly)
Returns the value of attribute song_id.
3 4 5 |
# File 'lib/xiami_radio/track.rb', line 3 def song_id @song_id end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
3 4 5 |
# File 'lib/xiami_radio/track.rb', line 3 def title @title end |
Instance Method Details
#downloader ⇒ Object
33 34 35 |
# File 'lib/xiami_radio/track.rb', line 33 def downloader @downloader ||= Downloader.new self end |
#duration ⇒ Object
16 17 18 |
# File 'lib/xiami_radio/track.rb', line 16 def duration @info[:length].to_f > 1 ? @info[:length].to_f : (@info[:length].to_f * 1_000_000) end |
#duration=(duration) ⇒ Object
20 21 22 |
# File 'lib/xiami_radio/track.rb', line 20 def duration=(duration) @info[:length] = duration end |
#fav ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/xiami_radio/track.rb', line 50 def fav uri = client.uri path: '/song/fav', query: URI.encode_www_form(ids: song_id, _xiamitoken: client.user.xiami_token) res = client.get(uri, headers: radio.headers_referer, format: :js) return '操作失败 (╯‵□′)╯︵┻━┻' unless res.code == '200' grade = /player_collected\('(\d)','(\d+)'\)/.match(res.body)[1] grade == '1' ? '已添加到音乐库' : '已从音乐库中移除' end |
#file_path ⇒ Object
37 38 39 40 41 42 |
# File 'lib/xiami_radio/track.rb', line 37 def file_path @info[:file_path] ||= begin downloader.start if downloader.file.nil? downloader.file.path end end |
#grade? ⇒ Boolean
24 25 26 |
# File 'lib/xiami_radio/track.rb', line 24 def grade? @info[:grade].to_i == 1 end |
#location(hd: false) ⇒ Object
12 13 14 |
# File 'lib/xiami_radio/track.rb', line 12 def location(hd: false) hd ? decode_location(hd_location) : decode_location(@info[:location]) end |
#reason ⇒ Object
28 29 30 31 |
# File 'lib/xiami_radio/track.rb', line 28 def reason @info[:reason] ||= {content: '来自电台推送'} OpenStruct.new @info[:reason] end |
#record ⇒ Object
44 45 46 47 48 |
# File 'lib/xiami_radio/track.rb', line 44 def record uri = client.uri path: '/count/playrecord', query: URI.encode_www_form(sid: song_id, type: 1, ishq: 1) client.get(uri, headers: radio.headers_referer, format: :js) end |