66from .audio_url_decoder import decode_audio_url
77from .exceptions import AccessDenied
88
9- RE_AUDIO = re .compile (r'audio\d +_\d+_audios\d+' )
9+ RE_AUDIO = re .compile (r'audio[-\d] +_\d+_audios\d+' )
1010
1111
1212class VkAudio :
@@ -16,15 +16,28 @@ class VkAudio:
1616 def __init__ (self , vk ):
1717 self ._vk = vk
1818
19- def get (self , owner_id , offset = 0 ):
19+ def get (self , owner_id = None , album_id = None , offset = 0 ):
2020 """ Получить список аудиозаписей пользователя
2121
2222 :param owner_id: ID владельца (отрицательные значения для групп)
23+ :param album_id: ID альбома (отрицательные значения для групп)
2324 :param offset: смещение
2425 """
2526
27+ if owner_id is None and album_id is None :
28+ raise TypeError (
29+ 'get() missing 1 required argument: album_id or owner_id'
30+ )
31+ elif owner_id is not None and album_id is not None :
32+ raise TypeError ('get() too many arguments' )
33+
34+ if album_id is not None :
35+ url = 'https://m.vk.com/audio?act=audio_playlist{}' .format (album_id )
36+ else :
37+ url = 'https://m.vk.com/audios{}' .format (owner_id )
38+
2639 response = self ._vk .http .get (
27- 'https://m.vk.com/audios{}' . format ( owner_id ) ,
40+ url ,
2841 params = {
2942 'offset' : offset
3043 },
@@ -33,12 +46,34 @@ def get(self, owner_id, offset=0):
3346
3447 if not response .text :
3548 raise AccessDenied (
36- 'You don\' t have permissions to browse {}\' s audio' .format (
49+ 'You don\' t have permissions to browse user\' s audio'
50+ )
51+
52+ return scrap_data (response .text )
53+
54+ def get_albums (self , owner_id , offset = 0 ):
55+ """ Получить список альбомов пользователя
56+
57+ :param owner_id: ID владельца (отрицательные значения для групп)
58+ :param offset: смещение
59+ """
60+
61+ response = self ._vk .http .get (
62+ 'https://m.vk.com/audio?act=audio_playlists{}' .format (owner_id ),
63+ params = {
64+ 'offset' : offset
65+ },
66+ allow_redirects = False
67+ )
68+
69+ if not response .text :
70+ raise AccessDenied (
71+ 'You don\' t have permissions to browse {}\' s albums' .format (
3772 owner_id
3873 )
3974 )
4075
41- return scrap_data (response .text )
76+ return scrap_albums (response .text )
4277
4378 def search_user (self , owner_id , q = '' ):
4479 """ Искать по аудиозаписям пользователя
@@ -92,8 +127,7 @@ def scrap_data(html):
92127
93128 soup = BeautifulSoup (html , 'html.parser' )
94129 tracks = []
95-
96- for audio in soup .find_all ('div' , {'class' : 'audio_item ai_has_btn' }):
130+ for audio in soup .find_all ('div' , {'class' : 'audio_item' }):
97131 ai_artist = audio .select ('.ai_artist' )
98132 artist = ai_artist [0 ].text
99133 link = audio .select ('.ai_body' )[0 ].input ['value' ]
@@ -110,3 +144,22 @@ def scrap_data(html):
110144 })
111145
112146 return tracks
147+
148+
149+ def scrap_albums (html ):
150+ """ Парсинг списка альбомов из html странцы """
151+
152+ soup = BeautifulSoup (html , 'html.parser' )
153+ albums = []
154+ for album in soup .find_all ('div' , {'class' : 'audioPlaylistsPage__item' }):
155+ link = album .select ('.audioPlaylistsPage__itemLink' )[0 ]['href' ]
156+
157+ albums .append ({
158+ 'artist' : album .select ('.audioPlaylistsPage__author' )[0 ].text ,
159+ 'title' : album .select ('.audioPlaylistsPage__title' )[0 ].text ,
160+ 'plays' : album .select ('.audioPlaylistsPage__stats' )[0 ].text ,
161+ 'id' : album ['class' ][1 ][25 :],
162+ 'url' : 'https://m.vk.com/audio?act=audio_playlist{}' .format (link )
163+ })
164+
165+ return albums
0 commit comments