vk_music 2.1.5 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,3 @@
1
- require "mechanize"
2
- require "json"
3
-
4
1
  ##
5
2
  # @!macro [new] options_hash_param
6
3
  # @param options [Hash] hash with options.
@@ -210,7 +207,7 @@ module VkMusic
210
207
 
211
208
  ##
212
209
  # @!macro [new] wall__up_to_option
213
- # @option up_to [Integer] :up_to (10) maximum amount of audios to load from wall.
210
+ # @option up_to [Integer] :up_to (50) maximum amount of audios to load from wall.
214
211
  #
215
212
  # @!macro [new] wall__with_url_option
216
213
  # @option options [Boolean] :with_url (true) automatically use {Client#from_id} to get download URLs.
@@ -260,7 +257,7 @@ module VkMusic
260
257
  raise ArgumentError, "Bad arguments", caller
261
258
  end
262
259
 
263
- options[:up_to] ||= 10
260
+ options[:up_to] ||= 50
264
261
  options[:with_url] = true if options[:with_url].nil?
265
262
 
266
263
  wall__json(owner_id, post_id, options)
@@ -269,6 +266,8 @@ module VkMusic
269
266
  ##
270
267
  # Get audios attached to post.
271
268
  #
269
+ # @note currently this method works incorrectly with reposts.
270
+ #
272
271
  # @overload post(url)
273
272
  # @param url [String] URL to post.
274
273
  #
@@ -277,7 +276,7 @@ module VkMusic
277
276
  # @option options [Integer] :owner_id numerical ID of wall owner.
278
277
  # @option options [Integer] :post_id numerical ID of post.
279
278
  #
280
- # @return [Array<Audio>] audios with download URLs.
279
+ # @return [Array<Audio>] array of audios. Possibly without download URL.
281
280
  def post(arg)
282
281
  begin
283
282
  case arg
@@ -294,20 +293,26 @@ module VkMusic
294
293
  raise ArgumentError, "Bad arguments", caller
295
294
  end
296
295
 
297
- amount = attached_audios_amount(owner_id: owner_id, post_id: post_id)
298
- wall(owner_id: owner_id, post_id: post_id, up_to: amount)
296
+ attached = attached_audios(owner_id: owner_id, post_id: post_id)
297
+ wall = wall(owner_id: owner_id, post_id: post_id, with_url: false)
298
+
299
+ no_link = attached.map do |a_empty|
300
+ # Here we just search for matching audios on wall
301
+ wall.find { |a| a.artist == a_empty.artist && a.title == a_empty.title } || a_empty
302
+ end
303
+ loaded_audios = from_id(no_link)
304
+
305
+ loaded_audios.map.with_index { |el, i| el || no_link[i] }
299
306
  end
300
307
 
301
308
  ##
302
309
  # Get audios with download URLs by their IDs and secrets.
303
310
  #
304
- # @note warning: audios must not match.
305
- #
306
- # @todo workaround for not unique audios in request
307
- #
308
311
  # @param args [Array<Audio, Array<(owner_id, audio_id, secret_1, secret_2)>, "#{owner_id}_#{id}_#{secret_1}_#{secret_2}">]
309
312
  #
310
- # @return [Array<Audio>] array of audios with download URLs.
313
+ # @return [Array<Audio, nil>] array of: audio with download URLs or audio
314
+ # audio without URL if wasn't able to get it for audio or +nil+ if
315
+ # matching element can't be retrieved for array or string.
311
316
  def from_id(args)
312
317
  begin
313
318
  args_formatted = args.map do |el|
@@ -315,7 +320,7 @@ module VkMusic
315
320
  when Array
316
321
  el.join("_")
317
322
  when Audio
318
- "#{el.owner_id}_#{el.id}_#{el.secret_1}_#{el.secret_2}"
323
+ el.full_id
319
324
  when String
320
325
  el # Do not change
321
326
  else
@@ -325,15 +330,29 @@ module VkMusic
325
330
  rescue
326
331
  raise ArgumentError, "Bad arguments", caller
327
332
  end
333
+ args_formatted.compact.uniq # Not dealing with nil or doubled IDs
328
334
 
329
- result = []
335
+ audios = []
330
336
  args_formatted.each_slice(10) do |subarray|
331
337
  json = load__json__audios_by_id(subarray)
