def onDownloadError(self, error): error = error.replace('<', ' ') error = error.replace('>', ' ') LOGGER.info(self.update.effective_chat.id) with download_dict_lock: try: download = download_dict[self.uid] del download_dict[self.uid] LOGGER.info(f"Deleting folder: {download.path()}") fs_utils.clean_download(download.path()) LOGGER.info(str(download_dict)) except Exception as e: LOGGER.error(str(e)) pass count = len(download_dict) if self.message.from_user.username: uname = f"@{self.message.from_user.username}" else: uname = f'<a href="tg://user?id={self.message.from_user.id}">{self.message.from_user.first_name}</a>' msg = f"{uname} your download has been stopped due to: {error}" sendMessage(msg, self.bot, self.update) if count == 0: self.clean() else: update_all_messages()
def shell(update: Update, context: CallbackContext): message = update.effective_message cmd = message.text.split(' ', 1) if len(cmd) == 1: message.reply_text('No command to execute was given.') return cmd = cmd[1] process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = process.communicate() reply = '' stderr = stderr.decode() stdout = stdout.decode() if stdout: reply += f"*Stdout*\n`{stdout}`\n" LOGGER.info(f"Shell - {cmd} - {stdout}") if stderr: reply += f"*Stderr*\n`{stderr}`\n" LOGGER.error(f"Shell - {cmd} - {stderr}") if len(reply) > 3000: with open('shell_output.txt', 'w') as file: file.write(reply) with open('shell_output.txt', 'rb') as doc: context.bot.send_document(document=doc, filename=doc.name, reply_to_message_id=message.message_id, chat_id=message.chat_id) else: message.reply_text(reply, parse_mode=ParseMode.MARKDOWN)
def sendStatusMessage(msg, bot): progress = get_readable_message() progress += f"<b>π₯ CPU:</b> {psutil.cpu_percent()}%\n" \ f"<b>π RAM:</b> {psutil.virtual_memory().percent}%\n" \ f"<b>π¦ DISK:</b> {psutil.disk_usage('/').percent}%\n" with download_dict_lock: dlspeed_bytes = 0 uldl_bytes = 0 for download in list(download_dict.values()): speedy = download.speed() if download.status() == MirrorStatus.STATUS_DOWNLOADING: if 'KiB/s' in speedy: dlspeed_bytes += float(speedy.split('K')[0]) * 1024 elif 'MiB/s' in speedy: dlspeed_bytes += float(speedy.split('M')[0]) * 1048576 if download.status() == MirrorStatus.STATUS_UPLOADING: if 'KB/s' in speedy: uldl_bytes += float(speedy.split('K')[0]) * 1024 elif 'MB/s' in speedy: uldl_bytes += float(speedy.split('M')[0]) * 1048576 dlspeed = get_readable_file_size(dlspeed_bytes) ulspeed = get_readable_file_size(uldl_bytes) progress += f"\n<b>DL:</b>{dlspeed}ps π»| <b>UL:</b>{ulspeed}ps πΊ\n" with status_reply_dict_lock: if msg.message.chat.id in list(status_reply_dict.keys()): try: message = status_reply_dict[msg.message.chat.id] deleteMessage(bot, message) del status_reply_dict[msg.message.chat.id] except Exception as e: LOGGER.error(str(e)) del status_reply_dict[msg.message.chat.id] message = sendMessage(progress, bot, msg) status_reply_dict[msg.message.chat.id] = message
def update_all_messages(): msg = get_readable_message() msg += f"<b>π₯οΈ CPU:</b> {psutil.cpu_percent()}%\n" \ f"<b>π RAM:</b> {psutil.virtual_memory().percent}%\n" \ f"<b>π¦ DISK:</b> {psutil.disk_usage('/').percent}%\n" with download_dict_lock: dlspeed_bytes = 0 uldl_bytes = 0 for download in list(download_dict.values()): speedy = download.speed() if download.status() == MirrorStatus.STATUS_DOWNLOADING: if 'KiB/s' in speedy: dlspeed_bytes += float(speedy.split('K')[0]) * 1024 elif 'MiB/s' in speedy: dlspeed_bytes += float(speedy.split('M')[0]) * 1048576 if download.status() == MirrorStatus.STATUS_UPLOADING: if 'KB/s' in speedy: uldl_bytes += float(speedy.split('K')[0]) * 1024 elif 'MB/s' in speedy: uldl_bytes += float(speedy.split('M')[0]) * 1048576 dlspeed = get_readable_file_size(dlspeed_bytes) ulspeed = get_readable_file_size(uldl_bytes) msg += f"\n<b>DL:</b>{dlspeed}ps π»| <b>UL:</b>{ulspeed}ps πΊ\n" with status_reply_dict_lock: for chat_id in list(status_reply_dict.keys()): if status_reply_dict[ chat_id] and msg != status_reply_dict[chat_id].text: if len(msg) == 0: msg = "Starting DL" try: editMessage(msg, status_reply_dict[chat_id]) except Exception as e: LOGGER.error(str(e)) status_reply_dict[chat_id].text = msg
def connect(self): try: self.conn = psycopg2.connect(DB_URI) self.cur = self.conn.cursor() except psycopg2.DatabaseError as error : LOGGER.error("Error in dbMang : ", error) self.err = True
def editMessage(text: str, message: Message, reply_markup=None): try: bot.edit_message_text(text=text, message_id=message.message_id, chat_id=message.chat.id,reply_markup=reply_markup, parse_mode='HTMl') except Exception as e: LOGGER.error(str(e))
def sendMarkup(text: str, bot, update: Update, reply_markup: InlineKeyboardMarkup): try: return bot.send_message(update.message.chat_id, reply_to_message_id=update.message.message_id, text=text, reply_markup=reply_markup, parse_mode='HTMl') except Exception as e: LOGGER.error(str(e))
def sendMessage(text: str, bot, update: Update): try: return bot.send_message(update.message.chat_id, reply_to_message_id=update.message.message_id, text=text, parse_mode='HTMl') except Exception as e: LOGGER.error(str(e))
def delete_all_messages(): with status_reply_dict_lock: for message in list(status_reply_dict.values()): try: deleteMessage(bot, message) del status_reply_dict[message.chat.id] except Exception as e: LOGGER.error(str(e))
def onTransferFinish(self, api: MegaApi, transfer: MegaTransfer, error): try: LOGGER.info(f'Transfer finished ({transfer}); Result: {transfer.getFileName()}') if transfer.isFolderTransfer() and transfer.isFinished() or transfer.getFileName() == self.name and not self.is_cancelled: self.listener.onDownloadComplete() self.continue_event.set() except Exception as e: LOGGER.error(e)
def update_all_messages(): total, used, free = shutil.disk_usage('.') used = get_readable_file_size(used) free = get_readable_file_size(free) msg = get_readable_message() msg += f"<b>π₯οΈ CPU:</b> {psutil.cpu_percent()}%" \ f"<b>π RAM:</b> {psutil.virtual_memory().percent}%" \ f"<b>π¦ DISK:</b> {psutil.disk_usage('/').percent}%" with download_dict_lock: dlspeed_bytes = 0 uldl_bytes = 0 for download in list(download_dict.values()): speedy = download.speed() if download.status() == MirrorStatus.STATUS_DOWNLOADING: if 'KiB/s' in speedy: dlspeed_bytes += float(speedy.split('K')[0]) * 1024 elif 'MiB/s' in speedy: dlspeed_bytes += float(speedy.split('M')[0]) * 1048576 if download.status() == MirrorStatus.STATUS_UPLOADING: if 'KB/s' in speedy: uldl_bytes += float(speedy.split('K')[0]) * 1024 elif 'MB/s' in speedy: uldl_bytes += float(speedy.split('M')[0]) * 1048576 dlspeed = get_readable_file_size(dlspeed_bytes) ulspeed = get_readable_file_size(uldl_bytes) msg += f"\n<b>USED:</b> {used} | <b>FREE:</b> {free}\n<b>DL:</b> {dlspeed}ps π» | <b>UL:</b> {ulspeed}ps πΊ\n" with status_reply_dict_lock: for chat_id in list(status_reply_dict.keys()): if status_reply_dict[ chat_id] and msg != status_reply_dict[chat_id].text: if len(msg) == 0: msg = "Starting DL" try: keyboard = [[ InlineKeyboardButton("π REFRESH π", callback_data=str(ONE)), InlineKeyboardButton("β CLOSE β", callback_data=str(TWO)), ]] editMessage(msg, status_reply_dict[chat_id], reply_markup=InlineKeyboardMarkup(keyboard)) except Exception as e: LOGGER.error(str(e)) status_reply_dict[chat_id].text = msg
def deleteMessage(bot, message: Message): try: bot.delete_message(chat_id=message.chat.id, message_id=message.message_id) except Exception as e: LOGGER.error(str(e))