Skip to content

Commit 6991a60

Browse files
committed
Fix audio URL decoding
1 parent 3a5f2a3 commit 6991a60

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

vk_api/audio_url_decoder.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
# -*- coding: utf-8 -*-
2+
from .exceptions import VkAudioUrlDecodeError
23

34
VK_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN0PQRSTUVWXYZO123456789+/="
45

56

7+
def splice(l, a, b, c):
8+
""" JS's Array.prototype.splice
9+
10+
var x = [1, 2, 3],
11+
y = x.splice(0, 2, 1337);
12+
13+
eq
14+
15+
x = [1, 2, 3]
16+
x, y = splice(x, 0, 2, 1337)
17+
"""
18+
19+
return l[:a] + [c] + l[a + b:], l[a:a + b]
20+
21+
622
def decode_audio_url(string):
723
vals = string.split("?extra=", 1)[1].split("#")
824

@@ -20,6 +36,12 @@ def decode_audio_url(string):
2036

2137
elif cmd == 'x':
2238
tstr = vk_xor(tstr, arg[0])
39+
elif cmd == 's':
40+
tstr = vk_s(tstr, arg[0])
41+
else:
42+
raise VkAudioUrlDecodeError(
43+
'Unknown decode cmd: "{}"; Please send bugreport'.format(cmd)
44+
)
2345

2446
return tstr
2547

@@ -73,3 +95,35 @@ def vk_xor(string, i):
7395
xor_val = ord(i[0])
7496

7597
return ''.join(chr(ord(s) ^ xor_val) for s in string)
98+
99+
100+
def vk_s_child(t, e):
101+
i = len(t)
102+
103+
if not i:
104+
return []
105+
106+
o = []
107+
e = int(e)
108+
109+
for a in range(i - 1, -1, -1):
110+
e = abs(e) + a + i
111+
o.append(e % i | 0)
112+
113+
return o[::-1]
114+
115+
116+
def vk_s(t, e):
117+
i = len(t)
118+
119+
if not i:
120+
return t
121+
122+
o = vk_s_child(t, e)
123+
t = list(t)
124+
125+
for a in range(1, i):
126+
t, y = splice(t, o[i - 1 - a], 1, t[a])
127+
t[a] = y[0]
128+
129+
return ''.join(t)

vk_api/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,11 @@ def try_again(self, key=None):
147147

148148
def __str__(self):
149149
return 'Captcha needed'
150+
151+
152+
class VkAudioException(Exception):
153+
pass
154+
155+
156+
class VkAudioUrlDecodeError(VkAudioException):
157+
pass

0 commit comments

Comments
 (0)