Improve real-time status updates and recursive scanning visibility.
This commit is contained in:
@@ -16,11 +16,20 @@ stats = {
|
||||
"failed": 0
|
||||
}
|
||||
|
||||
def print_progress(force=False):
|
||||
"""Prints a single-line progress update."""
|
||||
if force or stats["total_checked"] % 50 == 0:
|
||||
sys.stdout.write(f"\rStatus: {stats['total_checked']} checked, {stats['downloaded']} downloaded, {stats['skipped']} skipped, {stats['failed']} failed... ")
|
||||
sys.stdout.flush()
|
||||
def print_status(current_item=""):
|
||||
"""Prints a single-line progress update with optional current item."""
|
||||
# Build status string
|
||||
status_line = f"Checked: {stats['total_checked']} | Downloaded: {stats['downloaded']} | Skipped: {stats['skipped']} | Failed: {stats['failed']}"
|
||||
|
||||
if current_item:
|
||||
# Show a truncated version of the current path to keep it on one line
|
||||
max_len = 50
|
||||
display_item = current_item if len(current_item) <= max_len else "..." + current_item[-(max_len-3):]
|
||||
status_line += f" | Current: {display_item}"
|
||||
|
||||
# Use \r to return to start of line, and ljust to clear old text
|
||||
sys.stdout.write(f"\r{status_line.ljust(150)}")
|
||||
sys.stdout.flush()
|
||||
|
||||
def sanitize_filename(name):
|
||||
"""Removes invalid characters and handles Unicode whitespace for Windows."""
|
||||
@@ -129,6 +138,9 @@ def download_file(download_url, local_path, expected_size):
|
||||
|
||||
def download_folder_recursive(app, drive_id, item_path, local_root_path, report):
|
||||
try:
|
||||
# Show progress for every folder we enter
|
||||
print_status(item_path)
|
||||
|
||||
headers = get_headers(app)
|
||||
encoded_path = quote(item_path)
|
||||
url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/root:/{encoded_path}:/children"
|
||||
@@ -151,15 +163,20 @@ def download_folder_recursive(app, drive_id, item_path, local_root_path, report)
|
||||
elif 'file' in item:
|
||||
stats["total_checked"] += 1
|
||||
download_url = item.get('@microsoft.graph.downloadUrl')
|
||||
|
||||
if not download_url:
|
||||
stats["failed"] += 1
|
||||
report.append({"Path": f"{item_path}/{item_name}", "Error": "No URL", "Timestamp": datetime.now().isoformat()})
|
||||
continue
|
||||
|
||||
# Update status for the file we are about to check/download
|
||||
print_status(f"{item_path}/{item_name}")
|
||||
|
||||
success, status = download_file(download_url, local_path, item['size'])
|
||||
if success:
|
||||
if status == "Downloaded":
|
||||
stats["downloaded"] += 1
|
||||
# Force a newline for actual downloads so they stay in history
|
||||
sys.stdout.write(f"\nDownloaded: {item_path}/{item_name}\n")
|
||||
else:
|
||||
stats["skipped"] += 1
|
||||
@@ -167,8 +184,6 @@ def download_folder_recursive(app, drive_id, item_path, local_root_path, report)
|
||||
stats["failed"] += 1
|
||||
sys.stdout.write(f"\nFAILED: {item_path}/{item_name} - {status}\n")
|
||||
report.append({"Path": f"{item_path}/{item_name}", "Error": status, "Timestamp": datetime.now().isoformat()})
|
||||
|
||||
print_progress()
|
||||
|
||||
except Exception as e:
|
||||
err_msg = f"Folder error: {str(e)}"
|
||||
@@ -192,6 +207,7 @@ def main():
|
||||
print("Step 1: Authenticating with Microsoft Entra ID...")
|
||||
app = create_msal_app(tenant_id, client_id, client_secret)
|
||||
|
||||
report = []
|
||||
try:
|
||||
print("Step 2: Connecting to SharePoint Site...")
|
||||
site_id = get_site_id(app, site_url)
|
||||
@@ -206,13 +222,13 @@ def main():
|
||||
local_folder_path = os.path.normpath(os.path.join(local_path_base, *folder_parts))
|
||||
|
||||
print(f"\nStep 4: Processing folder: {folder if folder else 'Root'}")
|
||||
download_folder_recursive(app, drive_id, folder, local_folder_path, report := [])
|
||||
download_folder_recursive(app, drive_id, folder, local_folder_path, report)
|
||||
|
||||
except Exception as e:
|
||||
print(f"\nCRITICAL ERROR: {e}")
|
||||
report = [{"Path": "GENERAL", "Error": str(e), "Timestamp": datetime.now().isoformat()}]
|
||||
report.append({"Path": "GENERAL", "Error": str(e), "Timestamp": datetime.now().isoformat()})
|
||||
|
||||
print_progress(force=True)
|
||||
print_status("Done!")
|
||||
|
||||
report_file = f"download_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
|
||||
with open(report_file, 'w', newline='', encoding='utf-8') as f:
|
||||
|
||||
Reference in New Issue
Block a user