From c1662317f2d356c51f6b6de6a098d91fffa95e7e Mon Sep 17 00:00:00 2001 From: Lovi <62809003+Lovi-0@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:42:42 +0100 Subject: [PATCH] Fix index and playlist --- Src/Lib/Downloader/HLS/downloader.py | 78 ++++++++++++++++------------ Src/Lib/M3U8/parser.py | 1 - Test/Download_HLS.py | 4 +- Test/Download_MP4.py | 2 +- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/Src/Lib/Downloader/HLS/downloader.py b/Src/Lib/Downloader/HLS/downloader.py index 259aec6..ae1a927 100644 --- a/Src/Lib/Downloader/HLS/downloader.py +++ b/Src/Lib/Downloader/HLS/downloader.py @@ -114,10 +114,10 @@ class HttpClient: ) response.raise_for_status() - return response.text # Return the response text + return response.text except Exception as e: - logging.error(f"Request to {url} failed: {response.status_code} when get text.") + logging.info(f"Request to {url} failed with error: {e}") return 404 def get_content(self, url): @@ -740,50 +740,60 @@ class HLS_Downloader: # Determine whether to process a playlist or index if self.m3u8_playlist: + if self.m3u8_playlist is not None: - # Parse data from url and get if is a master playlist - self.instace_parserClass.parse_data(uri=self.m3u8_playlist, raw_content=HttpClient().get(self.m3u8_playlist)) - is_masterPlaylist = self.instace_parserClass.is_master_playlist + # Parse data from url and check if is a master playlist + test_raw_content = HttpClient().get(self.m3u8_playlist) + if test_raw_content != 404: + self.instace_parserClass.parse_data(uri=self.m3u8_playlist, raw_content=test_raw_content) + is_masterPlaylist = self.instace_parserClass.is_master_playlist - # Check if it's a real master playlist - if is_masterPlaylist: - if not GET_ONLY_LINK: - r_proc = self._process_playlist() + # Check if it's a real master playlist + if is_masterPlaylist: + if not GET_ONLY_LINK: + r_proc = self._process_playlist() - if r_proc == 404: - return 404 - else: - return None - + if r_proc == 404: + return 404 + else: + return None + + else: + return { + 'path': self.output_filename, + 'url': self.m3u8_playlist + } + else: - return { - 'path': self.output_filename, - 'url': self.m3u8_playlist - } - + console.log("[red]Error: URL passed to M3U8_Parser is an index playlist; expected a master playlist. Crucimorfo strikes again!") else: - console.log("[red]Error: URL passed to M3U8_Parser is an index playlist; expected a master playlist. Crucimorfo strikes again!") + console.log("[red]Error: m3u8_playlist is None") elif self.m3u8_index: + if self.m3u8_index is not None: - # Parse data from url and get if is a master playlist - self.instace_parserClass.parse_data(uri=self.m3u8_index, raw_content=HttpClient().get(self.m3u8_playlist)) - is_masterPlaylist = self.instace_parserClass.is_master_playlist + # Parse data from url and check if is a master playlist + test_raw_content = HttpClient().get(self.m3u8_index) + if test_raw_content != 404: + self.instace_parserClass.parse_data(uri=self.m3u8_index, raw_content=test_raw_content) + is_masterPlaylist = self.instace_parserClass.is_master_playlist - # Check if it's a real index playlist - if not is_masterPlaylist: - if not GET_ONLY_LINK: - self._process_index() - return None + # Check if it's a real index playlist + if not is_masterPlaylist: + if not GET_ONLY_LINK: + self._process_index() + return None + else: + return { + 'path': self.output_filename, + 'url': self.m3u8_index + } + else: - return { - 'path': self.output_filename, - 'url': self.m3u8_index - } - + console.log("[red]Error: URL passed to M3U8_Parser is an master playlist; expected a index playlist. Crucimorfo strikes again!") else: - console.log("[red]Error: URL passed to M3U8_Parser is an master playlist; expected a index playlist. Crucimorfo strikes again!") + console.log("[red]Error: m3u8_index is None") def _clean(self, out_path: str) -> None: diff --git a/Src/Lib/M3U8/parser.py b/Src/Lib/M3U8/parser.py index df83e40..302e919 100644 --- a/Src/Lib/M3U8/parser.py +++ b/Src/Lib/M3U8/parser.py @@ -444,7 +444,6 @@ class M3U8_Parser: - m3u8_content (str): The content of the M3U8 file. """ - # Get obj of the m3u8 text content download, dictionary with video, audio, segments, subtitles m3u8_obj = loads(raw_content, uri) diff --git a/Test/Download_HLS.py b/Test/Download_HLS.py index 921a429..d7a093b 100644 --- a/Test/Download_HLS.py +++ b/Test/Download_HLS.py @@ -14,6 +14,6 @@ from Src.Lib.Downloader import HLS_Downloader # Test HLS_Downloader( - output_filename=r".\EP_1.mp4", - m3u8_playlist="" + output_filename=r".\Video\undefined.mp4", + m3u8_index="" ).start() \ No newline at end of file diff --git a/Test/Download_MP4.py b/Test/Download_MP4.py index c3a7c77..72eadbd 100644 --- a/Test/Download_MP4.py +++ b/Test/Download_MP4.py @@ -15,5 +15,5 @@ from Src.Lib.Downloader import MP4_downloader # Test MP4_downloader( "", - "EP_1.mp4" + ".\Video\undefined.mp4" )