1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

use new config-module in downloader

This commit is contained in:
Mike Fährmann 2015-10-05 13:26:38 +02:00
parent 2026223ed1
commit 608d3193a9
2 changed files with 20 additions and 35 deletions

View File

@ -17,9 +17,7 @@ __email__ = "mike_faehrmann@web.de"
import os
import sys
import argparse
import configparser
from .download import DownloadManager
from . import config, download
def parse_cmdline_options():
parser = argparse.ArgumentParser(
@ -41,18 +39,10 @@ def parse_cmdline_options():
)
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():
config.load()
opts = parse_cmdline_options()
conf = parse_config_file(opts.config)
dlmgr = DownloadManager(opts, conf)
dlmgr = download.DownloadManager(opts)
try:
for url in opts.urls:

View File

@ -12,14 +12,14 @@ import re
import importlib
from .extractor.common import Message
from . import config
class DownloadManager():
def __init__(self, opts, config):
def __init__(self, opts):
self.opts = opts
self.config = config
self.modules = {}
self.extractors = ExtractorFinder(config)
self.extractors = ExtractorFinder()
def add(self, url):
job = DownloadJob(self, url)
@ -38,7 +38,7 @@ class DownloadManager():
if self.opts.dest:
return self.opts.dest
else:
return self.config.get("general", "destination", fallback="/tmp/")
return config.get(("base-directory",), default="/tmp/")
class DownloadJob():
@ -50,16 +50,14 @@ class DownloadJob():
return
self.directory = mngr.get_base_directory()
self.downloaders = {}
self.filename_fmt = mngr.config.get(
self.info["category"], "filename",
fallback=self.info["filename"]
self.filename_fmt = config.get(
("extractor", self.info["category"], "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)
def run(self):
@ -144,26 +142,23 @@ class DownloadJob():
class ExtractorFinder():
def __init__(self, config):
self.config = config
def get_for_url(self, url):
"""Get an extractor-instance suitable for 'url'"""
name, match = self.find_pattern_match(url)
if match:
module = importlib.import_module(".extractor." + name, __package__)
klass = getattr(module, module.info["extractor"])
return klass(match, self.config), module.info
return klass(match, {}), module.info
else:
print("no suitable extractor found")
return None, None
def find_pattern_match(self, url):
"""Find a pattern, that matches 'url', and return the (category,match) tuple"""
for category in self.config:
for key, value in self.config[category].items():
if key.startswith("regex"):
match = re.match(value, url)
"""Find a pattern that matches 'url' and return the (category,match) tuple"""
for category in config.get(("extractor",)):
patterns = config.get(("extractor", category, "pattern"), default=[])
for pattern in patterns:
match = re.match(pattern, url)
if match:
return category, match
for category, info in self.extractor_metadata():