Improved error control

This commit is contained in:
Alfonso E.M 2021-02-21 14:58:39 +01:00
parent c920632227
commit 7532edc2e7

View file

@ -120,8 +120,8 @@ async def set_progress(filename, message, received, total):
progress_message= "{0} % ({1} / {2})".format(percentage, received, total)
in_progress[filename] = progress_message
# if (int(percentage) % 5) == 0:
# await log_reply(message, progress_message)
if (int(percentage) % 5) == 0:
await log_reply(message, progress_message)
with TelegramClient(getSession(), api_id, api_hash,
@ -139,59 +139,65 @@ with TelegramClient(getSession(), api_id, api_hash,
return
print(event)
try:
if not event.media and event.message:
command = event.message.message
command = command.lower()
output = "Unknown command"
if not event.media and event.message:
command = event.message.message
command = command.lower()
output = "Unknown command"
if command == "list":
output = subprocess.run(["ls -l "+downloadFolder], shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,encoding="utf-8").stdout
elif command == "status":
try:
output = "".join([ "{0}: {1}\n".format(key,value) for (key, value) in in_progress.items()])
if output:
output = "Active downloads:\n\n" + output
else:
output = "No active downloads"
except:
output = "Some error occured while checking the status. Retry."
elif command == "clean":
output = "Cleaning "+tempFolder+"\n"
output+=subprocess.run(["rm "+tempFolder+"/*."+TELEGRAM_DAEMON_TEMP_SUFFIX], shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,encoding="utf-8").stdout
else:
output = "Available commands: list, status, clean"
if command == "list":
output = subprocess.run(["ls -l "+downloadFolder], shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,encoding="utf-8").stdout
elif command == "status":
try:
output = "".join([ "{0}: {1}\n".format(key,value) for (key, value) in in_progress.items()])
if output:
output = "Active downloads:\n\n" + output
else:
output = "No active downloads"
except:
output = "Some error occured while checking the status. Retry."
elif command == "clean":
output = "Cleaning "+tempFolder+"\n"
output+=subprocess.run(["rm "+tempFolder+"/*."+TELEGRAM_DAEMON_TEMP_SUFFIX], shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,encoding="utf-8").stdout
else:
output = "Available commands: list, status, clean"
await log_reply(event, output)
await log_reply(event, output)
if event.media:
filename=getFilename(event)
message=await event.reply("{0} added to queue".format(filename))
await queue.put([event, message])
if event.media:
filename=getFilename(event)
message=await event.reply("{0} added to queue".format(filename))
await queue.put([event, message])
except Exception as e:
print('Events handler error: ', e)
async def worker():
while True:
element = await queue.get()
event=element[0]
message=element[1]
try:
element = await queue.get()
event=element[0]
message=element[1]
filename=getFilename(event)
filename=getFilename(event)
await log_reply(
message,
"Downloading file {0} ({1} bytes)".format(filename,event.media.document.size)
)
await log_reply(
message,
"Downloading file {0} ({1} bytes)".format(filename,event.media.document.size)
)
download_callback = lambda received, total: set_progress(filename, message, received, total)
download_callback = lambda received, total: set_progress(filename, message, received, total)
await client.download_media(event.message, "{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), progress_callback = download_callback)
set_progress(filename, message, 100, 100)
rename("{0}/{1}.{2}".format(temFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), "{0}/{1}".format(downloadFolder,filename))
await log_reply(message, "{0} ready".format(filename))
queue.task_done()
await client.download_media(event.message, "{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), progress_callback = download_callback)
set_progress(filename, message, 100, 100)
rename("{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), "{0}/{1}".format(downloadFolder,filename))
await log_reply(message, "{0} ready".format(filename))
queue.task_done()
except Exception as e:
print('Queue worker error: ', e)
async def start():
tasks = []
loop = asyncio.get_event_loop()