1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-30 23:24:16 +01:00
svtplay-dl/lib/svtplay_dl/utils/parser.py

507 lines
19 KiB
Python
Raw Normal View History

2018-01-14 00:35:28 +01:00
import argparse
import logging
2019-08-25 00:40:39 +02:00
import os
import platform
2018-01-14 00:35:28 +01:00
from yaml import safe_load
2018-05-13 13:06:45 +02:00
configdata = None
if platform.system() == "Windows":
APPDATA = os.environ["APPDATA"]
CONFIGFILE = os.path.join(APPDATA, "svtplay-dl", "svtplay-dl.yaml")
else:
CONFIGFILE = os.path.expanduser("~/.svtplay-dl.yaml")
FILENAME = "{title}.s{season}e{episode}.{episodename}-{id}-{service}.{ext}"
2018-01-14 00:35:28 +01:00
2019-08-25 00:33:51 +02:00
class Options:
2018-01-30 21:43:19 +01:00
"""
Options used when invoking the script from another Python script.
Simple container class used when calling get_media() from another Python
script. The variables corresponds to the command line parameters parsed
in main() when the script is called directly.
When called from a script there are a few more things to consider:
* Logging is done to 'log'. main() calls setup_log() which sets the
logging to either stdout or stderr depending on the silent level.
A user calling get_media() directly can either also use setup_log()
or configure the log manually.
* Progress information is printed to 'progress_stream' which defaults to
sys.stderr but can be changed to any stream.
* Many errors results in calls to system.exit() so catch 'SystemExit'-
Exceptions to prevent the entire application from exiting if that happens.
"""
def __init__(self):
2018-05-13 13:06:45 +02:00
self.default = {}
def set(self, key, value):
self.default[key] = value
def get(self, key):
if key in self.default:
return self.default[key]
2021-12-18 20:18:25 +01:00
return None
2018-05-13 13:06:45 +02:00
def get_variable(self):
return self.default
2018-01-30 21:43:19 +01:00
def set_variable(self, value):
self.default = value
2018-01-30 21:43:19 +01:00
def gen_parser(version="unknown"):
2018-01-14 00:35:28 +01:00
parser = argparse.ArgumentParser(prog="svtplay-dl")
general = parser.add_argument_group()
2021-02-28 22:05:15 +01:00
general.add_argument("--version", action="version", version=f"%(prog)s {version}")
general.add_argument("-o", "--output", metavar="output", default=None, help="outputs to the given filename or folder")
general.add_argument("--filename", metavar="filename", default=FILENAME, help=f"filename format {FILENAME}")
2019-08-25 00:27:31 +02:00
general.add_argument(
2020-12-26 13:10:56 +01:00
"--subfolder",
action="store_true",
default=False,
help="Create a subfolder titled as the show, non-series gets in folder movies",
)
general.add_argument("--config", dest="configfile", metavar="configfile", default=CONFIGFILE, help="Specify configuration file")
general.add_argument("-f", "--force", action="store_true", dest="force", default=False, help="overwrite if file exists already")
general.add_argument("-r", "--resume", action="store_true", dest="resume", default=False, help="resume a download (RTMP obsolete)")
general.add_argument("-l", "--live", action="store_true", dest="live", default=False, help="enable for live streams (RTMP based ones)")
general.add_argument("-c", "--capture_time", default=-1, type=int, metavar="capture_time", help="define capture time in minutes of a live stream")
general.add_argument("-s", "--silent", action="store_true", dest="silent", default=False, help="be less verbose")
2019-08-25 00:27:31 +02:00
general.add_argument(
2020-12-26 13:10:56 +01:00
"--silent-semi",
action="store_true",
dest="silent_semi",
default=False,
help="only show a message when the file is downloaded",
2019-08-25 00:27:31 +02:00
)
2018-01-14 00:35:28 +01:00
general.add_argument("-u", "--username", default=None, help="username")
general.add_argument("-p", "--password", default=None, help="password")
2019-08-25 00:27:31 +02:00
general.add_argument(
2020-12-26 13:10:56 +01:00
"-t",
"--thumbnail",
action="store_true",
dest="thumbnail",
default=False,
help="download thumbnail from the site if available",
2019-08-25 00:27:31 +02:00
)
general.add_argument(
2020-12-26 13:10:56 +01:00
"-g",
"--get-url",
action="store_true",
dest="get_url",
default=False,
help="do not download any video, but instead print the URL.",
2019-08-25 00:27:31 +02:00
)
general.add_argument(
"--get-only-episode-url",
action="store_true",
dest="get_only_episode_url",
default=False,
help="do not get video URLs, only print the episode URL.",
)
general.add_argument(
2020-12-26 13:10:56 +01:00
"--dont-verify-ssl-cert",
action="store_false",
dest="ssl_verify",
default=True,
help="Don't attempt to verify SSL certificates.",
2019-08-25 00:27:31 +02:00
)
general.add_argument(
2020-12-26 13:10:56 +01:00
"--http-header",
dest="http_headers",
default=None,
metavar="header1=value;header2=value2",
help="A header to add to each HTTP request.",
2019-08-25 00:27:31 +02:00
)
general.add_argument(
2020-12-26 13:10:56 +01:00
"--cookies",
dest="cookies",
default=None,
metavar="cookie1=value;cookie2=value2",
help="A cookies to add to each HTTP request.",
)
2019-08-25 00:27:31 +02:00
general.add_argument(
2020-12-26 13:10:56 +01:00
"--exclude",
dest="exclude",
default=None,
metavar="WORD1,WORD2,...",
help="exclude videos with the WORD(s) in the filename. comma separated.",
2019-08-25 00:27:31 +02:00
)
general.add_argument("--after-date", dest="after_date", default=None, metavar="yyyy-MM-dd", help="only videos published on or after this date")
2019-08-25 00:27:31 +02:00
general.add_argument(
"--proxy",
dest="proxy",
default=None,
metavar="proxy",
help="Use the specified HTTP/HTTPS/SOCKS proxy. To enable experimental "
"SOCKS proxy, specify a proper scheme. For example "
"socks5://127.0.0.1:1080/.",
)
general.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help="explain what is going on")
general.add_argument("--nfo", action="store_true", dest="nfo", default=False, help="create a NFO file")
general.add_argument("--force-nfo", action="store_true", dest="force_nfo", default=False, help="download only NFO if used with --nfo")
general.add_argument(
2020-12-26 13:10:56 +01:00
"--only-audio",
action="store_true",
dest="only_audio",
default=False,
help="only download audio if audio and video is seperated",
)
general.add_argument(
2020-12-26 13:10:56 +01:00
"--only-video",
action="store_true",
dest="only_video",
default=False,
help="only download video if audio and video is seperated",
)
2018-01-14 00:35:28 +01:00
quality = parser.add_argument_group("Quality")
2019-08-25 00:27:31 +02:00
quality.add_argument(
"-q",
"--quality",
default=0,
metavar="quality",
help="choose what format to download based on bitrate / video resolution." "it will download the best format by default",
2019-08-25 00:27:31 +02:00
)
quality.add_argument(
2020-12-26 13:10:56 +01:00
"-Q",
"--flexible-quality",
default=0,
metavar="amount",
dest="flexibleq",
help="allow given quality (as above) to differ by an amount",
2019-08-25 00:27:31 +02:00
)
quality.add_argument("-P", "--preferred", default=None, metavar="preferred", help="preferred download method (dash, hls, or http)")
quality.add_argument("--list-quality", dest="list_quality", action="store_true", default=False, help="list the quality for a video")
2019-08-25 00:27:31 +02:00
quality.add_argument(
"--stream-priority",
dest="stream_prio",
default=None,
metavar="dash,hls,http",
2019-08-25 00:27:31 +02:00
help="If two streams have the same quality, choose the one you prefer",
)
quality.add_argument(
"--format-preferred",
dest="format_preferred",
default=None,
metavar="h264,h264-51",
help="Choose the format you prefer, --list-quality to show which one to choose from",
)
quality.add_argument(
"--audio-language",
dest="audio_language",
default=None,
help="Choose the language of the audio (it can override the default one), --list-quality to show which one to choose from",
)
quality.add_argument(
"--audio-role",
dest="audio_role",
default=None,
help="Choose the role of the audio (it can override the default one), --list-quality to show which one to choose from",
)
2022-05-13 08:36:34 +02:00
quality.add_argument(
"--resolution",
dest="resolution",
default=None,
help="Choose what video resolution to download e.g. 480,720,1080. comma seperated",
)
2018-01-14 00:35:28 +01:00
subtitle = parser.add_argument_group("Subtitle")
2019-08-25 00:27:31 +02:00
subtitle.add_argument(
2020-12-26 13:10:56 +01:00
"-S",
"--subtitle",
action="store_true",
dest="subtitle",
default=False,
help="download subtitle from the site if available. both merged and separately stored if used with -M",
2019-08-25 00:27:31 +02:00
)
subtitle.add_argument(
"-M",
"--merge-subtitle",
action="store_true",
dest="merge_subtitle",
default=False,
help="merge subtitle with video/audio file with corresponding ISO639-3 language code. also saved separately if used with -S",
2019-08-25 00:27:31 +02:00
)
subtitle.add_argument(
2020-12-26 13:10:56 +01:00
"--force-subtitle",
dest="force_subtitle",
default=False,
action="store_true",
help="download only subtitle if its used with -S",
2019-08-25 00:27:31 +02:00
)
subtitle.add_argument(
2020-12-26 13:10:56 +01:00
"--require-subtitle",
dest="require_subtitle",
default=False,
action="store_true",
help="download only if a subtitle is available",
2019-08-25 00:27:31 +02:00
)
subtitle.add_argument(
2020-12-26 13:10:56 +01:00
"--all-subtitles",
dest="get_all_subtitles",
default=False,
action="store_true",
help="Download all available subtitles for the video",
2019-08-25 00:27:31 +02:00
)
subtitle.add_argument(
2020-12-26 13:10:56 +01:00
"--raw-subtitles",
dest="get_raw_subtitles",
default=False,
action="store_true",
help="also download the subtitles in their native format",
2019-08-25 00:27:31 +02:00
)
subtitle.add_argument(
"--convert-subtitle-colors",
dest="convert_subtitle_colors",
default=False,
action="store_true",
help='converts the color information in subtitles, to <font color=""> tags',
)
2018-01-14 00:35:28 +01:00
alleps = parser.add_argument_group("All")
alleps.add_argument("-A", "--all-episodes", action="store_true", dest="all_episodes", default=False, help="try to download all episodes")
alleps.add_argument("--all-last", dest="all_last", default=-1, type=int, metavar="NN", help="get last NN episodes instead of all episodes")
alleps.add_argument("--include-clips", dest="include_clips", default=False, action="store_true", help="include clips from websites when using -A")
2018-01-14 00:35:28 +01:00
cmorep = parser.add_argument_group("C More")
cmorep.add_argument("--cmore-operatorlist", dest="cmoreoperatorlist", default=False, action="store_true", help="show operatorlist for cmore")
cmorep.add_argument("--cmore-operator", dest="cmoreoperator", default=None, metavar="operator")
2018-01-14 00:35:28 +01:00
postprocessing = parser.add_argument_group("Post-processing")
postprocessing.add_argument("--no-remux", dest="no_remux", default=False, action="store_true", help="Do not automatically remux to mp4")
postprocessing.add_argument(
"--no-merge",
dest="no_merge",
default=False,
action="store_true",
help="Do not automatically merge video, audio and possibly also subtitle(s) together",
)
postprocessing.add_argument("--no-postprocess", dest="no_postprocess", default=False, action="store_true", help="Do not postprocess anything")
postprocessing.add_argument(
2021-06-04 20:02:00 +02:00
"--keep-original",
dest="keep_original",
default=False,
action="store_true",
help="Do postprocessing while also keeping original files",
)
2021-06-25 09:52:25 +02:00
postprocessing.add_argument(
"--output-format",
dest="output_format",
default="mp4",
choices=["mp4", "mkv"],
help="format you want resulting file in (mkv or mp4), mp4 is default",
)
2019-08-25 00:27:31 +02:00
parser.add_argument("urls", nargs="*")
2018-01-30 21:43:19 +01:00
return parser
def parser(version):
parser = gen_parser(version)
options = parser.parse_args()
2018-01-14 00:39:16 +01:00
return parser, options
2018-05-13 13:06:45 +02:00
def setup_defaults():
options = Options()
options.set("output", None)
options.set("subfolder", False)
options.set("configfile", CONFIGFILE)
2018-05-13 13:06:45 +02:00
options.set("resume", False)
options.set("live", False)
options.set("capture_time", -1)
options.set("silent", False)
options.set("force", False)
options.set("quality", 0)
options.set("flexibleq", 0)
options.set("list_quality", False)
options.set("other", None)
options.set("subtitle", False)
options.set("username", None)
options.set("password", None)
options.set("thumbnail", False)
options.set("all_episodes", False)
options.set("all_last", -1)
options.set("merge_subtitle", False)
options.set("force_subtitle", False)
options.set("require_subtitle", False)
options.set("get_all_subtitles", False)
options.set("get_raw_subtitles", False)
options.set("convert_subtitle_colors", False)
options.set("preferred", None)
options.set("verbose", False)
options.set("nfo", False)
2018-12-11 00:06:22 +01:00
options.set("force_nfo", False)
2018-05-13 13:06:45 +02:00
options.set("output_auto", False)
options.set("service", None)
options.set("cookies", None)
options.set("exclude", None)
options.set("after_date", None)
2018-05-13 13:06:45 +02:00
options.set("get_url", False)
2019-07-25 09:01:40 +02:00
options.set("get_only_episode_url", False)
2018-05-13 13:06:45 +02:00
options.set("ssl_verify", True)
options.set("http_headers", None)
options.set("format_preferred", None)
options.set("audio_language", None)
options.set("audio_role", None)
2022-05-13 08:36:34 +02:00
options.set("resolution", None)
2018-05-13 13:06:45 +02:00
options.set("stream_prio", None)
options.set("no_remux", False)
options.set("no_merge", False)
options.set("no_postprocess", False)
options.set("keep_original", False)
2018-05-13 13:06:45 +02:00
options.set("silent_semi", False)
options.set("proxy", None)
options.set("include_clips", False)
options.set("cmoreoperatorlist", False)
options.set("filename", FILENAME)
options.set("only_audio", False)
options.set("only_video", False)
2021-06-25 09:52:25 +02:00
options.set("output_format", "mp4")
return _special_settings(options)
def parsertoconfig(config, parser):
config.set("output", parser.output)
config.set("filename", parser.filename)
config.set("subfolder", parser.subfolder)
config.set("configfile", parser.configfile)
config.set("resume", parser.resume)
config.set("live", parser.live)
config.set("capture_time", parser.capture_time)
config.set("silent", parser.silent)
config.set("force", parser.force)
config.set("quality", parser.quality)
config.set("flexibleq", parser.flexibleq)
config.set("list_quality", parser.list_quality)
config.set("subtitle", parser.subtitle)
config.set("merge_subtitle", parser.merge_subtitle)
config.set("silent_semi", parser.silent_semi)
config.set("username", parser.username)
config.set("password", parser.password)
config.set("thumbnail", parser.thumbnail)
config.set("all_episodes", parser.all_episodes)
config.set("all_last", parser.all_last)
config.set("force_subtitle", parser.force_subtitle)
config.set("require_subtitle", parser.require_subtitle)
config.set("preferred", parser.preferred)
config.set("verbose", parser.verbose)
config.set("nfo", parser.nfo)
config.set("force_nfo", parser.force_nfo)
config.set("exclude", parser.exclude)
config.set("after_date", parser.after_date)
config.set("get_url", parser.get_url)
2019-07-25 09:01:40 +02:00
config.set("get_only_episode_url", parser.get_only_episode_url)
config.set("ssl_verify", parser.ssl_verify)
config.set("http_headers", parser.http_headers)
config.set("cookies", parser.cookies)
config.set("format_preferred", parser.format_preferred)
config.set("audio_role", parser.audio_role)
2022-05-13 08:36:34 +02:00
config.set("resolution", parser.resolution)
config.set("audio_language", parser.audio_language)
config.set("stream_prio", parser.stream_prio)
config.set("no_remux", parser.no_remux)
config.set("no_merge", parser.no_merge)
config.set("no_postprocess", parser.no_postprocess)
config.set("keep_original", parser.keep_original)
config.set("get_all_subtitles", parser.get_all_subtitles)
config.set("get_raw_subtitles", parser.get_raw_subtitles)
config.set("convert_subtitle_colors", parser.convert_subtitle_colors)
config.set("include_clips", parser.include_clips)
config.set("cmoreoperatorlist", parser.cmoreoperatorlist)
config.set("cmoreoperator", parser.cmoreoperator)
config.set("proxy", parser.proxy)
config.set("only_audio", parser.only_audio)
config.set("only_video", parser.only_video)
2021-06-25 09:52:25 +02:00
config.set("output_format", parser.output_format)
return _special_settings(config)
def _special_settings(config):
if config.get("require_subtitle"):
if config.get("merge_subtitle"):
config.set("merge_subtitle", True)
else:
config.set("subtitle", True)
if config.get("silent_semi"):
config.set("silent", True)
if config.get("proxy"):
config.set("proxy", config.get("proxy").replace("socks5", "socks5h", 1))
2018-05-20 19:05:35 +02:00
config.set("proxy", dict(http=config.get("proxy"), https=config.get("proxy")))
if config.get("get_only_episode_url"):
config.set("get_url", True)
if config.get("get_all_subtitles"):
config.set("subtitle", True)
return config
2018-05-13 13:06:45 +02:00
def merge(old, new):
if isinstance(new, list):
new = {list(i.keys())[0]: i[list(i.keys())[0]] for i in new}
config = setup_defaults()
if new:
for item in new:
if item in new:
if new[item] != config.get(item): # Check if new value is not a default one.
old[item] = new[item]
else:
old[item] = new[item]
options = Options()
options.set_variable(old)
return options
2018-05-13 13:06:45 +02:00
def readconfig(config, configfile, service=None, preset=None):
2018-05-13 13:06:45 +02:00
global configdata
if configfile and configdata is None:
try:
with open(configfile) as fd:
data = fd.read()
configdata = safe_load(data)
except PermissionError:
2021-12-18 21:36:16 +01:00
logging.error("Permission denied while reading config: %s", configfile)
2018-05-13 13:06:45 +02:00
if configdata is None:
return config
# migrate old service name to new
old_name_in_config = False
if "service" in configdata and "dplay" in configdata["service"]:
old_name_in_config = True
logging.warning("'dplay' have been renamed to 'discoveryplus'")
configdata["service"]["discoveryplus"] = configdata["service"].pop("dplay")
if "service" in configdata and "viaplay" in configdata["service"]:
old_name_in_config = True
logging.warning("'viaplay' have been renamed to 'viafree'")
configdata["service"]["viafree"] = configdata["service"].pop("viaplay")
if old_name_in_config:
logging.warning("Old service names still work at the moment. To avoid the warnings you need to rename the service(s) to the new name(s)")
2018-05-13 13:06:45 +02:00
if "default" in configdata:
config = merge(config.get_variable(), configdata["default"])
2018-05-13 13:06:45 +02:00
if service and "service" in configdata and service in configdata["service"]:
config = merge(config.get_variable(), configdata["service"][service])
2018-05-13 13:06:45 +02:00
if preset and "presets" in configdata and preset in configdata["presets"]:
config = merge(config.get_variable(), configdata["presets"][preset])
2018-05-13 13:06:45 +02:00
return _special_settings(config)