-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvk_audio_extractor.py
56 lines (44 loc) · 1.76 KB
/
vk_audio_extractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import shutil
import requests
import vk_api
import datetime
def download_audio(title, link):
response = requests.get(link)
current_datetime = str(int(datetime.datetime.now().timestamp()))
filename = f"{str(title)}_[{current_datetime}].mp3"
if response.status_code == 200:
with open(filename, 'wb') as f:
f.write(response.content)
shutil.move(filename, 'temp_audios/' + filename)
print(f'Аудиофайл успешно скачан как {filename}')
return 'temp_audios/' + filename
else:
return False
def get_audio(post_id): # simply parsing vk-api data for direct links
access_token = 'vk_api_access_token'
vk_session = vk_api.VkApi(token=access_token)
vk = vk_session.get_api()
post_info = vk.wall.getById(posts=post_id)
post_attachments = post_info[0]['attachments']
# print(json.dumps(post_attachments, indent=4))
audio_files = []
for ind, attachment in enumerate(post_attachments):
if attachment['type'] != "audio":
continue
audio_attachment_dict = {
'is_file': False,
'url': attachment["audio"]["url"],
'artist': attachment["audio"]['artist'],
"title": attachment['audio']['title']}
if int(attachment["audio"]["duration"]) >= 30:
file_path = download_audio(audio_attachment_dict['title'], audio_attachment_dict["url"])
if not file_path:
continue
audio_attachment_dict['is_file'] = True
audio_attachment_dict['url'] = file_path
audio_files.append(audio_attachment_dict)
print(audio_files)
return audio_files
if __name__ == "__main__":
post_id = '-000000000_0' # sample post ID
get_audio(post_id)