From 8cd4108f97e80da9175c4a225d32eafadb234da1 Mon Sep 17 00:00:00 2001 From: Lovi <62809003+Lovi-0@users.noreply.github.com> Date: Sat, 4 Jan 2025 15:18:49 +0100 Subject: [PATCH] Fix download json config --- .../Lib/Downloader/HLS/segments.py | 18 +++++-- StreamingCommunity/Lib/M3U8/decryptor.py | 1 + StreamingCommunity/Util/_jsonConfig.py | 50 +++++++++++++------ config.json | 4 +- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/StreamingCommunity/Lib/Downloader/HLS/segments.py b/StreamingCommunity/Lib/Downloader/HLS/segments.py index 4905c7c..4445426 100644 --- a/StreamingCommunity/Lib/Downloader/HLS/segments.py +++ b/StreamingCommunity/Lib/Downloader/HLS/segments.py @@ -131,6 +131,7 @@ class M3U8_Segments: # Convert the content of the response to hexadecimal and then to bytes hex_content = binascii.hexlify(response.content).decode('utf-8') byte_content = bytes.fromhex(hex_content) + logging.info(f"URI: Hex content: {hex_content}, Byte content: {byte_content}") #console.print(f"[cyan]Find key: [red]{hex_content}") return byte_content @@ -160,6 +161,7 @@ class M3U8_Segments: iv = m3u8_parser.keys.get('iv') method = m3u8_parser.keys.get('method') + logging.info(f"M3U8_Decryption - IV: {iv}, method: {method}") # Create a decryption object with the key and set the method self.decryption = M3U8_Decryption(key, iv, method) @@ -308,7 +310,9 @@ class M3U8_Segments: except Exception as e: logging.error(f"Decryption failed for segment {index}: {str(e)}") - raise + self.interrupt_flag.set() # Interrupt the download process + self.stop_event.set() # Trigger the stopping event for all threads + break # Stop the current task immediately # Update progress and queue self.class_ts_estimator.update_progress_bar(content_size, duration, progress_bar) @@ -537,10 +541,14 @@ class M3U8_Segments: progress_bar.close() # Final verification - final_completion = (len(self.downloaded_segments) / total_segments) * 100 - if final_completion < 99.9: # Less than 99.9% complete - missing = set(range(total_segments)) - self.downloaded_segments - raise Exception(f"Download incomplete ({final_completion:.1f}%). Missing segments: {sorted(missing)}") + try: + final_completion = (len(self.downloaded_segments) / total_segments) * 100 + if final_completion < 99.9: # Less than 99.9% complete + missing = set(range(total_segments)) - self.downloaded_segments + raise Exception(f"Download incomplete ({final_completion:.1f}%). Missing segments: {sorted(missing)}") + + except: + pass # Verify output file if not os.path.exists(self.tmp_file_path): diff --git a/StreamingCommunity/Lib/M3U8/decryptor.py b/StreamingCommunity/Lib/M3U8/decryptor.py index fa8afe8..2671dbb 100644 --- a/StreamingCommunity/Lib/M3U8/decryptor.py +++ b/StreamingCommunity/Lib/M3U8/decryptor.py @@ -61,6 +61,7 @@ if crypto_installed: bytes: The decrypted content. """ start = time.perf_counter_ns() + #logging.info(f"Ciphertext: {ciphertext}") # Decrypt based on encryption method if self.method in {"AES", "AES-128"}: diff --git a/StreamingCommunity/Util/_jsonConfig.py b/StreamingCommunity/Util/_jsonConfig.py index c87918e..050f1a0 100644 --- a/StreamingCommunity/Util/_jsonConfig.py +++ b/StreamingCommunity/Util/_jsonConfig.py @@ -1,6 +1,7 @@ # 29.01.24 import os +import sys import json import httpx import logging @@ -31,25 +32,46 @@ class ConfigManager: # Download config.json else: - logging.info("Configuration file does not exist. Downloading...") - url = "https://raw.githubusercontent.com/Lovi-0/StreamingCommunity/refs/heads/main/config.json" + self.download_requirements( + 'https://raw.githubusercontent.com/Lovi-0/StreamingCommunity/refs/heads/main/config.json', + self.file_path + ) - with httpx.Client() as client: - response = client.get(url) - - if response.status_code == 200: - with open(self.file_path, 'w') as f: - f.write(response.text) + with open(self.file_path, 'r') as f: + self.config = json.loads(f) + logging.info("Configuration file downloaded and saved.") - self.config = json.loads(response.text) - logging.info("Configuration file downloaded and saved.") - - else: - logging.error(f"Failed to download configuration file. Status code: {response.status_code}") - + logging.info("Configuration file does not exist. Downloading...") + except Exception as e: logging.error(f"Error reading configuration file: {e}") + def download_requirements(self, url: str, filename: str): + """ + Download the requirements.txt file from the specified URL if not found locally using requests. + + Args: + url (str): The URL to download the requirements file from. + filename (str): The local filename to save the requirements file as. + """ + try: + import requests + + logging.info(f"{filename} not found locally. Downloading from {url}...") + response = requests.get(url) + + if response.status_code == 200: + with open(filename, 'wb') as f: + f.write(response.content) + + else: + logging.error(f"Failed to download {filename}. HTTP Status code: {response.status_code}") + sys.exit(0) + + except Exception as e: + logging.error(f"Failed to download {filename}: {e}") + sys.exit(0) + def read_key(self, section: str, key: str, data_type: type = str) -> Any: """Read a key from the configuration file. diff --git a/config.json b/config.json index 95865b8..9216015 100644 --- a/config.json +++ b/config.json @@ -15,8 +15,8 @@ "user": "admin", "pass": "adminadmin" }, - "add_siteName": true, - "disable_searchDomain": true, + "add_siteName": false, + "disable_searchDomain": false, "not_close": false }, "REQUESTS": {