1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-26 04:32:51 +01:00
gallery-dl/gallery_dl/__init__.py

119 lines
3.6 KiB
Python
Raw Normal View History

2015-04-05 17:15:27 +02:00
# -*- coding: utf-8 -*-
2017-01-30 19:40:15 +01:00
# Copyright 2014-2017 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.
2016-10-04 14:33:50 +02:00
from __future__ import unicode_literals, print_function
2017-01-30 19:40:15 +01:00
__author__ = "Mike Fährmann"
__copyright__ = "Copyright 2014-2017 Mike Fährmann"
__license__ = "GPLv2"
2014-10-12 21:56:44 +02:00
__maintainer__ = "Mike Fährmann"
2017-01-30 19:40:15 +01:00
__email__ = "mike_faehrmann@web.de"
2014-10-12 21:56:44 +02:00
2016-08-06 13:40:49 +02:00
import sys
2016-10-04 14:33:50 +02:00
if sys.hexversion < 0x3030000:
print("Python 3.3+ required", file=sys.stderr)
sys.exit(1)
2017-03-07 23:50:19 +01:00
import logging
2017-03-23 16:29:40 +01:00
from . import version, config, option, extractor, job, exception
2014-10-12 21:56:44 +02:00
__version__ = version.__version__
2017-03-11 01:47:57 +01:00
log = logging.getLogger("gallery-dl")
2017-01-30 19:40:15 +01:00
2017-03-07 23:50:19 +01:00
def initialize_logging():
# convert levelnames to lowercase
for level in (10, 20, 30, 40, 50):
name = logging.getLevelName(level)
logging.addLevelName(level, name.lower())
2017-03-11 01:47:57 +01:00
# setup basic logging to stderr
formatter = logging.Formatter("[%(name)s][%(levelname)s] %(message)s")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(logging.INFO)
root.addHandler(handler)
2017-03-07 23:50:19 +01:00
2016-12-04 16:11:54 +01:00
def sanatize_input(file):
for line in file:
line = line.strip()
if line:
yield line
2017-01-30 19:40:15 +01:00
2014-10-12 21:56:44 +02:00
def main():
try:
2017-03-07 23:50:19 +01:00
initialize_logging()
2017-03-23 16:29:40 +01:00
parser = option.build_parser()
args = parser.parse_args()
logging.getLogger().setLevel(args.loglevel)
2017-04-25 17:09:10 +02:00
if args.load_config:
config.load()
2015-11-14 17:22:56 +01:00
if args.cfgfiles:
config.load(*args.cfgfiles, strict=True)
2017-03-08 16:57:42 +01:00
if args.yamlfiles:
config.load(*args.yamlfiles, format="yaml", strict=True)
2017-03-23 16:29:40 +01:00
for key, value in args.options:
config.set(key, value)
if args.list_modules:
for module_name in extractor.modules:
print(module_name)
2016-09-14 09:51:01 +02:00
elif args.list_extractors:
for extr in extractor.extractors():
print(extr.__name__)
if extr.__doc__:
print(extr.__doc__)
if hasattr(extr, "test") and extr.test:
print("Example:", extr.test[0][0])
print()
else:
2016-12-04 16:11:54 +01:00
if not args.urls and not args.inputfile:
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
jobtype.maxdepth = args.list_urls
2015-12-10 02:14:28 +01:00
elif args.list_keywords:
2016-07-14 14:25:56 +02:00
jobtype = job.KeywordJob
elif args.list_data:
jobtype = job.DataJob
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
2016-12-04 16:11:54 +01:00
urls = args.urls
if args.inputfile:
try:
if args.inputfile == "-":
file = sys.stdin
else:
file = open(args.inputfile)
import itertools
urls = itertools.chain(urls, sanatize_input(file))
2017-03-16 03:47:08 +01:00
except OSError as err:
log.error(err)
2016-12-04 16:11:54 +01:00
for url in urls:
2016-07-14 14:57:42 +02:00
try:
log.debug("Starting %s for '%s'", jobtype.__name__, url)
2016-07-14 14:57:42 +02:00
jobtype(url).run()
except exception.NoExtractorError:
2017-03-11 01:47:57 +01:00
log.error("No suitable extractor found for '%s'", url)
2017-02-25 23:53:31 +01:00
except KeyboardInterrupt:
2016-08-06 13:40:49 +02:00
print("\nKeyboardInterrupt", file=sys.stderr)
2016-08-05 10:25:31 +02:00
except BrokenPipeError:
pass
except IOError as err:
2016-08-05 10:25:31 +02:00
import errno
if err.errno != errno.EPIPE:
2016-08-05 10:25:31 +02:00
raise