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
This commit is contained in:
Fede14it 2024-02-27 19:39:49 +01:00 committed by GitHub
parent fe687daf9d
commit a6a6d55379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 95 additions and 47 deletions

View File

@ -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_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) 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_name = f"{tv_name.replace('+', '_')}_S{str(season_select).zfill(2)}E{str(index_ep_select+1).zfill(2)}"
mp4_format = mp4_name + ".mp4" mp4_format = f"{mp4_name}.mp4"
mp4_path = os.path.join("videos", mp4_format) 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) 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) token = get_token(tv_id, domain)
num_season_find = get_info_tv(tv_id, tv_name, version, domain) num_season_find = get_info_tv(tv_id, tv_name, version, domain)
console.print(f"[blue]Season find: [red]{num_season_find}") 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")
season_select = int(msg.ask("\n[green]Insert season number: ")) console.print(f"\n[blue]Season find: [red]{num_season_find}")
season_select = str(msg.ask("\n[green]Insert season number: "))
if 1 <= season_select <= num_season_find: if "[" in season_select:
eps = get_info_season(tv_id, tv_name, domain, version, token, season_select) start, end = map(int, season_select[1:-1].split('-'))
result = list(range(start, end + 1))
for ep in eps: for n_season in result:
console.print(f"[green]Ep: [blue]{ep['n']} [green]=> [purple]{ep['name']}") eps = get_info_season(tv_id, tv_name, domain, version, token, n_season)
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: 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:
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") print("\n")
else:
console.print("[red]Wrong index for season")

View File

@ -458,6 +458,8 @@ def download_m3u8(m3u8_playlist=None, m3u8_index = None, m3u8_audio=None, m3u8_s
# Download m3u8 index, with segments # 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}") 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() M3U8_Downloader(m3u8_index, m3u8_audio, key=key, output_filename=output_filename).start()

53
run.py
View File

@ -41,20 +41,49 @@ def main():
Page.display_search_results(db_title) Page.display_search_results(db_title)
if len(db_title) != 0: 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: if selected_title['type'] == "movie":
selected_title = db_title[index_select] console.print(f"[green]\nMovie select: {selected_title['name']}")
download_film(selected_title['id'], selected_title['slug'], domain)
if selected_title['type'] == "movie": else:
console.print(f"[green]\nMovie select: {selected_title['name']}") console.print(f"[green]\nTv select: {selected_title['name']}")
download_film(selected_title['id'], selected_title['slug'], domain) download_tv(selected_title['id'], selected_title['slug'], site_version, domain)
else: else:
console.print(f"[green]\nTv select: {selected_title['name']}") console.print("[red]Wrong index for selection")
download_tv(selected_title['id'], selected_title['slug'], site_version, domain) elif "[" in index_select:
if "-" in index_select:
else: start, end = map(int, index_select[1:-1].split('-'))
console.print("[red]Wrong index for selection") 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: else:
console.print("[red]Cant find a single element") console.print("[red]Cant find a single element")