class VkMusic::Client
VK client
Constants
- DEFAULT_USERAGENT
Default user agent to use
- MAXIMUM_PLAYLIST_SIZE
Mximum size of VK playlist
Attributes
@return [Mechanize] client used to access web pages
@return [Integer] ID of client
@return [String] name of client
Public Class Methods
@param login [String, nil] @param password [String, nil] @param user_agent [String] @param agent [Mechanize?] if specified, provided agent will be used
# File lib/vk_music/client.rb, line 26 def initialize(login: nil, password: nil, user_agent: DEFAULT_USERAGENT, agent: nil) @login = login @password = password @agent = agent if @agent.nil? @agent = Mechanize.new @agent.user_agent = user_agent raise('Failed to login!') unless self.login end load_id_and_name VkMusic.log.info("Client#{@id}") { "Logged in as User##{@id} (#{@name})" } end
Public Instance Methods
Artist top audios. Specify either url
or name
of the artist @param url [String] @param name [String] @return [Array<Audio>] array of audios attached to post
# 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
Get user or group audios. Specify either url
or owner_id
@param url [String, nil] @param owner_id [Integer, nil] @param up_to [Integer] maximum amount of audios to load. If 0, no audios
would be loaded (plain information about playlist)
@return [Playlist?]
# 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
Search for audio or playlist
Possible values of type
option:
-
:audio
- search for audios -
:playlist
- search for playlists
@note some audios and playlists might be removed from search @todo search in group audios @param query [String] @param type [Symbol] @return [Array<Audio>, Array<Playlist>]
# 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 audios with download URLs by their IDs and secrets @param args [Array<Audio, (owner_id, audio_id, secret_1, secret_2),
"#{owner_id}_#{id}_#{secret_1}_#{secret_2}">]
@return [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
# 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
Make a login request @return [Boolean] whether login was successful
# File lib/vk_music/client.rb, line 43 def login VkMusic.log.info("Client#{@id}") { 'Logging in...' } login = Request::Login.new login.call(agent) login.send_form(@login, @password, agent) return true if login.success? VkMusic.log.warn("Client#{@id}") { "Login failed. Redirected to #{login.response.uri}" } false end
Get VK playlist. Specify either url
or +(owner_id,playlist_id,access_hash)+ @param url [String, nil] @param owner_id [Integer, nil] @param playlist_id [Integer, nil] @param access_hash [String, nil] access hash for the playlist. Might not exist @param up_to [Integer] maximum amount of audios to load. If 0, no audios
would be loaded (plain information about playlist)
@return [Playlist?]
# 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
Get audios attached to post. Specify either url
or +(owner_id,post_id)+ @param url [String] @param owner_id [Integer] @param post_id [Integer] @return [Array<Audio>] array of audios attached to post
# 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 download URLs of provided audios @param audios [Array<Audio>]
# 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
Get audios on wall of user or group starting. Specify either url
or owner_id
or +(owner_id,post_id)+
@param url [String] URL to post or profile page @param owner_id [Integer] numerical ID of wall owner @param owner_id [Integer] ID of post to start looking from. If not specified, will be
used ID of last post
@return [Playlist?]
# 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
Private Instance Methods
# File lib/vk_music/client.rb, line 185 def load_id_and_name VkMusic.log.info("Client#{@id}") { 'Loading user id and name' } my_page = Request::MyPage.new my_page.call(agent) @id = my_page.id @name = my_page.name end