Fix psutil for termux

This commit is contained in:
Lovi 2024-09-25 09:10:15 +02:00
parent 590533a141
commit 4d48f5fa92
3 changed files with 36 additions and 36 deletions

View File

@ -24,14 +24,14 @@ def search():
# Select title from list
select_title = run_get_select_title()
if select_title.type == 'TV':
download_series(select_title)
else:
if select_title.type == 'Movie':
download_film(select_title)
else:
download_series(select_title)
else:
console.print(f"\n[red]Nothing matching was found for[white]: [purple]{string_to_search}")
# Retry
search()
search()

View File

@ -277,12 +277,7 @@ class M3U8_Segments:
if response_size == 0:
response_size = int(len(response.content))
# Check length segments
"""
expected_length = int(response.headers.get('Content-Length', 0))
if not (expected_length != 0 and len(response.content) == expected_length):
console.print(f"Incomplete download for '{ts_url}' (received {len(response.content)} bytes, expected {expected_length}).")
"""
# Update progress bar with custom Class
self.class_ts_estimator.update_progress_bar(response_size, duration, progress_bar)
# Decrypt the segment content if decryption is needed

View File

@ -25,16 +25,16 @@ 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.
Initialize the M3U8_Ts_Estimator object.
Parameters:
- total_segments (int): Len of total segments to download
- total_segments (int): Length of total segments to download.
"""
self.ts_file_sizes = []
self.now_downloaded_size = 0
self.total_segments = total_segments
self.lock = threading.Lock()
self.speed = 0
self.speed = {"upload": "N/A", "download": "N/A"} # Default to N/A
self.speed_thread = threading.Thread(target=self.capture_speed)
self.speed_thread.daemon = True
self.speed_thread.start()
@ -58,37 +58,41 @@ class M3U8_Ts_Estimator:
def capture_speed(self, interval: float = 1):
"""
Capture the internet speed periodically and store the values in a deque.
Capture the internet speed periodically and store the values.
"""
def get_network_io():
io_counters = psutil.net_io_counters()
return io_counters
"""Get network I/O counters, handle missing psutil gracefully."""
try:
io_counters = psutil.net_io_counters()
return io_counters
except Exception as e:
logging.warning(f"Unable to access network I/O counters: {e}")
return None
# Get proc id
pid = os.getpid()
while True:
# Get value
old_value = get_network_io()
if old_value is None: # If psutil is not available, continue with default values
time.sleep(interval)
continue
time.sleep(interval)
new_value = get_network_io()
if new_value is None: # Handle again if psutil fails in the next call
time.sleep(interval)
continue
with self.lock:
upload_speed = (new_value.bytes_sent - old_value.bytes_sent) / interval
download_speed = (new_value.bytes_recv - old_value.bytes_recv) / interval
self.speed = ({
self.speed = {
"upload": format_transfer_speed(upload_speed),
"download": format_transfer_speed(download_speed)
})
old_value = new_value
}
def get_average_speed(self) -> float:
"""
Calculate the average internet speed from the values in the deque.
Calculate the average internet speed.
Returns:
float: The average internet speed in Mbps.
@ -112,15 +116,15 @@ class M3U8_Ts_Estimator:
# Return formatted mean size
return format_file_size(mean_size)
except ZeroDivisionError as e:
logging.error("Division by zero error occurred: %s", e)
return "0B"
except Exception as e:
logging.error("An unexpected error occurred: %s", e)
return "Error"
def get_downloaded_size(self) -> str:
"""
Get the total downloaded size formatted as a human-readable string.
@ -129,21 +133,21 @@ class M3U8_Ts_Estimator:
str: The total downloaded size as a human-readable string.
"""
return format_file_size(self.now_downloaded_size)
def update_progress_bar(self, total_downloaded: int, duration: float, progress_counter: tqdm) -> None:
"""
Updates the progress bar with information about the TS segment download.
Parameters:
total_downloaded (int): The len of the content of the downloaded TS segment.
total_downloaded (int): The length of the content of the downloaded TS segment.
duration (float): The duration of the segment download in seconds.
progress_counter (tqdm): The tqdm object representing the progress bar.
"""
# Add the size of the downloaded segment to the estimator
self.add_ts_file(total_downloaded * self.total_segments, total_downloaded, duration)
# Get downloaded size and total estimated size
downloaded_file_size_str = self.get_downloaded_size()
downloaded_file_size_str = self.get_downloaded_size()
file_total_size = self.calculate_total_size()
# Fix parameter for prefix
@ -166,3 +170,4 @@ class M3U8_Ts_Estimator:
f"{Colors.WHITE}[ {Colors.GREEN}{number_file_downloaded}{Colors.RED} {units_file_downloaded} "
f"{Colors.WHITE}| {Colors.CYAN}{average_internet_speed} {Colors.RED}{average_internet_unit}"
)