332
338
  subresult = audios__from_data(json["data"][0].to_a)
333
- raise Exceptions::ParseError, "Result size don't match: excepected #{subarray.size}, got #{subresult.size}", caller if subresult.size != subarray.size
334
- result.concat(subresult)
339
+ audios.concat(subresult)
340
+ end
341
+ Utility.debug("Loaded audios from ids: #{audios.map(&:pp).join(", ")}")
342
+
343
+ args.map do |el|
344
+ case el
345
+ when Array
346
+ audios.find { |audio| audio.owner_id == el[0].to_i && audio.id == el[1].to_i }
347
+ when Audio
348
+ next el if el.full_id.nil? # Audio was skipped
349
+ audios.find { |audio| audio.owner_id == el.owner_id && audio.id == el.id }
350
+ when String
351
+ audios.find { |audio| [audio.owner_id, audio.id] == el.split("_").first(2).map(&:to_i) }
352
+ else
353
+ nil # This shouldn't happen actually
354
+ end
335
355
  end
336
- result
337
356
  end
338
357
 
339
358
  ##
@@ -436,18 +455,18 @@ module VkMusic
436
455
  end
437
456
 
438
457
  ##
439
- # Get amount of audios attached to specified post.
458
+ # Get audios attached to specified post.
440
459
  #
441
- # @overload attached_audios_amount(url)
460
+ # @overload attached_audios(url)
442
461
  # @param url [String] URL to post.
443
462
  #
444
- # @overload attached_audios_amount(options)
463
+ # @overload attached_audios(options)
445
464
  # @macro options_hash_param
446
465
  # @option options [Integer] :owner_id numerical ID of wall owner.
447
466
  # @option options [Integer] :post_id numerical ID of post.
448
467
  #
449
- # @return [Integer] amount of audios.
450
- def attached_audios_amount(arg)
468
+ # @return [Array<Audio>] audios with only artist, title and duration.
469
+ def attached_audios(arg)
451
470
  begin
452
471
  case arg
453
472
  when String
@@ -472,7 +491,7 @@ module VkMusic
472
491
 
473
492
  raise Exceptions::ParseError, "Post not found: #{owner_id}_#{post_id}", caller unless page.css(".service_msg_error").empty?
474
493
  begin
475
- result = page.css(".wi_body > .pi_medias .medias_audio").size
494
+ result = page.css(".wi_body > .pi_medias .medias_audio").map { |e| Audio.from_node(e, @id) }
476
495
  rescue Exception => error
477
496
  raise Exceptions::ParseError, "Unable to get amount of audios in post #{owner_id}_#{post_id}. Error: #{error.message}", caller
478
497
  end
@@ -623,10 +642,6 @@ module VkMusic
623
642
 
624
643
  # Load playlist through JSON requests.
625
644
  def playlist__json(owner_id, playlist_id, access_hash, options)
626
- if options[:up_to] < 0 || options[:up_to] > 100
627
- Utility.warn("Current implementation of this method is not able to return more than 100 first audios with URL.")
628
- end
629
-
630
645
  # Trying to parse out audios
631
646
  begin
632
647
  first_json = load__json__playlist_section(owner_id, playlist_id, access_hash, offset: 0)
@@ -1,5 +1,3 @@
1
- require "execjs"
2
-
3
1
  module VkMusic
4
2
 
5
3
  ##
@@ -1,6 +1,3 @@
1
- require "cgi"
2
- require "logger"
3
-
4
1
  module VkMusic
5
2
 
6
3
  ##
@@ -108,6 +105,29 @@ module VkMusic
108
105
  raise ArgumentError, "Bad arguments", caller
109
106
  end
110
107
  end
108
+
109
+ ##
110
+ # Get content of text children of provided Node.
111
+ #
112
+ # @param node [Nokogiri::Xml::Node]
113
+ #
114
+ # @return [String]
115
+ def self.plain_text(node)
116
+ node.children.select(&:text?).map(&:text).join ""
117
+ end
118
+
119
+ ##
120
+ # Turn human readable track length to its size in seconds.
121
+ #
122
+ # @param str [String] string in format "(HH:MM:SS)" or something alike.
123
+ #
124
+ # @return [Integer] amount of seconds.
125
+ def self.parse_duration(str)
126
+ str.scan(/\d+/)
127
+ .map(&:to_i)
128
+ .reverse
129
+ .each_with_index.reduce(0) { |m, arr| m + arr[0] * 60**arr[1] }
130
+ end
111
131
 
