#!/usr/bin/env python3
import json
import requests
import os
import time
from urllib.parse import urlparse

# Extract all image URLs from the message history
def extract_image_urls_from_messages():
    # This will contain all the URLs from the message history
    urls = []
    
    # Sample URLs from the recent message history (these are the raw URLs without query params)
    base_urls = [
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478497463076655185/s4v13-final-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478496253783441429/s4v12-slam-e.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478496222498259035/s4v12-slam-d.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478496178130649118/s4v12-slam-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478496143733428305/s4v12-slam-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478496110199836786/s4v12-slam-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478494946624278629/s4v11-israeli-e.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478494917407019221/s4v11-israeli-d.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478494876969603163/s4v11-israeli-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478494845843800104/s4v11-israeli-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478494799958118561/s4v11-israeli-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478493308765343909/s4v10-punch-f.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478493281703821475/s4v10-punch-e.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478493248225022103/s4v10-punch-d.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478493222035787989/s4v10-punch-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478493192885239809/s4v10-punch-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478493166351941643/s4v10-punch-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478492466435854467/Screenshot_2026-03-03_at_8.40.53_PM.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478492086264402031/s4v9-kid-f.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478492059458338856/s4v9-kid-e.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478492025409245244/s4v9-kid-d.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478491992810852382/s4v9-kid-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478491963669090304/s4v9-kid-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478491917955235951/s4v9-kid-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478490305773834390/s4v8-after-frust-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478490267555467324/s4v8-after-frust-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478490202916782242/s4v8-after-frust-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478490174991368305/s4v8-punch-real-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478490140312862862/s4v8-punch-real-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478490104589979740/s4v8-punch-real-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478489031669973317/s4v7-punch-e.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478488990163140839/s4v7-punch-d.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478488957368008814/s4v7-punch-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478488925692629145/s4v7-punch-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478488891299332137/s4v7-punch-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478487107570761911/s4v6-party-e.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478487081998094497/s4v6-party-d.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478487057566531766/s4v6-party-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478487027501764723/s4v6-party-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478487003854278729/s4v6-party-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478485618924454022/s4v5-b-flash.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478485596103114763/s4v5-a-flash.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478485567959470090/s4v5-c-recraft.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478485541178835167/s4v5-b-recraft.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478485514087829605/s4v5-a-recraft.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478482381894713395/s4v4-greenberg-d.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478482357538263213/s4v4-greenberg-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478482323711332556/s4v4-greenberg-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478482298235257022/s4v4-greenberg-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478481703151341648/s4v3-recraft-c.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478481680921792723/s4v3-recraft-b.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478481653096779797/s4v3-recraft-a.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478480404565590247/s4v2-recraft-2.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478480382654550090/s4v2-recraft-1.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478480343144333405/s4v2-imagen-2.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478480319597514812/s4v2-flash-2.png",
        "https://cdn.discordapp.com/attachments/1475846477589905530/1478480298621669621/s4v2-flash-1.png"
    ]
    
    return base_urls

def main():
    os.makedirs("public/visuals-channel-images", exist_ok=True)
    
    urls = extract_image_urls_from_messages()
    
    success_count = 0
    for i, url in enumerate(urls, 1):
        try:
            # Extract filename
            filename = url.split('/')[-1]
            if '?' in filename:
                filename = filename.split('?')[0]
            
            print(f"Downloading {i}/{len(urls)}: {filename}")
            
            # Try downloading without auth first (base URL)
            response = requests.get(url, timeout=30)
            
            if response.status_code == 200:
                with open(f"public/visuals-channel-images/{filename}", 'wb') as f:
                    f.write(response.content)
                success_count += 1
                print(f"✅ Downloaded: {filename}")
            else:
                print(f"❌ Failed {response.status_code}: {filename}")
                
        except Exception as e:
            print(f"❌ Error downloading {filename}: {e}")
            continue
            
        time.sleep(0.2)  # Be nice to Discord
    
    # Create zip if we got any files
    if success_count > 0:
        os.system('cd public && zip -r visuals-channel-complete.zip visuals-channel-images/')
        size = os.path.getsize("public/visuals-channel-complete.zip")
        print(f"\n✅ Created: public/visuals-channel-complete.zip")
        print(f"📊 Downloaded {success_count}/{len(urls)} images")
        print(f"📦 Zip size: {size/1024/1024:.1f} MB")
    else:
        print("❌ No images downloaded successfully")

if __name__ == "__main__":
    main()