Merge branch 'options'
This commit is contained in:
@@ -17,7 +17,7 @@ __email__ = "mike_faehrmann@web.de"
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
from . import config, download
|
from . import config, jobs
|
||||||
|
|
||||||
def parse_cmdline_options():
|
def parse_cmdline_options():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
@@ -32,6 +32,15 @@ def parse_cmdline_options():
|
|||||||
metavar="DEST",
|
metavar="DEST",
|
||||||
help="destination directory"
|
help="destination directory"
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-o", "--option",
|
||||||
|
metavar="OPT", action="append", default=[],
|
||||||
|
help="option value",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--list-keywords", dest="keywords", action="store_true",
|
||||||
|
help="print a list of available keywords",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"urls",
|
"urls",
|
||||||
nargs="+", metavar="URL",
|
nargs="+", metavar="URL",
|
||||||
@@ -41,11 +50,16 @@ def parse_cmdline_options():
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
config.load()
|
config.load()
|
||||||
opts = parse_cmdline_options()
|
args = parse_cmdline_options()
|
||||||
dlmgr = download.DownloadManager(opts)
|
for opt in args.option:
|
||||||
|
try:
|
||||||
|
key, value = opt.split("=", 1)
|
||||||
|
config.set(key.split("."), value)
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
jobtype = jobs.KeywordJob if args.keywords else jobs.DownloadJob
|
||||||
try:
|
try:
|
||||||
for url in opts.urls:
|
for url in args.urls:
|
||||||
dlmgr.add(url)
|
jobtype(url).run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\nKeyboardInterrupt")
|
print("\nKeyboardInterrupt")
|
||||||
|
|||||||
@@ -1 +1,27 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright 2015 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.
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
def find(scheme):
|
||||||
|
"""Return downloader class suitable for handling the given scheme"""
|
||||||
|
try:
|
||||||
|
return _cache[scheme]
|
||||||
|
except KeyError:
|
||||||
|
try:
|
||||||
|
module = importlib.import_module("."+scheme, __package__)
|
||||||
|
klass = getattr(module, "Downloader")
|
||||||
|
_cache[scheme] = klass
|
||||||
|
return klass
|
||||||
|
except ImportError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
# internals
|
||||||
|
|
||||||
|
_cache = {}
|
||||||
|
|||||||
@@ -8,45 +8,17 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import importlib
|
from . import config, extractor, downloader, text
|
||||||
from . import config, extractor, text
|
|
||||||
from .extractor.common import Message
|
from .extractor.common import Message
|
||||||
|
|
||||||
class DownloadManager():
|
|
||||||
|
|
||||||
def __init__(self, opts):
|
|
||||||
self.opts = opts
|
|
||||||
self.modules = {}
|
|
||||||
|
|
||||||
def add(self, url):
|
|
||||||
job = DownloadJob(self, url)
|
|
||||||
job.run()
|
|
||||||
|
|
||||||
def get_downloader_module(self, scheme):
|
|
||||||
"""Return a downloader module suitable for 'scheme'"""
|
|
||||||
module = self.modules.get(scheme)
|
|
||||||
if module is None:
|
|
||||||
module = importlib.import_module(".downloader."+scheme, __package__)
|
|
||||||
self.modules[scheme] = module
|
|
||||||
return module
|
|
||||||
|
|
||||||
def get_base_directory(self):
|
|
||||||
"""Return the base-destination-directory for downloads"""
|
|
||||||
if self.opts.dest:
|
|
||||||
return self.opts.dest
|
|
||||||
else:
|
|
||||||
return config.get(("base-directory",), default="/tmp/")
|
|
||||||
|
|
||||||
|
|
||||||
class DownloadJob():
|
class DownloadJob():
|
||||||
|
|
||||||
def __init__(self, mngr, url):
|
def __init__(self, url):
|
||||||
self.mngr = mngr
|
|
||||||
self.extractor, self.info = extractor.find(url)
|
self.extractor, self.info = extractor.find(url)
|
||||||
if self.extractor is None:
|
if self.extractor is None:
|
||||||
print(url, ": No extractor found", sep="", file=sys.stderr)
|
print(url, ": No extractor found", sep="", file=sys.stderr)
|
||||||
return
|
return
|
||||||
self.directory = mngr.get_base_directory()
|
self.directory = self.get_base_directory()
|
||||||
self.downloaders = {}
|
self.downloaders = {}
|
||||||
self.filename_fmt = config.get(
|
self.filename_fmt = config.get(
|
||||||
("extractor", self.info["category"], "filename"),
|
("extractor", self.info["category"], "filename"),
|
||||||
@@ -91,15 +63,15 @@ class DownloadJob():
|
|||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
self.print_skip(path)
|
self.print_skip(path)
|
||||||
return
|
return
|
||||||
downloader = self.get_downloader(url)
|
dlinstance = self.get_downloader(url)
|
||||||
self.print_start(path)
|
self.print_start(path)
|
||||||
tries = downloader.download(url, path)
|
tries = dlinstance.download(url, path)
|
||||||
self.print_success(path, tries)
|
self.print_success(path, tries)
|
||||||
|
|
||||||
def set_directory(self, msg):
|
def set_directory(self, msg):
|
||||||
"""Set and create the target directory for downloads"""
|
"""Set and create the target directory for downloads"""
|
||||||
self.directory = os.path.join(
|
self.directory = os.path.join(
|
||||||
self.mngr.get_base_directory(),
|
self.get_base_directory(),
|
||||||
self.directory_fmt.format(**{
|
self.directory_fmt.format(**{
|
||||||
key: text.clean_path(value) for key, value in msg[1].items()
|
key: text.clean_path(value) for key, value in msg[1].items()
|
||||||
})
|
})
|
||||||
@@ -112,12 +84,17 @@ class DownloadJob():
|
|||||||
scheme = url[:pos] if pos != -1 else "http"
|
scheme = url[:pos] if pos != -1 else "http"
|
||||||
if scheme == "https":
|
if scheme == "https":
|
||||||
scheme = "http"
|
scheme = "http"
|
||||||
downloader = self.downloaders.get(scheme)
|
instance = self.downloaders.get(scheme)
|
||||||
if downloader is None:
|
if instance is None:
|
||||||
module = self.mngr.get_downloader_module(scheme)
|
klass = downloader.find(scheme)
|
||||||
downloader = module.Downloader()
|
instance = klass()
|
||||||
self.downloaders[scheme] = downloader
|
self.downloaders[scheme] = instance
|
||||||
return downloader
|
return instance
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_base_directory():
|
||||||
|
"""Return the base-destination-directory for downloads"""
|
||||||
|
return config.get(("base-directory",), default="/tmp/")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def print_start(path):
|
def print_start(path):
|
||||||
@@ -136,3 +113,32 @@ class DownloadJob():
|
|||||||
if tries == 0:
|
if tries == 0:
|
||||||
print("\r", end="")
|
print("\r", end="")
|
||||||
print("\r\033[1;32m", path, "\033[0m", sep="")
|
print("\r\033[1;32m", path, "\033[0m", sep="")
|
||||||
|
|
||||||
|
|
||||||
|
class KeywordJob():
|
||||||
|
|
||||||
|
def __init__(self, url):
|
||||||
|
self.extractor, self.info = extractor.find(url)
|
||||||
|
if self.extractor is None:
|
||||||
|
print(url, ": No extractor found", sep="", file=sys.stderr)
|
||||||
|
return
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
"""Execute/Run the download job"""
|
||||||
|
if self.extractor is None:
|
||||||
|
return
|
||||||
|
for msg in self.extractor:
|
||||||
|
if msg[0] == Message.Url:
|
||||||
|
print("Keywords for filenames:")
|
||||||
|
self.print_keywords(msg[2])
|
||||||
|
return
|
||||||
|
elif msg[0] == Message.Directory:
|
||||||
|
print("Keywords for directory names:")
|
||||||
|
self.print_keywords(msg[1])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def print_keywords(keywords):
|
||||||
|
offset = max(map(len, keywords.keys())) + 1
|
||||||
|
for key, value in sorted(keywords.items()):
|
||||||
|
print(key, ":", " "*(offset-len(key)), value, sep="")
|
||||||
|
print()
|
||||||
Reference in New Issue
Block a user