import requests
import sys
import json
sys.path.insert(0, r'C:\Users\Roel\clawd\scripts\demandgen-pipeline')
from config import WEBGAINS_API, PUBLISHER_ID, webgains_headers

# Get full feed details to find download URL
cid = 1622230
url = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{cid}/feeds'
r = requests.get(url, headers=webgains_headers(), params={'size': 100})
d = r.json()

# Print FULL first feed object
for f in d.get('data', []):
    count = f.get('products_successfully_stored', 0) or 0
    if count > 0:
        print(json.dumps(f, indent=2, default=str))
        break

# Try feed download endpoint patterns
print('\n--- Trying download endpoints ---')
feed_id = 16158  # Sneakermood
endpoints = [
    f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{cid}/feeds/{feed_id}/download',
    f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/feeds/{feed_id}/download',
    f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{cid}/feeds/{feed_id}',
    f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/feeds/{feed_id}',
]
for ep in endpoints:
    r = requests.get(ep, headers=webgains_headers(), allow_redirects=False)
    print(f'{r.status_code}: {ep}')
    if r.status_code in [200, 301, 302]:
        ct = r.headers.get('Content-Type', '')
        loc = r.headers.get('Location', '')
        print(f'  Content-Type: {ct}')
        if loc:
            print(f'  Location: {loc}')
        if 'json' in ct:
            print(f'  Body: {r.text[:300]}')
