merge 'bypost' functionality into metadata postprocessor
This commit is contained in:
@@ -33,13 +33,15 @@ class PatreonExtractor(Extractor):
|
|||||||
PatreonExtractor._warning = False
|
PatreonExtractor._warning = False
|
||||||
|
|
||||||
for post in self.posts():
|
for post in self.posts():
|
||||||
yield Message.Directory, post
|
|
||||||
|
|
||||||
ids = set()
|
ids = set()
|
||||||
post["num"] = 0
|
post["num"] = 0
|
||||||
content = post.get("content")
|
content = post.get("content")
|
||||||
postfile = post.get("post_file")
|
postfile = post.get("post_file")
|
||||||
|
|
||||||
|
yield Message.Directory, post
|
||||||
|
yield Message.Metadata, text.nameext_from_url(
|
||||||
|
post["creator"].get("image_url", ""), post)
|
||||||
|
|
||||||
for image in post["images"]:
|
for image in post["images"]:
|
||||||
url = image.get("download_url")
|
url = image.get("download_url")
|
||||||
if not url:
|
if not url:
|
||||||
@@ -69,11 +71,6 @@ class PatreonExtractor(Extractor):
|
|||||||
post["type"] = "content"
|
post["type"] = "content"
|
||||||
yield Message.Url, url, text.nameext_from_url(url, post)
|
yield Message.Url, url, text.nameext_from_url(url, post)
|
||||||
|
|
||||||
# Metadata for post using dummy url for formatting
|
|
||||||
post.update({"metadata_only": True})
|
|
||||||
url = post.get("creator").get("image_url")
|
|
||||||
yield Message.Metadata, url, text.nameext_from_url(url, post)
|
|
||||||
|
|
||||||
def posts(self):
|
def posts(self):
|
||||||
"""Return all relevant post objects"""
|
"""Return all relevant post objects"""
|
||||||
|
|
||||||
|
|||||||
@@ -98,9 +98,8 @@ class Job():
|
|||||||
self.handle_urllist(urls, kwds)
|
self.handle_urllist(urls, kwds)
|
||||||
|
|
||||||
elif msg[0] == Message.Metadata:
|
elif msg[0] == Message.Metadata:
|
||||||
_, url, kwds = msg
|
self.update_kwdict(msg[1])
|
||||||
self.update_kwdict(kwds)
|
self.handle_metadata(msg[1])
|
||||||
self.handle_metadata(url, kwds)
|
|
||||||
|
|
||||||
elif msg[0] == Message.Version:
|
elif msg[0] == Message.Version:
|
||||||
if msg[1] != 1:
|
if msg[1] != 1:
|
||||||
@@ -119,6 +118,9 @@ class Job():
|
|||||||
def handle_directory(self, kwdict):
|
def handle_directory(self, kwdict):
|
||||||
"""Handle Message.Directory"""
|
"""Handle Message.Directory"""
|
||||||
|
|
||||||
|
def handle_metadata(self, kwdict):
|
||||||
|
"""Handle Message.Metadata"""
|
||||||
|
|
||||||
def handle_queue(self, url, kwdict):
|
def handle_queue(self, url, kwdict):
|
||||||
"""Handle Message.Queue"""
|
"""Handle Message.Queue"""
|
||||||
|
|
||||||
@@ -234,19 +236,6 @@ class DownloadJob(Job):
|
|||||||
pp.run_after(pathfmt)
|
pp.run_after(pathfmt)
|
||||||
self._skipcnt = 0
|
self._skipcnt = 0
|
||||||
|
|
||||||
def handle_metadata(self, url, kwdict, fallback=None):
|
|
||||||
"""Download the resource specified in 'url'"""
|
|
||||||
postprocessors = self.postprocessors
|
|
||||||
pathfmt = self.pathfmt
|
|
||||||
|
|
||||||
# prepare download
|
|
||||||
pathfmt.set_filename(kwdict)
|
|
||||||
|
|
||||||
if postprocessors:
|
|
||||||
for pp in postprocessors:
|
|
||||||
pp.prepare(pathfmt)
|
|
||||||
return
|
|
||||||
|
|
||||||
def handle_urllist(self, urls, kwdict):
|
def handle_urllist(self, urls, kwdict):
|
||||||
"""Download the resource specified in 'url'"""
|
"""Download the resource specified in 'url'"""
|
||||||
fallback = iter(urls)
|
fallback = iter(urls)
|
||||||
@@ -260,6 +249,16 @@ class DownloadJob(Job):
|
|||||||
else:
|
else:
|
||||||
self.pathfmt.set_directory(kwdict)
|
self.pathfmt.set_directory(kwdict)
|
||||||
|
|
||||||
|
def handle_metadata(self, kwdict):
|
||||||
|
"""Run postprocessors with metadata from 'kwdict'"""
|
||||||
|
postprocessors = self.postprocessors
|
||||||
|
|
||||||
|
if postprocessors:
|
||||||
|
pathfmt = self.pathfmt
|
||||||
|
pathfmt.set_filename(kwdict)
|
||||||
|
for pp in postprocessors:
|
||||||
|
pp.run_metadata(pathfmt)
|
||||||
|
|
||||||
def handle_queue(self, url, kwdict):
|
def handle_queue(self, url, kwdict):
|
||||||
if "_extractor" in kwdict:
|
if "_extractor" in kwdict:
|
||||||
extr = kwdict["_extractor"].from_url(url)
|
extr = kwdict["_extractor"].from_url(url)
|
||||||
@@ -519,15 +518,15 @@ class DataJob(Job):
|
|||||||
def handle_url(self, url, kwdict):
|
def handle_url(self, url, kwdict):
|
||||||
self.data.append((Message.Url, url, self.filter(kwdict)))
|
self.data.append((Message.Url, url, self.filter(kwdict)))
|
||||||
|
|
||||||
def handle_metadata(self, url, kwdict):
|
|
||||||
self.data.append((Message.Url, url, self.filter(kwdict)))
|
|
||||||
|
|
||||||
def handle_urllist(self, urls, kwdict):
|
def handle_urllist(self, urls, kwdict):
|
||||||
self.data.append((Message.Urllist, list(urls), self.filter(kwdict)))
|
self.data.append((Message.Urllist, list(urls), self.filter(kwdict)))
|
||||||
|
|
||||||
def handle_directory(self, kwdict):
|
def handle_directory(self, kwdict):
|
||||||
self.data.append((Message.Directory, self.filter(kwdict)))
|
self.data.append((Message.Directory, self.filter(kwdict)))
|
||||||
|
|
||||||
|
def handle_metadata(self, kwdict):
|
||||||
|
self.data.append((Message.Metadata, self.filter(kwdict)))
|
||||||
|
|
||||||
def handle_queue(self, url, kwdict):
|
def handle_queue(self, url, kwdict):
|
||||||
self.data.append((Message.Queue, url, self.filter(kwdict)))
|
self.data.append((Message.Queue, url, self.filter(kwdict)))
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ modules = [
|
|||||||
"mtime",
|
"mtime",
|
||||||
"ugoira",
|
"ugoira",
|
||||||
"zip",
|
"zip",
|
||||||
"metadata_bypost",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
log = logging.getLogger("postprocessor")
|
log = logging.getLogger("postprocessor")
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ class PostProcessor():
|
|||||||
def run(pathfmt):
|
def run(pathfmt):
|
||||||
"""Execute the postprocessor for a file"""
|
"""Execute the postprocessor for a file"""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def run_metadata(pathfmt):
|
||||||
|
"""Execute the postprocessor for a file"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def run_after(pathfmt):
|
def run_after(pathfmt):
|
||||||
"""Execute postprocessor after moving a file to its target location"""
|
"""Execute postprocessor after moving a file to its target location"""
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ class MetadataPP(PostProcessor):
|
|||||||
self.path = self._path_append
|
self.path = self._path_append
|
||||||
self.extension = options.get("extension", ext)
|
self.extension = options.get("extension", ext)
|
||||||
|
|
||||||
|
if options.get("bypost"):
|
||||||
|
self.run_metadata, self.run = self.run, self.run_metadata
|
||||||
|
|
||||||
def run(self, pathfmt):
|
def run(self, pathfmt):
|
||||||
with open(self.path(pathfmt), "w", encoding="utf-8") as file:
|
with open(self.path(pathfmt), "w", encoding="utf-8") as file:
|
||||||
self.write(file, pathfmt.kwdict)
|
self.write(file, pathfmt.kwdict)
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# Copyright 2019 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.
|
|
||||||
|
|
||||||
"""Write metadata to JSON files"""
|
|
||||||
|
|
||||||
from .metadata import __postprocessor__ as MetadataPP
|
|
||||||
|
|
||||||
|
|
||||||
class Metadata_bypostPP(MetadataPP):
|
|
||||||
|
|
||||||
def __init__(self, pathfmt, options):
|
|
||||||
MetadataPP.__init__(self, pathfmt, options)
|
|
||||||
|
|
||||||
def prepare(self, pathfmt):
|
|
||||||
# Only run this processor on metadata messages, not individual images.
|
|
||||||
if pathfmt.kwdict.get("metadata_only"):
|
|
||||||
MetadataPP.run(self, pathfmt)
|
|
||||||
|
|
||||||
def run(self, pathfmt):
|
|
||||||
return
|
|
||||||
|
|
||||||
__postprocessor__ = Metadata_bypostPP
|
|
||||||
Reference in New Issue
Block a user