Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Commit

Permalink
added support for parallel processing in jpg2pdf
Browse files Browse the repository at this point in the history
fixed multiuser interruption in jpg2pdf command
code works now with multiple parallel users at the same time
  • Loading branch information
Philotheephilix committed Sep 14, 2023
1 parent 566c7bc commit 0b83ed4
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions multi_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
bot = ""
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
message=""
path=os.getcwd()
#Code for handling credential encryption and decryption
def decrypt():
a=1
Expand Down Expand Up @@ -83,7 +84,6 @@ def encrypt():
# Directory creation and verification
def init_dirs():
req_dir=("reels","temppdf","merged","tempimg")
path=os.getcwd()
try:
for i in req_dir:
os.mkdir(path+"\\"+i)
Expand All @@ -105,10 +105,18 @@ def remove(list):
#Code for file handling (jpg2pdf)
@bot.message_handler(content_types=['photo'])
def handle_photos(message):
msgid=str(message.chat.id)
try:
os.mkdir(path+"\\tempimg\\"+msgid+"\\")
os.mkdir(path+"\\temppdf\\"+msgid+"\\")
os.mkdir(path+"\\merged\\"+msgid+"\\")
except:
print("folder exists")
file_id = message.photo[-1].file_id
file = bot.get_file(file_id)
downloaded_file = bot.download_file(file.file_path)
file_name = 'tempimg/' + file.file_path.split('/')[-1]
print(file.file_path)
file_name = 'tempimg/'+msgid + "/"+file.file_path.split('/')[-1]
with open(file_name, 'wb') as new_file:
new_file.write(downloaded_file)
bot.reply_to(message, "Photo saved!")
Expand All @@ -122,17 +130,18 @@ def message(message):
#Code to handle jpg2pdf conversion
@bot.message_handler(commands=["jpg2pdf"])
def convert(message):
dir=tempdir+"\\tempimg"
msgid=str(message.chat.id)
dir=tempdir+"\\tempimg\\"+msgid+"\\"
a=os.listdir(dir)
print(a)
for i in a:
dirf=tempdir+"\\tempimg\\"+i
dirsav=tempdir+"\\temppdf\\"+i+".pdf"
dirf=tempdir+"\\tempimg\\"+msgid+"\\"+i
dirsav=tempdir+"\\temppdf\\"+msgid+"\\"+i+".pdf"
im=Image.open(dirf)
imc=im.convert('RGB')
imc.save(dirsav)
pdfs_dir =tempdir+"\\temppdf\\"
merged_file_name = 'merged\\merged.pdf'
pdfs_dir =tempdir+"\\temppdf\\"+msgid+"\\"
merged_file_name = 'merged\\'+msgid+'\\merged.pdf'
pdf_merger = PdfFileMerger()
for filename in os.listdir(pdfs_dir):
if filename.endswith('.pdf'):
Expand All @@ -142,17 +151,17 @@ def convert(message):
with open(merged_file_name, 'wb') as merged_file:
pdf_merger.write(merged_file)
print('PDFs merged successfully!')
with open("merged\\merged.pdf", 'rb') as pdf_file:
with open("merged\\"+msgid+"\\merged.pdf", 'rb') as pdf_file:
bot.send_document(message.chat.id, pdf_file)
bot.reply_to(message, "PDF file sent")
def junk_removal():
os.remove("merged\\merged.pdf")
os.remove("merged\\"+msgid+"\\merged.pdf")
print("merged pdf deleted....")
for i in a:
os.remove("tempimg\\"+i)
os.remove("tempimg\\"+msgid+"\\"+i)
print("temp img is deleted")
for i in a:
os.remove("temppdf\\"+i+".pdf")
os.remove("temppdf\\"+msgid+"\\"+i+".pdf")
print("temp pdf is deleted..")
junk_removal()
#Code to handle /check_email command
Expand Down Expand Up @@ -257,6 +266,7 @@ def city(message):
bot.send_message(message.chat.id, gw)
bot.send_message(message.chat.id, sr)
bot.send_message(message.chat.id, st)
print(message.chat.id)
is_weather="0"
elif sos_active=="1":
f=open("sos_list.txt","r")
Expand Down

0 comments on commit 0b83ed4

Please sign in to comment.