Skip to content

Commit b1d07dd

Browse files
authored
Fix output dir and filename
1 parent 166ff51 commit b1d07dd

2 files changed

Lines changed: 52 additions & 19 deletions

File tree

my5-dl.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ def get_content_info(episode_url: str) -> str:
7676
print("[!] Episode is not available")
7777
return
7878

79-
return resp["id"], resp["sea_num"], resp["sh_title"], resp["title"]
79+
return (
80+
resp["id"],
81+
resp["sea_num"],
82+
str(resp["ep_num"]),
83+
resp["sh_title"],
84+
resp["title"],
85+
)
8086
except Exception as ex:
8187
print(f"[!] Exception thrown when attempting to get the content ID: {ex}")
8288
raise
@@ -170,7 +176,7 @@ def get_pssh_from_mpd(mpd: str):
170176
)
171177
return
172178

173-
return re.findall("<cenc:pssh>(.*?)<\/cenc:pssh>", r.text)[1]
179+
return re.findall(r"<cenc:pssh>(.*?)</cenc:pssh>", r.text)[1]
174180
except Exception as ex:
175181
print(f"[!] Exception thrown when attempting to get the content ID: {ex}")
176182
raise
@@ -274,31 +280,52 @@ def decrypt_streams(decryption_key: str, output_title: str) -> list:
274280

275281
def merge_streams(
276282
files: list,
277-
show_name: str,
283+
show_title: str,
278284
season_number: str,
285+
episode_number: str,
279286
episode_title: str,
280287
subtitles_url: str,
281288
dl_subtitles: bool,
282289
):
283290
try:
284291
print("[*] Merging streams...")
285292

293+
date_regex = r"(monday|tuesday|wednesday|thursday|friday) \d{0,2} (january|february|march|april|may|june|july|august|september|october|november|december)"
294+
if re.match(date_regex, episode_title, re.I):
295+
episode_title = ""
296+
297+
if season_number is None:
298+
season_number = "01"
286299
if len(season_number) == 1:
287300
season_number = f"0{season_number}"
301+
if len(episode_number) == 1:
302+
episode_number = f"0{episode_number}"
288303

289304
if "Episode " in episode_title:
290-
episode_title = episode_title[8:]
291-
if len(episode_title) == 1:
292-
episode_title = f"0{episode_title}"
305+
if len(show_title.split(":")) == 2:
306+
episode_title = show_title.split(":")[1]
307+
else:
308+
episode_title = ""
293309

294-
filename = " ".join(
295-
f"{show_name} S{season_number}E{episode_title}".split()
296-
).replace(" ", ".")
297-
output_dir = f"{DOWNLOAD_DIR}/{safe_name(show_name.split(':')[0].strip())}/S{safe_name(season_number)}/"
298-
output_file = f"{output_dir}/{safe_name(filename)}"
310+
season_number = f"S{season_number}"
311+
episode_number = f"E{episode_number}"
312+
313+
if len(episode_title.split(":")) == 2:
314+
episode_title = episode_title.split(":")[1]
315+
316+
if show_title == episode_title or (
317+
len(show_title.split(":")) == 2
318+
and show_title.split(":")[1] in episode_title
319+
):
320+
episode_title = ""
299321

322+
output_dir = f"{DOWNLOAD_DIR}/{safe_name(show_title)}"
300323
os.makedirs(output_dir, exist_ok=True)
301324

325+
output_dir += " ".join(
326+
f"/{safe_name(show_title)} {season_number}{episode_number} {episode_title}".split()
327+
).replace(" ", ".")
328+
302329
mp4_decrypt = "ffmpeg"
303330
if USE_BIN_DIR:
304331
mp4_decrypt = "./bin/ffmpeg.exe"
@@ -314,7 +341,7 @@ def merge_streams(
314341
files[1],
315342
"-c",
316343
"copy",
317-
f"{output_file}.mp4",
344+
f"{output_dir}.mp4",
318345
]
319346
subprocess.run(args, check=True)
320347

@@ -326,7 +353,7 @@ def merge_streams(
326353
print("[*] Subtitles are not available")
327354
return
328355

329-
with open(f"{output_file}.vtt", mode="wb") as file:
356+
with open(f"{output_dir}.vtt", mode="wb") as file:
330357
file.write(resp.content)
331358
except Exception as ex:
332359
print(
@@ -337,6 +364,7 @@ def merge_streams(
337364
print("[!] Failed merging streams")
338365
raise
339366

367+
340368
def check_required_config_values() -> None:
341369
lets_go = True
342370
if not HMAC_SECRET:
@@ -353,6 +381,7 @@ def check_required_config_values() -> None:
353381
if not lets_go:
354382
sys.exit(1)
355383

384+
356385
def create_argument_parser():
357386
parser = argparse.ArgumentParser(description="Channel 5 downloader.")
358387
parser.add_argument(
@@ -368,10 +397,7 @@ def create_argument_parser():
368397
action="store_true",
369398
)
370399
parser.add_argument(
371-
"--url",
372-
"-u",
373-
help="The URL of the episode to download",
374-
required=True
400+
"--url", "-u", help="The URL of the episode to download", required=True
375401
)
376402
args = parser.parse_args()
377403

@@ -388,15 +414,20 @@ def main():
388414
dl_video = parser.download
389415
dl_subtitles = parser.subtitles
390416

391-
392417
# Generate the episode URL
393418
episode_url = generate_episode_url(url)
394419
if episode_url is None:
395420
print("[!] Failed to get the episode URL")
396421
sys.exit(1)
397422

398423
# Get the C5 content ID by parsing the reponse of the episode URL
399-
content_id, season_number, show_title, episode_title = get_content_info(episode_url)
424+
(
425+
content_id,
426+
season_number,
427+
episode_number,
428+
show_title,
429+
episode_title,
430+
) = get_content_info(episode_url)
400431
if content_id is None:
401432
print("[!] Failed to get the content ID")
402433
sys.exit(1)
@@ -424,6 +455,7 @@ def main():
424455
decrypted_file_names,
425456
show_title,
426457
season_number,
458+
episode_number,
427459
episode_title,
428460
subtitles_url,
429461
dl_subtitles,

utility.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def safe_name(val: str) -> str:
3737
(r"\?", ""),
3838
(r"\*", ""),
3939
(r"\"", ""),
40+
(r",", ""),
4041
]
4142
for pattern, repl in replacements:
4243
val = re.sub(pattern, repl, val, 0)

0 commit comments

Comments
 (0)