-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (49 loc) · 1.41 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import vk_api
import io
import os
def vk_auth(login, password, api = None):
vk_session = vk_api.VkApi(login, password)
vk_session.auth()
if api:
return vk_session.get_api()
else:
return vk_session
def download_file(url, path):
if not os.path.isdir(path):
os.mkdir(path)
response = requests.get(url, stream=True)
file_name = url.split('/')[-1]
file_path = f'{path}/{file_name}'
with open(file_path, 'wb') as jf:
jf.write(response.content)
return file_path
def upload_voice(account, url):
audio_file = download_file(url, 'messages')
upload = vk_api.upload.VkUpload(account)
vk_response = upload.audio_message(audio_file)
audio_message = vk_response.get('audio_message')
return audio_message
def voice_loop(account):
while True:
try:
url = input('\nGet URL (.ogg): ')
voice = upload_voice(account, url)
except KeyboardInterrupt as e:
print('\n')
break
except Exception as e:
print(e)
else:
print(f'vk.com/doc{voice.get("owner_id")}_{voice.get("id")}')
def main():
login = input('Login: ')
password = input('Password: ')
try:
account = vk_auth(login, password, True)
except Exception as e:
print(e)
else:
voice_loop(account)
if __name__ == '__main__':
main()