From 2a3619a51a4410bd181a87aadb23e5aa385c72b2 Mon Sep 17 00:00:00 2001 From: Farahat <67980665+iseeface@users.noreply.github.com> Date: Sat, 14 Dec 2024 16:27:00 +0700 Subject: [PATCH 1/4] feat: add progress download bar to find out what percentage the download process has reached --- src/TSRDownload.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/TSRDownload.py b/src/TSRDownload.py index d1ce81a..a275932 100644 --- a/src/TSRDownload.py +++ b/src/TSRDownload.py @@ -2,6 +2,7 @@ import requests, time, os, re from TSRUrl import TSRUrl from logger import logger +from tqdm import tqdm from exceptions import * @@ -89,3 +90,48 @@ def __getTSRDLTicketCookie(self) -> str: self.session.get(self.url.downloadUrl) self.ticketInitializedTime = time.time() * 1000 return response.cookies.get("tsrdlticket") + + @classmethod + def download(self, downloadPath: str) -> str: + logger.info(f"Starting download for: {self.url.url}") + timeToSleep = 15000 - (time.time() * 1000 - self.ticketInitializedTime) + if timeToSleep > 0: + time.sleep(timeToSleep / 1000) + + downloadUrl = self.__getDownloadUrl() + logger.debug(f"Got downloadUrl: {downloadUrl}") + fileName = self.__getFileName(downloadUrl) + logger.debug(f"Got fileName: {fileName}") + + startingBytes = ( + os.path.getsize(f"{downloadPath}/{fileName}.part") + if os.path.exists(f"{downloadPath}/{fileName}.part") + else 0 + ) + logger.debug(f"Got startingBytes: {startingBytes}") + request = self.session.get( + downloadUrl, + stream=True, + headers={"Range": f"bytes={startingBytes}-"}, + ) + logger.debug(f"Request status is: {request.status_code}") + + total_size = int(request.headers.get('content-length', 0)) + startingBytes # Total size of the file + with open(f"{downloadPath}/{fileName}.part", "wb") as file: + # Initialize tqdm progress bar + with tqdm(total=total_size, initial=startingBytes, unit='B', unit_scale=True, desc=fileName) as bar: + for index, chunk in enumerate(request.iter_content(1024 * 128)): + file.write(chunk) + bar.update(len(chunk)) # Update progress bar + + logger.debug(f"Removing .part from file name: {fileName}") + if os.path.exists(f"{downloadPath}/{fileName}"): + logger.debug(f"{downloadPath}/{fileName} Already exists! Replacing file") + os.replace(f"{downloadPath}/{fileName}.part", f"{downloadPath}/{fileName}") + else: + logger.debug(f"{downloadPath}/{fileName} doesn't exist! Renaming file") + os.rename( + f"{downloadPath}/{fileName}.part", + f"{downloadPath}/{fileName}", + ) + return fileName \ No newline at end of file From 2c786f4d031861d3795eec12c5239418a00e43d9 Mon Sep 17 00:00:00 2001 From: Farahat <67980665+iseeface@users.noreply.github.com> Date: Sat, 14 Dec 2024 16:50:31 +0700 Subject: [PATCH 2/4] docs: add requirements add tqdm in requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 4f5089a..efc03ad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ clipboard==0.0.4 requests==2.28.1 +tqdm==4.67.1 From 1470615921e2303e2e4165141b9c0a444aff082a Mon Sep 17 00:00:00 2001 From: Farahat <67980665+iseeface@users.noreply.github.com> Date: Sat, 14 Dec 2024 17:03:28 +0700 Subject: [PATCH 3/4] feat: add progress download bar to find out what percentage the download process has reached --- src/TSRDownload.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/TSRDownload.py b/src/TSRDownload.py index a275932..fdeb0fd 100644 --- a/src/TSRDownload.py +++ b/src/TSRDownload.py @@ -93,28 +93,29 @@ def __getTSRDLTicketCookie(self) -> str: @classmethod def download(self, downloadPath: str) -> str: - logger.info(f"Starting download for: {self.url.url}") + logger.info(f"\n\n=== Starting download for: {self.url.url} ===") timeToSleep = 15000 - (time.time() * 1000 - self.ticketInitializedTime) if timeToSleep > 0: + logger.info(f"Waiting for {timeToSleep / 1000:.2f} seconds before starting...") time.sleep(timeToSleep / 1000) downloadUrl = self.__getDownloadUrl() - logger.debug(f"Got downloadUrl: {downloadUrl}") + logger.debug(f"Got download URL: {downloadUrl}") fileName = self.__getFileName(downloadUrl) - logger.debug(f"Got fileName: {fileName}") + logger.debug(f"Got file name: {fileName}") startingBytes = ( os.path.getsize(f"{downloadPath}/{fileName}.part") if os.path.exists(f"{downloadPath}/{fileName}.part") else 0 ) - logger.debug(f"Got startingBytes: {startingBytes}") + logger.debug(f"Starting bytes: {startingBytes}") request = self.session.get( downloadUrl, stream=True, headers={"Range": f"bytes={startingBytes}-"}, ) - logger.debug(f"Request status is: {request.status_code}") + logger.debug(f"Request status: {request.status_code}") total_size = int(request.headers.get('content-length', 0)) + startingBytes # Total size of the file with open(f"{downloadPath}/{fileName}.part", "wb") as file: @@ -124,14 +125,16 @@ def download(self, downloadPath: str) -> str: file.write(chunk) bar.update(len(chunk)) # Update progress bar - logger.debug(f"Removing .part from file name: {fileName}") + logger.debug(f"\nRenaming .part file to: {fileName}") if os.path.exists(f"{downloadPath}/{fileName}"): - logger.debug(f"{downloadPath}/{fileName} Already exists! Replacing file") + logger.debug(f"{downloadPath}/{fileName} already exists! Replacing file.") os.replace(f"{downloadPath}/{fileName}.part", f"{downloadPath}/{fileName}") else: - logger.debug(f"{downloadPath}/{fileName} doesn't exist! Renaming file") + logger.debug(f"{downloadPath}/{fileName} doesn't exist! Renaming file.") os.rename( f"{downloadPath}/{fileName}.part", f"{downloadPath}/{fileName}", ) + + logger.info(f"\n=== Download completed: {fileName} ===\n") return fileName \ No newline at end of file From b60da1b3c7d0c298a1e5d6c9d275150d9f674914 Mon Sep 17 00:00:00 2001 From: Farahat <67980665+iseeface@users.noreply.github.com> Date: Sat, 14 Dec 2024 20:17:35 +0700 Subject: [PATCH 4/4] feat: add progress download bar my mistake was stacking the download function, and I have fixed it --- src/TSRDownload.py | 82 +++++++++++----------------------------------- 1 file changed, 20 insertions(+), 62 deletions(-) diff --git a/src/TSRDownload.py b/src/TSRDownload.py index fdeb0fd..fbc41f3 100644 --- a/src/TSRDownload.py +++ b/src/TSRDownload.py @@ -18,44 +18,50 @@ def __init__(self, url: TSRUrl, sessionId: str): @classmethod def download(self, downloadPath: str) -> str: - logger.info(f"Starting download for: {self.url.url}") + logger.info(f"\n\n=== Starting download for: {self.url.url} ===") timeToSleep = 15000 - (time.time() * 1000 - self.ticketInitializedTime) if timeToSleep > 0: + logger.info(f"Waiting for {timeToSleep / 1000:.2f} seconds before starting...") time.sleep(timeToSleep / 1000) downloadUrl = self.__getDownloadUrl() - logger.debug(f"Got downloadUrl: {downloadUrl}") + logger.debug(f"Got download URL: {downloadUrl}") fileName = self.__getFileName(downloadUrl) - logger.debug(f"Got fileName: {fileName}") + logger.debug(f"Got file name: {fileName}") startingBytes = ( os.path.getsize(f"{downloadPath}/{fileName}.part") if os.path.exists(f"{downloadPath}/{fileName}.part") else 0 ) - logger.debug(f"Got startingBytes: {startingBytes}") + logger.debug(f"Starting bytes: {startingBytes}") request = self.session.get( downloadUrl, stream=True, headers={"Range": f"bytes={startingBytes}-"}, ) - logger.debug(f"Request status is: {request.status_code}") - file = open(f"{downloadPath}/{fileName}.part", "wb") + logger.debug(f"Request status: {request.status_code}") - for index, chunk in enumerate(request.iter_content(1024 * 128)): - logger.debug(f"Downloading chunk #{index} of {downloadUrl}") - file.write(chunk) - file.close() - logger.debug(f"Removing .part from file name: {fileName}") + total_size = int(request.headers.get('content-length', 0)) + startingBytes # Total size of the file + with open(f"{downloadPath}/{fileName}.part", "wb") as file: + # Initialize tqdm progress bar + with tqdm(total=total_size, initial=startingBytes, unit='B', unit_scale=True, desc=fileName) as bar: + for index, chunk in enumerate(request.iter_content(1024 * 128)): + file.write(chunk) + bar.update(len(chunk)) # Update progress bar + + logger.debug(f"\nRenaming .part file to: {fileName}") if os.path.exists(f"{downloadPath}/{fileName}"): - logger.debug(f"{downloadPath}/{fileName} Already exists! Replacing file") + logger.debug(f"{downloadPath}/{fileName} already exists! Replacing file.") os.replace(f"{downloadPath}/{fileName}.part", f"{downloadPath}/{fileName}") else: - logger.debug(f"{downloadPath}/{fileName} doesn't exist! Renaming file") + logger.debug(f"{downloadPath}/{fileName} doesn't exist! Renaming file.") os.rename( f"{downloadPath}/{fileName}.part", f"{downloadPath}/{fileName}", ) + + logger.info(f"\n=== Download completed: {fileName} ===\n") return fileName @classmethod @@ -89,52 +95,4 @@ def __getTSRDLTicketCookie(self) -> str: ) self.session.get(self.url.downloadUrl) self.ticketInitializedTime = time.time() * 1000 - return response.cookies.get("tsrdlticket") - - @classmethod - def download(self, downloadPath: str) -> str: - logger.info(f"\n\n=== Starting download for: {self.url.url} ===") - timeToSleep = 15000 - (time.time() * 1000 - self.ticketInitializedTime) - if timeToSleep > 0: - logger.info(f"Waiting for {timeToSleep / 1000:.2f} seconds before starting...") - time.sleep(timeToSleep / 1000) - - downloadUrl = self.__getDownloadUrl() - logger.debug(f"Got download URL: {downloadUrl}") - fileName = self.__getFileName(downloadUrl) - logger.debug(f"Got file name: {fileName}") - - startingBytes = ( - os.path.getsize(f"{downloadPath}/{fileName}.part") - if os.path.exists(f"{downloadPath}/{fileName}.part") - else 0 - ) - logger.debug(f"Starting bytes: {startingBytes}") - request = self.session.get( - downloadUrl, - stream=True, - headers={"Range": f"bytes={startingBytes}-"}, - ) - logger.debug(f"Request status: {request.status_code}") - - total_size = int(request.headers.get('content-length', 0)) + startingBytes # Total size of the file - with open(f"{downloadPath}/{fileName}.part", "wb") as file: - # Initialize tqdm progress bar - with tqdm(total=total_size, initial=startingBytes, unit='B', unit_scale=True, desc=fileName) as bar: - for index, chunk in enumerate(request.iter_content(1024 * 128)): - file.write(chunk) - bar.update(len(chunk)) # Update progress bar - - logger.debug(f"\nRenaming .part file to: {fileName}") - if os.path.exists(f"{downloadPath}/{fileName}"): - logger.debug(f"{downloadPath}/{fileName} already exists! Replacing file.") - os.replace(f"{downloadPath}/{fileName}.part", f"{downloadPath}/{fileName}") - else: - logger.debug(f"{downloadPath}/{fileName} doesn't exist! Renaming file.") - os.rename( - f"{downloadPath}/{fileName}.part", - f"{downloadPath}/{fileName}", - ) - - logger.info(f"\n=== Download completed: {fileName} ===\n") - return fileName \ No newline at end of file + return response.cookies.get("tsrdlticket") \ No newline at end of file