From 92440de1f7166e2fe4d398931fc9ebb2252d89f5 Mon Sep 17 00:00:00 2001 From: Eddy Hintze Date: Sat, 25 Jan 2020 20:08:18 -0500 Subject: [PATCH] Fixed #13 & Fixed #14 as well re download bug --- CHANGELOG.md | 6 +++++ humblebundle_downloader/_version.py | 2 +- humblebundle_downloader/download_library.py | 29 ++++++++++++++++++--- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d46b17..6a75751 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Change log +### 0.1.2 +- Stop using md5 & sha1 hashes to check if file is unique (was creating duplicate downloads of the same file) +- Strip periods from end of directory & file names +- Rename older versions of a file before download the new one + + ### 0.1.1 - Delete failed downloaded files diff --git a/humblebundle_downloader/_version.py b/humblebundle_downloader/_version.py index df9144c..10939f0 100644 --- a/humblebundle_downloader/_version.py +++ b/humblebundle_downloader/_version.py @@ -1 +1 @@ -__version__ = '0.1.1' +__version__ = '0.1.2' diff --git a/humblebundle_downloader/download_library.py b/humblebundle_downloader/download_library.py index 052c1ac..c09d300 100644 --- a/humblebundle_downloader/download_library.py +++ b/humblebundle_downloader/download_library.py @@ -3,6 +3,7 @@ import sys import json import parsel import logging +import datetime import requests logger = logging.getLogger(__name__) @@ -15,7 +16,7 @@ def _clean_name(dirty_str): if c.isalpha() or c.isdigit() or c in allowed_chars: clean.append(c) - return "".join(clean).strip() + return "".join(clean).strip().rstrip('.') class DownloadLibrary: @@ -99,14 +100,27 @@ class DownloadLibrary: .format(product_r=product_r, url=url)) # Not sure which value will be best to use, so use them all file_info = { - 'md5': file_type.get('md5'), - 'sha1': file_type.get('sha1'), 'url_last_modified': product_r.headers['Last-Modified'], 'url_etag': product_r.headers['ETag'][1:-1], 'url_crc': product_r.headers['X-HW-Cache-CRC'], } - if file_info != self.cache_data.get(cache_file_key, {}): + cache_file_info = self.cache_data.get(cache_file_key, {}) + if file_info != cache_file_info: try: + # Check if older file exists, if so rename + if (os.path.isfile(local_filename) is True + and 'url_last_modified' in cache_file_info): + filename_parts = local_filename.rsplit('.', 1) + last_modified = datetime.datetime.strptime( + cache_file_info['url_last_modified'], + '%a, %d %b %Y %H:%M:%S %Z' + ).strftime('%Y-%m-%d') + new_name = "{name}_{date}.{ext}"\ + .format(name=filename_parts[0], + date=last_modified, + ext=filename_parts[1]) + os.rename(local_filename, new_name) + self._download_file(product_r, local_filename) except (Exception, KeyboardInterrupt) as e: @@ -179,6 +193,13 @@ class DownloadLibrary: except FileNotFoundError: cache_data = {} + # Remove md5 & sha1 keys from legacy cache data + for key, value in cache_data.items(): + if 'md5' in value: + del cache_data[key]['md5'] + if 'sha1' in value: + del cache_data[key]['sha1'] + return cache_data def _get_purchase_keys(self):