Skip to content

Commit 58d1275

Browse files
committed
Add albums parsing
1 parent fbd35b5 commit 58d1275

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

examples/get_album_audio.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,26 @@ def main():
2424
offset = 0
2525

2626
while True:
27-
audios = vkaudio.get(album_id='-89975290_74038117', offset=offset)
27+
albums = vkaudio.get(owner_id='194957739', get_albums=True, offset=offset)
2828

29-
if not audios:
29+
if not albums:
3030
break
3131

32-
for audio in audios:
33-
artists[audio['artist']] += 1
32+
for album in albums:
33+
artists[album['artist']] += 1
3434

35-
offset += len(audios)
35+
offset += len(albums)
3636

3737
# Составляем рейтинг первых 15
3838
print('\nTop 15:')
3939
for artist, tracks in artists.most_common(15):
4040
print('{} - {} tracks'.format(artist, tracks))
4141

42-
# Ищем треки самого популярного
43-
most_common_artist = artists.most_common(1)[0][0]
42+
# Ищем треки последнего альбома
43+
album = vkaudio.get(owner_id='194957739', get_albums=True)[0]
44+
print('\nSearch for', album['title'])
4445

45-
print('\nSearch for', most_common_artist)
46-
47-
tracks = vkaudio.search(q=most_common_artist)[:10]
46+
tracks = vkaudio.get(album_id=album['id'][25:])
4847

4948
for n, track in enumerate(tracks, 1):
5049
print('{}. {} {}'.format(n, track['title'], track['url']))

vk_api/audio.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .audio_url_decoder import decode_audio_url
77
from .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

1212
class VkAudio:
@@ -30,12 +30,16 @@ def get(self, owner_id=None, album_id=None, offset=0):
3030
)
3131
elif owner_id is not None and album_id is not None:
3232
raise TypeError('get() too many arguments')
33+
if album_id is not None and get_albums is True:
34+
raise TypeError('get() too many arguments')
3335

3436
id = owner_id
3537
url = 'https://m.vk.com/audios{}'
3638
if album_id is not None:
3739
id = album_id
3840
url = 'https://m.vk.com/audio?act=audio_playlist{}'
41+
if get_albums is True:
42+
url = 'https://m.vk.com/audio?act=audio_playlists{}'
3943

4044
response = self._vk.http.get(
4145
url.format(id),
@@ -52,6 +56,8 @@ def get(self, owner_id=None, album_id=None, offset=0):
5256
)
5357
)
5458

59+
if get_albums:
60+
return scrap_albums(response.text)
5561
return scrap_data(response.text)
5662

5763
def search_user(self, owner_id, q=''):
@@ -123,3 +129,22 @@ def scrap_data(html):
123129
})
124130

125131
return tracks
132+
133+
134+
def scrap_albums(html):
135+
""" Парсинг списка альбомов из html странцы """
136+
137+
soup = BeautifulSoup(html, 'html.parser')
138+
albums = []
139+
for album in soup.find_all('div', {'class': 'audioPlaylistsPage__item'}):
140+
link = album.select('.audioPlaylistsPage__itemLink')[0]['href']
141+
142+
albums.append({
143+
'artist': album.select('.audioPlaylistsPage__author')[0].text,
144+
'title': album.select('.audioPlaylistsPage__title')[0].text,
145+
'plays': album.select('.audioPlaylistsPage__stats')[0].text,
146+
'id': album['class'][1],
147+
'url': 'https://m.vk.com/audio?act=audio_playlist{}'.format(link)
148+
})
149+
150+
return albums

0 commit comments

Comments
 (0)