-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre-assemble-google-drive-post-takeout
72 lines (60 loc) · 2.64 KB
/
re-assemble-google-drive-post-takeout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import zipfile
import shutil
from pathlib import Path
from tqdm import tqdm
# Define the source and destination directories
SOURCE_DIR = Path("/path/to/source/folder")
DEST_DIR = Path("/path/to/destination/folder/Takeout")
# Create the destination directory if it does not exist
DEST_DIR.mkdir(parents=True, exist_ok=True)
def copy_files(src, dest):
for root, dirs, files in os.walk(src):
relative_path = os.path.relpath(root, src)
dest_path = dest / relative_path
dest_path.mkdir(parents=True, exist_ok=True)
for file in files:
src_file = Path(root) / file
dest_file = dest_path / file
if not dest_file.exists():
print(f"Copying {src_file} to {dest_file}")
shutil.copy2(src_file, dest_file)
else:
print(f"File {dest_file} already exists. Skipping.")
# List to keep track of successfully unzipped files
unzipped_files = []
# Loop through all zip files in the source directory in alphabetical order
for zip_file in sorted(SOURCE_DIR.glob("*.zip")):
print(f"Processing {zip_file}")
try:
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
# Get the list of files in the zip file
file_list = zip_ref.namelist()
total_files = len(file_list)
# Extract each zip file to a temporary directory
temp_dir = Path(zip_file.stem)
print(f"Extracting {zip_file} to {temp_dir}")
# Use tqdm to show progress bar
with tqdm(total=total_files, unit='file', desc=f'Extracting {zip_file.name}') as pbar:
for file in file_list:
zip_ref.extract(file, temp_dir)
pbar.update(1)
# Move the contents of the Takeout folder to the destination directory
takeout_dir = temp_dir / "Takeout"
if takeout_dir.exists():
print(f"Copying files from {takeout_dir} to {DEST_DIR}")
copy_files(takeout_dir, DEST_DIR)
else:
print(f"No Takeout directory found in {zip_file}")
# Clean up the temporary directory
print(f"Removing temporary directory {temp_dir}")
shutil.rmtree(temp_dir)
# Add the successfully unzipped file to the list
unzipped_files.append(zip_file.name)
except Exception as e:
print(f"Failed to process {zip_file}: {e}")
# Print the list of successfully unzipped files
print("\nSuccessfully unzipped files:")
for file in unzipped_files:
print(file)
print(f"\nAll zip files have been processed. Successfully unzipped files are listed above.")