vk_music 0.1.2 → 0.1.3

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: 25c3df47d7b4d786a9ee10b0eb5c899f572c58d4bc9a634cbbf70d46c93a8c36
4
- data.tar.gz: e55054576e38c23768e31646d2ca8217f60abd85256ea0d6dd2ea9d25eee73d0
3
+ metadata.gz: 9d584af9c00d7c7f501ceec5ca75d802bdb3bc921208cec733b58d174d52d9f9
4
+ data.tar.gz: 2beb29130f8e26d4e14d07663307137f7308088e81ba6b31bf6deb1a1e639639
5
5
  SHA512:
6
- metadata.gz: bcc72c7b4e438ba08072b48d4deb7e63d69fb624487b9fada6444b087dc9d6aa3af8ed29e27ffe449573bf0929e78b31e2b6b0daf2321d59583f4a1d0aa071cd
7
- data.tar.gz: 54066b7c151a1d3f2b29c166d67b5ba6a33a43c8824d0bdcdefd104d2f8f6efd447d94671be7d72b892721b84e0bd93d88ac89ff027298d27181209568bccc3a
6
+ metadata.gz: 1c355075749ef34b0c33f36578720d684a28b47e24dd5e092f1552eb46f03e89d2c57821f92974da1de48a57101502bd673c829926476fc632aec25172632d74
7
+ data.tar.gz: 272700646076074d4b9f1eff44c5f0828c3cefbf2a6547ba6bf18456d99fc0c47857aed213241ecd628fd609d819a88e455ce4e81b4a108f9f861fe8013efc65
@@ -30,7 +30,7 @@ module VkMusic
30
30
  def get_playlist(url, up_to = nil)
31
31
  # NOTICE: it is possible to use same type of requests as in get_audios method
32
32
  begin
33
- url, owner_id, id, access_hash = url.match(PLAYLIST_URL_REGEX).to_a
33
+ url, owner_id, id, access_hash = url.to_s.match(PLAYLIST_URL_REGEX).to_a
34
34
 
35
35
  # Load first page and get info
36
36
  first_page = load_playlist_page(owner_id: owner_id, id: id, access_hash: access_hash, offset: 0)
@@ -75,12 +75,15 @@ module VkMusic
75
75
  })
76
76
  end
77
77
 
78
- def get_audios(id, up_to = nil)
78
+ def get_audios(obj, up_to = nil)
79
79
  Warning.warn("Current implementation of method VkMusic::Client#get_audios is only able to load first 100 audios from user page.\n") if (up_to && up_to > 100)
80
80
  # NOTICE: this method is only able to load first 100 audios
81
81
  # NOTICE: it is possible to download 50 audios per request on "https://m.vk.com/audios#{owner_id}?offset=#{offset}", so it will cost A LOT to download all of audios (up to 200 requests).
82
82
  # NOTICE: it is possible to load up to 2000 audios **without url** if offset is negative
83
83
 
84
+ # Firstly, we need to get numeric id
85
+ id = get_id(obj.to_s)
86
+
84
87
  # Trying to parse out audios
85
88
  begin
86
89
  first_json = load_playlist_json_section(id: id.to_s, playlist_id: -1, offset: 0)
@@ -105,7 +108,38 @@ module VkMusic
105
108
  })
106
109
  end
107
110
 
111
+ def get_id(str)
112
+ case str
113
+ when VK_URL_REGEX
114
+ path = str.match(VK_URL_REGEX)[1]
115
+ get_id(path) # Recursive call
116
+ when VK_ID_REGEX
117
+ str
118
+ when VK_PREFIXED_ID_REGEX
119
+ id = str.match(/\d+/).to_s # Just numbers. Sign needed
120
+ id = "-#{id}" unless str.start_with?("id")
121
+ id
122
+ when VK_CUSTOM_ID_REGEX
123
+ begin
124
+ page = load_page("#{VK_URL[:home]}/#{str}")
125
+ rescue Exception => error
126
+ raise IdParseError, "unable to load page by id \"#{str}\". Error: #{error.message}"
127
+ end
128
+
129
+ unless page.at_css(".PageBlock .owner_panel")
130
+ # Ensure this isn't some random vk page
131
+ raise IdParseError, "page #{str} doesn't seem to be a group or user page"
132
+ end
133
+
134
+ id = page.link_with(href: VK_HREF_ID_CONTAINING_REGEX).href.slice(/-?\d+/) # Numbers with sign
135
+ id
136
+ else
137
+ raise IdParseError, "unable to convert \"#{str}\" into id"
138
+ end
139
+ end
140
+
108
141
  private
