vk_music 3.1.3 → 3.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c327f0b3925e4ed7f0def0ef082ea982867e49ce73e9e9a8d8aa2d1bc05f044
4
- data.tar.gz: cb2e29ae86aff312032d86c9fa6259fccffb2bd0ea988d51416a69a6d935634a
3
+ metadata.gz: 745b91709f47cb2b6b5bcaa013824a51f836977ecbe4cd1259a74d4a6f56e8c8
4
+ data.tar.gz: 9a7a2e9998adcd73a0c87d2934c6e763e8c76fdedcf08f757543497395e64322
5
5
  SHA512:
6
- metadata.gz: 6f1ad6834b2234ef252f44ad383b3d2fc5094c667e61a78fb4d84d1fe62d422b29d163c0c5a84da532ca313dbc9c62432e051b1eeb672ce941de3a84486d91ec
7
- data.tar.gz: 0b0d8dcff331325d6f4ee617683465e8ba4a15f48b3224250972d2d7082cb3b978b9b1371a570be7211a6fca8cd986e6447c9698e32c1ea055246adc796e903f
6
+ metadata.gz: 1bd33552a23e56c0372500fc1edc20736afed079f650d4405650a91a48d01300269aeb37f51935c2c9916c4ee52449b7abb29949e7b32eb572e4641ac0e05e13
7
+ data.tar.gz: a252e563da14bcf249e72ab997414dbdb14388fa2308f24afc99f0507d6c003bfd5f80e0335db8df30b8d1eb7300036c2380ad99fd47cf402083238891c4ba17
@@ -81,7 +81,7 @@ module VkMusic
81
81
  raise Exceptions::ParseError
82
82
  end
83
83
  raise ArgumentError unless owner_id && playlist_id
84
- use_web = up_to <= 200 if use_web.nil?
84
+ use_web ||= (up_to <= 200)
85
85
  if use_web
86
86
  playlist_web(owner_id, playlist_id, access_hash, up_to: up_to)
87
87
  else
@@ -467,26 +467,7 @@ module VkMusic
467
467
  # @param up_to [Integer] if less than 0, all audios will be loaded.
468
468
  # @return [Playlist]
469
469
  def playlist_web(owner_id, playlist_id, access_hash = nil, up_to: -1)
470
- # Load first page and get info
471
- first_page = load_page_playlist(owner_id, playlist_id, access_hash, offset: 0)
472
- begin
473
- # Parse out essential data
474
- title = first_page.at_css(".audioPlaylist__title").text.strip
475
- subtitle = first_page.at_css(".audioPlaylist__subtitle").text.strip
476
-
477
- footer_node = first_page.at_css(".audioPlaylist__footer")
478
- if footer_node
479
- footer_match = footer_node.text.strip.match(/^\d+/)
480
- real_size = footer_match ? footer_match[0].to_i : 0
481
- else
482
- real_size = 0
483
- end
484
- rescue
485
- raise Exceptions::ParseError
486
- end
487
- # Now we can be sure we are on correct page and have essential data.
488
-
489
- first_page_audios = audios_from_page(first_page)
470
+ first_page_audios, title, subtitle, real_size = playlist_first_page_web(owner_id, playlist_id, access_hash || "")
490
471
 
491
472
  # Check whether need to make additional requests
492
473
  up_to = real_size if (up_to < 0 || up_to > real_size)
@@ -514,18 +495,16 @@ module VkMusic
514
495
  # @param up_to [Integer] if less than 0, all audios will be loaded.
515
496
  # @return [Playlist]
516
497
  def playlist_json(owner_id, playlist_id, access_hash, up_to: -1)
517
- # Trying to parse out audios
518
- first_json = load_json_playlist_section(owner_id, playlist_id, access_hash, offset: 0)
519
- begin
520
- first_data = first_json["data"][0]
521
- first_data_audios = audios_from_data(first_data["list"])
522
- rescue
523
- raise Exceptions::ParseError
498
+ if playlist_id == -1
499
+ first_audios, title, subtitle, real_size = playlist_first_page_json(owner_id, playlist_id, access_hash || "")
500
+ else
501
+ first_audios, title, subtitle, real_size = playlist_first_page_web(owner_id, playlist_id, access_hash || "")
524
502
  end
503
+ # NOTE: We need to load first page from web to be able to unmask links in future
525
504
 
526
- real_size = first_data["totalCount"]
505
+ # Check whether need to make additional requests
527
506
  up_to = real_size if (up_to < 0 || up_to > real_size)
528
- list = first_data_audios.first(up_to)
507
+ list = first_audios.first(up_to)
529
508
  while list.length < up_to do
