Class: XiamiRadio::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/xiami_radio/track.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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

#artistObject (readonly)

Returns the value of attribute artist.



3
4
5
# File 'lib/xiami_radio/track.rb', line 3

def artist
  @artist
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/xiami_radio/track.rb', line 3

def client
  @client
end

#infoObject (readonly)

Returns the value of attribute info.



3
4
5
# File 'lib/xiami_radio/track.rb', line 3

def info
  @info
end

#radioObject (readonly)

Returns the value of attribute radio.



3
4
5
# File 'lib/xiami_radio/track.rb', line 3

def radio
  @radio
end

#song_idObject (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

#titleObject (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

#downloaderObject



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

def downloader
  @downloader ||= Downloader.new self
end

#durationObject



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

#favObject



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_pathObject



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

#reasonObject



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

def reason
  @info[:reason] ||= {content: '来自电台推送'}
  OpenStruct.new @info[:reason]
end

#recordObject



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