commit 274946d22a493a2daf4d4b3a6ee2f59d3904e987 Author: neko Date: Thu Jan 16 19:43:35 2025 +0500 Init PoC. Add .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39bb39a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.session diff --git a/main.py b/main.py new file mode 100644 index 0000000..8878295 --- /dev/null +++ b/main.py @@ -0,0 +1,57 @@ +from pyrogram import Client +from pyrogram import filters +import asyncio +import subprocess +from pathlib import Path + +# api_id = +#api_hash = "" +#bot_token = "" +# app = Client( +# "2pdf", +# api_id=api_id, api_hash=api_hash, +# bot_token=bot_token +# ) + +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 + +async def downloadDocument(file_id, 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 + 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) + +@app.on_message(filters.document) # Get all of the messages that have files in them +async def documentFetcher(client, message): + mime_type = message.document.mime_type + if validDocument(mime_type=mime_type) == True: + sourceDocumentPath = await downloadDocument(message.document.file_id, message.document.file_name) + outputDocumentPath = await convertDocument(sourceDocumentPath, message.document.file_name) + await uploadDocument(outputDocumentPath, message.chat.id, message.id) + else: + return # Hopefully message will get ignored and no futher resources will be used + +app.run() \ No newline at end of file