class XiamiRadio::Track
Attributes
album_name[R]
artist[R]
client[R]
info[R]
radio[R]
recorded[RW]
song_id[R]
title[R]
Public Class Methods
new(track, radio:)
click to toggle source
# File lib/xiami_radio/track.rb, line 6 def initialize(track, radio:) @info = track @title, @song_id, @album_name, @artist = track.values_at(:title, :song_id, :album_name, :artist) @radio = radio @client = Client.new user: User.instance end
Public Instance Methods
downloader()
click to toggle source
# File lib/xiami_radio/track.rb, line 34 def downloader @downloader ||= Downloader.new self end
duration()
click to toggle source
# File lib/xiami_radio/track.rb, line 17 def duration @info[:length].to_f > 1 ? @info[:length].to_f : (@info[:length].to_f * 1_000_000) end
duration=(duration)
click to toggle source
# File lib/xiami_radio/track.rb, line 21 def duration=(duration) @info[:length] = duration end
fav()
click to toggle source
# File lib/xiami_radio/track.rb, line 60 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()
click to toggle source
# File lib/xiami_radio/track.rb, line 38 def file_path @info[:file_path] ||= begin downloader.start sleep 0.1 while downloader.file.nil? || File.empty?(downloader.file) downloader.file.path end end
grade?()
click to toggle source
# File lib/xiami_radio/track.rb, line 25 def grade? @info[:grade].to_i == 1 end
location(hd: true)
click to toggle source
# File lib/xiami_radio/track.rb, line 13 def location(hd: true) hd ? decode_location(hd_location) : decode_location(@info[:location]) end
reason()
click to toggle source
# File lib/xiami_radio/track.rb, line 29 def reason @info[:reason] ||= {content: '来自电台推送'} OpenStruct.new @info[:reason] end
record(point = 1)
click to toggle source
# File lib/xiami_radio/track.rb, line 46 def record(point = 1) @recorded ||= begin Thread.start do client.init_http uri = client.uri path: '/count/playrecord', query: URI.encode_www_form( sid: song_id, type:10, start_point: point, _xiamitoken: client.user.xiami_token ) client.get(uri, headers: radio.headers_referer, format: :xhtml) XiamiRadio.logger.info "#{title} record completed" true end end end
Private Instance Methods
decode_location(location)
click to toggle source
# File lib/xiami_radio/track.rb, line 73 def decode_location(location) key, tmp_url = location[0].to_i, location[1..location.length] fr, ll, lu, true_url = tmp_url.length.divmod(key), [], [], '' key.times do |i| ll << (fr[1] > 0 ? fr[0] + 1 : fr[0]) lu << tmp_url[0,ll[i]] tmp_url = tmp_url[ll[i]..tmp_url.length] fr[1] -= 1 end ll[0].times do |i| lu.each do |piece| piece[i] && true_url << piece[i] end end URI.decode(true_url).gsub('^', '0') end
hd_location()
click to toggle source
# File lib/xiami_radio/track.rb, line 93 def hd_location @info[:hd_location] ||= begin uri = client.uri path: "/song/gethqsong/sid/#{song_id}" client.get(uri, headers: radio.headers_referer)[:location] end end