Skip to content

Commit bfc883d

Browse files
Марат Недогимовpython273
authored andcommitted
Convertation m3u8 in mp3 (get_audio_by_id) (#325)
1 parent 0334b8c commit bfc883d

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

vk_api/audio.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class VkAudio(object):
3131

3232
__slots__ = ('_vk', 'user_id')
3333

34-
def __init__(self, vk):
34+
def __init__(self, vk, convert_m3u8_links=True):
3535

3636
self.user_id = vk.method('users.get')[0]['id']
3737
self._vk = vk
@@ -223,7 +223,29 @@ def get_audio_by_id(self, owner_id, audio_id):
223223
)
224224
bs = BeautifulSoup(response.text, 'html.parser')
225225
link = bs.select_one('.ai_body input[type=hidden]').attrs['value']
226-
return decode_audio_url(link, self.user_id)
226+
decode_link = decode_audio_url(link, self.user_id)
227+
if self.convert_m3u8_links and 'm3u8' in decode_link:
228+
return link = RE_M3U8_TO_MP3.sub(r'\1/\2.mp3', decode_link)
229+
else:
230+
return decode_link
231+
232+
233+
def get_post_audio(self, owner_id, post_id):
234+
""" Получить список аудиозаписей из поста пользователя или группы
235+
:param owner_id: ID владельца (отрицательные значения для групп)
236+
:param post_id: ID поста
237+
"""
238+
response = self._vk.http.get(
239+
'https://m.vk.com/wall{}_{}'.format(owner_id, post_id)
240+
)
241+
242+
tracks = scrap_data(
243+
response.text,
244+
self.user_id,
245+
filter_root_el={'class': 'audios_list'}
246+
)
247+
248+
return tracks
227249

228250
def get_post_audio(self, owner_id, post_id):
229251
""" Получить список аудиозаписей из поста пользователя или группы

0 commit comments

Comments
 (0)