Merge pull request 'docopt-ng' (#22) from docopt-ng into stable

Reviewed-on: #22
This commit is contained in:
Barbagus 2023-01-03 08:45:46 +00:00
commit 8d216215dd
4 changed files with 57 additions and 86 deletions

View File

@ -48,12 +48,27 @@ Now you can run the script
$ python3 -m delarte --help
or
$ delarte --help
ArteTV downloader.
delarte - ArteTV downloader.
usage: delarte [-h|--help] - print this message
or: delarte program_page_url - show available versions
or: delarte program_page_url version - show available resolutions
or: delarte program_page_url version resolution - download the given video
Usage:
delarte (-h | --help)
delarte --version
delarte [options] URL
delarte [options] URL RENDITION
delarte [options] URL RENDITION VARIANT
Download a video from ArteTV streaming service. Omit RENDITION and/or
VARIANT to print the list of available values.
Arguments:
URL the URL from ArteTV website
RENDITION the rendition code [audio/subtitles language combination]
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
```
🔧 How it works

View File

@ -12,7 +12,8 @@ dynamic = ["version", "description"]
dependencies = [
"m3u8",
"webvtt-py",
"requests"
"requests",
"docopt-ng"
]
[project.urls]

View File

@ -3,18 +3,34 @@
"""delarte - ArteTV downloader.
usage: delarte [-h|--help] - print this message
or: delarte program_page_url - show available versions
or: delarte program_page_url version - show available resolutions
or: delarte program_page_url version resolution - download the given video
Usage:
delarte (-h | --help)
delarte --version
delarte [options] URL
delarte [options] URL RENDITION
delarte [options] URL RENDITION VARIANT
Download a video from ArteTV streaming service. Omit RENDITION and/or
VARIANT to print the list of available values.
Arguments:
URL the URL from ArteTV website
RENDITION the rendition code [audio/subtitles language combination]
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
"""
import sys
import time
import docopt
import requests
from . import cli, download_source, error, fetch_sources, naming, www
from . import __version__, download_source, error, fetch_sources, naming, www
class Abort(error.ModuleError):
@ -82,7 +98,7 @@ def _select_rendition(sources, rendition_code):
filtered = [s for s in sources if s.rendition.code == rendition_code]
if not filtered:
print(f"{rendition_code!r} is not a valid version, possible values are:")
print(f"{rendition_code!r} is not a valid rendition code, possible values are:")
_print_renditions(sources)
raise Abort()
@ -96,7 +112,7 @@ def _select_variant(sources, variant_code):
filtered = [s for s in sources if s.variant.code == variant_code]
if not filtered:
print(f"{variant_code!r} is not a valid resolution, possible values are:")
print(f"{variant_code!r} is not a valid variant code, possible values are:")
_print_variants(sources)
raise Abort()
@ -135,33 +151,28 @@ def create_progress():
def main():
"""CLI command."""
parser = cli.Parser()
args = parser.get_args_as_list()
if not args or args[0] == "-h" or args[0] == "--help":
print(__doc__)
return 0
args = docopt.docopt(__doc__, sys.argv[1:], version=__version__)
try:
target_id, www_lang = www.parse_url(args.pop(0))
target_id, www_lang = www.parse_url(args["URL"])
http_session = requests.sessions.Session()
sources = fetch_sources(http_session, target_id, www_lang)
if not args:
print(f"Available versions:")
if not args["RENDITION"]:
print(f"Available renditions:")
_print_renditions(sources)
return 0
sources = _select_rendition(sources, args.pop(0))
sources = _select_rendition(sources, args["RENDITION"])
if not args:
print(f"Available resolutions:")
if not args["VARIANT"]:
print(f"Available variants:")
_print_variants(sources)
return 0
sources = _select_variant(sources, args.pop(0))
sources = _select_variant(sources, args["VARIANT"])
file_names = [
naming.build_file_name(s, i, len(sources)) for i, s in enumerate(sources, 1)
@ -191,12 +202,14 @@ def main():
except error.ModuleError as e:
print(str(e))
# print(repr(e))
if args["--debug"]:
print(repr(e))
return 1
except requests.HTTPError as e:
print("Network error.")
# print(str(e))
if args["--debug"]:
print(str(e))
return 1

View File

@ -1,58 +0,0 @@
# License: GNU AGPL v3: http://www.gnu.org/licenses/
# This file is part of `delarte` (https://git.afpy.org/fcode/delarte.git)
"""
usage: delarte [-h|--help] - print this message
or: delarte program_page_url - show available versions
or: delarte program_page_url version - show available video resolutions
or: delarte program_page_url version resolution - download the given video
"""
import argparse
class Parser(argparse.ArgumentParser):
"""Parser responsible for parsing CLI arguments."""
def __init__(self):
"""Generate a parser."""
super().__init__(
description="downloads Arte's videos with subtitles",
epilog=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
self.add_argument(
"url",
help="url of Arte movie's webpage",
action="store",
type=str,
nargs="?",
)
self.add_argument(
"version",
help="one of the language code proposed by Arte",
action="store",
type=str,
nargs="?",
)
self.add_argument(
"resolution",
help="video resolution",
action="store",
type=str,
nargs="?",
)
def get_args_as_list(self):
"""Get arguments from CLI as a list.
Returns:
List: ordered list of arguments, None removed
"""
args_namespace = self.parse_args()
args_list = [
args_namespace.url,
args_namespace.version,
args_namespace.resolution,
]
return [arg for arg in args_list if arg is not None]