Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Commit

Permalink
Merge branch 'fix/search' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fizvlad committed Nov 2, 2020
2 parents b51ed46 + 87c0aa4 commit 797ada9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
vk_music (3.1.5)
vk_music (3.1.7)
execjs (~> 2.7)
json (~> 2.0)
logger (~> 1.4)
Expand Down
38 changes: 34 additions & 4 deletions lib/vk_music/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ def initialize(username: "", password: "", user_agent: Constants::DEFAULT_USER_A
def find(query = "", type: :audio)
raise ArgumentError if query.empty?
uri = URI(Constants::URL::VK[:audios])
search_page = load_ajax(uri, { "q" => query })
case type
when :audio
uri.query = Utility.hash_to_params({ "act" => "search", "q" => query })
audios_from_page(uri)
audios_from_ajax(search_page)
when :playlist
uri.query = Utility.hash_to_params({ "q" => query, "tab" => "global" })
urls = playlist_urls_from_page(uri)
urls = playlist_urls_from_page(search_page)
urls.map { |url| playlist(url: url, up_to: 0, use_web: false) }
else
raise ArgumentError
Expand Down Expand Up @@ -363,6 +362,23 @@ def load_json(url)
raise Exceptions::ParseError, error.message, caller
end
end
##
# Load response to AJAX post request.
# @param url [String, URI]
# @return [Nokogiri::XML::Document]
def load_ajax(url, query = {})
uri = URI(url) if url.class != URI
query["_ajax"] = 1
headers = { "Content-Type" => "application/x-www-form-urlencoded", "x-requested-with" => "XMLHttpRequest" }
VkMusic.debug("Loading #{uri} with query #{query}")
begin
page = @agent.post(uri, query, headers)
str = JSON.parse(page.body.strip)["data"][2]
Nokogiri::XML("<body>#{CGI.unescapeElement(str)}</body>")
rescue
raise Exceptions::RequestError
end
end

##
# Load playlist web page.
Expand Down Expand Up @@ -458,6 +474,20 @@ def audios_from_data(data)
raise Exceptions::ParseError
end
end
##
# Load audios from AJAX data.
# @param page [Nokogiri::XML::Document]
# @return [Array<Audio>]
def audios_from_ajax(page)
begin
page.css(".audio_item.ai_has_btn").map do |elem|
data = JSON.parse(elem.attribute("data-audio"))
Audio.from_data(data, @id)
end
rescue
raise Exceptions::ParseError
end
end

##
# Load playlist through web page requests.
Expand Down
2 changes: 1 addition & 1 deletion lib/vk_music/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module VkMusic
##
# Library version.
VERSION = "3.1.6"
VERSION = "3.1.7"
end
3 changes: 2 additions & 1 deletion test/test_find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_search_with_hash
results = CLIENT.search("Sexualizer")
refute_empty(results, "There must be some good music")
assert_instance_of(VkMusic::Audio, results[0], "Results of search must be of class Audio")
assert(results[0].title.include? "Sexualizer")
assert(results[0].url_accessable?, "Audio must be accessable")
end

Expand All @@ -42,7 +43,7 @@ def test_find_playlist
end

def test_find_unexisting_playlist
results = CLIENT.find("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", type: :playlist)
results = CLIENT.find("asdasdasdasdad1231232131asfaf", type: :playlist)
assert_empty(results, "There must be no results for such query")
end

Expand Down

0 comments on commit 797ada9

Please sign in to comment.