1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 03:02:50 +01:00
gallery-dl/gallery_dl/jobs.py

160 lines
5.1 KiB
Python
Raw Normal View History

# -*- 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 os
import sys
2015-11-12 02:35:30 +01:00
from . import config, extractor, downloader, text
2015-11-24 19:47:51 +01:00
from .extractor.message import Message
2015-04-08 01:51:48 +02:00
class DownloadJob():
2015-11-12 02:35:30 +01:00
def __init__(self, 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:
2015-11-06 13:21:53 +01:00
print(url, ": No extractor found", sep="", file=sys.stderr)
2015-06-28 12:45:52 +02:00
return
2015-11-12 02:35:30 +01:00
self.directory = self.get_base_directory()
2015-04-08 01:51:48 +02:00
self.downloaders = {}
2015-11-26 22:55:11 +01:00
self.queue = None
2015-10-05 13:26:38 +02:00
self.filename_fmt = config.get(
2015-11-21 00:30:31 +01:00
("extractor", self.extractor.category, "filename"),
default=self.extractor.filename_fmt
2015-10-05 13:26:38 +02:00
)
segments = config.get(
2015-11-21 00:30:31 +01:00
("extractor", self.extractor.category, "directory"),
default=self.extractor.directory_fmt
)
self.directory_fmt = os.path.join(*segments)
2015-04-08 01:51:48 +02:00
def run(self):
2015-06-28 12:45:52 +02:00
"""Execute/Run the download job"""
2015-04-08 01:51:48 +02:00
if self.extractor is None:
2015-11-06 13:21:53 +01:00
return
2015-04-08 01:51:48 +02:00
for msg in self.extractor:
if msg[0] == Message.Url:
self.download(msg)
2015-04-08 19:06:50 +02:00
elif msg[0] == Message.Headers:
self.get_downloader("http:").set_headers(msg[1])
elif msg[0] == Message.Cookies:
self.get_downloader("http:").set_cookies(msg[1])
2015-04-08 01:51:48 +02:00
elif msg[0] == Message.Directory:
self.set_directory(msg)
2015-11-26 22:55:11 +01:00
elif msg[0] == Message.Queue:
self.enqueue(msg[1])
2015-04-08 01:51:48 +02:00
elif msg[0] == Message.Version:
if msg[1] != 1:
raise "unsupported message-version ({}, {})".format(
2015-11-21 00:30:31 +01:00
self.extractor.category, msg[1]
2015-04-08 01:51:48 +02:00
)
# TODO: support for multiple message versions
2015-11-26 22:55:11 +01:00
if self.queue:
for url in self.queue:
DownloadJob(url).run()
2015-04-08 01:51:48 +02:00
def download(self, msg):
"""Download the resource specified in 'msg'"""
_, url, metadata = msg
2015-10-09 02:24:41 +02:00
filename = text.clean_path(self.filename_fmt.format(**metadata))
2015-04-08 01:51:48 +02:00
path = os.path.join(self.directory, filename)
if os.path.exists(path):
self.print_skip(path)
return
2015-11-12 02:35:30 +01:00
dlinstance = self.get_downloader(url)
2015-04-08 01:51:48 +02:00
self.print_start(path)
2015-11-12 02:35:30 +01:00
tries = dlinstance.download(url, path)
2015-04-08 01:51:48 +02:00
self.print_success(path, tries)
def set_directory(self, msg):
"""Set and create the target directory for downloads"""
self.directory = os.path.join(
2015-11-12 02:35:30 +01:00
self.get_base_directory(),
2015-10-09 02:24:41 +02:00
self.directory_fmt.format(**{
key: text.clean_path(value) for key, value in msg[1].items()
})
2015-04-08 01:51:48 +02:00
)
os.makedirs(self.directory, exist_ok=True)
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.downloaders[scheme] = instance
return instance
2015-11-26 22:55:11 +01:00
def enqueue(self, url):
"""Add url to work-queue"""
try:
self.queue.append(url)
except AttributeError:
self.queue = [url]
2015-11-12 02:35:30 +01:00
@staticmethod
def get_base_directory():
"""Return the base-destination-directory for downloads"""
return config.get(("base-directory",), default="/tmp/")
@staticmethod
def print_start(path):
2015-04-10 01:21:35 +02:00
"""Print a message indicating the start of a download"""
print(path, end="")
sys.stdout.flush()
@staticmethod
def print_skip(path):
2015-04-10 01:21:35 +02:00
"""Print a message indicating that a download has been skipped"""
print("\033[2m", path, "\033[0m", sep="")
@staticmethod
def print_success(path, tries):
2015-04-10 01:21:35 +02:00
"""Print a message indicating the completion of a download"""
if tries == 0:
print("\r", end="")
print("\r\033[1;32m", path, "\033[0m", sep="")
2015-11-13 01:02:49 +01:00
class KeywordJob():
def __init__(self, url):
2015-11-21 00:30:31 +01:00
self.extractor = extractor.find(url)
2015-11-13 01:02:49 +01:00
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()