142
+
109
143
  # Loading pages
110
144
  def load_page(url)
111
145
  uri = URI(url) if url.class != URI
@@ -153,6 +187,7 @@ module VkMusic
153
187
  end
154
188
 
155
189
 
190
+ # Login
156
191
  def login(username, password)
157
192
  # Loading login page
158
193
  homepage = load_page(VK_URL[:home])
@@ -168,7 +203,7 @@ module VkMusic
168
203
  # Parsing information about this profile
169
204
  profile = load_page(VK_URL[:profile])
170
205
  @name = profile.title
171
- @id = profile.link_with(href: /audios/).href.slice(/\d+/)
206
+ @id = profile.link_with(href: VK_HREF_ID_CONTAINING_REGEX).href.slice(/\d+/)
172
207
  end
173
208
 
174
209
  def unmask_link(link)
@@ -19,6 +19,14 @@ module VkMusic
19
19
  :password => "pass",
20
20
  }
21
21
 
22
+
23
+ VK_ID_REGEX = /^-?\d+$/
24
+ VK_PREFIXED_ID_REGEX = /^(?:id|club|group|public|event)\d+$/ # TODO: Rework. This one is REALLY dirty. Not quite sure every page can return correct id with this regex
25
+ VK_CUSTOM_ID_REGEX = /^\w+$/
26
+ VK_URL_REGEX = /(?:https?:\/\/)?(?:m\.|www\.)?vk\.com\/(\w+)/
27
+
28
+ VK_HREF_ID_CONTAINING_REGEX = /(?:audios|photo|write|owner_id=)-?\d+/
29
+
22
30
  # Playlist
23
31
  PLAYLIST_URL_REGEX = /.*audio_playlist(-?[\d]+)_([\d]+)(?:(?:(?:&access_hash=)|\/|%2F)([\da-z]+))?/
24
32
 
@@ -16,4 +16,8 @@ module VkMusic
16
16
  # Unable to load or parse audios section
17
17
  end
18
18
 
19
+ class IdParseError < AudiosParseError
20
+ # Unable to convert string to id
21
+ end
22
+
19
23
  end
@@ -7,6 +7,17 @@ module VkMusic
7
7
  "#{(s / 60).to_s.rjust(2, "0")}:#{(s % 60).to_s.rjust(2, "0")}";
8
8
  end
9
9
 
10
+ def self.guess_request_type(str)
11
+ # Guess what type of request is this. Returns Symbol: :find, :playlist, :audios
12
+ if str.match? PLAYLIST_URL_REGEX
13
+ :playlist
14
+ elsif str.match? VK_URL_REGEX
15
+ :audios
16
+ else
17
+ :find
18
+ end
19
+ end
20
+
10
21
  end
11
22
 
12
23
  end
