#!/usr/bin/env python3
"""Try alternative Webgains feed download endpoints"""
import requests, json

API = "https://platform-api.webgains.com"
PUB = "1347930"
TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxMDAwMzEiLCJqdGkiOiIwNjM5ODEwYzQ0YzM1MTBlZmZiMGRkYWNiM2EyNWFlNTg2YzFmNmQ5ODNjNGQ2MDA4NzkwZmM0NTkzY2EyNjg4ZGViZDE4NTgwOThmYjJjYiIsImlhdCI6MTc3MTUxNjc4OS44MzAxMzUsIm5iZiI6MTc3MTUxNjc4OS44MzAxMzcsImV4cCI6MjA4NzA0OTU4OS44MjcwODgsInN1YiI6IjIwMTcwOCIsInNjb3BlcyI6W119.ZxlCDsyBI7_SNUWjO-PUxDYKvQVfuUBzU5n-SgqHdAZCnOj9253yVcJIS5NRXJLrnyx0E_9bn8ymfXS71cvPISA4hPAlKCSMjdkv9f2k2FFwdAlQQ6mdhBB2m9dzHUIn34QwA8s4-ikB1ATKIZeqiJL-3Gh9eiMYPpvaJdCCjN-oGPMhhlehkgA04cZSgwy6HfKgb6uPvUSG9w_tvN8U06uEHKV11DedQHNWWGzuXpd2FEL3baV0z9KjSNMZsJZHx2yHUb6gVWJwaR7_louahMvrPXG-YLgnGvNMcPDPjm-z5HJ-JKcPv9_FBqAYGItZxgeGgVoS8MVhW6lc1a-6CvYtVanfXF42MPPNP-Qq76qRtHPLRBdkIATMuBxVJrMzlprT3n4F0sUUltGR4ixZuzMUm5LeyPMHgmjy2UfYzsl9eVqx0oJCmKUyY9maBXwFLJa8J1NHM_nWp3hPpCGkMyhzHK1DIf_z8Wirg1hp5vKts93eERWJBlQwhucUUS_KnZeC_gVaQ7kT4jCwaEWkpPxPxyXWRoK0mcYS_JGz3JbpVhRLj5DVYIB9k54DXqjYlCx8ezJZE9sf2SX3VWvPvD1xSQD1caTCm0To0B-MkeU1YpEfWdUslpCJXrpOy2Wbs8iDHR1BzNrMnOEr4zbz_EoB4JJe3tGTGjzNLNIw6XI"
H = {"Authorization": f"Bearer {TOKEN}"}
CAMP = "1622230"
FEED = "17108"  # Van Arendonk NL new - 21850 products

# Try various endpoints
endpoints = [
    # Products endpoint with large size
    (f"{API}/auth/publishers/{PUB}/campaigns/{CAMP}/feeds/products?feeds[]={FEED}&size=5000", "Large size"),
    # Try size=10000
    (f"{API}/auth/publishers/{PUB}/campaigns/{CAMP}/feeds/products?feeds[]={FEED}&size=10000", "Size 10000"),
    # Try CSV export  
    (f"{API}/auth/publishers/{PUB}/campaigns/{CAMP}/feeds/products/export?feeds[]={FEED}", "Export"),
    # Try download
    (f"{API}/auth/publishers/{PUB}/campaigns/{CAMP}/feeds/products/download?feeds[]={FEED}", "Download"),
    # Try feed-specific product endpoint
    (f"{API}/auth/publishers/{PUB}/campaigns/{CAMP}/feeds/{FEED}/products?size=5000", "Feed products"),
]

for url, label in endpoints:
    try:
        r = requests.get(url, headers=H, timeout=30)
        ct = r.headers.get("content-type", "")
        print(f"\n{label}: {r.status_code} ({ct})")
        if r.status_code == 200:
            if "json" in ct:
                data = r.json()
                if isinstance(data, dict):
                    total = data.get("recordsTotal", "?")
                    got = len(data.get("data", []))
                    print(f"  recordsTotal={total}, got={got}")
                    if got > 0:
                        print(f"  First: {data['data'][0].get('title', '')[:60]}")
                        print(f"  Last: {data['data'][-1].get('title', '')[:60]}")
                else:
                    print(f"  Response type: {type(data)}, len: {len(data) if hasattr(data, '__len__') else '?'}")
            else:
                print(f"  Size: {len(r.content)} bytes")
                print(f"  Preview: {r.text[:300]}")
        else:
            print(f"  Body: {r.text[:200]}")
    except Exception as e:
        print(f"{label}: ERROR {e}")

# Also try the old Webgains feed URL format
print("\n=== Trying classic Webgains feed URLs ===")
classic_urls = [
    f"https://feeds.webgains.com/feeds/{FEED}.csv",
    f"https://feeds.webgains.com/{PUB}/{FEED}.csv",
    f"https://productdata.webgains.com/feeds/{FEED}",
]
for url in classic_urls:
    try:
        r = requests.get(url, headers=H, timeout=10, allow_redirects=True)
        print(f"  {url} -> {r.status_code} ({r.headers.get('content-type','')}) size={len(r.content)}")
        if r.status_code == 200:
            print(f"  Preview: {r.text[:300]}")
    except Exception as e:
        print(f"  {url} -> ERROR {e}")
