Skip to content

Commit 68109cd

Browse files
authored
Creating and editing an audio in the web version. (#482)
1 parent 803254e commit 68109cd

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

vk_api/audio.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .audio_url_decoder import decode_audio_url
1717
from .exceptions import AccessDenied
1818
from .utils import set_cookies_from_list
19+
from .upload import FilesOpener
1920

2021
RE_ALBUM_ID = re.compile(r'act=audio_playlist(-?\d+)_(\d+)')
2122
RE_ACCESS_HASH = re.compile(r'access_hash=(\w+)')
@@ -253,6 +254,63 @@ def search_user(self, owner_id=None, q=''):
253254
else:
254255
return []
255256

257+
def edit_audio(self, audio_id: int, owner_id: int, hash: str, performer: str, title: str, text: str = "", genre: int = 1001):
258+
""" Редактировать аудиозапись
259+
260+
:param audio_id: ID аудиозаписи
261+
:param owner_id: ID владельца (отрицательные значения для групп)
262+
:param hash: хэш для редактирования аудиозаписи
263+
:param performer: название аудиозаписи
264+
:param title: заголовок аудиозаписи
265+
:param text: текст аудиозаписи
266+
:param genre: жанр аудиозаписи
267+
"""
268+
response = self._vk.http.post(
269+
'https://vk.com/al_audio.php',
270+
data={
271+
'al': 1,
272+
'act': 'edit_audio',
273+
'aid': audio_id,
274+
'oid': owner_id,
275+
'force_edit_hash': '',
276+
'hash': hash,
277+
'performer': performer,
278+
'text': text,
279+
'title': title,
280+
'genre': genre
281+
}
282+
)
283+
json_response = json.loads(response.text.replace('<!--', ''))
284+
return json_response["payload"][1][0]
285+
286+
def upload_audio(self, audio: str, group_id: int = 0):
287+
""" Загрузка аудиозаписи
288+
289+
:param group_id: ID группы, для юзера - 0
290+
"""
291+
response = self._vk.http.post(
292+
'https://vk.com/al_audio.php',
293+
data={
294+
'al': 1,
295+
'act': 'new_audio',
296+
'boxhash': base36encode(),
297+
'gid': group_id
298+
}
299+
)
300+
url = re.search("(https?:[^']*)", json.loads(response.text.replace('<!--', ''))["payload"][1][2]).group(0)
301+
with FilesOpener(audio, key_format='file') as f:
302+
uploader_response = self._vk.http.post(url, files=f).json()
303+
response = self._vk.http.post(
304+
'https://vk.com/al_audio.php',
305+
data={
306+
'al': 1,
307+
'act': 'done_add',
308+
'go_uploader_response': json.dumps(uploader_response),
309+
'upldr': 1
310+
}
311+
)
312+
return json.loads(response.text.replace('<!--', ''))["payload"][1][0]
313+
256314
def search(self, q, count=100, offset=0):
257315
""" Искать аудиозаписи
258316
@@ -704,3 +762,14 @@ def scrap_albums(html):
704762
})
705763

706764
return albums
765+
766+
def base36encode():
767+
number = int(time.time() * 1000)
768+
alphabet = '0123456789abcdefghijklmnopqrstuvwxyz'
769+
base36 = ''
770+
771+
while number != 0:
772+
number, i = divmod(number, len(alphabet))
773+
base36 = alphabet[i] + base36
774+
775+
return base36

0 commit comments

Comments
 (0)