aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/postprocessor
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2020-01-21 01:08:43 -0500
committerLibravatarUnit 193 <unit193@ubuntu.com>2020-01-21 01:08:43 -0500
commit4366125d2580982abb57bc65a26fc1fb8ef2a5df (patch)
tree743a26348e360c8b7f5eb89d4f704b015e902e68 /gallery_dl/postprocessor
parentbc435e826dbe37969d9cbe280f58810d054932cc (diff)
New upstream version 1.12.3upstream/1.12.3
Diffstat (limited to 'gallery_dl/postprocessor')
-rw-r--r--gallery_dl/postprocessor/__init__.py3
-rw-r--r--gallery_dl/postprocessor/compare.py62
-rw-r--r--gallery_dl/postprocessor/zip.py21
3 files changed, 71 insertions, 15 deletions
diff --git a/gallery_dl/postprocessor/__init__.py b/gallery_dl/postprocessor/__init__.py
index e63d442..7a3bf23 100644
--- a/gallery_dl/postprocessor/__init__.py
+++ b/gallery_dl/postprocessor/__init__.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-# Copyright 2018-2019 Mike Fährmann
+# Copyright 2018-2020 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
@@ -13,6 +13,7 @@ import logging
modules = [
"classify",
+ "compare",
"exec",
"metadata",
"mtime",
diff --git a/gallery_dl/postprocessor/compare.py b/gallery_dl/postprocessor/compare.py
new file mode 100644
index 0000000..ddbcef0
--- /dev/null
+++ b/gallery_dl/postprocessor/compare.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2020 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.
+
+"""Compare versions of the same file and replace/enumerate them on mismatch"""
+
+from .common import PostProcessor
+import os
+
+
+class ComparePP(PostProcessor):
+
+ def __init__(self, pathfmt, options):
+ PostProcessor.__init__(self)
+ if options.get("action") == "enumerate":
+ self.run = self._run_enumerate
+ if options.get("shallow"):
+ self.compare = self._compare_size
+
+ def run(self, pathfmt):
+ try:
+ if self.compare(pathfmt.realpath, pathfmt.temppath):
+ pathfmt.delete = True
+ except OSError:
+ pass
+
+ def _run_enumerate(self, pathfmt):
+ num = 1
+ try:
+ while not self.compare(pathfmt.realpath, pathfmt.temppath):
+ pathfmt.prefix = str(num) + "."
+ pathfmt.set_extension(pathfmt.extension, False)
+ num += 1
+ pathfmt.delete = True
+ except OSError:
+ pass
+
+ def compare(self, f1, f2):
+ return self._compare_size(f1, f2) and self._compare_content(f1, f2)
+
+ @staticmethod
+ def _compare_size(f1, f2):
+ return os.stat(f1).st_size == os.stat(f2).st_size
+
+ @staticmethod
+ def _compare_content(f1, f2):
+ size = 16384
+ with open(f1, "rb") as fp1, open(f2, "rb") as fp2:
+ while True:
+ buf1 = fp1.read(size)
+ buf2 = fp2.read(size)
+ if buf1 != buf2:
+ return False
+ if not buf1:
+ return True
+
+
+__postprocessor__ = ComparePP
diff --git a/gallery_dl/postprocessor/zip.py b/gallery_dl/postprocessor/zip.py
index 42f7608..a43c43a 100644
--- a/gallery_dl/postprocessor/zip.py
+++ b/gallery_dl/postprocessor/zip.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-# Copyright 2018-2019 Mike Fährmann
+# Copyright 2018-2020 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
@@ -9,17 +9,17 @@
"""Store files in ZIP archives"""
from .common import PostProcessor
+from .. import util
import zipfile
-import os
class ZipPP(PostProcessor):
COMPRESSION_ALGORITHMS = {
"store": zipfile.ZIP_STORED,
- "zip": zipfile.ZIP_DEFLATED,
+ "zip" : zipfile.ZIP_DEFLATED,
"bzip2": zipfile.ZIP_BZIP2,
- "lzma": zipfile.ZIP_LZMA,
+ "lzma" : zipfile.ZIP_LZMA,
}
def __init__(self, pathfmt, options):
@@ -64,18 +64,11 @@ class ZipPP(PostProcessor):
self.zfile.close()
if self.delete:
- try:
- # remove target directory
- os.rmdir(self.path)
- except OSError:
- pass
+ util.remove_directory(self.path)
if self.zfile and not self.zfile.NameToInfo:
- try:
- # delete empty zip archive
- os.unlink(self.zfile.filename)
- except OSError:
- pass
+ # remove empty zip archive
+ util.remove_file(self.zfile.filename)
__postprocessor__ = ZipPP