1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# -*- coding: utf-8 -*-
# Copyright 2014-2025 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Common classes and constants used by downloader modules."""
import os
from .. import config, util
_config = config._config
class DownloaderBase():
"""Base class for downloaders"""
scheme = ""
def __init__(self, job):
extractor = job.extractor
self.log = job.get_logger("downloader." + self.scheme)
opts = self._extractor_config(extractor)
if opts:
self.opts = opts
self.config = self.config_opts
self.out = job.out
self.session = extractor.session
self.part = self.config("part", True)
self.partdir = self.config("part-directory")
if self.partdir:
self.partdir = util.expand_path(self.partdir)
os.makedirs(self.partdir, exist_ok=True)
proxies = self.config("proxy", util.SENTINEL)
if proxies is util.SENTINEL:
self.proxies = extractor._proxies
else:
self.proxies = util.build_proxy_map(proxies, self.log)
def config(self, key, default=None):
"""Interpolate downloader config value for 'key'"""
return config.interpolate(("downloader", self.scheme), key, default)
def config_opts(self, key, default=None, conf=_config):
if key in conf:
return conf[key]
value = self.opts.get(key, util.SENTINEL)
if value is not util.SENTINEL:
return value
return config.interpolate(("downloader", self.scheme), key, default)
def _extractor_config(self, extractor):
path = extractor._cfgpath
if not isinstance(path, list):
return self._extractor_opts(path[1], path[2])
opts = {}
for cat, sub in reversed(path):
popts = self._extractor_opts(cat, sub)
if popts:
opts.update(popts)
return opts
def _extractor_opts(self, category, subcategory):
cfg = config.get(("extractor",), category)
if not cfg:
return None
copts = cfg.get(self.scheme)
if copts:
if subcategory in cfg:
try:
sopts = cfg[subcategory].get(self.scheme)
if sopts:
opts = copts.copy()
opts.update(sopts)
return opts
except Exception:
self._report_config_error(subcategory, cfg[subcategory])
return copts
if subcategory in cfg:
try:
return cfg[subcategory].get(self.scheme)
except Exception:
self._report_config_error(subcategory, cfg[subcategory])
return None
def _report_config_error(self, subcategory, value):
config.log.warning("Subcategory '%s' set to '%s' instead of object",
subcategory, util.json_dumps(value).strip('"'))
def download(self, url, pathfmt):
"""Write data from 'url' into the file specified by 'pathfmt'"""
|