Files
wevads-platform/scripts/get-image-host.py
2026-02-26 04:53:11 +01:00

144 lines
3.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""
GET IMAGE HOST FOR ISP
Retourne le meilleur hébergement d'images pour un ISP donné
"""
import psycopg2
import sys
import re
DB_CONFIG = {
'host': 'localhost',
'database': 'adx_system',
'user': 'admin',
'password': 'admin123'
}
def get_db():
return psycopg2.connect(**DB_CONFIG)
def detect_isp(email):
"""Detect ISP from email"""
conn = get_db()
cur = conn.cursor()
cur.execute("""
SELECT isp_name, isp_patterns
FROM admin.isp_hosting_rules
WHERE isp_name != 'other'
ORDER BY isp_name
""")
for isp_name, patterns in cur.fetchall():
for pattern in patterns:
# Convert SQL LIKE pattern to regex
regex = pattern.replace('%', '.*').replace('_', '.')
if re.match(regex, email, re.IGNORECASE):
conn.close()
return isp_name
conn.close()
return 'other'
def get_hosting_rules(isp):
"""Get hosting rules for ISP"""
conn = get_db()
cur = conn.cursor()
cur.execute("""
SELECT
accepts_s3_images, accepts_tracking_pixel,
preferred_image_hosts, blocked_image_hosts
FROM admin.isp_hosting_rules
WHERE isp_name = %s
""", (isp,))
result = cur.fetchone()
conn.close()
if result:
return {
's3_ok': result[0],
'pixel_ok': result[1],
'preferred': result[2] or [],
'blocked': result[3] or []
}
return {'s3_ok': True, 'pixel_ok': True, 'preferred': [], 'blocked': []}
def get_best_image_host(email):
"""Get best image host for email's ISP"""
isp = detect_isp(email)
rules = get_hosting_rules(isp)
conn = get_db()
cur = conn.cursor()
# Get available hosts
cur.execute("""
SELECT name, base_url
FROM admin.image_hosts
WHERE is_active = true
""")
hosts = {row[0]: row[1] for row in cur.fetchall()}
conn.close()
# Priority: preferred hosts first
for pref in rules['preferred']:
for host_name, host_url in hosts.items():
if pref.lower() in host_url.lower():
return {
'isp': isp,
'host': host_name,
'url': host_url,
's3_ok': rules['s3_ok'],
'pixel_ok': rules['pixel_ok']
}
# Fallback to any non-blocked host
for host_name, host_url in hosts.items():
blocked = False
for b in rules['blocked']:
if b.lower() in host_url.lower():
blocked = True
break
if not blocked:
return {
'isp': isp,
'host': host_name,
'url': host_url,
's3_ok': rules['s3_ok'],
'pixel_ok': rules['pixel_ok']
}
# Ultimate fallback
return {
'isp': isp,
'host': 'imgur',
'url': 'https://i.imgur.com/',
's3_ok': False,
'pixel_ok': rules['pixel_ok']
}
def main():
if len(sys.argv) < 2:
print("Usage: python3 get-image-host.py <email>")
print("\nExamples:")
for email in ['user@gmail.com', 'user@hotmail.com', 'user@yahoo.com', 'user@gmx.de', 'user@videotron.ca']:
result = get_best_image_host(email)
print(f" {email:<25} → ISP: {result['isp']:<10} | Host: {result['host']:<10} | S3: {result['s3_ok']} | Pixel: {result['pixel_ok']}")
return
email = sys.argv[1]
result = get_best_image_host(email)
print(f"Email: {email}")
print(f"ISP: {result['isp']}")
print(f"Best Host: {result['host']}")
print(f"Base URL: {result['url']}")
print(f"S3 OK: {result['s3_ok']}")
print(f"Tracking Pixel OK: {result['pixel_ok']}")
if __name__ == '__main__':
main()