Refactoring - Updated the clear_downloads function.

This commit is contained in:
ClaytonWWilson 2019-05-24 17:24:30 -04:00
parent c876291100
commit dc27ff581f
3 changed files with 68 additions and 23 deletions

View File

@ -1,7 +1,7 @@
from authentication import login from authentication import login
from main import download from main import download
from main import wait from main import wait
from utils import clearFolders from utils import clear_downloads
from utils import clearScreen from utils import clearScreen
from search import search from search import search
@ -14,7 +14,7 @@ while True:
"=============================\n") "=============================\n")
print("1. Search theTVDB.com") print("1. Search theTVDB.com")
print("2. Clear download folders") print("2. Clear downloaded data")
print("3. Login/Change login") print("3. Login/Change login")
print("4. Install Requirements") print("4. Install Requirements")
print("5. Check for updates\n") print("5. Check for updates\n")
@ -29,7 +29,7 @@ while True:
wait() wait()
elif choice == "2": elif choice == "2":
clearScreen() clearScreen()
clearFolders() clear_downloads()
wait() wait()
elif choice == "3": # TODO add a printout that tells the user who is currently logged in elif choice == "3": # TODO add a printout that tells the user who is currently logged in
clearScreen() clearScreen()

View File

@ -8,7 +8,7 @@ import requests
import urllib.parse import urllib.parse
from utils import APIConnector from utils import APIConnector
from utils import clearFolders from utils import clear_downloads
from utils import clearScreen from utils import clearScreen
from utils import create_file_name from utils import create_file_name
from authentication import check_timestamp from authentication import check_timestamp

View File

@ -2,6 +2,7 @@ import json
import os import os
import re import re
import requests import requests
import shutil
@ -27,25 +28,69 @@ class APIConnector:
def send_http_req(self, api_path): def send_http_req(self, api_path):
return requests.get(api_path, headers=self.auth_headers) return requests.get(api_path, headers=self.auth_headers)
def clearFolders(): # TODO implement this # recursively counts the number of folders and files in the download folder
folders = ["banner", "fanart", "poster"] stats = [-1, 0] # [folders, files] Start at -1 to ignore the "downloads" folder itself
del_count = 0 def stat_downloads(path):
for folder in folders: if os.path.isfile(path):
if os.path.exists(folder): stats[1] += 1
image_list = os.listdir(folder) return stats
if len(image_list) != 0: else:
print("Clearing " + folder + "/") stats[0] += 1
for x in image_list: # TODO check if folder is empty for file_entry in os.listdir(path):
print("Deleting {}/{}".format(folder, x)) stat_downloads(os.path.join(path, file_entry))
del_path = os.path.join(folder + "\\" + x) return stats
os.remove(del_path)
del_count += 1 # The following function is from https://stackoverflow.com/questions/1392413/calculating-a-directorys-size-using-python
print() def get_dir_size(start_path='.'):
else: total_size = 0
print("'{}' is already empty".format(folder)) for dirpath, dirnames, filenames in os.walk(start_path):
else: for f in filenames:
os.makedirs(folder) fp = os.path.join(dirpath, f)
print("Deleted {} images.\n".format(del_count)) 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(): def clearScreen():
IS_WINDOWS = os.name == "nt" IS_WINDOWS = os.name == "nt"