diff options
| author | 2019-07-20 05:51:44 -0400 | |
|---|---|---|
| committer | 2019-07-20 05:51:44 -0400 | |
| commit | 2a63a9c9b7032a76894c48ac4d9cea732fcaee49 (patch) | |
| tree | 3d5f633ff69cd393036a3dabc4d4533c8484f9ad /gallery_dl/util.py | |
| parent | 195c45911e79c33cf0bb986721365fb06df5a153 (diff) | |
New upstream version 1.9.0upstream/1.9.0
Diffstat (limited to 'gallery_dl/util.py')
| -rw-r--r-- | gallery_dl/util.py | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 5c0ae41..14ae3d2 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -12,6 +12,7 @@ import re import os import sys import json +import time import shutil import string import _string @@ -20,6 +21,7 @@ import datetime import operator import itertools import urllib.parse +from email.utils import mktime_tz, parsedate_tz from . import text, exception @@ -530,7 +532,7 @@ class PathFormat(): self.basedirectory = expand_path( extractor.config("base-directory", (".", "gallery-dl"))) - if os.altsep: + if os.altsep and os.altsep in self.basedirectory: self.basedirectory = self.basedirectory.replace(os.altsep, os.sep) def open(self, mode="wb"): @@ -539,13 +541,9 @@ class PathFormat(): def exists(self, archive=None): """Return True if the file exists on disk or in 'archive'""" - if (archive and archive.check(self.keywords) or - self.has_extension and os.path.exists(self.realpath)): - if not self.has_extension: - # adjust display name - self.set_extension("") - if self.path[-1] == ".": - self.path = self.path[:-1] + if archive and archive.check(self.keywords): + return self.fix_extension() + if self.has_extension and os.path.exists(self.realpath): return True return False @@ -588,6 +586,14 @@ class PathFormat(): self.keywords["extension"] = extension self.build_path() + def fix_extension(self, _=None): + if not self.has_extension: + self.set_extension("") + if self.path[-1] == ".": + self.path = self.path[:-1] + self.temppath = self.realpath = self.realpath[:-1] + return True + def build_path(self): """Use filename-keywords and directory to build a full path""" try: @@ -629,17 +635,24 @@ class PathFormat(): os.unlink(self.temppath) return - if self.temppath == self.realpath: - return - - try: - os.replace(self.temppath, self.realpath) - return - except OSError: - pass - - shutil.copyfile(self.temppath, self.realpath) - os.unlink(self.temppath) + if self.temppath != self.realpath: + # move temp file to its actual location + try: + os.replace(self.temppath, self.realpath) + except OSError: + shutil.copyfile(self.temppath, self.realpath) + os.unlink(self.temppath) + + if "_mtime" in self.keywords: + # set file modification time + mtime = self.keywords["_mtime"] + if mtime: + try: + if isinstance(mtime, str): + mtime = mktime_tz(parsedate_tz(mtime)) + os.utime(self.realpath, (time.time(), mtime)) + except Exception: + pass @staticmethod def adjust_path(path): |
