File size: 3,003 Bytes
3fb80e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from fastapi import FastAPI, HTTPException, Header
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from curl_cffi import requests
from bs4 import BeautifulSoup
import json
import re

app = FastAPI()

# Allow your frontend to talk to this API
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], 
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# --- YOUR CUSTOM PASSWORD ---
MY_SECRET_API_KEY = "my_custom_scraper_key_007"

class ScrapeRequest(BaseModel):
    url: str

def fetch_links_clean(url: str):
    session = requests.Session()
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
        "Referer": "https://www.iaai.com/",
    }

    scraped_links = []

    try:
        response = session.get(url, impersonate="chrome120", headers=headers)
        if response.status_code != 200:
            return {"error": f"Failed to fetch IAA page. Status code: {response.status_code}"}

        soup = BeautifulSoup(response.text, 'html.parser')
        image_keys = []

        deep_zoom = soup.find("div", id="deepzoom")
        if deep_zoom and deep_zoom.get("dimensionsallimagekeys"):
            image_keys = json.loads(deep_zoom["dimensionsallimagekeys"])
        
        if not image_keys:
            matches = re.findall(r'\[\{"k":.*?"w":\d+,"h":\d+.*?\}\]', response.text)
            for m in matches:
                try:
                    data = json.loads(m)
                    if data and 'k' in data[0]:
                        image_keys = data
                        break
                except: continue

        for img in image_keys:
            key = img.get('k')
            if not key: continue

            primary_link = f"https://vis.iaai.com/resizer?imageKeys={key}&width=1024&height=768"
            check = session.head(primary_link, impersonate="chrome120", headers=headers)
            
            if check.status_code == 200:
                scraped_links.append(primary_link)
            else:
                fallback_link = f"https://vis.iaai.com/resizer?imageKeys={key}&width=845&height=633"
                scraped_links.append(fallback_link)

        return {"success": True, "links": scraped_links}

    except Exception as e:
        return {"success": False, "error": str(e)}

# The locked endpoint
@app.post("/api/get-images")
def get_images(request: ScrapeRequest, api_key: str = Header(None)):
    
    # Reject request if the API key is wrong or missing
    if api_key != MY_SECRET_API_KEY:
        raise HTTPException(status_code=401, detail="Unauthorized. Invalid API Key.")

    if not request.url.startswith("http"):
        raise HTTPException(status_code=400, detail="Invalid URL format.")
    
    result = fetch_links_clean(request.url)
    
    if "error" in result:
        raise HTTPException(status_code=500, detail=result["error"])
        
    return result