Skip to content

Commit b2e7c41

Browse files
committed
Return back audio url decoder; Refactor audio url decoder
1 parent 2b270ad commit b2e7c41

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

vk_api/audio.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from bs4 import BeautifulSoup
55

6+
from .audio_url_decoder import decode_audio_url
67
from .exceptions import AccessDenied
78

89
RE_AUDIO = re.compile(r'audio\d+_\d+_audios\d+')
@@ -94,6 +95,9 @@ def scrap_data(html):
9495
artist = ai_artist[0].text
9596
link = audio.select('.ai_body')[0].input['value']
9697

98+
if 'audio_api_unavailable' in link:
99+
link = decode_audio_url(link)
100+
97101
tracks.append({
98102
'artist': artist,
99103
'title': audio.select('.ai_title')[0].text,

vk_api/audio_url_decoder.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- coding: utf-8 -*-
2+
3+
VK_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN0PQRSTUVWXYZO123456789+/="
4+
5+
6+
def decode_audio_url(string):
7+
vals = string.split("?extra=", 1)[1].split("#")
8+
9+
tstr = vk_o(vals[0])
10+
ops_list = vk_o(vals[1]).split('\x09')[::-1]
11+
12+
for op_data in ops_list:
13+
cmd, *arg = op_data.split('\x0b')
14+
15+
if cmd == 'v':
16+
tstr = tstr[::-1]
17+
18+
elif cmd == 'r':
19+
tstr = vk_r(tstr, arg[0])
20+
21+
elif cmd == 'x':
22+
tstr = vk_xor(tstr, arg[0])
23+
24+
return tstr
25+
26+
27+
def vk_o(string):
28+
result = []
29+
index2 = 0
30+
31+
for s in string:
32+
sym_index = VK_STR.find(s)
33+
34+
if sym_index != -1:
35+
if index2 % 4 != 0:
36+
i = (i << 6) + sym_index
37+
else:
38+
i = sym_index
39+
40+
if index2 % 4 != 0:
41+
index2 += 1
42+
shift = -2 * index2 & 6
43+
result += [chr(0xFF & (i >> shift))]
44+
else:
45+
index2 += 1
46+
47+
return ''.join(result)
48+
49+
50+
def vk_r(string, i):
51+
vk_str2 = VK_STR + VK_STR
52+
vk_str2_len = len(vk_str2)
53+
54+
result = []
55+
56+
for s in string:
57+
index = vk_str2.find(s)
58+
59+
if index != -1:
60+
offset = index - int(i)
61+
62+
if offset < 0:
63+
offset += vk_str2_len
64+
65+
result += [vk_str2[offset]]
66+
else:
67+
result += [s]
68+
69+
return ''.join(result)
70+
71+
72+
def vk_xor(string, i):
73+
xor_val = ord(i[0])
74+
75+
return ''.join(chr(ord(s) ^ xor_val) for s in string)

0 commit comments

Comments
 (0)