Implement basic naming options
This commit is contained in:
parent
d4616f6298
commit
ecba66d27a
12
README.md
12
README.md
|
@ -66,9 +66,15 @@ Arguments:
|
||||||
VARIANT the variant code [video quality version]
|
VARIANT the variant code [video quality version]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h --help print this message
|
-h --help print this message
|
||||||
--version print current version of the program
|
--version print current version of the program
|
||||||
--debug on error, print debugging information
|
--debug on error, print debugging information
|
||||||
|
--name-use-id use the program ID
|
||||||
|
--name-use-slug use the URL slug
|
||||||
|
--name-sep=<sep> field separator [default: - ]
|
||||||
|
--name-seq-pfx=<pfx> sequence counter prefix [default: - ]
|
||||||
|
--name-seq-no-pad disable sequence zero-padding
|
||||||
|
--name-add-resolution add resolution tag
|
||||||
```
|
```
|
||||||
|
|
||||||
🔧 How it works
|
🔧 How it works
|
||||||
|
|
|
@ -15,12 +15,14 @@ def fetch_sources(http_session, url):
|
||||||
from .hls import fetch_program_tracks
|
from .hls import fetch_program_tracks
|
||||||
from .www import parse_url
|
from .www import parse_url
|
||||||
|
|
||||||
site, target_id = parse_url(url)
|
site, program_id, slug = parse_url(url)
|
||||||
|
|
||||||
variants = dict()
|
variants = dict()
|
||||||
renditions = dict()
|
renditions = dict()
|
||||||
|
|
||||||
program_meta, program_index_urls = fetch_program_info(http_session, site, target_id)
|
p_meta, program_index_urls = fetch_program_info(http_session, site, program_id)
|
||||||
|
|
||||||
|
program = Program(program_id, slug, p_meta)
|
||||||
|
|
||||||
for program_index_url in program_index_urls:
|
for program_index_url in program_index_urls:
|
||||||
v_tracks, a_track, s_track = fetch_program_tracks(
|
v_tracks, a_track, s_track = fetch_program_tracks(
|
||||||
|
@ -41,7 +43,7 @@ def fetch_sources(http_session, url):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
return Sources(
|
return Sources(
|
||||||
program_meta,
|
program,
|
||||||
[Variant(key, source) for key, source in variants.items()],
|
[Variant(key, source) for key, source in variants.items()],
|
||||||
[Rendition(key, source) for key, source in renditions.items()],
|
[Rendition(key, source) for key, source in renditions.items()],
|
||||||
)
|
)
|
||||||
|
@ -144,7 +146,7 @@ def compile_sources(sources, **naming_options):
|
||||||
build_file_name = file_name_builder(v_meta, a_meta, s_meta, **naming_options)
|
build_file_name = file_name_builder(v_meta, a_meta, s_meta, **naming_options)
|
||||||
|
|
||||||
return Target(
|
return Target(
|
||||||
sources.program,
|
sources.program.meta,
|
||||||
VideoTrack(v_meta, v_url),
|
VideoTrack(v_meta, v_url),
|
||||||
AudioTrack(a_meta, a_url),
|
AudioTrack(a_meta, a_url),
|
||||||
SubtitlesTrack(s_meta, s_url) if s_meta else None,
|
SubtitlesTrack(s_meta, s_url) if s_meta else None,
|
||||||
|
|
|
@ -19,9 +19,15 @@ Arguments:
|
||||||
VARIANT the variant code [video quality version]
|
VARIANT the variant code [video quality version]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h --help print this message
|
-h --help print this message
|
||||||
--version print current version of the program
|
--version print current version of the program
|
||||||
--debug on error, print debugging information
|
--debug on error, print debugging information
|
||||||
|
--name-use-id use the program ID
|
||||||
|
--name-use-slug use the URL slug
|
||||||
|
--name-sep=<sep> field separator [default: - ]
|
||||||
|
--name-seq-pfx=<pfx> sequence counter prefix [default: - ]
|
||||||
|
--name-seq-no-pad disable sequence zero-padding
|
||||||
|
--name-add-resolution add resolution tag
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
@ -174,7 +180,14 @@ def main():
|
||||||
|
|
||||||
select_variant(sources, _validate_variant(variants, args["VARIANT"]))
|
select_variant(sources, _validate_variant(variants, args["VARIANT"]))
|
||||||
|
|
||||||
target = compile_sources(sources)
|
target = compile_sources(
|
||||||
|
sources,
|
||||||
|
**{
|
||||||
|
k[7:].replace("-", "_"): v
|
||||||
|
for k, v in args.items()
|
||||||
|
if k.startswith("--name-")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
progress = create_progress()
|
progress = create_progress()
|
||||||
|
|
||||||
|
|
|
@ -91,10 +91,18 @@ class Rendition(NamedTuple):
|
||||||
source: tuple[str, Optional[str]]
|
source: tuple[str, Optional[str]]
|
||||||
|
|
||||||
|
|
||||||
|
class Program(NamedTuple):
|
||||||
|
"""A program representation."""
|
||||||
|
|
||||||
|
id: str
|
||||||
|
slug: str
|
||||||
|
meta: ProgramMeta
|
||||||
|
|
||||||
|
|
||||||
class Sources(NamedTuple):
|
class Sources(NamedTuple):
|
||||||
"""A program's sources."""
|
"""A program's sources."""
|
||||||
|
|
||||||
program: ProgramMeta
|
program: Program
|
||||||
variants: list[Variant]
|
variants: list[Variant]
|
||||||
renditions: list[Rendition]
|
renditions: list[Rendition]
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,55 @@
|
||||||
# This file is part of `delarte` (https://git.afpy.org/fcode/delarte.git)
|
# This file is part of `delarte` (https://git.afpy.org/fcode/delarte.git)
|
||||||
|
|
||||||
"""Provide contextualized based file naming utility."""
|
"""Provide contextualized based file naming utility."""
|
||||||
|
import re
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
from .model import Program, VideoMeta, AudioMeta, SubtitlesMeta
|
||||||
|
|
||||||
|
|
||||||
def file_name_builder(v_meta, a_meta, s_meta, **options):
|
def file_name_builder(
|
||||||
|
v_meta: VideoMeta,
|
||||||
|
a_meta: AudioMeta,
|
||||||
|
s_meta: Optional[SubtitlesMeta],
|
||||||
|
*,
|
||||||
|
use_id=False,
|
||||||
|
use_slug=False,
|
||||||
|
sep=" - ",
|
||||||
|
seq_pfx=" - ",
|
||||||
|
seq_no_pad=False,
|
||||||
|
add_resolution=False,
|
||||||
|
):
|
||||||
"""Create a file namer from context."""
|
"""Create a file namer from context."""
|
||||||
|
|
||||||
def build_file_name(p_meta):
|
def sub_sequence_counter(match):
|
||||||
|
index = match[1]
|
||||||
|
if not seq_no_pad:
|
||||||
|
index = (len(match[2]) - len(index)) * "0" + index
|
||||||
|
|
||||||
|
return seq_pfx + index
|
||||||
|
|
||||||
|
def replace_sequence_counter(s: str) -> str:
|
||||||
|
return re.sub(r"\((\d+)/(\d+)\)", sub_sequence_counter, s)
|
||||||
|
|
||||||
|
def build_file_name(program: Program) -> str:
|
||||||
"""Create a file name for given program."""
|
"""Create a file name for given program."""
|
||||||
return p_meta.title.replace("/", "-")
|
if use_id:
|
||||||
|
return program.id
|
||||||
|
|
||||||
|
if use_slug:
|
||||||
|
return program.slug
|
||||||
|
|
||||||
|
fields = [replace_sequence_counter(program.meta.title)]
|
||||||
|
if program.meta.subtitle:
|
||||||
|
fields.add(replace_sequence_counter(program.meta.subtitles))
|
||||||
|
|
||||||
|
if add_resolution:
|
||||||
|
fields.append(f"{v_meta.height}p")
|
||||||
|
|
||||||
|
name = sep.join(fields)
|
||||||
|
name = re.sub(r'[/:<>"\\|?*]', "", name)
|
||||||
|
name = re.sub(r"\s+", " ", name)
|
||||||
|
|
||||||
|
return name
|
||||||
|
|
||||||
return build_file_name
|
return build_file_name
|
||||||
|
|
|
@ -24,6 +24,7 @@ def parse_url(url):
|
||||||
if (_ := path.pop(0)) != "videos":
|
if (_ := path.pop(0)) != "videos":
|
||||||
raise InvalidUrl("PATH", url, _)
|
raise InvalidUrl("PATH", url, _)
|
||||||
|
|
||||||
target_id = path.pop(0)
|
id = path.pop(0)
|
||||||
|
slug = path.pop(0)
|
||||||
|
|
||||||
return site, target_id
|
return site, id, slug
|
||||||
|
|
Loading…
Reference in New Issue
Block a user