From 44bca45a38370a7b00ae005bed730ce3354a4fca Mon Sep 17 00:00:00 2001 From: Kylmakalle Date: Sun, 25 Jun 2017 15:34:44 +0300 Subject: [PATCH] Fixed attachments preview and .doc(x) duplicating --- bot.py | 2 +- vk_messages.py | 87 ++++++++++++++++++++++++++++---------------------- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/bot.py b/bot.py index f97385b..0e3b1f3 100644 --- a/bot.py +++ b/bot.py @@ -81,7 +81,7 @@ def verifycode(code): def info_extractor(info): - info = info[0].url[8:-1].split('.') + info = info[-1].url[8:-1].split('.') return info diff --git a/vk_messages.py b/vk_messages.py index b8e239b..47c65a4 100644 --- a/vk_messages.py +++ b/vk_messages.py @@ -32,13 +32,13 @@ class VkPolling: def handle_messages(m, vk_user, bot, chat_id, mainmessage=None): user = vk.API(vk_user.session).users.get(user_ids=m["uid"], fields=[])[0] if 'body' in m and not 'attachment' in m and not 'geo' in m and not 'fwd_messages' in m: - data = add_reply_info(m, user["first_name"], user["last_name"]) + data = add_user_info(m, user["first_name"], user["last_name"])[:-1] + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() if 'attachment' in m: attachment_handler(m, user, bot, chat_id, mainmessage) if 'geo' in m: - data = add_reply_info(m, user["first_name"], user["last_name"]) + data = add_user_info(m, user["first_name"], user["last_name"]) + add_reply_info(m) geo = bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() bot.send_venue(chat_id, m['geo']['coordinates'].split(' ')[0], m['geo']['coordinates'].split(' ')[1], @@ -46,7 +46,8 @@ def handle_messages(m, vk_user, bot, chat_id, mainmessage=None): disable_notification=check_notification(m), reply_to_message_id=geo.message_id).wait() if 'fwd_messages' in m: - data = add_reply_info(m, user["first_name"], user["last_name"]) + 'Пересланные сообщения' + data = add_user_info(m, user["first_name"], + user["last_name"]) + 'Пересланные сообщения' + add_reply_info(m) reply = bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait().message_id @@ -63,22 +64,23 @@ def handle_updates(vk_user, bot, chat_id, updates): def attachment_handler(m, user, bot, chat_id, mainmessage=None): if m['attachment']['type'] == 'photo': for photo in m['attachments']: - data = add_reply_info(m, user['first_name'], user['last_name']) + 'Фото'.format( - get_max_src(photo['photo'])) + data = add_user_info(m, user['first_name'], user['last_name']) + 'Фото'.format( + get_max_src(photo['photo'])) + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() if m['attachment']['type'] == 'video': for vid in m['attachments']: link = 'https://vk.com/video{}_{}'.format(vid['video']['owner_id'], vid['video']['vid']) - data = add_reply_info(m, user['first_name'], user['last_name']) + 'Видео'.format(link) + data = add_user_info(m, user['first_name'], user['last_name']) + 'Видео'.format( + link) + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() if m['attachment']['type'] == 'audio': for audio in m['attachments']: - data = add_reply_info(m, user['first_name'], user['last_name']) + '🎵 {} - {}'.format( + data = add_user_info(m, user['first_name'], user['last_name']) + '🎵 {} - {}'.format( audio['audio']['artist'], - audio['audio']['title']) + audio['audio']['title']) + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() if m['attachment']['type'] == 'doc': @@ -86,8 +88,8 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None): if doc['doc']['ext'] == 'gif': try: link = doc['doc']['url'] - data = add_reply_info(m, user["first_name"], user["last_name"]) + 'GIF'.format( - link) + data = add_user_info(m, user["first_name"], user["last_name"]) + 'GIF'.format( + link) + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() except: @@ -96,9 +98,9 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None): elif doc['doc']['ext'] == 'pdf' or doc['doc']['ext'] == 'zip': try: link = doc['doc']['url'] - data = add_reply_info(m, user["first_name"], - user["last_name"], ) + 'Документ'.format( - link) + data = add_user_info(m, user["first_name"], + user["last_name"], ) + 'Документ'.format( + link) + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() except: @@ -107,7 +109,8 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None): elif doc['doc']['ext'] == 'jpg' or doc['doc']['ext'] == 'png': try: link = doc['doc']['url'] - data = add_reply_info(m, user["first_name"], user["last_name"], ) + 'Документ' + data = add_user_info(m, user["first_name"], + user["last_name"], ) + 'Документ' + add_reply_info(m) notification = bot.send_message(chat_id, data, parse_mode='HTML', disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() @@ -121,8 +124,8 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None): elif doc['doc']['ext'] == 'ogg': try: link = doc['doc']['url'] - data = add_reply_info(m, user["first_name"], user["last_name"], ) + \ - 'Аудио'.format(link) + data = add_user_info(m, user["first_name"], user["last_name"], ) + \ + 'Аудио'.format(link) + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() except: @@ -130,37 +133,46 @@ def attachment_handler(m, user, bot, chat_id, mainmessage=None): elif doc['doc']['ext'] == 'doc' or doc['doc']['ext'] == 'docx': try: - data = add_reply_info(m, user["first_name"], user["last_name"], ) + 'Документ' + data = add_user_info(m, user["first_name"], + user["last_name"], ) + 'Документ' + add_reply_info(m) notification = bot.send_message(chat_id, data, parse_mode='HTML', disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() uploading = bot.send_chat_action(chat_id, 'upload_document') file = wget.download(requests.get(doc['doc']['url']).url) - bot.send_document(chat_id, open(file, 'rb'), + openedfile = open(file, 'rb') + bot.send_document(chat_id, openedfile, reply_to_message_id=notification.message_id, disable_notification=check_notification(m)).wait() uploading.wait() - file.close() + openedfile.close() os.remove(file) except: send_doc_link(doc, m, user, bot, chat_id, mainmessage) - else: send_doc_link(doc, m, user, bot, chat_id, mainmessage) if m['attachment']['type'] == 'sticker': link = m['attachment']['sticker']['photo_512'] - data = add_reply_info(m, user["first_name"], user["last_name"], ) + 'Стикер'.format(link) + data = add_user_info(m, user["first_name"], user["last_name"]) + 'Стикер'.format( + link) + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() # TODO: Wall Posts and comments +def check_expansion(document): + print(document['doc']['title'].split('.')) + if len(document['doc']['title'].split('.')) - 1: + return document['doc']['title'] + else: + return document['doc']['title'] + '.' + document['doc']['ext'] + + def send_doc_link(doc, m, user, bot, chat_id, mainmessage=None): link = doc['doc']['url'] - data = add_reply_info(m, user["first_name"], user["last_name"]) + \ - 'Документ\n{}'.format(link, - doc['doc']['title'] + '.' + doc['doc']['ext']) + data = add_user_info(m, user["first_name"], user["last_name"]) + \ + 'Документ\n{}'.format(link, check_expansion(doc)) + add_reply_info(m) bot.send_message(chat_id, data, parse_mode='HTML', disable_web_page_preview=False, disable_notification=check_notification(m), reply_to_message_id=mainmessage).wait() @@ -172,27 +184,24 @@ def check_forward_id(msg): return None -def add_reply_info(m, first_name, last_name): +def add_reply_info(m): + if 'chat_id' in m: + return ''.format(m['uid'], m['chat_id'], check_forward_id(m)) + else: + return ''.format(m['uid'], check_forward_id(m)) + + +def add_user_info(m, first_name, last_name): if m['body']: if 'chat_id' in m: - return '{} {} @ {}:\n{}\n'.format(m['uid'], m['chat_id'], - check_forward_id(m), - first_name, - last_name, m['title'], - m['body'].replace('
', '\n')) + return '{} {} @ {}:\n{}\n'.format(first_name, last_name, m['title'], m['body'].replace('
', '\n')) else: - return '{} {}:\n{}\n'.format(m['uid'], check_forward_id(m), first_name, - last_name, - m['body'].replace('
', '\n')) + return '{} {}:\n{}\n'.format(first_name, last_name, m['body'].replace('
', '\n')) else: if 'chat_id' in m: - return '{} {} @ {}:\n'.format(m['uid'], m['chat_id'], - check_forward_id(m), - first_name, - last_name, m['title']) + return '{} {} @ {}:\n'.format(first_name, last_name, m['title']) else: - return '{} {}:\n'.format(m['uid'], check_forward_id(m), first_name, - last_name) + return '{} {}:\n'.format(first_name, last_name) def check_notification(value):