+ def _handle_old_directory(self, base_dir):
+ """ Deal with any old directories from previous versions of the code.
+ """
+ old_dir = os.path.join(base_dir, slugify(self.name))
+ if os.path.exists(old_dir):
+ logging.warning("Found old style download_dir. Moving.")
+ rename_unique(old_dir, self.download_dir)
+
+ def _handle_outdated_directory(self):
+ """ Move the current download directory sideways if the thing has changed.
+ """
+ if not os.path.exists(self.download_dir):
+ # No old directory to move.
+ return None
+ timestamp_file = os.path.join(self.download_dir, TIMESTAMP_FILE)
+ if not os.path.exists(timestamp_file):
+ # Old form of download directory
+ target_dir_name = "{} - old".format(self.download_dir)
+ else:
+ target_dir_name = "{} - {}".format(self.download_dir, self.last_time.strftime(SAFE_DATETIME_FORMAT))
+ return rename_unique(self.download_dir, target_dir_name)
+
+ def _find_last_download(self, base_dir):
+ """ Look for the most recent previous download (if any) of the thing.
+ """
+ logging.info("Looking for old things")
+
+ # First the DL directory itself.
+ timestamp_file = os.path.join(self.download_dir, TIMESTAMP_FILE)
+
+ latest = None
+ latest_time = None
+
+ try:
+ logging.debug("Checking for existing download in normal place.")
+ with open(timestamp_file) as ts_fh:
+ timestamp_text = ts_fh.read().strip()
+ latest_time = datetime.datetime.strptime(timestamp_text, DEFAULT_DATETIME_FORMAT)
+ latest = self.download_dir
+ except FileNotFoundError:
+ # No existing download directory. huh.
+ pass
+ except TypeError:
+ logging.warning("Invalid timestamp file found in {}".format(self.download_dir))
+
+ # TODO: Maybe look for old download directories.
+
+ # Now look for 7z files
+ candidates = glob.glob(os.path.join(base_dir, "{}*.7z".format(self.thing_id)))
+ # +3 to allow for ' - '
+ leading_length = len(self.slug) + 3
+ for path in candidates:
+ candidate = os.path.basename(path)
+ try:
+ logging.debug("Examining '{}' - '{}'".format(candidate, candidate[leading_length:-3]))
+ candidate_time = datetime.datetime.strptime(candidate[leading_length:-3], SAFE_DATETIME_FORMAT)
+ except ValueError:
+ logging.warning("There was an error finding the date in {}. Ignoring.".format(candidate))
+ continue
+ try:
+ if candidate_time > latest_time:
+ latest_time = candidate_time
+ latest = candidate
+ except TypeError:
+ latest_time = candidate_time
+ latest = candidate
+ logging.info("Found last old thing: {} / {}".format(latest, latest_time))
+ return (latest, latest_time)
+
+ def download(self, base_dir, compress):
+ """ Download all files for a given thing.
+ Returns True iff the thing is now downloaded (not iff it downloads the thing!)
+ """