530
509
  json = load_json_playlist_section(owner_id, playlist_id, access_hash, offset: list.length)
531
510
  audios = begin
@@ -538,11 +517,11 @@ module VkMusic
538
517
 
539
518
  begin
540
519
  Playlist.new(list,
541
- id: first_data["id"],
542
- owner_id: first_data["owner_id"],
543
- access_hash: first_data["access_hash"],
544
- title: CGI.unescapeHTML(first_data["title"].to_s),
545
- subtitle: CGI.unescapeHTML(first_data["subtitle"].to_s),
520
+ id: playlist_id,
521
+ owner_id: owner_id,
522
+ access_hash: access_hash,
523
+ title: title,
524
+ subtitle: subtitle,
546
525
  real_size: real_size
547
526
  )
548
527
  rescue
@@ -550,6 +529,57 @@ module VkMusic
550
529
  end
551
530
  end
552
531
 
532
+ ##
533
+ # Load playlist first page in web and return essential data.
534
+ # @note not suitable for user audios
535
+ # @param owner_id [Integer]
536
+ # @param playlist_id [Integer]
537
+ # @param access_hash [String, nil]
538
+ # @return [Array<Array, String, String, Integer>] array with audios from first page, title, subtitle and playlist real size.
539
+ def playlist_first_page_web(owner_id, playlist_id, access_hash)
540
+ first_page = load_page_playlist(owner_id, playlist_id, access_hash, offset: 0)
541
+ begin
542
+ # Parse out essential data
543
+ title = first_page.at_css(".audioPlaylist__title").text.strip
544
+ subtitle = first_page.at_css(".audioPlaylist__subtitle").text.strip
545
+
546
+ footer_node = first_page.at_css(".audioPlaylist__footer")
547
+ if footer_node
548
+ footer_match = footer_node.text.strip.match(/^\d+/)
549
+ real_size = footer_match ? footer_match[0].to_i : 0
550
+ else
551
+ real_size = 0
552
+ end
553
+
554
+ first_audios = audios_from_page(first_page)
555
+ rescue
556
+ raise Exceptions::ParseError
557
+ end
558
+ [first_audios, title, subtitle, real_size]
559
+ end
560
+
561
+ ##
562
+ # Load playlist first page in JSON and return essential data.
563
+ # @param owner_id [Integer]
564
+ # @param playlist_id [Integer]
565
+ # @param access_hash [String, nil]
566
+ # @return [Array<Array, String, String, Integer>] array with audios from first page, title, subtitle and playlist real size.
567
+ def playlist_first_page_json(owner_id, playlist_id, access_hash)
568
+ first_json = load_json_playlist_section(owner_id, playlist_id, access_hash, offset: 0)
569
+ begin
570
+ first_data = first_json["data"][0]
571
+ first_data_audios = audios_from_data(first_data["list"])
572
+ rescue
573
+ raise Exceptions::ParseError
574
+ end
575
+
576
+ real_size = first_data["totalCount"]
577
+ title = CGI.unescapeHTML(first_data["title"].to_s)
578
+ subtitle = CGI.unescapeHTML(first_data["subtitle"].to_s)
579
+
580
+ [first_data_audios, title, subtitle, real_size]
581
+ end
582
+
553
583
  ##
554
584
  # Found playlist URLs on *global* search page.
555
585
  # @param obj [Mechanize::Page, String, URI]
@@ -52,7 +52,7 @@ module VkMusic
52
52
  VK_AUDIOS_URL_POSTFIX = /^audios(-?\d+)$/
53
53
  ##
54
54
  # Playlist URL regular expression.
55
- VK_PLAYLIST_URL_POSTFIX = /.*(?:audio_playlist|album\/)(-?\d+)_(\d+)(?:(?:(?:.*(?=&access_hash=)&access_hash=)|\/|%2F|_)([\da-z]+))?/
55
+ VK_PLAYLIST_URL_POSTFIX = /.*(?:audio_playlist|album\/|playlist\/)(-?\d+)_(\d+)(?:(?:(?:.*(?=&access_hash=)&access_hash=)|\/|%2F|_)([\da-z]+))?/
56
56
  ##
57
57
  # Post URL regular expression #1.
58
58
  VK_POST_URL_POSTFIX = /.*post(-?\d+)_(\d+)/
@@ -1,5 +1,5 @@
1
1
  module VkMusic
2
2
  ##
3
3
  # Library version.
4
- VERSION = "3.1.3"
4
+ VERSION = "3.1.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vk_music
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.3
4
+ version: 3.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fizvlad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-09 00:00:00.000000000 Z
11
+ date: 2020-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler