Files
wevads-platform/scripts/sync-offers.py
2026-02-26 04:53:11 +01:00

130 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""
OFFER SYNC SYSTEM - Import offers from affiliate networks
"""
import psycopg2
import requests
from datetime import datetime
DB_CONFIG = {
'host': 'localhost',
'database': 'adx_system',
'user': 'admin',
'password': 'admin123'
}
def get_db():
return psycopg2.connect(**DB_CONFIG)
def sync_everflow(network_id, network_name, api_url, api_key):
"""Sync offers from Everflow API"""
print(f" Syncing Everflow API...")
headers = {'X-Eflow-API-Key': api_key}
try:
response = requests.get(f"{api_url}/v1/affiliates/alloffers", headers=headers, timeout=60)
if response.status_code == 200:
data = response.json()
offers = data.get('offers', [])
print(f" Found {len(offers)} offers")
result = []
for o in offers:
name = o.get('name', 'Unknown')
# Extract country code from name (e.g., "FR - Pre-Lander..." -> "FR")
country = name[:2] if len(name) >= 2 and name[1] == ' ' or (len(name) >= 3 and name[2] == ' ') else 'US'
if len(name) >= 2 and name[2] == ' ':
country = name[:2]
result.append({
'network_id': network_id,
'network_name': network_name,
'offer_id': str(o.get('network_offer_id')),
'offer_name': name[:255],
'country_code': country[:5],
'vertical': 'sweepstakes',
'tracking_url': o.get('tracking_url', '')[:1000]
})
return result
except Exception as e:
print(f" Error: {e}")
return []
def import_offers(offers, conn):
"""Import offers into existing table structure"""
cur = conn.cursor()
imported = 0
for o in offers:
try:
# Check if exists
cur.execute("SELECT id FROM admin.affiliate_offers WHERE offer_id = %s AND network_id = %s",
(o['offer_id'], o['network_id']))
exists = cur.fetchone()
if exists:
# Update
cur.execute("""
UPDATE admin.affiliate_offers SET
offer_name = %s,
tracking_url = %s,
updated_at = NOW()
WHERE offer_id = %s AND network_id = %s
""", (o['offer_name'], o['tracking_url'], o['offer_id'], o['network_id']))
else:
# Insert
cur.execute("""
INSERT INTO admin.affiliate_offers
(offer_id, network_id, offer_name, country_code, vertical, tracking_url, network, status, is_active)
VALUES (%s, %s, %s, %s, %s, %s, %s, 'active', true)
""", (o['offer_id'], o['network_id'], o['offer_name'], o['country_code'],
o['vertical'], o['tracking_url'], o['network_name']))
imported += 1
except Exception as e:
print(f" Insert error: {e}")
conn.commit()
return imported
def main():
print("=" * 50)
print("OFFER SYNC SYSTEM")
print(f"Started: {datetime.now()}")
print("=" * 50)
conn = get_db()
cur = conn.cursor()
cur.execute("""
SELECT id, name, api_url, api_key
FROM admin.affiliate_networks
WHERE sync_enabled = true AND api_url IS NOT NULL
""")
networks = cur.fetchall()
total_imported = 0
for net_id, name, api_url, api_key in networks:
print(f"\nNetwork: {name}")
offers = []
if 'eflow' in api_url.lower():
offers = sync_everflow(net_id, name, api_url, api_key)
if offers:
imported = import_offers(offers, conn)
total_imported += imported
print(f" Imported: {imported} offers")
cur.execute("UPDATE admin.affiliate_networks SET last_sync = NOW() WHERE id = %s", (net_id,))
conn.commit()
print(f"\n{'=' * 50}")
print(f"SYNC COMPLETE: {total_imported} offers imported")
print(f"{'=' * 50}")
conn.close()
if __name__ == '__main__':
main()