112
132
  end
113
133
 
@@ -0,0 +1,7 @@
1
+ module VkMusic
2
+
3
+ ##
4
+ # Library version.
5
+ VERSION = "2.2.0"
6
+
7
+ end
data/vk_music.gemspec CHANGED
@@ -1,22 +1,40 @@
1
- Gem::Specification.new do |s|
2
- s.name = "vk_music"
3
- s.summary = "Provides interface to work with VK music via HTTP requests"
4
- s.description = "Library to work with audios on popular Russian social network vk.com. VK disabled their public API for audios, so it is now necessary to use parsers instead."
5
- s.version = "2.1.5"
6
- s.author = "Kuznetsov Vladislav"
7
- s.email = "fizvlad@mail.ru"
8
- s.homepage = "https://github.com/fizvlad/vk-music-rb"
9
- s.platform = Gem::Platform::RUBY
10
- s.required_ruby_version = ">=2.3.1"
11
- s.files = Dir[ "bin/**/**", "lib/**/**", "test/**/**", "LICENSE", "Rakefile", "README.md", "vk_music.gemspec" ]
12
- s.test_files = Dir[ "test/test*.rb" ]
13
- s.license = "MIT"
14
-
15
- s.add_runtime_dependency "mechanize", "~>2.7"
16
- s.add_runtime_dependency "net-http-persistent", "2.9.4" # Required for mechanize. Future versions cause error.
17
- s.add_runtime_dependency "execjs", "~>2.7"
18
- s.add_runtime_dependency "json", "~>2.0"
19
-
20
- s.add_development_dependency "rake", "~>12.3"
21
- s.add_development_dependency "yard", "~>0.9"
22
- end
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "vk_music/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "vk_music"
7
+ spec.version = VkMusic::VERSION
8
+ spec.authors = ["Fizvlad"]
9
+ spec.email = ["fizvlad@mail.ru"]
10
+
11
+ spec.summary = "Provides interface to work with VK music via HTTP requests"
12
+ spec.description = "Library to work with audios on popular Russian social network vk.com. VK disabled their public API for audios, so it is now necessary to use parsers instead."
13
+ spec.homepage = "https://github.com/fizvlad/vk-music-rb"
14
+ spec.license = "MIT"
15
+
16
+ spec.required_ruby_version = ">=2.3.1"
17
+
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/fizvlad/vk-music-rb"
21
+ spec.metadata["changelog_uri"] = "https://github.com/fizvlad/vk-music-rb/releases"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 2.0"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "yard", "~>0.9"
33
+ spec.add_development_dependency "minitest", "~> 5.0"
34
+
35
+ spec.add_runtime_dependency "logger", "~>1.4"
36
+ spec.add_runtime_dependency "mechanize", "~>2.7"
37
+ spec.add_runtime_dependency "net-http-persistent", "2.9.4" # Required for mechanize. Future versions cause error.
38
+ spec.add_runtime_dependency "execjs", "~>2.7"
39
+ spec.add_runtime_dependency "json", "~>2.0"
40
+ end
metadata CHANGED
@@ -1,45 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vk_music
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.5
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Kuznetsov Vladislav
7
+ - Fizvlad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-03 00:00:00.000000000 Z
11
+ date: 2019-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: mechanize
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.7'
20
- type: :runtime
19
+ version: '2.0'
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.7'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: net-http-persistent
28
+ name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 2.9.4
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: logger
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
34
76
  type: :runtime
35
77
  prerelease: false
36
78
  version_requirements: !ruby/object:Gem::Requirement
37
79
  requirements:
38
- - - '='
80
+ - - "~>"
39
81
  - !ruby/object:Gem::Version
40
- version: 2.9.4
82
+ version: '1.4'
41
83
  - !ruby/object:Gem::Dependency
42
- name: execjs
84
+ name: mechanize
43
85
  requirement: !ruby/object:Gem::Requirement
44
86
  requirements:
45
87
  - - "~>"
@@ -53,58 +95,64 @@ dependencies:
53
95
  - !ruby/object:Gem::Version
54
96
  version: '2.7'
55
97
  - !ruby/object:Gem::Dependency
56
- name: json
98
+ name: net-http-persistent
57
99
  requirement: !ruby/object:Gem::Requirement
