1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 18:53:21 +01:00
gallery-dl/gallery_dl/__init__.py

111 lines
3.2 KiB
Python
Raw Normal View History

2015-04-05 17:15:27 +02:00
# -*- coding: utf-8 -*-
2016-03-06 21:00:42 +01:00
# Copyright 2014-2016 Mike Fährmann
2015-04-05 17:15:27 +02:00
#
# 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.
2014-10-12 21:56:44 +02:00
__author__ = "Mike Fährmann"
2016-03-06 21:00:42 +01:00
__copyright__ = "Copyright 2014-2016 Mike Fährmann"
2014-10-12 21:56:44 +02:00
2015-04-05 17:15:27 +02:00
__license__ = "GPLv2"
2015-12-03 02:31:23 +01:00
__version__ = "0.4.1"
2014-10-12 21:56:44 +02:00
__maintainer__ = "Mike Fährmann"
__email__ = "mike_faehrmann@web.de"
import os
import argparse
2016-07-21 13:13:53 +02:00
import json
2016-07-14 14:25:56 +02:00
from . import config, extractor, job
2014-10-12 21:56:44 +02:00
def build_cmdline_parser():
2015-04-05 17:15:27 +02:00
parser = argparse.ArgumentParser(
2014-10-12 21:56:44 +02:00
description='Download images from various sources')
2015-04-05 17:15:27 +02:00
parser.add_argument(
"-c", "--config",
2015-11-14 17:22:56 +01:00
metavar="CFG", dest="cfgfiles", action="append",
help="additional configuration files",
2015-04-05 17:15:27 +02:00
)
parser.add_argument(
"-d", "--dest",
metavar="DEST",
2015-11-14 17:22:56 +01:00
help="destination directory",
2015-04-05 17:15:27 +02:00
)
2015-11-10 01:56:31 +01:00
parser.add_argument(
"-o", "--option",
2015-11-12 02:26:27 +01:00
metavar="OPT", action="append", default=[],
2016-07-21 13:13:53 +02:00
help="additional 'key=value' option values",
2015-11-10 01:56:31 +01:00
)
2015-12-10 02:14:28 +01:00
parser.add_argument(
"-g", "--get-urls", dest="list_urls", action="store_true",
help="print download urls",
)
parser.add_argument(
"--list-modules", dest="list_modules", action="store_true",
help="print a list of available modules/supported sites",
)
parser.add_argument(
2015-12-10 02:14:28 +01:00
"--list-keywords", dest="list_keywords", action="store_true",
2015-11-14 17:22:56 +01:00
help="print a list of available keywords for the given URLs",
)
2015-04-05 17:15:27 +02:00
parser.add_argument(
"urls",
nargs="*", metavar="URL",
2015-04-05 17:15:27 +02:00
help="url to download images from"
)
return parser
2014-10-12 21:56:44 +02:00
2016-07-21 13:13:53 +02:00
def parse_option(opt):
try:
key, value = opt.split("=", 1)
try:
value = json.loads(value)
except json.decoder.JSONDecodeError:
pass
config.set(key.split("."), value)
except ValueError:
print("Invalid 'key=value' pair:", opt)
2014-10-12 21:56:44 +02:00
def main():
try:
config.load()
parser = build_cmdline_parser()
args = parser.parse_args()
2015-11-14 17:22:56 +01:00
if args.cfgfiles:
config.load(*args.cfgfiles, strict=True)
2015-11-14 16:07:10 +01:00
if args.dest:
config.set(("base-directory",), args.dest)
for opt in args.option:
2016-07-21 13:13:53 +02:00
parse_option(opt)
if args.list_modules:
for module_name in extractor.modules:
print(module_name)
else:
if not args.urls:
parser.error("the following arguments are required: URL")
2016-07-21 13:13:53 +02:00
2015-12-10 02:14:28 +01:00
if args.list_urls:
2016-07-14 14:25:56 +02:00
jobtype = job.UrlJob
2015-12-10 02:14:28 +01:00
elif args.list_keywords:
2016-07-14 14:25:56 +02:00
jobtype = job.KeywordJob
2015-12-10 02:14:28 +01:00
else:
2016-07-14 14:25:56 +02:00
jobtype = job.DownloadJob
2016-07-21 13:13:53 +02:00
for url in args.urls:
2016-07-14 14:57:42 +02:00
try:
jobtype(url).run()
except exception.NoExtractorError:
print("No suitable extractor found for URL '", url, "'", sep="")
except exception.AuthenticationError:
print("Authentication failed. Please provide a valid "
"username/password pair.")
2016-07-21 13:13:53 +02:00
except KeyboardInterrupt:
2015-06-28 12:45:52 +02:00
print("\nKeyboardInterrupt")