Class: VkMusic::Audio

Inherits:
Object
  • Object
show all
Defined in:
lib/vk_music/audio.rb

Overview

Class representing VK audio.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Audio

Initialize new audio.

Parameters:

  • options (Hash) (defaults to: {})

    hash with options.

Options Hash (options):

  • :id (Integer, nil)
  • :owner_id (Integer, nil)
  • :secret_1 (String, nil)
  • :secret_2 (String, nil)
  • :artist (String)
  • :title (String)
  • :duration (Integer)
  • :url_encoded (String, nil)
  • :url (String, nil)
  • :client_id (Integer, nil)


140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/vk_music/audio.rb', line 140

def initialize(options = {})
  @id          = Utility.unless_nil_to Integer, options[:id]
  @owner_id    = Utility.unless_nil_to Integer, options[:owner_id]
  @secret_1    = Utility.unless_nil_to String, options[:secret_1]
  @secret_2    = Utility.unless_nil_to String, options[:secret_2]
  @artist      = options[:artist].to_s
  @title       = options[:title].to_s
  @duration    = options[:duration].to_i
  @url_encoded = Utility.unless_nil_to String, options[:url_encoded]
  @url         = Utility.unless_nil_to String, options[:url]
  @client_id   = Utility.unless_nil_to Integer, options[:client_id]
end

Instance Attribute Details

#artistString (readonly)

Returns name of artist.

Returns:

  • (String)

    name of artist.



27
28
29
# File 'lib/vk_music/audio.rb', line 27

def artist
  @artist
end

#durationInteger (readonly)

Returns duration of track in seconds.

Returns:

  • (Integer)

    duration of track in seconds.



35
36
37
# File 'lib/vk_music/audio.rb', line 35

def duration
  @duration
end

#idInteger? (readonly)

Returns ID of audio.

Returns:

  • (Integer, nil)

    ID of audio.



15
16
17
# File 'lib/vk_music/audio.rb', line 15

def id
  @id
end

#owner_idInteger? (readonly)

Returns ID of audio owner.

Returns:

  • (Integer, nil)

    ID of audio owner.



19
20
21
# File 'lib/vk_music/audio.rb', line 19

def owner_id
  @owner_id
end

#secret_1String? (readonly)

Returns part of secret hash which used when using act=reload_audio.

Returns:

  • (String, nil)

    part of secret hash which used when using act=reload_audio.



23
24
25
# File 'lib/vk_music/audio.rb', line 23

def secret_1
  @secret_1
end

#secret_2String? (readonly)

Returns part of secret hash which used when using act=reload_audio.

Returns:

  • (String, nil)

    part of secret hash which used when using act=reload_audio.



23
24
25
# File 'lib/vk_music/audio.rb', line 23

def secret_2
  @secret_2
end

#titleString (readonly)

Returns title of song.

Returns:

  • (String)

    title of song.



31
32
33
# File 'lib/vk_music/audio.rb', line 31

def title
  @title
end

#url_encodedString? (readonly)

Returns encoded download URL.

Returns:

  • (String, nil)

    encoded download URL.



56
57
58
# File 'lib/vk_music/audio.rb', line 56

def url_encoded
  @url_encoded
end

Class Method Details

.from_data(data, client_id) ⇒ Audio

Initialize new audio from VK data array.

Parameters:

  • data (Array)
  • client_id (Integer)

Returns:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/vk_music/audio.rb', line 184

def self.from_data(data, client_id)
  url_encoded = data[2].to_s
  
  secrets = data[13].to_s.split("/")

  new({
    :id => data[0].to_i,
    :owner_id => data[1].to_i,
    :secret_1 => secrets[3],
    :secret_2 => secrets[5],
    :artist => CGI.unescapeHTML(data[4]),
    :title => CGI.unescapeHTML(data[3]),
    :duration => data[5].to_i,
    :url_encoded => url_encoded,
    :url => nil,
    :client_id => client_id
  })
end

.from_node(node, client_id) ⇒ Audio

Initialize new audio from Nokogiri HTML node.

