From a6a6d55379e07a7c13700ba114c2028ddea33bac Mon Sep 17 00:00:00 2001 From: Fede14it <115009551+Fede14it@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:39:49 +0100 Subject: [PATCH] Migliorie per il codice (#45) * Season range and all download * fix if/elif * fix if download all season * Refactor user input handling in main function now you can choose more than one movie or tv * Refactor file paths and create subdirectories for tv show now fill number season and episode to arrive to 2 characters * Update console print message in tv.py --- Src/Api/tv.py | 85 +++++++++++++++++++++++---------------- Src/Lib/FFmpeg/my_m3u8.py | 4 +- run.py | 53 ++++++++++++++++++------ 3 files changed, 95 insertions(+), 47 deletions(-) diff --git a/Src/Api/tv.py b/Src/Api/tv.py index ff406d2..59449f8 100644 --- a/Src/Api/tv.py +++ b/Src/Api/tv.py @@ -134,9 +134,9 @@ def dw_single_ep(tv_id, eps, index_ep_select, domain, token, tv_name, season_sel m3u8_url = get_m3u8_url(json_win_video, json_win_param, render_quality) m3u8_key = get_m3u8_key_ep(json_win_video, json_win_param, tv_name, season_select, index_ep_select+1, eps[index_ep_select]['name'], token_render) - mp4_name = f"{tv_name.replace('+', '_')}_S{str(season_select)}E{str(index_ep_select+1)}" - mp4_format = mp4_name + ".mp4" - mp4_path = os.path.join("videos", mp4_format) + mp4_name = f"{tv_name.replace('+', '_')}_S{str(season_select).zfill(2)}E{str(index_ep_select+1).zfill(2)}" + mp4_format = f"{mp4_name}.mp4" + mp4_path = os.path.join("videos",tv_name, mp4_format) m3u8_url_audio = get_m3u8_playlist(json_win_video, json_win_param, tv_name, season_select, index_ep_select+1, eps[index_ep_select]['name'], token_render) @@ -150,38 +150,55 @@ def main_dw_tv(tv_id, tv_name, version, domain): token = get_token(tv_id, domain) num_season_find = get_info_tv(tv_id, tv_name, version, domain) - console.print(f"[blue]Season find: [red]{num_season_find}") - season_select = int(msg.ask("\n[green]Insert season number: ")) - - if 1 <= season_select <= num_season_find: - eps = get_info_season(tv_id, tv_name, domain, version, token, season_select) - - for ep in eps: - console.print(f"[green]Ep: [blue]{ep['n']} [green]=> [purple]{ep['name']}") - index_ep_select = str(msg.ask("\n[green]Insert ep [red]number [yellow]or [red](*) [green]to download all ep [yellow]or [red][1-2] [green]for a range of ep: ")) - - # Download range [] - if "[" in index_ep_select: - start, end = map(int, index_ep_select[1:-1].split('-')) - result = list(range(start, end + 1)) - - for n_randge_ep in result: - index_ep_select = int(n_randge_ep) - dw_single_ep(tv_id, eps, n_randge_ep-1, domain, token, tv_name, season_select) - - # Download single ep - elif index_ep_select != "*": - if 1 <= int(index_ep_select) <= len(eps): - index_ep_select = int(index_ep_select) - 1 - dw_single_ep(tv_id, eps, index_ep_select, domain, token, tv_name, season_select) - else: - console.print("[red]Wrong index for ep") - - # Download all - else: + console.print("\n[green]Insert season [red]number [yellow]or [red](*) [green]to download all seasons [yellow]or [red][1-2] [green]for a range of season") + console.print(f"\n[blue]Season find: [red]{num_season_find}") + season_select = str(msg.ask("\n[green]Insert season number: ")) + if "[" in season_select: + start, end = map(int, season_select[1:-1].split('-')) + result = list(range(start, end + 1)) + for n_season in result: + eps = get_info_season(tv_id, tv_name, domain, version, token, n_season) for ep in eps: - dw_single_ep(tv_id, eps, int(ep['n'])-1, domain, token, tv_name, season_select) + dw_single_ep(tv_id, eps, int(ep['n'])-1, domain, token, tv_name, n_season) print("\n") + elif season_select != "*": + season_select = int(season_select) + if 1 <= season_select <= num_season_find: + eps = get_info_season(tv_id, tv_name, domain, version, token, season_select) + for ep in eps: + console.print(f"[green]Ep: [blue]{ep['n']} [green]=> [purple]{ep['name']}") + index_ep_select = str(msg.ask("\n[green]Insert ep [red]number [yellow]or [red](*) [green]to download all ep [yellow]or [red][1-2] [green]for a range of ep: ")) + + # Download range [] + if "[" in index_ep_select: + start, end = map(int, index_ep_select[1:-1].split('-')) + result = list(range(start, end + 1)) + + for n_randge_ep in result: + index_ep_select = int(n_randge_ep) + dw_single_ep(tv_id, eps, n_randge_ep-1, domain, token, tv_name, season_select) + + # Download single ep + elif index_ep_select != "*": + if 1 <= int(index_ep_select) <= len(eps): + index_ep_select = int(index_ep_select) - 1 + dw_single_ep(tv_id, eps, index_ep_select, domain, token, tv_name, season_select) + else: + console.print("[red]Wrong index for ep") + + # Download all + else: + for ep in eps: + dw_single_ep(tv_id, eps, int(ep['n'])-1, domain, token, tv_name, season_select) + print("\n") + + else: + console.print("[red]Wrong index for season") else: - console.print("[red]Wrong index for season") + for n_season in range(1, num_season_find+1): + eps = get_info_season(tv_id, tv_name, domain, version, token, n_season) + for ep in eps: + dw_single_ep(tv_id, eps, int(ep['n'])-1, domain, token, tv_name, n_season) + print("\n") + diff --git a/Src/Lib/FFmpeg/my_m3u8.py b/Src/Lib/FFmpeg/my_m3u8.py index 256f6af..bcb34a7 100644 --- a/Src/Lib/FFmpeg/my_m3u8.py +++ b/Src/Lib/FFmpeg/my_m3u8.py @@ -458,6 +458,8 @@ def download_m3u8(m3u8_playlist=None, m3u8_index = None, m3u8_audio=None, m3u8_s # Download m3u8 index, with segments - os.makedirs("videos", exist_ok=True) + # os.makedirs("videos", exist_ok=True) + path = output_filename.split("\\") + os.makedirs("\\".join(path[:-1]), exist_ok=True) if log: console.log(f"[green]Dowload m3u8 from index [white]=> [purple]{m3u8_index}") M3U8_Downloader(m3u8_index, m3u8_audio, key=key, output_filename=output_filename).start() diff --git a/run.py b/run.py index f4ef1dc..b4549eb 100644 --- a/run.py +++ b/run.py @@ -41,20 +41,49 @@ def main(): Page.display_search_results(db_title) if len(db_title) != 0: - index_select = int(msg.ask("\n[blue]Index to download: ")) + console.print(f"\n[blue]Total result: {len(db_title)}") + console.print( + "\n[green]Insert index [red]number [yellow]or [red][1-2] [green]for a range of movies/tv [yellow]or [red][1,3,5] [green]to select discontinued movie/tv" + ) + console.print("\n[red]In case of tv show you will have to choose season and episode to download") + index_select = str(msg.ask("\n[blue]Index to download: ")) + if index_select.isnumeric(): + index_select = int(index_select) + if 0 <= index_select <= len(db_title) - 1: + selected_title = db_title[index_select] - if 0 <= index_select <= len(db_title) - 1: - selected_title = db_title[index_select] - - if selected_title['type'] == "movie": - console.print(f"[green]\nMovie select: {selected_title['name']}") - download_film(selected_title['id'], selected_title['slug'], domain) + if selected_title['type'] == "movie": + console.print(f"[green]\nMovie select: {selected_title['name']}") + download_film(selected_title['id'], selected_title['slug'], domain) + else: + console.print(f"[green]\nTv select: {selected_title['name']}") + download_tv(selected_title['id'], selected_title['slug'], site_version, domain) else: - console.print(f"[green]\nTv select: {selected_title['name']}") - download_tv(selected_title['id'], selected_title['slug'], site_version, domain) - - else: - console.print("[red]Wrong index for selection") + console.print("[red]Wrong index for selection") + elif "[" in index_select: + if "-" in index_select: + start, end = map(int, index_select[1:-1].split('-')) + result = list(range(start, end + 1)) + for n in result: + selected_title = db_title[n] + if selected_title['type'] == "movie": + console.print(f"[green]\nMovie select: {selected_title['name']}") + download_film(selected_title['id'], selected_title['slug'], domain) + else: + console.print(f"[green]\nTv select: {selected_title['name']}") + download_tv(selected_title['id'], selected_title['slug'], site_version, domain) + elif "," in index_select: + result = list(map(int, index_select[1:-1].split(','))) + for n in result: + selected_title = db_title[n] + if selected_title['type'] == "movie": + console.print(f"[green]\nMovie select: {selected_title['name']}") + download_film(selected_title['id'], selected_title['slug'], domain) + else: + console.print(f"[green]\nTv select: {selected_title['name']}") + download_tv(selected_title['id'], selected_title['slug'], site_version, domain) + else: + console.print("[red]Wrong index for selection") else: console.print("[red]Cant find a single element")