Reviewed-on: #27
This commit is contained in:
commit
ba2dd96b36
12
README.md
12
README.md
|
@ -66,9 +66,15 @@ Arguments:
|
|||
VARIANT the variant code [video quality version]
|
||||
|
||||
Options:
|
||||
-h --help print this message
|
||||
--version print current version of the program
|
||||
--debug on error, print debugging information
|
||||
-h --help print this message
|
||||
--version print current version of the program
|
||||
--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
|
||||
|
|
|
@ -15,12 +15,14 @@ def fetch_sources(http_session, url):
|
|||
from .hls import fetch_program_tracks
|
||||
from .www import parse_url
|
||||
|
||||
site, target_id = parse_url(url)
|
||||
site, program_id, slug = parse_url(url)
|
||||
|
||||
variants = 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:
|
||||
v_tracks, a_track, s_track = fetch_program_tracks(
|
||||
|
@ -41,7 +43,7 @@ def fetch_sources(http_session, url):
|
|||
raise ValueError
|
||||
|
||||
return Sources(
|
||||
program_meta,
|
||||
program,
|
||||
[Variant(key, source) for key, source in variants.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)
|
||||
|
||||
return Target(
|
||||
sources.program,
|
||||
sources.program.meta,
|
||||
VideoTrack(v_meta, v_url),
|
||||
AudioTrack(a_meta, a_url),
|
||||
SubtitlesTrack(s_meta, s_url) if s_meta else None,
|
||||
|
|
|
@ -19,9 +19,15 @@ Arguments:
|
|||
VARIANT the variant code [video quality version]
|
||||
|
||||
Options:
|
||||
-h --help print this message
|
||||
--version print current version of the program
|
||||
--debug on error, print debugging information
|
||||
-h --help print this message
|
||||
--version print current version of the program
|
||||
--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
|
||||
|
@ -174,7 +180,14 @@ def main():
|
|||
|
||||
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()
|
||||
|
||||
|
|
|
@ -91,10 +91,18 @@ class Rendition(NamedTuple):
|
|||
source: tuple[str, Optional[str]]
|
||||
|
||||
|
||||
class Program(NamedTuple):
|
||||
"""A program representation."""
|
||||
|
||||
id: str
|
||||
slug: str
|
||||
meta: ProgramMeta
|
||||
|
||||
|
||||
class Sources(NamedTuple):
|
||||
"""A program's sources."""
|
||||
|
||||
program: ProgramMeta
|
||||
program: Program
|
||||
variants: list[Variant]
|
||||
renditions: list[Rendition]
|
||||
|
||||
|
|
|
@ -2,13 +2,54 @@
|
|||
# This file is part of `delarte` (https://git.afpy.org/fcode/delarte.git)
|
||||
|
||||
"""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."""
|
||||
|
||||
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"\s+\((\d+)/(\d+)\)", sub_sequence_counter, s)
|
||||
|
||||
def build_file_name(program: Program) -> str:
|
||||
"""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)
|
||||
|
||||
return name
|
||||
|
||||
return build_file_name
|
||||
|
|
|
@ -24,6 +24,7 @@ def parse_url(url):
|
|||
if (_ := path.pop(0)) != "videos":
|
||||
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