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) progress_message= "{0} % ({1} / {2})".format(percentage, received, total)
in_progress[filename] = progress_message in_progress[filename] = progress_message
# if (int(percentage) % 5) == 0: if (int(percentage) % 5) == 0:
# await log_reply(message, progress_message) await log_reply(message, progress_message)
with TelegramClient(getSession(), api_id, api_hash, with TelegramClient(getSession(), api_id, api_hash,
@ -140,57 +140,63 @@ with TelegramClient(getSession(), api_id, api_hash,
print(event) print(event)
if not event.media and event.message: try:
command = event.message.message
command = command.lower()
output = "Unknown command"
if command == "list": if not event.media and event.message:
output = subprocess.run(["ls -l "+downloadFolder], shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,encoding="utf-8").stdout command = event.message.message
elif command == "status": command = command.lower()
try: output = "Unknown command"
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) 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 event.media: await log_reply(event, output)
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(): async def worker():
while True: while True:
element = await queue.get() try:
event=element[0] element = await queue.get()
message=element[1] event=element[0]
message=element[1]
filename=getFilename(event) filename=getFilename(event)
await log_reply( await log_reply(
message, message,
"Downloading file {0} ({1} bytes)".format(filename,event.media.document.size) "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) 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) set_progress(filename, message, 100, 100)
rename("{0}/{1}.{2}".format(temFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), "{0}/{1}".format(downloadFolder,filename)) rename("{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), "{0}/{1}".format(downloadFolder,filename))
await log_reply(message, "{0} ready".format(filename)) await log_reply(message, "{0} ready".format(filename))
queue.task_done() queue.task_done()
except Exception as e:
print('Queue worker error: ', e)
async def start(): async def start():
tasks = [] tasks = []