#!/usr/bin/env python3
"""Try all TradeTracker affiliate sites to find sneaker feeds."""
import requests
import re

SOAP_URL = 'https://ws.tradetracker.com/soap/affiliate'
HEADERS = {'Content-Type': 'text/xml; charset=utf-8'}

soap_auth = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://schemas.tradetracker.com/affiliate">
  <soap:Body>
    <ns:authenticate>
      <ns:customerID>35058</ns:customerID>
      <ns:passphrase>bf0b9eef0da6caf55fe614518375280fe5c4ecca</ns:passphrase>
      <ns:sandbox>false</ns:sandbox>
      <ns:locale>nl_NL</ns:locale>
      <ns:demo>false</ns:demo>
    </ns:authenticate>
  </soap:Body>
</soap:Envelope>'''

r = requests.post(SOAP_URL, data=soap_auth, headers={**HEADERS, 'SOAPAction': 'authenticate'}, timeout=15)
cookies = r.cookies

# Try Manify and Ladify sites which are fashion/sneaker related
site_ids = [155885, 290654]  # Manify, Ladify

for site_id in site_ids:
    print(f'\n=== Site {site_id} ===')
    
    # Get campaigns
    soap = f'''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://schemas.tradetracker.com/affiliate">
  <soap:Body>
    <ns:getCampaigns>
      <ns:affiliateSiteID>{site_id}</ns:affiliateSiteID>
      <ns:campaignFilter/>
    </ns:getCampaigns>
  </soap:Body>
</soap:Envelope>'''
    r2 = requests.post(SOAP_URL, data=soap, headers={**HEADERS, 'SOAPAction': 'getCampaigns'}, cookies=cookies, timeout=30)
    campaigns = re.findall(r'<item[^>]*>(.*?)</item>', r2.text, re.DOTALL)
    print(f'Campaigns: {len(campaigns)}')
    
    sneaker_campaigns = []
    for c in campaigns:
        cid = re.search(r'<ID[^>]*>(.*?)</ID>', c)
        name = re.search(r'<name[^>]*>(.*?)</name>', c)
        if cid and name:
            cname = name.group(1)
            keywords = ['sneaker', 'schoen', 'shoe', 'sport', 'nike', 'adidas', 'puma', 'asics', 
                        'foot', 'keep', 'mooie', 'outlet', 'fashion', 'kleding']
            is_relevant = any(kw in cname.lower() for kw in keywords)
            if is_relevant:
                sneaker_campaigns.append((cid.group(1), cname))
                print(f'  *** [{cid.group(1)}] {cname}')
            else:
                print(f'  [{cid.group(1)}] {cname}')

    # Get product feeds
    soap_pf = f'''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://schemas.tradetracker.com/affiliate">
  <soap:Body>
    <ns:getProductFeeds>
      <ns:affiliateSiteID>{site_id}</ns:affiliateSiteID>
    </ns:getProductFeeds>
  </soap:Body>
</soap:Envelope>'''
    r3 = requests.post(SOAP_URL, data=soap_pf, headers={**HEADERS, 'SOAPAction': 'getProductFeeds'}, cookies=cookies, timeout=30)
    feeds = re.findall(r'<item[^>]*>(.*?)</item>', r3.text, re.DOTALL)
    print(f'Product feeds: {len(feeds)}')
    if r3.status_code != 200:
        print(f'  Error: {r3.status_code} - {r3.text[:500]}')
    for f in feeds:
        fid = re.search(r'<ID[^>]*>(.*?)</ID>', f)
        name = re.search(r'<name[^>]*>(.*?)</name>', f)
        pcount = re.search(r'<productCount[^>]*>(.*?)</productCount>', f)
        url_match = re.search(r'<URL[^>]*>(.*?)</URL>', f)
        if fid and name:
            print(f'  [{fid.group(1)}] {name.group(1)} [{pcount.group(1) if pcount else "?"} products]')
            if url_match:
                print(f'    URL: {url_match.group(1)[:200]}')
