mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 10:42:34 +01:00
use new config-module in downloader
This commit is contained in:
parent
2026223ed1
commit
608d3193a9
@ -17,9 +17,7 @@ __email__ = "mike_faehrmann@web.de"
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
import configparser
|
from . import config, download
|
||||||
|
|
||||||
from .download import DownloadManager
|
|
||||||
|
|
||||||
def parse_cmdline_options():
|
def parse_cmdline_options():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
@ -41,18 +39,10 @@ def parse_cmdline_options():
|
|||||||
)
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
def parse_config_file(path):
|
|
||||||
config = configparser.ConfigParser(
|
|
||||||
interpolation=None,
|
|
||||||
)
|
|
||||||
config.optionxform = lambda opt: opt
|
|
||||||
config.read(os.path.expanduser(path))
|
|
||||||
return config
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
config.load()
|
||||||
opts = parse_cmdline_options()
|
opts = parse_cmdline_options()
|
||||||
conf = parse_config_file(opts.config)
|
dlmgr = download.DownloadManager(opts)
|
||||||
dlmgr = DownloadManager(opts, conf)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for url in opts.urls:
|
for url in opts.urls:
|
||||||
|
@ -12,14 +12,14 @@ import re
|
|||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
from .extractor.common import Message
|
from .extractor.common import Message
|
||||||
|
from . import config
|
||||||
|
|
||||||
class DownloadManager():
|
class DownloadManager():
|
||||||
|
|
||||||
def __init__(self, opts, config):
|
def __init__(self, opts):
|
||||||
self.opts = opts
|
self.opts = opts
|
||||||
self.config = config
|
|
||||||
self.modules = {}
|
self.modules = {}
|
||||||
self.extractors = ExtractorFinder(config)
|
self.extractors = ExtractorFinder()
|
||||||
|
|
||||||
def add(self, url):
|
def add(self, url):
|
||||||
job = DownloadJob(self, url)
|
job = DownloadJob(self, url)
|
||||||
@ -38,7 +38,7 @@ class DownloadManager():
|
|||||||
if self.opts.dest:
|
if self.opts.dest:
|
||||||
return self.opts.dest
|
return self.opts.dest
|
||||||
else:
|
else:
|
||||||
return self.config.get("general", "destination", fallback="/tmp/")
|
return config.get(("base-directory",), default="/tmp/")
|
||||||
|
|
||||||
|
|
||||||
class DownloadJob():
|
class DownloadJob():
|
||||||
@ -50,16 +50,14 @@ class DownloadJob():
|
|||||||
return
|
return
|
||||||
self.directory = mngr.get_base_directory()
|
self.directory = mngr.get_base_directory()
|
||||||
self.downloaders = {}
|
self.downloaders = {}
|
||||||
self.filename_fmt = mngr.config.get(
|
self.filename_fmt = config.get(
|
||||||
self.info["category"], "filename",
|
("extractor", self.info["category"], "filename"),
|
||||||
fallback=self.info["filename"]
|
default=self.info["filename"]
|
||||||
|
)
|
||||||
|
segments = config.get(
|
||||||
|
("extractor", self.info["category"], "directory"),
|
||||||
|
default=self.info["directory"]
|
||||||
)
|
)
|
||||||
try:
|
|
||||||
segments = mngr.config.get(
|
|
||||||
self.info["category"], "directory"
|
|
||||||
).split("/")
|
|
||||||
except Exception:
|
|
||||||
segments = self.info["directory"]
|
|
||||||
self.directory_fmt = os.path.join(*segments)
|
self.directory_fmt = os.path.join(*segments)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
@ -144,26 +142,23 @@ class DownloadJob():
|
|||||||
|
|
||||||
class ExtractorFinder():
|
class ExtractorFinder():
|
||||||
|
|
||||||
def __init__(self, config):
|
|
||||||
self.config = config
|
|
||||||
|
|
||||||
def get_for_url(self, url):
|
def get_for_url(self, url):
|
||||||
"""Get an extractor-instance suitable for 'url'"""
|
"""Get an extractor-instance suitable for 'url'"""
|
||||||
name, match = self.find_pattern_match(url)
|
name, match = self.find_pattern_match(url)
|
||||||
if match:
|
if match:
|
||||||
module = importlib.import_module(".extractor." + name, __package__)
|
module = importlib.import_module(".extractor." + name, __package__)
|
||||||
klass = getattr(module, module.info["extractor"])
|
klass = getattr(module, module.info["extractor"])
|
||||||
return klass(match, self.config), module.info
|
return klass(match, {}), module.info
|
||||||
else:
|
else:
|
||||||
print("no suitable extractor found")
|
print("no suitable extractor found")
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
def find_pattern_match(self, url):
|
def find_pattern_match(self, url):
|
||||||
"""Find a pattern, that matches 'url', and return the (category,match) tuple"""
|
"""Find a pattern that matches 'url' and return the (category,match) tuple"""
|
||||||
for category in self.config:
|
for category in config.get(("extractor",)):
|
||||||
for key, value in self.config[category].items():
|
patterns = config.get(("extractor", category, "pattern"), default=[])
|
||||||
if key.startswith("regex"):
|
for pattern in patterns:
|
||||||
match = re.match(value, url)
|
match = re.match(pattern, url)
|
||||||
if match:
|
if match:
|
||||||
return category, match
|
return category, match
|
||||||
for category, info in self.extractor_metadata():
|
for category, info in self.extractor_metadata():
|
||||||
|
Loading…
Reference in New Issue
Block a user