From dc27ff581f6d6d3b9f59fe055feb6014198fc4d2 Mon Sep 17 00:00:00 2001 From: ClaytonWWilson Date: Fri, 24 May 2019 17:24:30 -0400 Subject: [PATCH] Refactoring - Updated the clear_downloads function. --- launcher.py | 6 ++-- search.py | 2 +- utils.py | 83 +++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 68 insertions(+), 23 deletions(-) diff --git a/launcher.py b/launcher.py index 6ad30e0..35b38f5 100644 --- a/launcher.py +++ b/launcher.py @@ -1,7 +1,7 @@ from authentication import login from main import download from main import wait -from utils import clearFolders +from utils import clear_downloads from utils import clearScreen from search import search @@ -14,7 +14,7 @@ while True: "=============================\n") print("1. Search theTVDB.com") - print("2. Clear download folders") + print("2. Clear downloaded data") print("3. Login/Change login") print("4. Install Requirements") print("5. Check for updates\n") @@ -29,7 +29,7 @@ while True: wait() elif choice == "2": clearScreen() - clearFolders() + clear_downloads() wait() elif choice == "3": # TODO add a printout that tells the user who is currently logged in clearScreen() diff --git a/search.py b/search.py index 0037501..b0999f8 100644 --- a/search.py +++ b/search.py @@ -8,7 +8,7 @@ import requests import urllib.parse from utils import APIConnector -from utils import clearFolders +from utils import clear_downloads from utils import clearScreen from utils import create_file_name from authentication import check_timestamp diff --git a/utils.py b/utils.py index 0f1ec1f..cf8267e 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,7 @@ import json import os import re import requests +import shutil @@ -27,25 +28,69 @@ class APIConnector: def send_http_req(self, api_path): return requests.get(api_path, headers=self.auth_headers) -def clearFolders(): # TODO implement this - folders = ["banner", "fanart", "poster"] - del_count = 0 - for folder in folders: - if os.path.exists(folder): - image_list = os.listdir(folder) - if len(image_list) != 0: - print("Clearing " + folder + "/") - for x in image_list: # TODO check if folder is empty - print("Deleting {}/{}".format(folder, x)) - del_path = os.path.join(folder + "\\" + x) - os.remove(del_path) - del_count += 1 - print() - else: - print("'{}' is already empty".format(folder)) - else: - os.makedirs(folder) - print("Deleted {} images.\n".format(del_count)) +# recursively counts the number of folders and files in the download folder +stats = [-1, 0] # [folders, files] Start at -1 to ignore the "downloads" folder itself +def stat_downloads(path): + if os.path.isfile(path): + stats[1] += 1 + return stats + else: + stats[0] += 1 + for file_entry in os.listdir(path): + stat_downloads(os.path.join(path, file_entry)) + return stats + +# The following function is from https://stackoverflow.com/questions/1392413/calculating-a-directorys-size-using-python +def get_dir_size(start_path='.'): + total_size = 0 + for dirpath, dirnames, filenames in os.walk(start_path): + for f in filenames: + fp = os.path.join(dirpath, f) + total_size += os.path.getsize(fp) + return total_size + +def clear_downloads(): + if os.path.exists("downloads"): + series_count = 0 + for series in os.listdir("downloads"): + series_count += 1 + + counts = stat_downloads("downloads") + total_size = get_dir_size(start_path="downloads") + shutil.rmtree("downloads") + + # Convert total_size in bytes to a human-readable format + sizes = ["B", "KB", "MB", "GB", "TB"] + size_index = 0 + + while total_size > 1024: + total_size /= 1024 + size_index += 1 + + total_size_str = "{:.2f} {}".format(total_size, sizes[size_index]) + + print("Deleted {} series, {} folders, and {} files totaling {}".format(series_count, counts[0], counts[1], total_size_str)) + else: + print("There isn't anything to delete.") + + # folders = ["banner", "fanart", "poster"] + # del_count = 0 + # for folder in folders: + # if os.path.exists(folder): + # image_list = os.listdir(folder) + # if len(image_list) != 0: + # print("Clearing " + folder + "/") + # for x in image_list: # TODO check if folder is empty + # print("Deleting {}/{}".format(folder, x)) + # del_path = os.path.join(folder + "\\" + x) + # os.remove(del_path) + # del_count += 1 + # print() + # else: + # print("'{}' is already empty".format(folder)) + # else: + # os.makedirs(folder) + # print("Deleted {} images.\n".format(del_count)) def clearScreen(): IS_WINDOWS = os.name == "nt"