aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/postprocessor/hash.py
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2024-09-07 18:33:19 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2024-09-07 18:33:19 -0400
commit1f3ffe32342852fd9ea9e7704022488f3a1222bd (patch)
treecb255a091b73e96840de0f6f44b36dff1acab4b9 /gallery_dl/postprocessor/hash.py
parentb5e56c51e491b41f9eb6a895459c185788a377e5 (diff)
New upstream version 1.27.4.upstream/1.27.4
Diffstat (limited to 'gallery_dl/postprocessor/hash.py')
-rw-r--r--gallery_dl/postprocessor/hash.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/gallery_dl/postprocessor/hash.py b/gallery_dl/postprocessor/hash.py
new file mode 100644
index 0000000..92a7477
--- /dev/null
+++ b/gallery_dl/postprocessor/hash.py
@@ -0,0 +1,71 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2024 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.
+
+"""Compute file hash digests"""
+
+from .common import PostProcessor
+import hashlib
+
+
+class HashPP(PostProcessor):
+
+ def __init__(self, job, options):
+ PostProcessor.__init__(self, job)
+
+ self.chunk_size = options.get("chunk-size", 32768)
+ self.filename = options.get("filename")
+
+ hashes = options.get("hashes")
+ if isinstance(hashes, dict):
+ self.hashes = list(hashes.items())
+ elif isinstance(hashes, str):
+ self.hashes = []
+ for h in hashes.split(","):
+ name, sep, key = h.partition(":")
+ self.hashes.append((key if sep else name, name))
+ elif hashes:
+ self.hashes = hashes
+ else:
+ self.hashes = (("md5", "md5"), ("sha1", "sha1"))
+
+ events = options.get("event")
+ if events is None:
+ events = ("file",)
+ elif isinstance(events, str):
+ events = events.split(",")
+ job.register_hooks({event: self.run for event in events}, options)
+
+ def run(self, pathfmt):
+ hashes = [
+ (key, hashlib.new(name))
+ for key, name in self.hashes
+ ]
+
+ size = self.chunk_size
+ with self._open(pathfmt) as fp:
+ while True:
+ data = fp.read(size)
+ if not data:
+ break
+ for _, h in hashes:
+ h.update(data)
+
+ for key, h in hashes:
+ pathfmt.kwdict[key] = h.hexdigest()
+
+ if self.filename:
+ pathfmt.build_path()
+
+ def _open(self, pathfmt):
+ try:
+ return open(pathfmt.temppath, "rb")
+ except OSError:
+ return open(pathfmt.realpath, "rb")
+
+
+__postprocessor__ = HashPP