import subprocess import sys import argparse from datetime import datetime, timedelta from pathlib import Path def run_download_script(base_save_to: str, base_path_to_scan: str, target_date: datetime) -> bool: """ Execute the Download-ODLivePhotos.ps1 PowerShell script with year/month paths Args: base_save_to: Base destination path base_path_to_scan: Base source path in OneDrive target_date: Date to determine year/month """ # Format year and month year = target_date.strftime("%Y") month = target_date.strftime("%m") # Construct full paths save_to = str(Path(base_save_to) / year / month) path_to_scan = str(Path(base_path_to_scan) / year / month) try: # Get the script path (assuming it's in the same directory) script_path = Path(__file__).parent / "Download-ODLivePhotosV2.ps1" # Construct the PowerShell command cmd = [ 'powershell.exe', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', str(script_path), '-SaveTo', save_to, '-PathToScan', path_to_scan ] print(f"Processing {year}/{month}") print(f"Executing: {' '.join(cmd)}") result = subprocess.run( cmd, capture_output=True, text=True, check=True ) if result.stdout: print("Output:") print(result.stdout) return True except subprocess.CalledProcessError as e: print(f"Error executing PowerShell script:", file=sys.stderr) print(f"Exit code: {e.returncode}", file=sys.stderr) if e.stdout: print("Output:", file=sys.stderr) print(e.stdout, file=sys.stderr) if e.stderr: print("Error:", file=sys.stderr) print(e.stderr, file=sys.stderr) return False # Process current month (and last month if today is the 1st) # python run_ps_download.py --base-save-to "D:\Photos\OneDrive-Photo" --base-path-to-scan "\Photos\Auto-saved" # Process specific month (2 for last month, 3 for month before last, etc.) # python run_ps_download.py --base-save-to "D:\Photos\OneDrive-Photo" --base-path-to-scan "\Photos\Auto-saved" --months 2 if __name__ == "__main__": parser = argparse.ArgumentParser(description='Execute Download-ODLivePhotos.ps1') parser.add_argument('--base-save-to', required=True, help='Base destination path (year/month will be appended)') parser.add_argument('--base-path-to-scan', required=True, help='Base source path in OneDrive (year/month will be appended)') parser.add_argument('--months', type=int, default=0, help='Optional: specific month to process (1: current month, 2: last month, etc.). ' 'If not set: processes current month, plus last month if today is first day of month') args = parser.parse_args() # Get current date now = datetime.now() if args.months > 0: # Specific month was requested months_ago = args.months - 1 target_date = now.replace(day=1) - timedelta(days=months_ago * 28) success = run_download_script(args.base_save_to, args.base_path_to_scan, target_date) else: # Default behavior # Always process current month success = run_download_script(args.base_save_to, args.base_path_to_scan, now) # If it's the first day of the month, also process last month if now.day == 1: last_month = now.replace(day=1) - timedelta(days=1) success = success and run_download_script(args.base_save_to, args.base_path_to_scan, last_month) sys.exit(0 if success else 1)