def getIDbypath(dst_id, src_name): global count if count < 10: try: dst_endpoint_id = _gd.get_dst_endpoint_id(_gd(), dst_id, src_name) if dst_endpoint_id: dst_endpoint_link = r"https://drive.google.com/open?id={}".format( dst_endpoint_id['id']) dst_info = { "dst_endpoint_id": dst_endpoint_id['id'], "dst_endpoint_link": dst_endpoint_link, "linkstatus": True } count = 0 return dst_info except: count += 1 return getIDbypath(dst_id, src_name) else: dst_info = { "dst_endpoint_id": " ", "dst_endpoint_link": " ", "linkstatus": False } count = 0 return dst_info
def get_src_name_from_id(update, taget_id, list_name): cook_list = [] cook_list = list(list_name) if len(taget_id) >= 11 and len(taget_id) < 28: target_info = _gd.drive_get(_gd(),drive_id=taget_id) cook_list.append( {"G_type": "G_drive", "G_id": taget_id, "G_name": target_info['name'],} ) elif len(taget_id) in judge_folder_len: cook_list.append( { "G_type": "G_Folder", "G_id": taget_id, "G_name": _gd().file_get_name(file_id=taget_id), } ) else: update.effective_message.reply_text(_msg.get_fav_len_invaild(_lang, taget_id)) return ConversationHandler.END return cook_list
def task_process(chat_id, command, task, ns): # mark is in processing in db task_list.update_one({"_id": task["_id"]}, {"$set": {"status": 2,}}) db_counters.update({"_id": "last_task"}, {"task_id": task["_id"]}, upsert=True) chat_id = chat_id message = bot.send_message(chat_id=chat_id, text=_text[_lang]["ready_to_task"]) message_id = message.message_id interval = 0.1 timeout = 60 xtime = 0 old_working_line = 0 current_working_line = 0 task_current_prog_num = 0 task_total_prog_num = 0 task_percent = 0 task_current_prog_size = "0" task_total_prog_size = "0 Bytes" task_in_size_speed = "-" task_in_file_speed = "-" task_eta_in_file = "-" task_current_prog_size_tail = "" task_total_prog_size_tail = "" start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) for toutput in run(command): if ns.x == 1: global icopyprocess icopyprocess.kill() regex_working_file = r"^ * " regex_elapsed_time = r"^Elapsed time:" regex_total_files = ( r"Transferred:\s+(\d+) / (\d+), (\d+)%(?:,\s*([\d.]+\sFiles/s))?" ) regex_total_size = ( r"Transferred:[\s]+([\d.]+\s*)([kMGTP]?) / ([\d.]+[\s]?)([kMGTP]?Bytes)," r"\s*(?:\-|(\d+)\%),\s*([\d.]+\s*[kMGTP]?Bytes/s),\s*ETA\s*([\-0-9hmsdwy]+)" ) output = toutput if output: task_total_files = re.search(regex_total_files, output) task_total_size = re.search(regex_total_size, output) task_elapsed_time = re.findall(regex_elapsed_time, output) task_working_file = re.findall(regex_working_file, output) if task_total_files: task_current_prog_num = task_total_files.group(1) task_total_prog_num = task_total_files.group(2) task_percent = int(task_total_files.group(3)) task_in_file_speed = task_total_files.group(4) if task_total_size: task_current_prog_size = task_total_size.group(1).strip() task_current_prog_size_tail = task_total_size.group(2) task_total_prog_size = task_total_size.group(3).strip() task_total_prog_size_tail = task_total_size.group(4) task_in_size_speed = task_total_size.group(6) task_eta_in_file = task_total_size.group(7) if task_elapsed_time: global now_elapsed_time now_elapsed_time = output.replace(" ", "").split(":")[1] if task_working_file: global current_working_file current_working_line += 1 current_working_file = ( output.lstrip("* ").rsplit(":")[0].rstrip("Transferred") ) global prog_bar prog_bar = _bar.status(0) if task_percent != 0: prog_bar = _bar.status(task_percent) global message_info message_info = ( _text[_lang]["task_src_info"] + "\n" + "📃" + task["src_name"] + "\n" + "----------------------------------------" + "\n" + _text[_lang]["task_dst_info"] + "\n" + "📁" + task["dst_name"] + ":" + "\n" + " ┕─📃" + task["src_name"] + "\n" + "----------------------------------------" + "\n\n" + _text[_lang]["task_start_time"] + start_time + "\n\n" + _text[_lang]["task_files_size"] + str(task_current_prog_size) + task_current_prog_size_tail + "/" + str(task_total_prog_size) + task_total_prog_size_tail + "\n" + _text[_lang]["task_files_num"] + str(task_current_prog_num) + "/" + str(task_total_prog_num) + "\n" + _text[_lang]["task_status"] + "\n\n" + str(task_in_size_speed) + " | " + str(task_in_file_speed) + "\n\n" + str(task_percent) + "%" + str(prog_bar) ) if ( int(time.time()) - xtime > interval and old_working_line != current_working_line ): Timer( 0, task_message_box, args=( bot, chat_id, message_id, " ༺ ✪iCopy✪ ༻ \n" + _text[_lang]["doing"] + " | " + "🏳️" + _text[_lang]["current_task_id"] + str(task["_id"]) + "\n\n" + message_info + "\n\n" + current_working_file[:30] + "\n" + "ETA : " + str(task_eta_in_file), ), ).start() old_working_line = current_working_line global old_working_file old_working_file = current_working_file time.sleep(3.5) xtime = time.time() if ( int(time.time()) - xtime > timeout and current_working_file == old_working_file and task_percent > 5 ): global interruption interruption = 1 break old_working_file = "" finished_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) dst_endpoint_id = _gd.get_dst_endpoint_id(_gd(), dst_id, src_name) if dst_endpoint_id: dst_endpoint_link = r"https://drive.google.com/open?id={}".format( dst_endpoint_id["id"] ) if ns.x == 0: time.sleep(5) prog_bar = _bar.status(100) bot.edit_message_text( chat_id=chat_id, message_id=message_id, text=" ༺ ✪iCopy✪ ༻ \n" + _text[_lang]["done"] + " | " + "🏳️" + _text[_lang]["current_task_id"] + str(task["_id"]) + "\n\n" + message_info.replace( " ┕─📃" + task["src_name"], " ┕─📃" + '<a href="{}">{}</a>'.format(dst_endpoint_link, task["src_name"]), ) + "\n" + _text[_lang]["task_finished_time"] + finished_time + "\n" + _text[_lang]["elapsed_time"] + str(now_elapsed_time), parse_mode=ParseMode.HTML, disable_web_page_preview=True, ) check_is_reset = task_list.find_one({"_id": task["_id"]}) if check_is_reset['is_reset'] == 0: if task_total_prog_size != 0 and task_current_prog_size != 0: task_list.update_one( {"_id": task["_id"]}, { "$set": { "status": 1, "start_time": start_time, "finished_time": finished_time, "task_current_prog_num": int(task_current_prog_num), "task_total_prog_num": int(task_total_prog_num), "task_current_prog_size": float(task_current_prog_size), "task_total_prog_size": float(task_total_prog_size), "task_current_prog_size_tail": task_current_prog_size_tail, "task_total_prog_size_tail": task_total_prog_size_tail, "dst_endpoint_link": dst_endpoint_link, "dst_endpoint_id": dst_endpoint_id["id"], } }, ) else: task_list.update_one( {"_id": task["_id"]}, { "$set": { "status": 1, "start_time": start_time, "finished_time": finished_time, "task_current_prog_num": int(task_current_prog_num), "task_total_prog_num": int(task_total_prog_num), "task_current_prog_size": int(task_current_prog_size), "task_total_prog_size": int(task_total_prog_size), "task_current_prog_size_tail": task_current_prog_size_tail, "task_total_prog_size_tail": task_total_prog_size_tail, "dst_endpoint_link": dst_endpoint_link, "dst_endpoint_id": dst_endpoint_id["id"], } }, ) elif check_is_reset['is_reset'] == 1: if "task_current_prog_num" and "task_total_prog_num" in check_is_reset: task_list.update_one( {"_id": task["_id"]}, { "$set": { "status": 1, "start_time": start_time, "finished_time": finished_time, "task_current_prog_num": int(task_current_prog_num) + check_is_reset['task_current_prog_num'], "task_total_prog_num": int(task_total_prog_num) + check_is_reset['task_total_prog_num'], "task_current_prog_size": 0, "task_total_prog_size": 0, "task_current_prog_size_tail": "", "task_total_prog_size_tail": "", "dst_endpoint_link": dst_endpoint_link, "dst_endpoint_id": dst_endpoint_id["id"], } }, ) else: task_list.update_one( {"_id": task["_id"]}, { "$set": { "status": 1, "start_time": start_time, "finished_time": finished_time, "task_current_prog_num": int(task_current_prog_num), "task_total_prog_num": int(task_total_prog_num), "task_current_prog_size": 0, "task_total_prog_size": 0, "task_current_prog_size_tail": "", "task_total_prog_size_tail": "", "dst_endpoint_link": dst_endpoint_link, "dst_endpoint_id": dst_endpoint_id["id"], } }, ) if ns.x == 1: bot.edit_message_text( chat_id=chat_id, message_id=message_id, text=" ༺ ✪iCopy✪ ༻ \n" + _text[_lang]["killed"] + " | " + "🏳️" + _text[_lang]["current_task_id"] + str(task["_id"]) + "\n\n" + message_info.replace( "\n\n" + _text[_lang]["task_start_time"] + start_time, "" ).replace( "\n" + _text[_lang]["task_status"] + "\n\n" + str(task_in_size_speed) + " | " + str(task_in_file_speed), "", ) + "\n" + _text[_lang]["is_killed_by_user"], ) task_list.update_one( {"_id": task["_id"]}, { "$set": { "status": 1, "error": 9, "start_time": start_time, "finished_time": finished_time, } }, ) if interruption == 1: bot.edit_message_text( chat_id=chat_id, message_id=message_id, text=" ༺ ✪iCopy✪ ༻ \n" + _text[_lang]["interrupted"] + " | " + "🏳️" + _text[_lang]["current_task_id"] + str(task["_id"]) + "\n\n" + message_info.replace( "\n\n" + _text[_lang]["task_start_time"] + start_time, "" ).replace( "\n" + _text[_lang]["task_status"] + "\n\n" + str(task_in_size_speed) + " | " + str(task_in_file_speed), "", ) + "\n" + _text[_lang]["is_interrupted_error"], ) task_list.update_one( {"_id": task["_id"]}, { "$set": { "status": 1, "error": 1, "start_time": start_time, "finished_time": finished_time, } }, ) prog_bar = _bar.status(0)
def dedupe_mode(update, context): get_callback = update.callback_query.data dedu_msg = bot.edit_message_text( chat_id=update.callback_query.message.chat_id, message_id=update.callback_query.message.message_id, text=_text[_lang]["ready_to_dedupe"], reply_markup=None, ) dedu_chat_id = dedu_msg.chat_id dedu_message_id = dedu_msg.message_id dedu_mode = get_callback if "dst_endpoint_id" and "dst_endpoint_link" in check_task: dedu_task_id = check_task["_id"] dedu_name = check_task["src_name"] dedu_id = check_task["dst_endpoint_id"] dedu_link = check_task["dst_endpoint_link"] else: dst_endpoint_id = _gd.get_dst_endpoint_id( _gd(), check_task["dst_id"], check_task["src_name"] ) if dst_endpoint_id: dst_endpoint_link = r"https://drive.google.com/open?id={}".format( dst_endpoint_id["id"] ) load.task_list.update_one( {"_id": int(check_task["_id"])}, { "$set": { "dst_endpoint_id": dst_endpoint_id["id"], "dst_endpoint_link": dst_endpoint_link, }, }, ) dedu_task_id = check_task["_id"] dedu_name = check_task["src_name"] dedu_id = dst_endpoint_id dedu_link = dst_endpoint_link progress = _mp( target=_d_payload.dedupe_task, args=( ns, dedu_mode, dedu_chat_id, dedu_message_id, dedu_task_id, dedu_link, dedu_id, dedu_name, ), ) progress.start() bot.edit_message_text( chat_id=dedu_chat_id, message_id=dedu_message_id, text=_text[_lang]["deduping"], ) return ConversationHandler.END
def size(update, context): entry_cmd = update.effective_message.text match_cmd = re.search(r"^\/size ([1-9]\d*)$", entry_cmd, flags=re.I) if "/size" == entry_cmd: update.effective_message.reply_text(_text[_lang]["request_share_link"]) return _stage.COOK_ID elif match_cmd: limit_query = load.db_counters.find_one({"_id": "task_list_id"}) check_query = match_cmd.group(1) size_msg = update.effective_message.reply_text(_text[_lang]["ready_to_size"]) size_chat_id = size_msg.chat_id size_message_id = size_msg.message_id if int(check_query) <= limit_query["future_id"]: check_task = load.task_list.find_one({"_id": int(check_query)}) if check_task["status"] == 1: if "dst_endpoint_link" and "dst_endpoint_id" in check_task: task_id = str(check_query) task_link = check_task["dst_endpoint_link"] endpoint_id = check_task["dst_endpoint_id"] endpoint_name = check_task["src_name"] progress = _mp( target=_s_payload.owner_size, args=( ns, size_chat_id, size_message_id, task_id, task_link, endpoint_id, endpoint_name, ), ) progress.start() context.bot.edit_message_text( chat_id=size_chat_id, message_id=size_message_id, text=_text[_lang]["sizing"], ) return ConversationHandler.END else: dst_endpoint_id = _gd.get_dst_endpoint_id( _gd(), check_task["dst_id"], check_task["src_name"] ) if dst_endpoint_id: dst_endpoint_link = r"https://drive.google.com/open?id={}".format( dst_endpoint_id["id"] ) load.task_list.update_one( {"_id": int(check_query)}, { "$set": { "dst_endpoint_id": dst_endpoint_id["id"], "dst_endpoint_link": dst_endpoint_link, }, }, ) task_id = str(check_query) task_link = dst_endpoint_link endpoint_id = dst_endpoint_id["id"] endpoint_name = check_task["src_name"] progress = _mp( target=_s_payload.owner_size, args=( ns, size_chat_id, size_message_id, task_id, task_link, endpoint_id, endpoint_name, ), ) progress.start() context.bot.edit_message_text( chat_id=size_chat_id, message_id=size_message_id, text=_text[_lang]["sizing"], ) return ConversationHandler.END else: bot.edit_message_text( chat_id=size_chat_id, message_id=size_message_id, text=_text[_lang]["support_error"], ) return ConversationHandler.END elif check_task["status"] == 0: bot.edit_message_text( chat_id=size_chat_id, message_id=size_message_id, text=_text[_lang]["task_is_in_queue"] + "\n" + _text[_lang]["finished_could_be_check"], ) return ConversationHandler.END elif check_task["status"] == 2: bot.edit_message_text( chat_id=size_chat_id, message_id=size_message_id, text=_text[_lang]["doing"] + "\n" + _text[_lang]["finished_could_be_check"], ) return ConversationHandler.END else: bot.edit_message_text( chat_id=size_chat_id, message_id=size_message_id, text=_text[_lang]["over_limit_to_check"], ) return ConversationHandler.END elif entry_cmd.split(" ")[1].strip() == "fav": update.effective_message.reply_text( _text[_lang]["request_target_folder"], reply_markup=_KB.dst_keyboard(update, context), ) return _stage.COOK_FAV_TO_SIZE