|
|
|
# -*- coding: utf8 -*-
|
|
|
|
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
|
|
|
|
|
|
|
|
# Сообщения для работы с документами
|
|
|
|
|
|
|
|
from bot_sys import log, config, user_access
|
|
|
|
from bot_modules import groups_utils
|
|
|
|
from template import simple_message
|
|
|
|
#import odf
|
|
|
|
|
|
|
|
def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_ErrorMessage, access_mode = user_access.AccessMode.EDIT):
|
|
|
|
async def DocFiles(a_Message):
|
|
|
|
user_id = str(a_Message.from_user.id)
|
|
|
|
user_groups= groups_utils.GetUserGroupData(a_Bot, user_id)
|
|
|
|
if not user_access.CheckAccess(a_Bot.GetRootIDs(), a_AccessFunc(), user_groups, access_mode):
|
|
|
|
return await simple_message.AccessDeniedMessage(a_Bot, a_GetButtonsFunc, user_id, a_Message, user_groups)
|
|
|
|
|
|
|
|
msg = a_CaptionMessage.GetDesc()
|
|
|
|
msg = msg.replace('@time', a_Bot.GetLog().GetTime())
|
|
|
|
|
|
|
|
files = a_FilesFunc(user_id)
|
|
|
|
if not files:
|
|
|
|
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups)
|
|
|
|
return
|
|
|
|
|
|
|
|
for file_path, dict_replace in files.items():
|
|
|
|
if not dict_replace:
|
|
|
|
continue
|
|
|
|
new_file = await MakeDocFile(a_Bot, file_path, dict_replace, user_id)
|
|
|
|
document = await GetFile(a_Bot, new_file)
|
|
|
|
if document is None:
|
|
|
|
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups)
|
|
|
|
else:
|
|
|
|
await a_Bot.SendDocument(
|
|
|
|
user_id,
|
|
|
|
document,
|
|
|
|
msg,
|
|
|
|
simple_message.ProxyGetButtonsTemplate(a_GetButtonsFunc)(a_Message, user_groups),
|
|
|
|
simple_message.ProxyGetButtonsTemplate(a_GetInlineButtonsFunc)(a_Message, user_groups)
|
|
|
|
)
|
|
|
|
return DocFiles
|
|
|
|
|
|
|
|
from odf import opendocument, text, teletype
|
|
|
|
|
|
|
|
async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id):
|
|
|
|
print ('MakeDocFile', a_FilePath)
|
|
|
|
#FixBadZipfile(a_FilePath)
|
|
|
|
document = opendocument.load(a_FilePath) # odf.opendocuement.
|
|
|
|
if document == None:
|
|
|
|
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_FilePath}.')
|
|
|
|
return None
|
|
|
|
|
|
|
|
for element in document.getElementsByType(text.Span):
|
|
|
|
extracted_text = teletype.extractText(element)
|
|
|
|
print('MakeDocFile', extracted_text)
|
|
|
|
|
|
|
|
for rep_this, to_this in a_DictReplace.items():
|
|
|
|
if extracted_text.find(rep_this) != -1:
|
|
|
|
extracted_text = extracted_text.replace(rep_this, to_this)
|
|
|
|
|
|
|
|
new_element = text.Span()
|
|
|
|
new_element.setAttribute('stylename', element.getAttribute('stylename'))
|
|
|
|
new_element.addText(extracted_text)
|
|
|
|
|
|
|
|
element.parentNode.insertBefore(new_element, element)
|
|
|
|
element.parentNode.removeChild(element)
|
|
|
|
|
|
|
|
new_file_path = a_FilePath[:-4]
|
|
|
|
new_file_path += f"_{a_user_id}.odt"
|
|
|
|
document.save(new_file_path)
|
|
|
|
return new_file_path
|
|
|
|
|
|
|
|
async def GetFile(a_Bot, a_Path):
|
|
|
|
if not a_Path:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
document = open(a_Path, 'rb')
|
|
|
|
a_Bot.GetLog().Success(f'Загружен файл {a_Path}')
|
|
|
|
return document
|
|
|
|
except Exception as e:
|
|
|
|
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_Path}. Ошибка {str(e)}')
|
|
|
|
return None
|