From 93a594beef8fbab78a10dc396d36ff5e6901b661 Mon Sep 17 00:00:00 2001 From: Ghost <62809003+Ghost6446@users.noreply.github.com> Date: Sat, 8 Jun 2024 12:39:39 +0200 Subject: [PATCH] Fix speed download --- Src/Api/Altadefinizione/site.py | 4 ++- Src/Lib/M3U8/estimator.py | 48 +++++++++++++++------------------ config.json | 2 +- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Src/Api/Altadefinizione/site.py b/Src/Api/Altadefinizione/site.py index 1a53609..ca271c0 100644 --- a/Src/Api/Altadefinizione/site.py +++ b/Src/Api/Altadefinizione/site.py @@ -15,6 +15,8 @@ from unidecode import unidecode from Src.Util.table import TVShowManager from Src.Util.console import console from Src.Util._jsonConfig import config_manager +from Src.Util.headers import get_headers + # Logic class from .Core.Class.SearchType import MediaManager, MediaItem @@ -44,7 +46,7 @@ def title_search(title_search: str) -> int: """ # Send request to search for titles - response = requests.get(f"https://{AD_SITE_NAME}.{AD_DOMAIN_NOW}/page/1/?story={unidecode(title_search.replace(' ', '+'))}&do=search&subaction=search&titleonly=3") + response = requests.get(f"https://{AD_SITE_NAME}.{AD_DOMAIN_NOW}/page/1/?story={unidecode(title_search.replace(' ', '+'))}&do=search&subaction=search&titleonly=3", headers={'user-agent': get_headers()}) response.raise_for_status() # Create soup and find table diff --git a/Src/Lib/M3U8/estimator.py b/Src/Lib/M3U8/estimator.py index e762a40..5fd41a7 100644 --- a/Src/Lib/M3U8/estimator.py +++ b/Src/Lib/M3U8/estimator.py @@ -22,20 +22,13 @@ TQDM_USE_LARGE_BAR = config_manager.get_int('M3U8_DOWNLOAD', 'tqdm_use_large_bar class M3U8_Ts_Estimator: def __init__(self, total_segments: int): - """ - Initialize the TSFileSizeCalculator object. - - Args: - - workers (int): The number of workers using with ThreadPool. - - total_segments (int): Len of total segments to download - """ self.ts_file_sizes = [] self.now_downloaded_size = 0 - self.average_over = 3 + self.average_over = 5 self.list_speeds = deque(maxlen=self.average_over) - self.smoothed_speeds = [] self.total_segments = total_segments - self.lock = threading.Lock() + self.last_segment_duration = 1 + self.last_segment_size = 0 def add_ts_file(self, size: int, size_download: int, duration: float): """ @@ -50,26 +43,23 @@ class M3U8_Ts_Estimator: logging.error("Invalid input values: size=%d, size_download=%d, duration=%f", size, size_download, duration) return - # Calculate speed outside of the lock + # Calibrazione dinamica del tempo + self.last_segment_duration = duration + + # Considerazione della variazione di dimensione del segmento + self.last_segment_size = size_download + + # Calcolo velocità try: - speed_mbps = (size_download * 8) / (duration * 1_000_000) + speed_mbps = (size_download * 8) / (duration * 1024 * 1024) + except ZeroDivisionError as e: logging.error("Division by zero error while calculating speed: %s", e) return - # Only update shared data within the lock - with self.lock: - self.ts_file_sizes.append(size) - self.now_downloaded_size += size_download - self.list_speeds.append(speed_mbps) - - # Calculate moving average - smoothed_speed = sum(self.list_speeds) / len(self.list_speeds) - self.smoothed_speeds.append(smoothed_speed) - - # Update smooth speeds - if len(self.smoothed_speeds) > self.average_over: - self.smoothed_speeds.pop(0) + self.ts_file_sizes.append(size) + self.now_downloaded_size += size_download + self.list_speeds.append(speed_mbps) def calculate_total_size(self) -> str: """ @@ -103,7 +93,13 @@ class M3U8_Ts_Estimator: Returns: float: The average speed in megabytes per second (MB/s). """ - return ((sum(self.smoothed_speeds) / len(self.smoothed_speeds)) / 8 ) * 10 # MB/s + + # Smooth the speeds for better accuracy using the window defined by average_over + smoothed_speed = sum(self.list_speeds) / min(len(self.list_speeds), self.average_over) + predicted_speed = smoothed_speed * (self.last_segment_size / (1024 * 1024)) / self.last_segment_duration + + # Convert to mb/s + return predicted_speed / 8 def get_downloaded_size(self) -> str: """ diff --git a/config.json b/config.json index e815bf8..cedc3a0 100644 --- a/config.json +++ b/config.json @@ -45,6 +45,6 @@ "SITE": { "streamingcommunity": "foo", "animeunity": "to", - "altadefinizione": "food" + "altadefinizione": "vodka" } }