107 lines
5.3 KiB
Python
107 lines
5.3 KiB
Python
import subprocess
|
|
import sys
|
|
import argparse
|
|
from datetime import datetime, timedelta
|
|
from pathlib import Path
|
|
|
|
def get_access_token():
|
|
return "bearer EwAIBK1DBAAUnIIyw3fu//VwydnOZRl0mzVDtSEAAa3wFdyLrgrC3j2sOkOMd3hNpH8n3dYiP1vgycIPwjjzXom9dpAiplwB/4doYFX%2bvSJpKhKGpe9WwptRrjYG1uRxD0smkGkpLj/GWDqUDPfxyPr0RJwbdfSaLGW8kxEUSOVCzx4431HKY7lzJD5fb3UD7TONoUpwdLxzn/2p4j335Wqa4vmzWKmE6CspARSh3rb/9iNomEcm44rea7G%2bTewk2fuQTvcAPPv%2bRJ76sdxjfArcxXuDOEHzUq9vw72wIBLGN8o2T5HAk24dkPFFossGtUz4O47sObcnOufmJ8NZr8avVbQts6Wl6lgNSaScyEwgBbPg72l965eyoyAV6g8QZgAAEKFGOGv3PMlPVLjh66AKYurQAoyHnVIe%2bKx2pyhT3Fi4CCwuiJbOE4AEfWdYmNfzMzcsfpv90s5m7w9g3mIRLVUCKPuEwhDOWJFEsiWmi9RD%2bfhJfWeXa5BzRIVjXoz/oAvwKyGJc6VTMmPvLLh1MpvmWgqF9tpqBx6d28sXcqoXGya29MrQutOyd%2bUanqOggcgXl4Lhh3hvq0AdaEz1NgT0BHBMekcoLXbffUmMFF68rMa63f/4uCeH7S2SWd1IjflRMg9SnBX1PA0By0Z5MRNJLuYv44KgCazLbFXs6HtozaDy8TAY3m8QRMfPpu2oz1juaD4lPmmClZ47SyXoRqoy%2b4tfY/XWNmcl8T4o3uh9rE33b5e9gn4WMiQDE/n4OPlWa451y01IBSuuueBlUByDF6JQ7WtCAwbbaSANqA5uN35qI45sDlhWezqnBDeCbcAmpgcOutTao%2bNOtSe44icp1tj69TujEtzxq5W00TJe4EBz9WuYenZ0iJFgvT9WM2UMEHa%2bZEOFKjMWRutOU%2bv96eTjMiLPGpbkq9Wgq09B60kLE4wWq4/nbGmbItvxITQmMz5QCJi42kQaKSq3VUjKZudBpEeEBag0hRBZfO4E1S5x5FNeurqSXfbD4lrv6oZet5ysWUYILg8CeRycRJ8nXfpdWM0gwhLK30Fwz4KyR0oviUpne04QbDo87Fg20XKSuSzj4b8Pm1hvmPhtT1jdcaSNmExveIsAzpmxYal49JoVXS1jcQcFZ488gbWKMQ0jJ034DRl/0Bm9ETYrfoERbnBi1Gc8%2bm22%2bn67CPdq6hf%2bz9O8eJEEZZ7qoPSXE%2bUrGzu44gu%2bpp/17BRm/shxB%2b81le/8BPosyl2zew6Pa1amsIT2Flma7AYr7E33lsHj9yniWrbCNXUMuwQkvNp%2blkrHpHH9aKNHBNdWSU98xSrRf5ysz2GfbKC6K2aceujz44m94rnQxZkEFQQwMtJOb/EC"
|
|
|
|
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)
|
|
|
|
access_token = get_access_token()
|
|
|
|
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,
|
|
'-AccessToken', access_token
|
|
]
|
|
|
|
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) |