Class: VkMusic::Client

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

Overview

VK client

Constant Summary collapse

DEFAULT_USERAGENT =

Default user agent to use

'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) ' \
'AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/86.0.4240.111 Mobile Safari/537.36'
MAXIMUM_PLAYLIST_SIZE =

Mximum size of VK playlist

10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login: nil, password: nil, user_agent: DEFAULT_USERAGENT, agent: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • login (String, nil) (defaults to: nil)
  • password (String, nil) (defaults to: nil)
  • user_agent (String) (defaults to: DEFAULT_USERAGENT)
  • agent (Mechanize?) (defaults to: nil)

    if specified, provided agent will be used



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vk_music/client.rb', line 26

def initialize(login: nil, password: nil, user_agent: DEFAULT_USERAGENT, agent: nil)
  @login = 
  @password = password
  @agent = agent
  if @agent.nil?
    @agent = Mechanize.new
    @agent.user_agent = user_agent

    raise('Failed to login!') unless self.
  end

  load_id_and_name
  VkMusic.log.info("Client#{@id}") { "Logged in as User##{@id} (#{@name})" }
end

Instance Attribute Details

#agentMechanize (readonly)

Returns client used to access web pages.

Returns:

  • (Mechanize)

    client used to access web pages



20
21
22
# File 'lib/vk_music/client.rb', line 20

def agent
  @agent
end

#idInteger (readonly)

Returns ID of client.

Returns:

  • (Integer)

    ID of client



16
17
18
# File 'lib/vk_music/client.rb', line 16

def id
  @id
end

#nameString (readonly)

Returns name of client.

Returns:

  • (String)

    name of client



18
19
20
# File 'lib/vk_music/client.rb', line 18

def name
  @name
end

Instance Method Details

#artist(url: nil, name: nil) ⇒ Array<Audio>

Artist top audios. Specify either url or name of the artist

Parameters:

  • url (String) (defaults to: nil)
  • name (String) (defaults to: nil)

Returns:

  • (Array<Audio>)

    array of audios attached to post



145
146
147
148
149
150
151
# File 'lib/vk_music/client.rb', line 145

def artist(url: nil, name: nil)
  name = Utility::ArtistUrlParser.call(url) if url

  return [] if name.nil? || name.empty?

  Utility::ArtistLoader.call(agent, id, name)
end

#audios(url: nil, owner_id: nil, up_to: MAXIMUM_PLAYLIST_SIZE) ⇒ Playlist?

Get user or group audios. Specify either url or owner_id

Parameters:

  • url (String, nil) (defaults to: nil)
  • owner_id (Integer, nil) (defaults to: nil)
  • up_to (Integer) (defaults to: MAXIMUM_PLAYLIST_SIZE)

    maximum amount of audios to load. If 0, no audios would be loaded (plain information about playlist)

Returns:



100
101
102
103
104
105
# File 'lib/vk_music/client.rb', line 100

def audios(url: nil, owner_id: nil, up_to: MAXIMUM_PLAYLIST_SIZE)
  owner_id = Utility::ProfileIdResolver.call(agent, url) if url
  return if owner_id.nil?

  Utility::AudiosLoader.call(agent, id, owner_id, up_to)
end

#find(query = '', type: :audio) ⇒ Array<Audio>, Array<Playlist> Also known as: search

TODO:

search in group audios

Note:

some audios and playlists might be removed from search

Search for audio or playlist

Possible values of type option:

  • :audio - search for audios

  • :playlist - search for playlists

Parameters:

  • query (String) (defaults to: '')
  • type (Symbol) (defaults to: :audio)

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vk_music/client.rb', line 64

def find(query = '', type: :audio)
  return [] if query.empty?

  page = Request::Search.new(query, id)
  page.call(agent)

  case type
  when :audio, :audios then page.audios
  when :playlist, :playlists then page.playlists
  else []
  end
end

#get_urls(args) ⇒ Array<Audio, nil> Also known as: from_id

Get audios with download URLs by their IDs and secrets

Parameters:

  • args (Array<Audio, (owner_id, audio_id, secret_1, secret_2), "#{owner_id}_#{id}_#{secret_1}_#{secret_2}">)

Returns:

  • (Array<Audio, nil>)

    array of: audio with download URLs or audio without URL if wasn’t able to get it for audio or nil if matching element can’t be retrieved for array or string



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vk_music/client.rb', line 159

