11from bs4 import BeautifulSoup
22import re
3- from .exceptions import AccessRightsError
3+ from .exceptions import AccessDenied
44
55RE_VALUE = re .compile (r'>(.*?)<' )
66RE_DURATION = re .compile (r'data-dur="([0-9]*)"' )
@@ -10,35 +10,36 @@ class VKAudio:
1010 def __init__ (self , vk ):
1111 self ._vk = vk
1212
13- def get (self , ** kwargs ):
14- response = self ._vk .http .get ('https://m.vk.com/audios{}' .format (kwargs ['owner_id' ]),
15- params = {'offset' : kwargs .get ('offset' , 0 )},
16- allow_redirects = False )
13+ def get (self , owner_id , offset = 0 ):
14+ """ Получение html со списком аудиозаписей пользователя"""
15+ response = self ._vk .http .get (
16+ 'https://m.vk.com/audios{}' .format (owner_id ),
17+ params = {'offset' : offset },
18+ allow_redirects = False )
19+
1720 if response .text == '' :
18- raise AccessRightsError ("You dont have permissions to browse {}'s audios" .format (kwargs ['owner_id' ]))
21+ raise AccessDenied ("You dont have permissions to browse {}'s audios" .format (kwargs ['owner_id' ]))
1922 return scrap_data (response .text )
2023
21- def search (self , ** kwargs ):
22- response = self ._vk .http .get ('https://m.vk.com/audio' , params = {'act' : 'search' ,
23- 'q' : kwargs ['q' ],
24- 'offset' : kwargs .get ('offset' , 0 )})
24+ def search (self , q = '' , offset = 0 ):
25+ """ Получение html со списком аудиозаписей по запросу """
26+ response = self ._vk .http .get (
27+ 'https://m.vk.com/audio' ,
28+ params = {'act' : 'search' ,
29+ 'q' : q ,
30+ 'offset' : offset })
2531 return scrap_data (response .text )
2632
2733
2834def value (tag , duration = False ):
29- string = str ( tag )
35+ """ Извлечение значений из html тега """
3036 if duration :
31- regex = RE_DURATION
32- else :
33- regex = RE_VALUE
34- extracted = regex .search (string )
35- if extracted :
36- groups = extracted .groups ()
37- return groups [0 ]
38- return None
37+ return tag [0 ]['data-dur' ]
38+ return tag [0 ].text
3939
4040
4141def scrap_data (html ):
42+ """ Сбор информации из html и записывание ее в словарь """
4243 soup = BeautifulSoup (html , "html.parser" )
4344 songs = []
4445
0 commit comments