import os import zipfile import pandas as pd from config import settings class ZipExporter: def __init__(self): self.base_dir = settings.DOWNLOAD_DIR def export_zip(self, metadata_df: pd.DataFrame, output_zip_path: str): """Exports all downloaded files into a single ZIP archive.""" if not os.path.exists(output_zip_path): os.makedirs(output_zip_path, exist_ok=True) print(f"\nšŸ“¦ Starting ZIP export to: {output_zip_path}") with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zf: for index, row in metadata_df.iterrows(): file_path = row['download_path'] if os.path.exists(file_path): # Add the file to the zip, using only the relative path inside the zip arcname = os.path.relpath(file_path, self.base_dir) zf.write(file_path, arcname) print(f" - Added: {arcname}") else: print(f" - Warning: File missing during zip export: {file_path}") print(f"šŸŽ‰ ZIP Export Complete! Archive saved at: {output_zip_path}") return True def export_csv(self, metadata_df: pd.DataFrame, output_csv_path: str): """Exports the metadata DataFrame to a CSV file.""" if not metadata_df.empty: metadata_df.to_csv(output_csv_path, index=False) print(f"šŸ’¾ CSV Export Complete! Metadata saved to: {output_csv_path}") return True print("āš ļø Metadata DataFrame is empty. CSV export skipped.") return False