From 4553cf9f45e95facdd17444bcd2786cc1bcaf2d4 Mon Sep 17 00:00:00 2001 From: Ghost <62809003+Ghost6446@users.noreply.github.com> Date: Tue, 4 Jun 2024 23:18:16 +0200 Subject: [PATCH] Auto switch proxy. --- Src/Api/Animeunity/anime.py | 4 +- Src/Api/Animeunity/costant.py | 4 +- .../Core/Vix_player/player.py | 2 +- Src/Api/Streamingcommunity/costant.py | 2 + Src/Api/Streamingcommunity/film.py | 4 +- Src/Api/Streamingcommunity/series.py | 4 +- Src/Lib/Hls/segments.py | 43 +++++++++++++------ 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/Src/Api/Animeunity/anime.py b/Src/Api/Animeunity/anime.py index 26a6357..79a2355 100644 --- a/Src/Api/Animeunity/anime.py +++ b/Src/Api/Animeunity/anime.py @@ -18,7 +18,7 @@ from .Core.Util import manage_selection # Config ROOT_PATH = config_manager.get('DEFAULT', 'root_path') -from .costant import ANIME_FOLDER, SERIES_FOLDER, MOVIE_FOLDER +from .costant import ANIME_FOLDER, SERIES_FOLDER, MOVIE_FOLDER, STATIC_IP_SERVER # Variable @@ -61,7 +61,7 @@ def download_episode(index_select: int): ) # Start downloading - obj_download.start() + obj_download.start(STATIC_IP_SERVER) def donwload_series(tv_id: int, tv_name: str): diff --git a/Src/Api/Animeunity/costant.py b/Src/Api/Animeunity/costant.py index 68ec402..28f0252 100644 --- a/Src/Api/Animeunity/costant.py +++ b/Src/Api/Animeunity/costant.py @@ -2,4 +2,6 @@ ANIME_FOLDER = "animeunity" SERIES_FOLDER= "Serie" -MOVIE_FOLDER = "Movie" \ No newline at end of file +MOVIE_FOLDER = "Movie" + +STATIC_IP_SERVER = ['57.129.7.85', '57.129.7.188', '57.129.7.174', '57.129.4.77', '57.129.16.196', '57.129.16.156', '57.129.16.139', '57.129.16.135', '57.129.13.175', '57.129.13.157', '51.38.112.237', '51.195.107.7', '51.195.107.230', '162.19.255.78', '162.19.255.36', '162.19.255.224', '162.19.255.223', '162.19.254.244', '162.19.254.232', '162.19.254.230', '162.19.253.242', '162.19.249.48', '162.19.245.142', '162.19.231.20', '162.19.229.177', '162.19.228.128', '162.19.228.127', '162.19.228.105', '141.95.1.32', '141.95.1.196', '141.95.1.102', '141.95.0.50', '141.95.0.248', '135.125.237.84', '135.125.233.236'] \ No newline at end of file diff --git a/Src/Api/Streamingcommunity/Core/Vix_player/player.py b/Src/Api/Streamingcommunity/Core/Vix_player/player.py index e72e7e0..bd72041 100644 --- a/Src/Api/Streamingcommunity/Core/Vix_player/player.py +++ b/Src/Api/Streamingcommunity/Core/Vix_player/player.py @@ -238,5 +238,5 @@ class VideoSource: new_query = urlencode(final_params) # Encode final_params into a query string new_url = m._replace(query=new_query) # Replace the old query string with the new one final_url = urlunparse(new_url) # Construct the final URL from the modified parts - + return final_url diff --git a/Src/Api/Streamingcommunity/costant.py b/Src/Api/Streamingcommunity/costant.py index 6038c67..de48a7f 100644 --- a/Src/Api/Streamingcommunity/costant.py +++ b/Src/Api/Streamingcommunity/costant.py @@ -3,3 +3,5 @@ STREAMING_FOLDER = "streamingcommunity" MOVIE_FOLDER = "Movie" SERIES_FOLDER = "Serie" + +STATIC_IP_SERVER = ['57.129.7.85', '57.129.7.188', '57.129.7.174', '57.129.4.77', '57.129.16.196', '57.129.16.156', '57.129.16.139', '57.129.16.135', '57.129.13.175', '57.129.13.157', '51.38.112.237', '51.195.107.7', '51.195.107.230', '162.19.255.78', '162.19.255.36', '162.19.255.224', '162.19.255.223', '162.19.254.244', '162.19.254.232', '162.19.254.230', '162.19.253.242', '162.19.249.48', '162.19.245.142', '162.19.231.20', '162.19.229.177', '162.19.228.128', '162.19.228.127', '162.19.228.105', '141.95.1.32', '141.95.1.196', '141.95.1.102', '141.95.0.50', '141.95.0.248', '135.125.237.84', '135.125.233.236'] \ No newline at end of file diff --git a/Src/Api/Streamingcommunity/film.py b/Src/Api/Streamingcommunity/film.py index 2b9d7bf..765a5cf 100644 --- a/Src/Api/Streamingcommunity/film.py +++ b/Src/Api/Streamingcommunity/film.py @@ -18,7 +18,7 @@ from .Core.Vix_player.player import VideoSource # Config ROOT_PATH = config_manager.get('DEFAULT', 'root_path') -from .costant import STREAMING_FOLDER, MOVIE_FOLDER +from .costant import STREAMING_FOLDER, MOVIE_FOLDER, STATIC_IP_SERVER # Variable @@ -59,4 +59,4 @@ def download_film(id_film: str, title_name: str, domain: str): Downloader( m3u8_playlist = master_playlist, output_filename = os.path.join(mp4_path, mp4_format) - ).start() \ No newline at end of file + ).start(STATIC_IP_SERVER) \ No newline at end of file diff --git a/Src/Api/Streamingcommunity/series.py b/Src/Api/Streamingcommunity/series.py index 9b2ecc0..1d0458b 100644 --- a/Src/Api/Streamingcommunity/series.py +++ b/Src/Api/Streamingcommunity/series.py @@ -20,7 +20,7 @@ from .Core.Util import manage_selection, map_episode_title # Config ROOT_PATH = config_manager.get('DEFAULT', 'root_path') -from .costant import STREAMING_FOLDER, SERIES_FOLDER +from .costant import STREAMING_FOLDER, SERIES_FOLDER, STATIC_IP_SERVER # Variable @@ -96,7 +96,7 @@ def donwload_video(tv_name: str, index_season_selected: int, index_episode_selec Downloader( m3u8_playlist = master_playlist, output_filename = os.path.join(mp4_path, mp4_name) - ).start() + ).start(STATIC_IP_SERVER) def donwload_episode(tv_name: str, index_season_selected: int, donwload_all: bool = False) -> None: diff --git a/Src/Lib/Hls/segments.py b/Src/Lib/Hls/segments.py index fe2a131..8dfbf05 100644 --- a/Src/Lib/Hls/segments.py +++ b/Src/Lib/Hls/segments.py @@ -109,7 +109,7 @@ class M3U8_Segments: logging.info(f"Key: ('hex': {hex_content}, 'byte': {byte_content})") return byte_content - def __test_ip(self): + def __test_ip(self, url_to_test: str): """ Tests each proxy IP by sending a request to a corresponding segment URL. """ @@ -117,16 +117,18 @@ class M3U8_Segments: failed_ips = [] for i in range(len(self.fake_proxy_ip)): - url_to_test = self.segments[i] try: + response = requests.get(url_to_test, verify=False, retries=0) + + if response == None: + logging.error(f"[Work] to make request using: {url_to_test}") + failed_ips.append(i) - # Attempt to send a GET request to the URL - requests.get(url_to_test, verify=self.verify_ssl) except: # Log the error and add the IP to the list of failed IPs - logging.error(f"Failed to make request using IP in this request: {url_to_test}") + logging.error(f"[Fail] to make request using IP in this request: {url_to_test}") failed_ips.append(i) # Remove the failed IPs from the fake_proxy_ip list @@ -134,8 +136,13 @@ class M3U8_Segments: # Exit the program if 50% requests failed if len(failed_ips) / 2 > len(self.fake_proxy_ip): - logging.error("All requests with ip failed. Exiting the program.") - sys.exit(0) + logging.error("All requests with ip failed.") + + # Set to not use proxy + self.fake_proxy_ip = None + self.fake_proxy = False + + return False def parse_data(self, m3u8_content: str) -> None: """ @@ -182,10 +189,13 @@ class M3U8_Segments: for i in range(len(self.segments)): segment_url = self.segments[i] - self.segments[i] = self.__gen_proxy__(segment_url, self.segments.index(segment_url)) + # Set to not use proxy if 50% failed + if not self.__test_ip(segment_url): + console.log("[red]Cant use proxy switch to normal url.") + self.fake_proxy = False + break - # Test new url with ip - self.__test_ip() + self.segments[i] = self.__gen_proxy__(segment_url, self.segments.index(segment_url)) # Save new playlist of segment path_m3u8_file = os.path.join(self.tmp_folder, "playlist_fix.m3u8") @@ -226,12 +236,17 @@ class M3U8_Segments: Returns: str: The modified URL with the new IP address. """ - new_ip_address = self.fake_proxy_ip[url_index % len(self.fake_proxy_ip)] + if self.fake_proxy: - # Parse the original URL and replace the hostname with the new IP address - parsed_url = urlparse(url)._replace(netloc=new_ip_address) + new_ip_address = self.fake_proxy_ip[url_index % len(self.fake_proxy_ip)] - return urlunparse(parsed_url) + # Parse the original URL and replace the hostname with the new IP address + parsed_url = urlparse(url)._replace(netloc=new_ip_address) + + return urlunparse(parsed_url) + + else: + return url def make_requests_stream(self, ts_url: str, index: int, progress_bar: tqdm) -> None: """