@@ -0,0 +1,103 @@
1
+ require "minitest/autorun"
2
+ require_relative "../lib/vk_music.rb"
3
+
4
+ begin
5
+ CLIENT = VkMusic::Client.new(username: ARGV[0], password: ARGV[1])
6
+ rescue VkMusic::LoginError
7
+ puts "Unable to login! Please check provided credetionals"
8
+ exit
9
+ end
10
+
11
+ class TestVkMusic < MiniTest::Test
12
+
13
+ def test_user_link_with_id
14
+ id = CLIENT.get_id("https://vk.com/id51842614")
15
+ assert_equal("51842614", id, "Ids don't match")
16
+ end
17
+
18
+ def test_user_custom_link
19
+ id = CLIENT.get_id("https://vk.com/kopatych56")
20
+ assert_equal("51842614", id, "Ids don't match")
21
+ end
22
+
23
+ def test_user_custom
24
+ id = CLIENT.get_id("kopatych56")
25
+ assert_equal("51842614", id, "Ids don't match")
26
+ end
27
+
28
+ def test_user_id
29
+ id = CLIENT.get_id("51842614")
30
+ assert_equal("51842614", id, "Ids don't match")
31
+ end
32
+
33
+ def test_user_id_with_prefix
34
+ id = CLIENT.get_id("id51842614")
35
+ assert_equal("51842614", id, "Ids don't match")
36
+ end
37
+
38
+
39
+ def test_group_link_with_id
40
+ id = CLIENT.get_id("https://vk.com/public39786657")
41
+ assert_equal("-39786657", id, "Ids don't match")
42
+ end
43
+
44
+ def test_group_custom_link
45
+ id = CLIENT.get_id("https://vk.com/mashup")
46
+ assert_equal("-39786657", id, "Ids don't match")
47
+ end
48
+
49
+ def test_group_custom
50
+ id = CLIENT.get_id("mashup")
51
+ assert_equal("-39786657", id, "Ids don't match")
52
+ end
53
+
54
+ def test_group_id
55
+ id = CLIENT.get_id("-39786657")
56
+ assert_equal("-39786657", id, "Ids don't match")
57
+ end
58
+
59
+ def test_group_id_with_prefix
60
+ id = CLIENT.get_id("public39786657")
61
+ assert_equal("-39786657", id, "Ids don't match")
62
+ end
63
+
64
+
65
+ def test_user_deleted
66
+ id = CLIENT.get_id("https://vk.com/id245722576")
67
+ assert_equal("245722576", id, "Ids don't match")
68
+ end
69
+
70
+ def test_user_no_photos
71
+ id = CLIENT.get_id("drop_the_treble")
72
+ assert_equal("437727675", id, "Ids don't match")
73
+ end
74
+
75
+ def test_user_private
76
+ id = CLIENT.get_id("poppingeyesocketcherry") # I hope this guy won't change his custom
77
+ assert_equal("300415", id, "Ids don't match")
78
+ end
79
+
80
+ def test_group_no_photos
81
+ id = CLIENT.get_id("vk.com/opentestroom")
82
+ assert_equal("-184089233", id, "Ids don't match")
83
+ end
84
+
85
+ def test_bad_url
86
+ assert_raises(VkMusic::IdParseError) do
87
+ CLIENT.get_id("https://vk.com/feed")
88
+ end
89
+ end
90
+
91
+ def test_bad_custom
92
+ assert_raises(VkMusic::IdParseError) do
93
+ CLIENT.get_id("a")
94
+ end
95
+ end
96
+
97
+ def test_empty
98
+ assert_raises(VkMusic::IdParseError) do
99
+ CLIENT.get_id("")
100
+ end
101
+ end
102
+
103
+ end
@@ -59,4 +59,9 @@ class TestVkMusic < MiniTest::Test
59
59
  assert_empty(pl, "This group got no audios")
60
60
  end
61
61
 
62
+ def test_group_by_custom_id
63
+ pl = CLIENT.get_audios("mashup")
64
+ refute_empty(pl, "This group got audios")
65
+ end
66
+
62
67
  end
@@ -2,7 +2,7 @@ Gem::Specification.new do |s|
2
2
  s.name = "vk_music"
3
3
  s.summary = "Provides interface to work with VK music via HTTP requests"
4
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 = "0.1.2"
5
+ s.version = "0.1.3"
6
6
  s.author = "Kuznetsov Vladislav"
7
7
  s.email = "fizvlad@mail.ru"
8
8
  s.homepage = "https://github.com/fizvlad/vk-music-rb"
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: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuznetsov Vladislav
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-01 00:00:00.000000000 Z
11
+ date: 2019-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -70,6 +70,7 @@ files:
70
70
  - lib/vk_music/link_decoder.rb
71
71
  - lib/vk_music/playlist.rb
72
72
  - lib/vk_music/utility.rb
73
+ - test/test_get_id.rb
73
74
  - test/test_login.rb
74
75
  - test/test_playlist.rb
75
76
  - test/test_search.rb
@@ -100,6 +101,7 @@ signing_key:
100
101
  specification_version: 4
101
102
  summary: Provides interface to work with VK music via HTTP requests
102
103
  test_files:
104
+ - test/test_get_id.rb
103
105
  - test/test_login.rb
104
106
  - test/test_playlist.rb
105
107
  - test/test_search.rb