63 lines
2.7 KiB
Python
63 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Import Everflow offers into WEVADS DB"""
|
|
import requests, psycopg2, json
|
|
|
|
r = requests.get('https://api.eflow.team/v1/affiliates/alloffers',
|
|
headers={'X-Eflow-API-Key': 'b0PPowhR3CI9EtcqtLHA'}, timeout=30)
|
|
ef_offers = r.json().get('offers', [])
|
|
|
|
conn = psycopg2.connect(host='localhost', database='adx_system', user='admin', password='admin123')
|
|
cur = conn.cursor()
|
|
|
|
imported = 0
|
|
updated = 0
|
|
for o in ef_offers:
|
|
name = o.get('name', '')
|
|
noid = o.get('network_offer_id')
|
|
tracking = o.get('tracking_url', '')
|
|
if o.get('offer_status') != 'active' or not tracking:
|
|
continue
|
|
|
|
country = name[:2] if len(name) > 2 and name[2] in [' ', '-'] else 'WW'
|
|
|
|
cur.execute("SELECT id FROM affiliate.offers WHERE production_id = %s", (str(noid),))
|
|
existing = cur.fetchone()
|
|
|
|
if existing:
|
|
cur.execute("UPDATE affiliate.offers SET offer_url = %s WHERE id = %s", (tracking, existing[0]))
|
|
cur.execute("UPDATE affiliate.links SET value = %s WHERE offer_id = %s AND type = 'preview'", (tracking, existing[0]))
|
|
updated += 1
|
|
print(f" UPD #{existing[0]}: {name[:50]}")
|
|
else:
|
|
cur.execute("SELECT COALESCE(MAX(id),0)+1 FROM affiliate.offers")
|
|
nid = cur.fetchone()[0]
|
|
|
|
cur.execute(
|
|
"INSERT INTO affiliate.offers (id, status, affiliate_network_id, affiliate_network_name, name, offer_url, production_id, campaign_id, countries, type, payout, expiration_date, available_days, created_by, created_date) "
|
|
"VALUES (%s, 'Activated', 6, 'Double M', %s, %s, %s, %s, %s, 'CC', 0, '2027-12-31', '1234567', 'system', CURRENT_DATE)",
|
|
(nid, name, tracking, str(noid), str(noid), country))
|
|
|
|
cur.execute("SELECT COALESCE(MAX(id),0)+1 FROM affiliate.links")
|
|
lid = cur.fetchone()[0]
|
|
cur.execute(
|
|
"INSERT INTO affiliate.links (id, status, affiliate_network_id, offer_id, creative_id, type, value, created_by, created_date) "
|
|
"VALUES (%s, 'Activated', 6, %s, 0, 'preview', %s, 'system', CURRENT_DATE)",
|
|
(lid, nid, tracking))
|
|
|
|
prio = 2 if country == 'DE' else 1
|
|
cur.execute(
|
|
"INSERT INTO admin.brain_offer_config (offer_id, priority, is_active, link_status, link_http_code) "
|
|
"VALUES (%s, %s, true, 'live', 204) "
|
|
"ON CONFLICT (offer_id) DO UPDATE SET link_status='live', link_http_code=204, is_active=true",
|
|
(nid, prio))
|
|
|
|
imported += 1
|
|
print(f" NEW #{nid}: [{country}] {name[:50]}")
|
|
|
|
conn.commit()
|
|
print(f"\nImported: {imported} new, Updated: {updated}")
|
|
cur.execute("SELECT COUNT(*) FROM affiliate.offers WHERE status='Activated'")
|
|
print(f"Total active: {cur.fetchone()[0]}")
|
|
cur.close(); conn.close()
|
|
|