Skip to content

Commit a5ba65f

Browse files
committed
Fix VkAudio
1 parent 4f274f9 commit a5ba65f

1 file changed

Lines changed: 47 additions & 5 deletions

File tree

vk_api/audio.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .audio_url_decoder import decode_audio_url
1515
from .exceptions import AccessDenied
16+
from .utils import set_cookies_from_list
1617

1718
RE_ALBUM_ID = re.compile(r'act=audio_playlist(-?\d+)_(\d+)')
1819
RE_ACCESS_HASH = re.compile(r'access_hash=(\w+)')
@@ -31,14 +32,52 @@ class VkAudio(object):
3132

3233
__slots__ = ('_vk', 'user_id')
3334

34-
def __init__(self, vk, convert_m3u8_links=True):
35+
DEFAULT_COOKIES = [
36+
{ # если не установлено, то первый запрос ломается
37+
'version': 0,
38+
'name': 'remixaudio_show_alert_today',
39+
'value': '0',
40+
'port': None,
41+
'port_specified': False,
42+
'domain': '.vk.com',
43+
'domain_specified': True,
44+
'domain_initial_dot': True,
45+
'path': '/',
46+
'path_specified': True,
47+
'secure': True,
48+
'expires': None,
49+
'discard': False,
50+
'comment': None,
51+
'comment_url': None,
52+
'rfc2109': False,
53+
'rest': {}
54+
}, { # для аудио из постов
55+
'version': 0,
56+
'name': 'remixmdevice',
57+
'value': '1920/1080/2/!!-!!!!',
58+
'port': None,
59+
'port_specified': False,
60+
'domain': '.vk.com',
61+
'domain_specified': True,
62+
'domain_initial_dot': True,
63+
'path': '/',
64+
'path_specified': True,
65+
'secure': True,
66+
'expires': None,
67+
'discard': False,
68+
'comment': None,
69+
'comment_url': None,
70+
'rfc2109': False,
71+
'rest': {}
72+
}
73+
]
3574

75+
def __init__(self, vk, convert_m3u8_links=True):
3676
self.user_id = vk.method('users.get')[0]['id']
3777
self._vk = vk
78+
self.convert_m3u8_links = convert_m3u8_links
3879

39-
self._vk.http.cookies.update({
40-
'remixmdevice': '1920/1080/1/!!-!!!!'
41-
})
80+
set_cookies_from_list(self._vk.http.cookies, self.DEFAULT_COOKIES)
4281

4382
def get_iter(self, owner_id=None, album_id=None, access_hash=None):
4483
""" Получить список аудиозаписей пользователя (по частям)
@@ -230,7 +269,7 @@ def get_audio_by_id(self, owner_id, audio_id):
230269
link = bs.select_one('.ai_body input[type=hidden]').attrs['value']
231270
decode_link = decode_audio_url(link, self.user_id)
232271
if self.convert_m3u8_links and 'm3u8' in decode_link:
233-
return link = RE_M3U8_TO_MP3.sub(r'\1/\2.mp3', decode_link)
272+
return RE_M3U8_TO_MP3.sub(r'\1/\2.mp3', decode_link)
234273
else:
235274
return decode_link
236275

@@ -264,6 +303,9 @@ def scrap_data(html, user_id, filter_root_el=None, convert_m3u8_links=True):
264303

265304
root_el = soup.find(**filter_root_el)
266305

306+
if root_el is None:
307+
raise ValueError('Could not find root el for audio')
308+
267309
for audio in root_el.find_all('div', {'class': 'audio_item'}):
268310
if 'audio_item_disabled' in audio['class']:
269311
continue

0 commit comments

Comments
 (0)