Spaces:
Sleeping
Sleeping
Download server.py from classVerse/scanningspace: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/spaces/classVerse/scanningspace/resolve/main/server.py
- Command line
-
hf download hf://spaces/classVerse/scanningspace/server.py
-
curl -L -o server.py https://huggingface.co/spaces/classVerse/scanningspace/resolve/main/server.py
3 kB
| 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 | |
| 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 |