Skip to content

Commit 9234976

Browse files
authored
Add user audio follow and unfollow functionality (#483)
1 parent 156a562 commit 9234976

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

vk_api/audio.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
RE_ACCESS_HASH = re.compile(r'access_hash=(\w+)')
2222
RE_M3U8_TO_MP3 = re.compile(r'/[0-9a-f]+(/audios)?/([0-9a-f]+)/index.m3u8')
2323

24+
RE_USER_AUDIO_HASH = re.compile(r"AudioUtils.(un)?followOwner\(\d+, '([^)]+)'\)")
25+
2426
RPS_DELAY_RELOAD_AUDIO = 1.5
2527
RPS_DELAY_LOAD_SECTION = 2.0
2628

@@ -543,6 +545,56 @@ def get_post_audio(self, owner_id, post_id):
543545

544546
return tracks
545547

548+
def follow_user(self, user_id):
549+
data = self._vk.http.get(f"https://vk.com/audios{user_id}")
550+
551+
user_hash = RE_USER_AUDIO_HASH.search(data.text)
552+
if user_hash is None:
553+
raise AccessDenied(
554+
'You don\'t have permissions to browse {}\'s audio'.format(
555+
user_id
556+
)
557+
)
558+
user_hash = user_hash.groups()[1]
559+
560+
response = self._vk.http.post(
561+
'https://vk.com/al_audio.php',
562+
data={
563+
'al': 1,
564+
'act': 'follow_owner',
565+
'owner_id': user_id,
566+
'hash': user_hash,
567+
}
568+
)
569+
json_response = json.loads(response.text.replace('<!--', ''))
570+
571+
return json_response
572+
573+
def unfollow_user(self, user_id):
574+
data = self._vk.http.get(f"https://vk.com/audios{user_id}")
575+
576+
user_hash = RE_USER_AUDIO_HASH.search(data.text)
577+
if user_hash is None:
578+
raise AccessDenied(
579+
'You don\'t have permissions to browse {}\'s audio'.format(
580+
user_id
581+
)
582+
)
583+
user_hash = user_hash.groups()[1]
584+
585+
response = self._vk.http.post(
586+
'https://vk.com/al_audio.php',
587+
data={
588+
'al': 1,
589+
'act': 'unfollow_owner',
590+
'owner_id': user_id,
591+
'hash': user_hash,
592+
}
593+
)
594+
json_response = json.loads(response.text.replace('<!--', ''))
595+
596+
return json_response
597+
546598

547599
def scrap_ids(audio_data):
548600
""" Парсинг списка хэшей аудиозаписей из json объекта """

0 commit comments

Comments
 (0)