diff --git a/main.py b/main.py index 8878295..bc55bb4 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from pyrogram import filters import asyncio import subprocess from pathlib import Path +import shutil # api_id = #api_hash = "" @@ -15,34 +16,31 @@ from pathlib import Path app = Client("2pdf") -def validDocument(mime_type): # Checks if we support this file format - if mime_type == "application/msword" or mime_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document": - return True - else: - return False +def validDocument(mime_type): + valid_mime_types = [ + "application/msword", # .doc + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx + "application/vnd.ms-excel", # xls + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" # .xlsx + ] + return mime_type in valid_mime_types async def downloadDocument(file_id, file_name): - filepath = await app.download_media(file_id, file_name=file_name) + filepath = await (app.download_media(file_id, file_name=file_name)) return filepath async def convertDocument(sourceDocumentPath, sourceDocumentName): - convertArgs = "/usr/bin/libreoffice --headless -env:UserInstallation=file:///tmp/2g2pdf_${USER} --convert-to pdf " + "\"downloads/" + sourceDocumentName + "\"" + " --outdir output" - subprocess.call(convertArgs, shell=True) - outputDocumentPath = "$(pwd)/output/" + sourceDocumentName - sourceCleanup = "rm " + "\"downloads/" + sourceDocumentName + "\"" - outputDocumentName = Path(f"{sourceDocumentName}").with_suffix('.pdf') - subprocess.call(sourceCleanup, shell=True) - outputDocumentPath = Path("./output") / outputDocumentName + libreofficePath = shutil.which("libreoffice") # Find full path to the libreoffice binary + outputDir = "output" + Path(outputDir).mkdir(parents=True, exist_ok=True) # Ensure directory exists + subprocess.run(args=[libreofficePath, "--headless", "--convert-to", "pdf", "--outdir", outputDir, sourceDocumentPath], check=True) + outputDocumentPath = Path(outputDir) / Path(f"{sourceDocumentName}").with_suffix('.pdf') + subprocess.run(args=["rm", sourceDocumentPath], check=True) return outputDocumentPath async def uploadDocument(outputDocumentPath, chat, message): await app.send_document(document=outputDocumentPath, chat_id=chat, reply_to_message_id=message) - - -@app.on_message(filters.text) -async def echo(client, message): - print(message.text) - await message.reply(message.text) + subprocess.run(args=["rm", outputDocumentPath]) @app.on_message(filters.document) # Get all of the messages that have files in them async def documentFetcher(client, message):