Update uploadDocument and rewrite convertDocument, update .gitignore
This commit is contained in:
parent
72041a472d
commit
f00950f95b
36
main.py
36
main.py
|
@ -3,6 +3,7 @@ from pyrogram import filters
|
||||||
import asyncio
|
import asyncio
|
||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
|
||||||
# api_id =
|
# api_id =
|
||||||
#api_hash = ""
|
#api_hash = ""
|
||||||
|
@ -15,34 +16,31 @@ from pathlib import Path
|
||||||
|
|
||||||
app = Client("2pdf")
|
app = Client("2pdf")
|
||||||
|
|
||||||
def validDocument(mime_type): # Checks if we support this file format
|
def validDocument(mime_type):
|
||||||
if mime_type == "application/msword" or mime_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
valid_mime_types = [
|
||||||
return True
|
"application/msword", # .doc
|
||||||
else:
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx
|
||||||
return False
|
"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):
|
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
|
return filepath
|
||||||
|
|
||||||
async def convertDocument(sourceDocumentPath, sourceDocumentName):
|
async def convertDocument(sourceDocumentPath, sourceDocumentName):
|
||||||
convertArgs = "/usr/bin/libreoffice --headless -env:UserInstallation=file:///tmp/2g2pdf_${USER} --convert-to pdf " + "\"downloads/" + sourceDocumentName + "\"" + " --outdir output"
|
libreofficePath = shutil.which("libreoffice") # Find full path to the libreoffice binary
|
||||||
subprocess.call(convertArgs, shell=True)
|
outputDir = "output"
|
||||||
outputDocumentPath = "$(pwd)/output/" + sourceDocumentName
|
Path(outputDir).mkdir(parents=True, exist_ok=True) # Ensure directory exists
|
||||||
sourceCleanup = "rm " + "\"downloads/" + sourceDocumentName + "\""
|
subprocess.run(args=[libreofficePath, "--headless", "--convert-to", "pdf", "--outdir", outputDir, sourceDocumentPath], check=True)
|
||||||
outputDocumentName = Path(f"{sourceDocumentName}").with_suffix('.pdf')
|
outputDocumentPath = Path(outputDir) / Path(f"{sourceDocumentName}").with_suffix('.pdf')
|
||||||
subprocess.call(sourceCleanup, shell=True)
|
subprocess.run(args=["rm", sourceDocumentPath], check=True)
|
||||||
outputDocumentPath = Path("./output") / outputDocumentName
|
|
||||||
return outputDocumentPath
|
return outputDocumentPath
|
||||||
|
|
||||||
async def uploadDocument(outputDocumentPath, chat, message):
|
async def uploadDocument(outputDocumentPath, chat, message):
|
||||||
await app.send_document(document=outputDocumentPath, chat_id=chat, reply_to_message_id=message)
|
await app.send_document(document=outputDocumentPath, chat_id=chat, reply_to_message_id=message)
|
||||||
|
subprocess.run(args=["rm", outputDocumentPath])
|
||||||
|
|
||||||
@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
|
@app.on_message(filters.document) # Get all of the messages that have files in them
|
||||||
async def documentFetcher(client, message):
|
async def documentFetcher(client, message):
|
||||||
|
|
Loading…
Reference in New Issue