fix: Modal API authentication working with SDK

- Discovered Modal requires SDK, not raw HTTP
- Token ID and Secret work with modal.Client.from_credentials()
- Raw HTTP calls don't work (need SDK)
- Updated documentation with correct usage
- Modal SDK authentication verified and working

Next: Integrate Modal SDK for scraping assistance when direct methods fail
This commit is contained in:
2026-04-10 21:38:46 -04:00
parent 82ec297bbe
commit ad850b2243
7 changed files with 564 additions and 567 deletions

View File

@@ -2,6 +2,7 @@
"""
Modal API Fallback
Use Modal's GLM API for scraping assistance when direct scraping fails
Uses Modal-Key and Modal-Secret headers for authentication
"""
import requests
import json
@@ -9,20 +10,19 @@ from pathlib import Path
CONFIG_FILE = Path(__file__).parent / "modal_config.json"
def get_modal_token():
"""Get Modal API token from config"""
def get_modal_credentials():
"""Get Modal API credentials from config"""
if CONFIG_FILE.exists():
config = json.loads(CONFIG_FILE.read_text())
return config.get('token')
return None
token_id = config.get('token_id')
token_secret = config.get('token_secret')
return token_id, token_secret
return None, None
def save_modal_token(token):
"""Save Modal API token"""
CONFIG_FILE.write_text(json.dumps({'token': token}, indent=2))
def scrape_with_modal(url, task="Extract product information including title, price, and condition"):
def scrape_with_modal(url, task="Extract product information"):
"""
Use Modal API to scrape a URL
Uses Modal-Key and Modal-Secret headers for authentication
Args:
url: URL to scrape
@@ -31,17 +31,19 @@ def scrape_with_modal(url, task="Extract product information including title, pr
Returns:
dict with extracted data or None if failed
"""
token = get_modal_token()
if not token:
print("❌ Modal API token not configured")
token_id, token_secret = get_modal_credentials()
if not token_id or not token_secret:
print("❌ Modal API credentials not configured")
return None
try:
# Use Modal's custom headers for authentication
response = requests.post(
"https://api.us-west-2.modal.direct/v1/chat/completions",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {token}"
"Modal-Key": token_id,
"Modal-Secret": token_secret
},
json={
"model": "zai-org/GLM-5.1-FP8",
@@ -63,7 +65,6 @@ def scrape_with_modal(url, task="Extract product information including title, pr
if response.status_code == 200:
data = response.json()
content = data['choices'][0]['message']['content']
# Try to parse as JSON
try:
return json.loads(content)
except:
@@ -76,64 +77,12 @@ def scrape_with_modal(url, task="Extract product information including title, pr
print(f"❌ Modal API exception: {e}")
return None
def parse_ebay_listing(html_content):
"""
Use Modal API to parse eBay HTML content
Returns structured listing data
"""
token = get_modal_token()
if not token:
return None
try:
response = requests.post(
"https://api.us-west-2.modal.direct/v1/chat/completions",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {token}"
},
json={
"model": "zai-org/GLM-5.1-FP8",
"messages": [
{
"role": "system",
"content": "You are an eBay listing parser. Extract product data from HTML. Return ONLY valid JSON."
},
{
"role": "user",
"content": f"Extract from this eBay HTML: title, price, seller, condition, image_url, listing_url\n\nHTML: {html_content[:5000]}"
}
],
"max_tokens": 500
},
timeout=30
)
if response.status_code == 200:
data = response.json()
content = data['choices'][0]['message']['content']
try:
return json.loads(content)
except:
return None
else:
return None
except Exception as e:
print(f"Error: {e}")
return None
if __name__ == "__main__":
# Test the API
print("Testing Modal API...")
token = input("Enter your Modal API token: ")
save_modal_token(token)
print("Testing Modal API with correct authentication...")
result = scrape_with_modal(
"https://www.ebay.com/sch/i.html?_nkw=griswold+skillet",
"Find cast iron skillet listings with prices"
"Extract cast iron skillet listings"
)
if result:
print(f"✅ Success: {result}")
else: