#!/usr/bin/env python3 """ Debug Instagram context issue """ import os from pathlib import Path from dotenv import load_dotenv import instaloader load_dotenv() username = os.getenv('INSTAGRAM_USERNAME') password = os.getenv('INSTAGRAM_PASSWORD') target = os.getenv('INSTAGRAM_TARGET') print(f"Username: {username}") print(f"Target: {target}") # Test different loader creation approaches print("\n" + "="*50) print("Testing context availability:") print("="*50) # Method 1: Default loader print("\n1. Default Instaloader():") L1 = instaloader.Instaloader() print(f" Has context: {L1.context is not None}") print(f" Context type: {type(L1.context)}") # Method 2: With parameters print("\n2. Instaloader with params:") L2 = instaloader.Instaloader( quiet=True, download_pictures=False, download_videos=False ) print(f" Has context: {L2.context is not None}") # Method 3: After login print("\n3. After login:") L3 = instaloader.Instaloader() print(f" Before login - Has context: {L3.context is not None}") try: L3.login(username, password) print(f" After login - Has context: {L3.context is not None}") print(f" Context logged in: {L3.context.is_logged_in if L3.context else 'N/A'}") except Exception as e: print(f" Login failed: {e}") # Method 4: Test what our scraper does print("\n4. Testing our scraper pattern:") from src.base_scraper import ScraperConfig from src.instagram_scraper import InstagramScraper config = ScraperConfig( source_name='instagram', brand_name='hvacknowitall', data_dir=Path('test_data'), logs_dir=Path('test_logs'), timezone='America/Halifax' ) print("Creating scraper...") scraper = InstagramScraper(config) print(f" Scraper loader context: {scraper.loader.context is not None}") if scraper.loader.context: print(f" Context logged in: {scraper.loader.context.is_logged_in}") # Test if we can get a profile without error print("\n5. Testing profile fetch:") try: if scraper.loader.context: profile = instaloader.Profile.from_username(scraper.loader.context, target) print(f"✅ Got profile: @{profile.username}") else: print("❌ No context available") except Exception as e: print(f"❌ Profile fetch failed: {e}")