Parameters:

  • node (Nokogiri::XML::Node)

    node, which match following CSS selector: .audio_item.ai_has_btn

  • client_id (Integer)

Returns:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/vk_music/audio.rb', line 160

def self.from_node(node, client_id)
  url_encoded = node.at_css("input").attribute("value").to_s
  url_encoded = nil if url_encoded == Constants::URL::VK[:audio_unavailable]
  id_array = node.attribute("data-id").to_s.split("_")
  
  new({
    :id => id_array[1].to_i,
    :owner_id => id_array[0].to_i,
    :artist => node.at_css(".ai_artist").text.strip,
    :title => node.at_css(".ai_title").text.strip,
    :duration => node.at_css(".ai_dur").attribute("data-dur").to_s.to_i,
    :url_encoded => url_encoded.to_s,
    :url => nil,
    :client_id => client_id
  })
end

Instance Method Details

#ppString

Returns extended information about audio.

Returns:

  • (String)

    extended information about audio.



84
85
86
# File 'lib/vk_music/audio.rb', line 84

def pp
  "#{to_s} (Able to get decoded URL: #{url_available? ? "yes" : "no"}, able to get URL from VK: #{url_accessable? ? "yes" : "no"})"
end

#to_sString

Returns information about audio.

Returns:

  • (String)

    information about audio.



78
79
80
# File 'lib/vk_music/audio.rb', line 78

def to_s
  "#{@artist} - #{@title} [#{Utility.format_seconds(@duration)}]"
end

#update_url(decoded_url) ⇒ self #update_url(audio) ⇒ self #update_url(options) ⇒ self

Update audio URLs.

Overloads:

  • #update_url(decoded_url) ⇒ self

    Simply save download URL.

    Parameters:

    • decoded_url (String)
  • #update_url(audio) ⇒ self

    Copy URLs from this audio (no checks applied).

    Parameters:

  • #update_url(options) ⇒ self

    Parameters:

    • options (Hash)

      hash with options.

    Options Hash (options):

    • :url (String, nil)

      decoded download URL.

    • :url_encoded (String, nil)

      decoded download URL.

    • :client_id (String, nil)

      decoded download URL.

Returns:

  • (self)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vk_music/audio.rb', line 106

def update_url(arg)
  case arg
    when String
      @url = arg
    when Audio
      # Only save URL if it's already decoded and cached
      @url = arg.url if arg.url_cached?
      @url_encoded = arg.url_encoded
      @client_id = arg.client_id
    when Hash
      @url_encoded = Utility.unless_nil_to String, options[:url_encoded]
      @url         = Utility.unless_nil_to String, options[:url]
      @client_id   = Utility.unless_nil_to Integer, options[:client_id]
    else
      raise ArgumentError, "Bad arguments", caller
  end
  self
end

#urlString?

Access decoded download URL.

If link was already decoded, returns cached value. Else decodes existing link. If no link can be provided, returns nil.

Returns:

  • (String, nil)

    decoded download URL or nil if not available.



44
45
46
47
48
49
50
51
52
# File 'lib/vk_music/audio.rb', line 44

def url
  if url_cached?
    @url
  elsif @url_encoded && @client_id
    @url = VkMusic::LinkDecoder.unmask_link(@url_encoded, @client_id)
  else
    @url # nil
  end
end

#url_accessable?Boolean

Returns whether it’s possible to get download URL with Client#from_id.

Returns:

  • (Boolean)

    whether it’s possible to get download URL with Client#from_id.



72
73
74
# File 'lib/vk_music/audio.rb', line 72

def url_accessable?
  !!(@id && @owner_id && @secret_1 && @secret_2)
end

#url_available?Boolean

Returns whether able to get download URL without web requests.

Returns:

  • (Boolean)

    whether able to get download URL without web requests.



66
67
68
# File 'lib/vk_music/audio.rb', line 66

def url_available?
  !!(url_cached? || (@url_encoded && @client_id))
end

#url_cached?Boolean

Returns whether decoded URL is already cached.

Returns:

  • (Boolean)

    whether decoded URL is already cached.



60
61
62
# File 'lib/vk_music/audio.rb', line 60

def url_cached?
  !!(@url)
end