Refactored metadata extraction -> created an EpisodeData dataclass to represent metadata clearly. Modularized download process: Broke down huge main function into several parts. Added error handling for yt_dlp. Added docstrings to functions for ease of understanding.
52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
from datetime import datetime, timezone, timedelta
|
|
from typing import List, Dict
|
|
|
|
import subprocess
|
|
|
|
|
|
def time_to_milliseconds(time,length_to_cut) -> int:
|
|
return int(time * 1000 - length_to_cut * 1000)
|
|
|
|
def add_html_tags_to_description(input_text) -> str:
|
|
return("<p>"+input_text.replace("\n\n", "</p>\n<p>").replace("\n", "<br>")+"</p>")
|
|
|
|
|
|
def get_friday_number(extracted_timestamp) -> int:
|
|
dt = datetime.fromtimestamp(extracted_timestamp)
|
|
start_of_year = datetime(dt.year, 1, 1)
|
|
days_until_first_friday = (4 - start_of_year.weekday()) % 7
|
|
first_friday = start_of_year + timedelta(days=days_until_first_friday)
|
|
fridays_passed = (dt - first_friday).days // 7 + 1
|
|
return fridays_passed
|
|
|
|
def return_url_of_latest_episode(base_url:str) -> str:
|
|
result = subprocess.run(["get_iplayer","--pid-recursive-list",base_url], capture_output=True, text=True)
|
|
latest_episode_id = result.stdout.split("\n")[-3].split(",")[-1][1:]
|
|
return (f"https://www.bbc.co.uk/sounds/play/{latest_episode_id}")
|
|
|
|
def modify_chapters_for_ffmpeg(chapters: List[Dict], length_to_cut: float) -> str:
|
|
"""
|
|
Converts chapter times to ffmpeg-compatible metadata format, adjusting by length_to_cut.
|
|
|
|
Args:
|
|
chapters (list): List of chapter dicts with "start_time", "end_time", and "title".
|
|
length_to_cut (int/float): Amount of time to cut from start, in seconds.
|
|
|
|
Returns:
|
|
str: Chapters formatted as ffmpeg metadata.
|
|
"""
|
|
for entry in chapters:
|
|
if "start_time" in entry:
|
|
entry["start_time"]=time_to_milliseconds(entry["start_time"],length_to_cut)
|
|
if "end_time" in entry:
|
|
entry["end_time"]=time_to_milliseconds(entry["end_time"],length_to_cut)
|
|
|
|
chapter_format = ";FFMETADATA1\n"
|
|
for entry in chapters:
|
|
chapter_format+=("[CHAPTER]\n")
|
|
chapter_format+=("TIMEBASE=1/1000\n")
|
|
chapter_format+=(f"START={entry['start_time']}\n")
|
|
chapter_format+=(f"END={entry['end_time']}\n")
|
|
chapter_format+=(f"title={entry['title']}\n\n")
|
|
|
|
return(chapter_format) |