Init PoC. Add .gitignore

This commit is contained in:
neko 2025-01-16 19:43:35 +05:00
commit 274946d22a
2 changed files with 58 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.session

57
main.py Normal file
View File

@ -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()