Created
August 31, 2021 09:12
-
-
Save yukkerike/11232f8424315002f5d7e40168c8e324 to your computer and use it in GitHub Desktop.
Скачать вложения из диалога ВКонтакте (vk.com)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Данный скрипт скачает вложения из диалога в папку, в которой лежит данный скрипт, | |
# поэтому запускайте его из папки, где лежит только он, чтобы избежать появления мусора. | |
# Запуск скрипта: python3 main.py | |
# Для работы скрипта необходимо установить библиотеку vk_api | |
# python3 -m pip install vk_api | |
# На windows пишите py вместо python3. | |
access_token = "" # токен, может быть получен здесь: | |
# https://oauth.vk.com/authorize?client_id=2685278&scope=69632&redirect_uri=https://oauth.vk.com/blank.html&display=page&response_type=token&revoke=1 | |
peer_id = 2000000000 # может быть как id беседы (2000000000 + id), так и id личного диалога | |
user_id = 'all' # id автора, по которому фильтруются сообщения, | |
# либо 'all', чтобы анализировать все сообщения в диалоге | |
attach_type = 'all' # может принимать значения 'all', 'photo', 'audio_message', 'doc' | |
import vk_api | |
import requests | |
import os | |
import time | |
cwd = os.path.dirname(os.path.abspath(__file__)) | |
vk_session = vk_api.VkApi(token=access_token, api_version='5.131') | |
vk = vk_session.get_api() | |
offset = 0 | |
def find_biggest_image(sizes): | |
maxHeight = 0 | |
maxUrl = "" | |
for j in sizes: | |
if j['height'] > maxHeight: | |
maxHeight = j['height'] | |
maxUrl = j['url'] | |
return maxUrl | |
def get_link(attachment): | |
if attachment['type'] == 'photo': | |
return find_biggest_image(attachment['photo']['sizes']), '.jpg' | |
elif attachment['type'] == 'audio_message': | |
return attachment['audio_message']['link_mp3'], '.mp3' | |
elif attachment['type'] == 'doc': | |
return attachment['doc']['url'], '.'+attachment['doc']['ext'] | |
else: | |
return (None,)*2 | |
def messages_lookup(messages): | |
for message in messages: | |
if (user_id == 'all' or message['from_id'] == int(user_id)) and message['attachments'] != []: | |
for i, attachment in enumerate(message['attachments']): | |
if attach_type != 'all' and attachment['type'] != attach_type: continue | |
link, extension = get_link(attachment) | |
if link is None: continue | |
with open(os.path.join(cwd, time.strftime('%d.%m.%Y_%H:%M:%S', time.localtime(message['date'])) + f'_{i}.' + extension), 'wb') as f: | |
f.write(requests.get(link).content) | |
if 'fwd_messages' in message and message['fwd_messages'] != []: | |
messages_lookup(message['fwd_messages']) | |
while True: | |
tmp = vk.messages.getHistory(peer_id=peer_id, count=200, offset=offset)['items'] | |
if not len(tmp): break | |
messages_lookup(tmp) | |
offset += 200 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment