import asyncio import os from datetime import datetime, timedelta from pathlib import Path from aiohttp import ClientSession from blinkpy.blinkpy import Blink from blinkpy.auth import Auth import logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s' ) class BlinkHistoryDownloader: def __init__(self, save_path: str): self.save_path = Path(save_path) self.blink: Blink | None = None self.downloaded_clips = 0 self.total_size = 0 self.session: ClientSession | None = None def format_size(self, size_bytes: int) -> str: for unit in ['B', 'KB', 'MB', 'GB']: if size_bytes < 1024: return f"{size_bytes:.2f} {unit}" size_bytes /= 1024 return f"{size_bytes:.2f} TB" async def setup(self) -> None: self.session = ClientSession() self.blink = Blink(session=self.session) auth = Auth({ "username": os.getenv("BLINK_USERNAME"), "password": os.getenv("BLINK_PASSWORD") }) self.blink.auth = auth await self.blink.start() async def download_all_videos(self) -> None: if not self.blink: return # Go back to 2022 start_date = "2022/01/01" logging.info(f"Starting download from {start_date}") # Create folder for all downloads self.save_path.mkdir(parents=True, exist_ok=True) try: # Use the original working method await self.blink.download_videos( str(self.save_path), since=start_date, delay=2 ) # Count downloaded files and size files = list(self.save_path.glob("**/*.mp4")) self.downloaded_clips = len(files) self.total_size = sum(f.stat().st_size for f in files) logging.info( f"Downloaded {self.downloaded_clips} clips " f"(Total size: {self.format_size(self.total_size)})" ) except Exception as e: logging.error(f"Error downloading videos: {e}") async def main(): base_path = Path(__file__).parent history_path = base_path / "historical_clips" downloader = BlinkHistoryDownloader(str(history_path)) try: await downloader.setup() await downloader.download_all_videos() logging.info( f"Download complete! Downloaded {downloader.downloaded_clips} clips " f"(Total size: {downloader.format_size(downloader.total_size)})" ) except Exception as err: logging.error(f"Error occurred: {err}") finally: if downloader.session: await downloader.session.close() if __name__ == "__main__": asyncio.run(main())