58
100
  requirements:
59
- - - "~>"
101
+ - - '='
60
102
  - !ruby/object:Gem::Version
61
- version: '2.0'
103
+ version: 2.9.4
62
104
  type: :runtime
63
105
  prerelease: false
64
106
  version_requirements: !ruby/object:Gem::Requirement
65
107
  requirements:
66
- - - "~>"
108
+ - - '='
67
109
  - !ruby/object:Gem::Version
68
- version: '2.0'
110
+ version: 2.9.4
69
111
  - !ruby/object:Gem::Dependency
70
- name: rake
112
+ name: execjs
71
113
  requirement: !ruby/object:Gem::Requirement
72
114
  requirements:
73
115
  - - "~>"
74
116
  - !ruby/object:Gem::Version
75
- version: '12.3'
76
- type: :development
117
+ version: '2.7'
118
+ type: :runtime
77
119
  prerelease: false
78
120
  version_requirements: !ruby/object:Gem::Requirement
79
121
  requirements:
80
122
  - - "~>"
81
123
  - !ruby/object:Gem::Version
82
- version: '12.3'
124
+ version: '2.7'
83
125
  - !ruby/object:Gem::Dependency
84
- name: yard
126
+ name: json
85
127
  requirement: !ruby/object:Gem::Requirement
86
128
  requirements:
87
129
  - - "~>"
88
130
  - !ruby/object:Gem::Version
89
- version: '0.9'
90
- type: :development
131
+ version: '2.0'
132
+ type: :runtime
91
133
  prerelease: false
92
134
  version_requirements: !ruby/object:Gem::Requirement
93
135
  requirements:
94
136
  - - "~>"
95
137
  - !ruby/object:Gem::Version
96
- version: '0.9'
138
+ version: '2.0'
97
139
  description: Library to work with audios on popular Russian social network vk.com.
98
140
  VK disabled their public API for audios, so it is now necessary to use parsers instead.
99
- email: fizvlad@mail.ru
141
+ email:
142
+ - fizvlad@mail.ru
100
143
  executables: []
101
144
  extensions: []
102
145
  extra_rdoc_files: []
103
146
  files:
104
- - LICENSE
147
+ - ".gitignore"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - LICENSE.txt
105
152
  - README.md
106
153
  - Rakefile
107
154
  - bin/console
155
+ - bin/setup
108
156
  - lib/vk_music.rb
109
157
  - lib/vk_music/audio.rb
110
158
  - lib/vk_music/client.rb
@@ -113,21 +161,15 @@ files:
113
161
  - lib/vk_music/link_decoder.rb
114
162
  - lib/vk_music/playlist.rb
115
163
  - lib/vk_music/utility.rb
116
- - test/test_attached_audios_amount.rb
117
- - test/test_audios.rb
118
- - test/test_find.rb
119
- - test/test_from_id.rb
120
- - test/test_last_post_id.rb
121
- - test/test_login.rb
122
- - test/test_page_id.rb
123
- - test/test_playlist.rb
124
- - test/test_post.rb
125
- - test/test_wall.rb
164
+ - lib/vk_music/version.rb
126
165
  - vk_music.gemspec
127
166
  homepage: https://github.com/fizvlad/vk-music-rb
128
167
  licenses:
129
168
  - MIT
130
- metadata: {}
169
+ metadata:
170
+ homepage_uri: https://github.com/fizvlad/vk-music-rb
171
+ source_code_uri: https://github.com/fizvlad/vk-music-rb
172
+ changelog_uri: https://github.com/fizvlad/vk-music-rb/releases
131
173
  post_install_message:
132
174
  rdoc_options: []
133
175
  require_paths:
@@ -147,14 +189,4 @@ rubygems_version: 3.0.4
147
189
  signing_key:
148
190
  specification_version: 4
149
191
  summary: Provides interface to work with VK music via HTTP requests
150
- test_files:
151
- - test/test_attached_audios_amount.rb
152
- - test/test_audios.rb
153
- - test/test_find.rb
154
- - test/test_from_id.rb
155
- - test/test_last_post_id.rb
156
- - test/test_login.rb
157
- - test/test_page_id.rb
158
- - test/test_playlist.rb
159
- - test/test_post.rb
160
- - test/test_wall.rb
192
+ test_files: []