def get_urls(args)
  ids = Utility::AudiosIdsGetter.call(args)
  audios = Utility::AudiosFromIdsLoader.call(agent, ids, id)

  args.map do |el|
    # NOTE: can not load unaccessable audio, so just returning it
    next el if el.is_a?(Audio) && !el.url_accessable?

    audios.find { |a| a.id_matches?(el) }
  end
end

#loginBoolean

Make a login request

Returns:

  • (Boolean)

    whether login was successful



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

def 
  VkMusic.log.info("Client#{@id}") { 'Logging in...' }
   = Request::Login.new
  .call(agent)
  .send_form(@login, @password, agent)
  return true if .success?

  VkMusic.log.warn("Client#{@id}") { "Login failed. Redirected to #{.response.uri}" }
  false
end

#playlist(url: nil, owner_id: nil, playlist_id: nil, access_hash: nil, up_to: MAXIMUM_PLAYLIST_SIZE) ⇒ Playlist?

Get VK playlist. Specify either url or (owner_id,playlist_id,access_hash)

Parameters:

  • url (String, nil) (defaults to: nil)
  • owner_id (Integer, nil) (defaults to: nil)
  • playlist_id (Integer, nil) (defaults to: nil)
  • access_hash (String, nil) (defaults to: nil)

    access hash for the playlist. Might not exist

  • up_to (Integer) (defaults to: MAXIMUM_PLAYLIST_SIZE)

    maximum amount of audios to load. If 0, no audios would be loaded (plain information about playlist)

Returns:



86
87
88
89
90
91
92
# File 'lib/vk_music/client.rb', line 86

def playlist(url: nil, owner_id: nil, playlist_id: nil, access_hash: nil,
             up_to: MAXIMUM_PLAYLIST_SIZE)
  owner_id, playlist_id, access_hash = Utility::PlaylistUrlParser.call(url) if url
  return if owner_id.nil? || playlist_id.nil?

  Utility::PlaylistLoader.call(agent, id, owner_id, playlist_id, access_hash, up_to)
end

#post(url: nil, owner_id: nil, post_id: nil) ⇒ Array<Audio>

Get audios attached to post. Specify either url or (owner_id,post_id)

Parameters:

  • url (String) (defaults to: nil)
  • owner_id (Integer) (defaults to: nil)
  • post_id (Integer) (defaults to: nil)

Returns:

  • (Array<Audio>)

    array of audios attached to post



133
134
135
136
137
138
139
# File 'lib/vk_music/client.rb', line 133

def post(url: nil, owner_id: nil, post_id: nil)
  owner_id, post_id = Utility::PostUrlParser.call(url) if url

  return [] if owner_id.nil? || post_id.nil?

  Utility::PostLoader.call(agent, id, owner_id, post_id)
end

#update_urls(audios) ⇒ Object

Update download URLs of provided audios

Parameters:



174
175
176
177
178
179
180
181
# File 'lib/vk_music/client.rb', line 174

def update_urls(audios)
  with_url = get_urls(audios)
  audios.each.with_index do |audio, i|
    audio_with_url = with_url[i]
    audio.update(audio_with_url) if audio_with_url
  end
  audios
end

#wall(url: nil, owner_id: nil, post_id: nil) ⇒ Playlist?

Get audios on wall of user or group starting. Specify either url or owner_id

or +(owner_id,post_id)+

Parameters:

  • url (String) (defaults to: nil)

    URL to post or profile page

  • owner_id (Integer) (defaults to: nil)

    numerical ID of wall owner

  • owner_id (Integer) (defaults to: nil)

    ID of post to start looking from. If not specified, will be used ID of last post

Returns:



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vk_music/client.rb', line 114

def wall(url: nil, owner_id: nil, post_id: nil)
  owner_id, post_id = Utility::PostUrlParser.call(url) if url
  if post_id.nil?
    if url
      owner_id, post_id = Utility::LastProfilePostLoader.call(agent, url: url)
    elsif owner_id
      owner_id, post_id = Utility::LastProfilePostLoader.call(agent, owner_id: owner_id)
    end
  end
  return if owner_id.nil? || post_id.nil?

  Utility::WallLoader.call(agent, id, owner_id, post_id)
end