#!/usr/bin/env python3
"""Download Awin product feeds for sneaker shops."""
import requests
import gzip
import csv
import io
import json

TOKEN = 'bebd7839-013a-434d-a3da-7db2ecefb6b1'
PUB_ID = '374301'

# Awin product feed download URL format:
# https://productdata.awin.com/datafeed/list/apikey/{token}/language/nl/fid/{feedId}/columns/{cols}/format/csv/delimiter/%2C/compression/gzip/
# But we need to know the feed IDs first.

# Try the datafeed list API with proper auth
print("=== Try datafeed list with API key in URL ===")
r = requests.get(f'https://productdata.awin.com/datafeed/list/apikey/{TOKEN}', timeout=30)
print(f'Status: {r.status_code}')
print(f'Response: {r.text[:3000]}')

# Try alternative: get feeds for specific advertisers
sneaker_advertisers = {
    8193: 'JD Sports NL',
    8472: 'Schuurman Schoenen NL-BE',
    16092: 'Foot Locker NL',
    18468: 'Lounge by Zalando NL',
    77016: 'adidas NL',
    8646: 'UGG NL',
}

print("\n=== Try feed download per advertiser ===")
for adv_id, name in sneaker_advertisers.items():
    # Try getting feed list for this advertiser
    url = f'https://productdata.awin.com/datafeed/list/apikey/{TOKEN}/language/nl/cid/{adv_id}/format/csv/delimiter/%2C/compression/gzip/'
    r = requests.get(url, timeout=30, stream=True)
    print(f'\n[{adv_id}] {name}: {r.status_code} ({r.headers.get("content-type", "?")})')
    if r.status_code == 200:
        ct = r.headers.get('content-type', '')
        content = r.content
        print(f'  Size: {len(content)} bytes')
        if 'gzip' in ct or content[:2] == b'\x1f\x8b':
            try:
                text = gzip.decompress(content).decode('utf-8', errors='replace')
                lines = text.split('\n')
                print(f'  Lines: {len(lines)}')
                if lines:
                    print(f'  Header: {lines[0][:200]}')
                for line in lines[1:6]:
                    print(f'  {line[:200]}')
            except Exception as e:
                print(f'  Decompress error: {e}')
        else:
            text = content.decode('utf-8', errors='replace')
            print(f'  Content: {text[:1000]}')
    else:
        print(f'  Error: {r.text[:300]}')
