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

363 lines
12 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Copyright 2015-2017 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 sys
import time
2015-12-12 01:16:02 +01:00
import json
import hashlib
2017-03-28 13:12:44 +02:00
from . import extractor, downloader, config, util, output, exception
2015-11-24 19:47:51 +01:00
from .extractor.message import Message
2017-01-30 19:40:15 +01:00
2015-12-12 00:11:05 +01:00
class Job():
"""Base class for Job-types"""
2017-05-27 16:16:57 +02:00
ufile = None
2015-04-08 01:51:48 +02:00
2017-09-30 18:52:23 +02:00
def __init__(self, url, parent=None):
2017-02-25 23:53:31 +01:00
self.url = url
2015-11-21 00:30:31 +01:00
self.extractor = extractor.find(url)
2015-06-28 12:45:52 +02:00
if self.extractor is None:
2016-07-14 14:25:56 +02:00
raise exception.NoExtractorError(url)
2017-06-02 09:10:58 +02:00
self.extractor.log.debug(
2017-12-05 23:29:11 +01:00
"Using %s for '%s'", self.extractor.__class__.__name__, url)
2015-12-12 00:11:05 +01:00
# url predicates
predicates = [util.UniquePredicate()]
image = config.get(("_", "image"), {})
2017-09-09 14:51:31 +02:00
if "filter" in image:
predicates.append(util.FilterPredicate(image["filter"]))
if "range" in image:
pred = util.RangePredicate(image["range"])
if pred.lower > 1 and "filter" not in image:
pred.index += self.extractor.skip(pred.lower - 1)
predicates.append(pred)
self.pred_url = util.build_predicate(predicates)
# queue predicates
predicates = []
chapter = config.get(("_", "chapter"), {})
if "filter" in chapter:
predicates.append(util.FilterPredicate(chapter["filter"]))
2017-09-09 14:51:31 +02:00
if "range" in chapter:
predicates.append(util.RangePredicate(chapter["range"]))
self.pred_queue = util.build_predicate(predicates)
2017-09-30 18:52:23 +02:00
# category transfer
if parent and parent.extractor.categorytransfer:
self.extractor.category = parent.extractor.category
self.extractor.subcategory = parent.extractor.subcategory
def run(self):
"""Execute or run the job"""
try:
2017-03-11 01:47:57 +01:00
log = self.extractor.log
for msg in self.extractor:
2017-02-26 02:06:56 +01:00
self.dispatch(msg)
2017-02-25 23:53:31 +01:00
except exception.AuthenticationError:
2017-03-11 01:47:57 +01:00
log.error("Authentication failed. Please provide a valid "
"username/password pair.")
2017-02-25 23:53:31 +01:00
except exception.AuthorizationError:
2017-03-11 01:47:57 +01:00
log.error("You do not have permission to access the resource "
"at '%s'", self.url)
except exception.NotFoundError as exc:
res = str(exc) or "resource (gallery/image/user)"
2017-03-11 01:47:57 +01:00
log.error("The %s at '%s' does not exist", res, self.url)
except exception.HttpError as exc:
log.error("HTTP request failed: %s", exc)
except exception.FormatError as exc:
err, obj = exc.args
log.error("Applying %s format string failed: %s: %s",
obj, err.__class__.__name__, err)
except exception.FilterError as exc:
err = exc.args[0]
log.error("Evaluating filter expression failed: %s: %s",
err.__class__.__name__, err)
except exception.StopExtraction:
pass
except OSError as exc:
log.error("Unable to download data: %s", exc)
except Exception as exc:
log.error(("An unexpected error occurred: %s - %s. "
"Please run gallery-dl again with the --verbose flag, "
"copy its output and report this issue on "
"https://github.com/mikf/gallery-dl/issues ."),
exc.__class__.__name__, exc)
log.debug("Traceback", exc_info=True)
2017-02-26 02:06:56 +01:00
def dispatch(self, msg):
"""Call the appropriate message handler"""
2017-03-17 09:39:46 +01:00
if msg[0] == Message.Url:
_, url, kwds = msg
if self.pred_url(url, kwds):
self.update_kwdict(kwds)
self.handle_url(url, kwds)
2017-02-26 02:06:56 +01:00
elif msg[0] == Message.Directory:
self.update_kwdict(msg[1])
self.handle_directory(msg[1])
2017-03-17 09:39:46 +01:00
elif msg[0] == Message.Queue:
_, url, kwds = msg
if self.pred_queue(url, kwds):
self.handle_queue(url, kwds)
2017-02-26 02:06:56 +01:00
elif msg[0] == Message.Version:
if msg[1] != 1:
raise "unsupported message-version ({}, {})".format(
self.extractor.category, msg[1]
)
# TODO: support for multiple message versions
def handle_url(self, url, keywords):
"""Handle Message.Url"""
def handle_directory(self, keywords):
"""Handle Message.Directory"""
def handle_queue(self, url, keywords):
"""Handle Message.Queue"""
def update_kwdict(self, kwdict):
"""Add 'category' and 'subcategory' keywords"""
kwdict["category"] = self.extractor.category
kwdict["subcategory"] = self.extractor.subcategory
2015-12-12 00:11:05 +01:00
2017-05-27 16:16:57 +02:00
def _write_unsupported(self, url):
if self.ufile:
print(url, file=self.ufile, flush=True)
2017-01-30 19:40:15 +01:00
2015-12-12 00:11:05 +01:00
class DownloadJob(Job):
"""Download images into appropriate directory/filename locations"""
2017-09-30 18:52:23 +02:00
def __init__(self, url, parent=None):
Job.__init__(self, url, parent)
2017-03-28 13:12:44 +02:00
self.pathfmt = util.PathFormat(self.extractor)
self.sleep = self.extractor.config("sleep")
2015-04-08 01:51:48 +02:00
self.downloaders = {}
self.out = output.select()
2015-04-08 01:51:48 +02:00
def handle_url(self, url, keywords):
"""Download the resource specified in 'url'"""
self.pathfmt.set_keywords(keywords)
if self.pathfmt.exists():
self.out.skip(self.pathfmt.path)
2015-04-08 01:51:48 +02:00
return
if self.sleep:
time.sleep(self.sleep)
2015-11-12 02:35:30 +01:00
dlinstance = self.get_downloader(url)
dlinstance.download(url, self.pathfmt)
2015-04-08 01:51:48 +02:00
def handle_directory(self, keywords):
2015-04-08 01:51:48 +02:00
"""Set and create the target directory for downloads"""
self.pathfmt.set_directory(keywords)
2015-04-08 01:51:48 +02:00
def handle_queue(self, url, keywords):
try:
2017-09-30 18:52:23 +02:00
DownloadJob(url, self).run()
2017-05-24 15:15:06 +02:00
except exception.NoExtractorError:
2017-05-27 16:16:57 +02:00
self._write_unsupported(url)
2015-04-08 01:51:48 +02:00
def get_downloader(self, url):
"""Return, and possibly construct, a downloader suitable for 'url'"""
pos = url.find(":")
scheme = url[:pos] if pos != -1 else "http"
if scheme == "https":
scheme = "http"
2015-11-12 02:35:30 +01:00
instance = self.downloaders.get(scheme)
if instance is None:
klass = downloader.find(scheme)
instance = klass(self.extractor.session, self.out)
2015-11-12 02:35:30 +01:00
self.downloaders[scheme] = instance
return instance
2015-11-13 01:02:49 +01:00
2015-12-12 00:11:05 +01:00
class KeywordJob(Job):
"""Print available keywords"""
2015-11-13 01:02:49 +01:00
2017-05-17 14:31:14 +02:00
def handle_url(self, url, keywords):
2017-09-30 18:52:23 +02:00
print("\nKeywords for filenames and --filter:")
print("------------------------------------")
2017-05-17 14:31:14 +02:00
self.print_keywords(keywords)
raise exception.StopExtraction()
def handle_directory(self, keywords):
print("Keywords for directory names:")
print("-----------------------------")
self.print_keywords(keywords)
2015-11-13 01:02:49 +01:00
def handle_queue(self, url, keywords):
if not keywords:
self.extractor.log.info(
"This extractor delegates work to other extractors "
"and does not provide any keywords on its own. Try "
"'gallery-dl -K \"%s\"' instead.", url)
else:
print("Keywords for --chapter-filter:")
print("------------------------------")
self.print_keywords(keywords)
if self.extractor.categorytransfer:
print()
2017-09-30 18:52:23 +02:00
KeywordJob(url, self).run()
2017-08-10 17:36:21 +02:00
raise exception.StopExtraction()
2015-11-13 01:02:49 +01:00
@staticmethod
2017-05-17 14:31:14 +02:00
def print_keywords(keywords, prefix=""):
2015-12-12 01:16:02 +01:00
"""Print key-value pairs with formatting"""
2017-05-17 14:31:14 +02:00
suffix = "]" if prefix else ""
2015-11-13 01:02:49 +01:00
for key, value in sorted(keywords.items()):
2017-05-17 14:31:14 +02:00
key = prefix + key + suffix
if isinstance(value, dict):
2017-05-17 14:31:14 +02:00
KeywordJob.print_keywords(value, key + "[")
elif isinstance(value, list):
if value and isinstance(value[0], dict):
2017-05-17 14:31:14 +02:00
KeywordJob.print_keywords(value[0], key + "[][")
else:
2017-05-17 14:31:14 +02:00
print(key, "[]", sep="")
for val in value:
2017-05-17 14:31:14 +02:00
print(" -", val)
else:
# string or number
2017-05-17 14:31:14 +02:00
print(key, "\n ", value, sep="")
2015-12-10 02:14:28 +01:00
2015-12-12 00:11:05 +01:00
class UrlJob(Job):
"""Print download urls"""
maxdepth = -1
2017-09-30 18:52:23 +02:00
def __init__(self, url, parent=None, depth=1):
Job.__init__(self, url, parent)
self.depth = depth
if depth == self.maxdepth:
self.handle_queue = self.handle_url
2015-12-10 02:14:28 +01:00
@staticmethod
def handle_url(url, _):
print(url)
2016-08-11 13:20:21 +02:00
def handle_queue(self, url, _):
try:
2017-09-30 18:52:23 +02:00
UrlJob(url, self, self.depth + 1).run()
except exception.NoExtractorError:
2017-05-27 16:16:57 +02:00
self._write_unsupported(url)
2015-12-12 01:16:02 +01:00
2017-02-26 02:06:56 +01:00
class TestJob(DownloadJob):
"""Generate test-results for extractor runs"""
2015-12-12 01:16:02 +01:00
class HashIO():
"""Minimal file-like interface"""
def __init__(self, hashobj):
self.hashobj = hashobj
self.path = ""
self.size = 0
self.has_extension = True
def __enter__(self):
return self
def __exit__(self, *args):
pass
2017-10-25 12:55:36 +02:00
def open(self, mode):
self.size = 0
return self
def write(self, content):
"""Update SHA1 hash"""
self.size += len(content)
self.hashobj.update(content)
def tell(self):
return self.size
2017-10-25 12:55:36 +02:00
def part_size(self):
return 0
def __init__(self, url, parent=None, content=False):
DownloadJob.__init__(self, url, parent)
2017-01-30 19:40:15 +01:00
self.content = content
self.urllist = []
2017-01-30 19:40:15 +01:00
self.hash_url = hashlib.sha1()
2015-12-12 01:16:02 +01:00
self.hash_keyword = hashlib.sha1()
self.hash_content = hashlib.sha1()
if content:
self.fileobj = self.HashIO(self.hash_content)
2015-12-12 01:16:02 +01:00
2017-02-26 02:06:56 +01:00
def run(self):
for msg in self.extractor:
self.dispatch(msg)
2017-02-26 02:06:56 +01:00
def handle_url(self, url, keywords):
self.update_url(url)
self.update_keyword(keywords)
self.update_content(url)
2015-12-12 01:16:02 +01:00
def handle_directory(self, keywords):
self.update_keyword(keywords)
2015-12-12 01:16:02 +01:00
def handle_queue(self, url, keywords):
2015-12-12 01:16:02 +01:00
self.update_url(url)
self.update_keyword(keywords)
2015-12-12 01:16:02 +01:00
def update_url(self, url):
"""Update the URL hash"""
self.urllist.append(url)
2015-12-12 01:16:02 +01:00
self.hash_url.update(url.encode())
def update_keyword(self, kwdict):
"""Update the keyword hash"""
2015-12-12 01:16:02 +01:00
self.hash_keyword.update(
json.dumps(kwdict, sort_keys=True).encode()
)
def update_content(self, url):
"""Update the content hash"""
if self.content:
self.get_downloader(url).download(url, self.fileobj)
class DataJob(Job):
"""Collect extractor results and dump them"""
def __init__(self, url, parent=None, file=sys.stdout):
Job.__init__(self, url, parent)
self.file = file
self.data = []
self.ensure_ascii = config.get(("output", "ascii"), True)
def run(self):
# collect data
try:
for msg in self.extractor:
self.dispatch(msg)
except Exception as exc:
self.data.append((exc.__class__.__name__, str(exc)))
except BaseException:
pass
# dump to 'file'
json.dump(
self.data, self.file,
sort_keys=True, indent=2, ensure_ascii=self.ensure_ascii
)
self.file.write("\n")
def handle_url(self, url, keywords):
self.data.append((Message.Url, url, keywords.copy()))
def handle_directory(self, keywords):
self.data.append((Message.Directory, keywords.copy()))
def handle_queue(self, url, keywords):
self.data.append((Message.Queue, url, keywords.copy()))