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= "1347930"
CAMP_ID = "1622230"
FEED_ID = "18588"  # Baskets

HEADERS = {"Authorization": f"Bearer {TOKEN}", "Accept": "*/*"}

# Try common Webgains CSV download patterns
patterns = [
    f"https://platform-api.webgains.com/auth/publishers/{PUBLISHER_ID}/feeds/{FEED_ID}/download",
    f"https://platform-api.webgains.com/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMP_ID}/feeds/{FEED_ID}/download",
    f"https://feeds.webgains.com/download/{PUBLISHER_ID}/{FEED_ID}",
    f"https://feeds.webgains.com/{PUBLISHER_ID}/{FEED_ID}.csv",
    f"https://productdata.webgains.com/feeds/{FEED_ID}",
    f"https://api.webgains.com/2.0/feeds/{FEED_ID}/download",
    # Try the non-auth feed endpoint
    f"https://platform-api.webgains.com/publishers/{PUBLISHER_ID}/campaigns/{CAMP_ID}/feeds/{FEED_ID}",
    # Try export endpoint
    f"https://platform-api.webgains.com/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMP_ID}/product-feeds/{FEED_ID}/export",
    f"https://platform-api.webgains.com/auth/publishers/{PUBLISHER_ID}/product-feeds/{FEED_ID}/export",
]

for url in patterns:
    try:
        r = requests.get(url, headers=HEADERS, timeout=10, allow_redirects=True, stream=True)
        ct = r.headers.get('content-type', '')
        print(f"{r.status_code} {ct[:50]:50s} {url}")
        if r.status_code == 200 and ('csv' in ct or 'text' in ct or 'octet' in ct):
            # Show first 500 bytes
            chunk = next(r.iter_content(500))
            print(f"  CONTENT: {chunk[:300]}")
        r.close()
    except Exception as e:
        print(f"ERR {str(e)[:50]:50s} {url}")
