dplocki / podcast-downloader

The Python script for downloading new mp3 from RSS given channels
GNU General Public License v3.0
109 stars 15 forks source link

Final filename can exceed 255 chars #27

Closed npow closed 1 year ago

npow commented 1 year ago

Describe the bug Final filename can exceed 255 if template string includes another pattern in addition to the title, causing the program to crash.

To Reproduce Steps to reproduce the behavior:

Expected behavior Program should not crash. Need to truncate expanded template.

Desktop (please complete the following information):

Additional context Checked the code. Looks like the truncation only applies to the title, and not the expanded template.

def str_to_filename(value: str) -> str:
    value = unicodedata.normalize("NFKC", value)
    value = re.sub(r"[\u0000-\u001F\u007F\*/:<>\?\\\|]", " ", value)

    return value.strip()[:FILE_NAME_CHARACTER_LIMIT]

def file_template_to_file_name(name_template: str, entity: RSSEntity) -> str:
    return (
        name_template.replace("%file_name%", link_to_file_name(entity.link))
        .replace("%publish_date%", time.strftime("%Y%m%d", entity.published_date))
        .replace("%file_extension%", link_to_extension(entity.link))
        .replace("%title%", str_to_filename(entity.title))
    )