Class: VkMusic::Client
- Inherits:
-
Object
- Object
- VkMusic::Client
- 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
-
#agent ⇒ Mechanize
readonly
Client used to access web pages.
-
#id ⇒ Integer
readonly
ID of client.
-
#name ⇒ String
readonly
Name of client.
Instance Method Summary collapse
-
#artist(url: nil, name: nil) ⇒ Array<Audio>
Artist top audios.
-
#audios(url: nil, owner_id: nil, up_to: MAXIMUM_PLAYLIST_SIZE) ⇒ Playlist?
Get user or group audios.
-
#find(query = '', type: :audio) ⇒ Array<Audio>, Array<Playlist>
(also: #search)
Search for audio or playlist.
-
#get_urls(args) ⇒ Array<Audio, nil>
(also: #from_id)
Get audios with download URLs by their IDs and secrets.
-
#initialize(login: nil, password: nil, user_agent: DEFAULT_USERAGENT, agent: nil) ⇒ Client
constructor
A new instance of Client.
-
#login ⇒ Boolean
Make a login request.
-
#playlist(url: nil, owner_id: nil, playlist_id: nil, access_hash: nil, up_to: MAXIMUM_PLAYLIST_SIZE) ⇒ Playlist?
Get VK playlist.
-
#post(url: nil, owner_id: nil, post_id: nil) ⇒ Array<Audio>
Get audios attached to post.
-
#update_urls(audios) ⇒ Object
Update download URLs of provided audios.
-
#wall(url: nil, owner_id: nil, post_id: nil) ⇒ Playlist?
Get audios on wall of user or group starting.
Constructor Details
#initialize(login: nil, password: nil, user_agent: DEFAULT_USERAGENT, agent: nil) ⇒ Client
Returns a new instance of Client.
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 = 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 |
Instance Attribute Details
#agent ⇒ Mechanize (readonly)
Returns client used to access web pages.
20 21 22 |
# File 'lib/vk_music/client.rb', line 20 def agent @agent end |
#id ⇒ Integer (readonly)
Returns ID of client.
16 17 18 |
# File 'lib/vk_music/client.rb', line 16 def id @id end |
#name ⇒ String (readonly)
Returns 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
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
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
search in group audios
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
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
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 |
#login ⇒ Boolean
Make a login request
43 44 45 46 47 48 49 50 51 52 |
# 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 |
#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)
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)
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
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)+
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 |