async def copy(self) -> None: """Copy file/folder in GDrive""" if not self._parent_id: await self._message.edit("First set parent path by `.gset`", del_in=5) return await self._message.edit("`Loading GDrive Copy...`") file_id, _ = self._get_file_id() pool.submit_thread(self._copy, file_id) start_t = datetime.now() count = 0 while not self._is_finished: count += 1 if self._message.process_is_canceled: self._cancel() if self._progress is not None and count >= 5: count = 0 await self._message.try_to_edit(self._progress) await asyncio.sleep(1) end_t = datetime.now() m_s = (end_t - start_t).seconds if isinstance(self._output, HttpError): out = f"**ERROR** : `{self._output._get_reason()}`" elif self._output is not None and not self._is_canceled: out = f"**Copied Successfully** __in {m_s} seconds__\n\n{self._output}" elif self._output is not None and self._is_canceled: out = self._output else: out = "`failed to copy.. check logs?`" await self._message.edit(out, disable_web_page_preview=True, log=True)
async def download(self) -> None: """Download file/folder from GDrive""" await self._message.edit("`Loading GDrive Download...`") if not os.path.isdir(Config.DOWN_PATH): os.mkdir(Config.DOWN_PATH) file_id, _ = self._get_file_id() pool.submit_thread(self._download, file_id) start_t = datetime.now() count = 0 while not self._is_finished: count += 1 if self._message.process_is_canceled: self._cancel() if self._progress is not None and count >= 5: count = 0 await self._message.try_to_edit(self._progress) await asyncio.sleep(1) end_t = datetime.now() m_s = (end_t - start_t).seconds if isinstance(self._output, HttpError): out = f"**ERROR** : `{self._output._get_reason()}`" elif self._output is not None and not self._is_canceled: out = f"**Downloaded Successfully** __in {m_s} seconds__\n\n`{self._output}`" elif self._output is not None and self._is_canceled: out = self._output else: out = "`failed to download.. check logs?`" await self._message.edit(out, disable_web_page_preview=True, log=True)
def combine(self) -> None: """Combine Split files""" file_name, ext = splitext(basename(self._path)) self._final_file_path = join(dirname(self._path), file_name) file_list = sorted( glob(self._final_file_path + f".{'[0-9]' * len(ext.lstrip('.'))}")) self._total = len(file_list) self._file_size = sum((os.stat(f_).st_size for f_ in file_list)) pool.submit_thread(self._combine_worker, file_list)
def split(self, split_size: int) -> None: """Split files""" split_size = int(split_size) * 1024 * 1024 self._file_size = os.stat(self._path).st_size if self._chunk_size > split_size: self._chunk_size = split_size times = int(ceil(split_size / self._chunk_size)) self._total = int(ceil(self._file_size / split_size)) self._final_file_path = join( dirname(self._path), f"split_{basename(self._path).replace('.', '_')}") if not isdir(self._final_file_path): os.makedirs(self._final_file_path) pool.submit_thread(self._split_worker, times)
def pack_path(self, tar: bool) -> None: """PACK file path""" file_paths = [] def explorer(path: Path) -> None: if path.is_file(): self._total += 1 file_paths.append(str(path)) elif path.is_dir(): for i in path.iterdir(): explorer(i) explorer(Path(self._file_path)) file_name = basename(self._file_path) if tar: file_name += '.tar' p_type = tar_open else: file_name += '.zip' p_type = ZipFile self._final_file_path = join(Config.DOWN_PATH, file_name) pool.submit_thread(self._zip, p_type, file_paths, self._final_file_path)
async def upload(self) -> None: """Upload from file/folder/link/tg file to GDrive""" if not os.path.isdir(Config.DOWN_PATH): os.mkdir(Config.DOWN_PATH) replied = self._message.reply_to_message is_url = re.search(r"(?:https?|ftp)://[^\|\s]+\.[^\|\s]+", self._message.input_str) dl_loc = None if replied and replied.media: await self._message.edit("`Downloading From TG...`") c_time = time.time() file_name = Config.DOWN_PATH if self._message.input_str: file_name = os.path.join(Config.DOWN_PATH, self._message.input_str) dl_loc = await userbot.download_media( message=replied, file_name=file_name, progress=progress, progress_args=("trying to download", userbot, self._message, c_time)) if self._message.process_is_canceled: await self._message.edit("`Process Canceled!`", del_in=5) return dl_loc = os.path.join(Config.DOWN_PATH, os.path.basename(dl_loc)) elif is_url: await self._message.edit("`Downloading From URL...`") url = is_url[0] file_name = unquote_plus(os.path.basename(url)) if "|" in self._message.input_str: file_name = self._message.input_str.split("|")[1].strip() dl_loc = os.path.join(Config.DOWN_PATH, file_name) try: downloader = SmartDL(url, dl_loc, progress_bar=False) downloader.start(blocking=False) count = 0 while not downloader.isFinished(): if self._message.process_is_canceled: downloader.stop() raise Exception('Process Canceled!') total_length = downloader.filesize if downloader.filesize else 0 downloaded = downloader.get_dl_size() percentage = downloader.get_progress() * 100 speed = downloader.get_speed(human=True) estimated_total_time = downloader.get_eta(human=True) progress_str = \ "__{}__\n" + \ "```[{}{}]```\n" + \ "× **Progress** : `{}%`\n" + \ "× **URL** : `{}`\n" + \ "× **FILENAME** : `{}`\n" + \ "× **Completed** : `{}`\n" + \ "× **Total** : `{}`\n" + \ "× **Speed** : `{}`\n" + \ "× **ETA** : `{}`" progress_str = progress_str.format( "trying to download", ''.join( (Config.FINISHED_PROGRESS_STR for i in range(math.floor(percentage / 5)))), ''.join( (Config.UNFINISHED_PROGRESS_STR for i in range(20 - math.floor(percentage / 5)))), round(percentage, 2), url, file_name, humanbytes(downloaded), humanbytes(total_length), speed, estimated_total_time) count += 1 if count >= 5: count = 0 await self._message.try_to_edit( progress_str, disable_web_page_preview=True) await asyncio.sleep(1) except Exception as d_e: await self._message.err(d_e) return file_path = dl_loc if dl_loc else self._message.input_str if not os.path.exists(file_path): await self._message.err("invalid file path provided?") return if "|" in file_path: file_path, file_name = file_path.split("|") new_path = os.path.join(os.path.dirname(file_path.strip()), file_name.strip()) os.rename(file_path.strip(), new_path) file_path = new_path await self._message.edit("`Loading GDrive Upload...`") pool.submit_thread(self._upload, file_path) start_t = datetime.now() count = 0 while not self._is_finished: count += 1 if self._message.process_is_canceled: self._cancel() if self._progress is not None and count >= 5: count = 0 await self._message.try_to_edit(self._progress) await asyncio.sleep(1) if dl_loc and os.path.exists(dl_loc): os.remove(dl_loc) end_t = datetime.now() m_s = (end_t - start_t).seconds if isinstance(self._output, HttpError): out = f"**ERROR** : `{self._output._get_reason()}`" elif self._output is not None and not self._is_canceled: out = f"**Uploaded Successfully** __in {m_s} seconds__\n\n{self._output}" elif self._output is not None and self._is_canceled: out = self._output else: out = "`failed to upload.. check logs?`" await self._message.edit(out, disable_web_page_preview=True, log=True)