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alculator:
|
| 5 |
+
CRITERIA = {
|
| 6 |
+
"condamnation_definitive": {"points": 50, "label": "Condamnation pénale définitive"},
|
| 7 |
+
"mise_en_examen": {"points": 30, "label": "Mise en examen en cours"},
|
| 8 |
+
"prise_illegale_interets": {"points": 40, "label": "Condamnation prise illégale d’intérêts"},
|
| 9 |
+
"rapport_accablant": {"points": 15, "label": "Affaires classées mais rapport accablant"},
|
| 10 |
+
"changement_parti": {"points": 20, "label": "Changement de parti (3+ fois)"},
|
| 11 |
+
"promesses_non_tenues": {"points": 25, "label": "Promesses non tenues > 70%"},
|
| 12 |
+
"cumul_mandats": {"points": 10, "label": "Cumul de mandats / Parachutage"},
|
| 13 |
+
"liens_lobbies": {"points": 15, "label": "Liens avec des lobbies"},
|
| 14 |
+
"revenu_suspect": {"points": 20, "label": "Revenu patrimonial suspect"},
|
| 15 |
+
"mensonge_parlement": {"points": 30, "label": "Mensonge avéré devant le Parlement"},
|
| 16 |
+
"détournement_fonds_publics": {"points": 45, "label": "Détournement de fonds publics"},
|
| 17 |
+
"vente_industrie_strategique": {"points": 75, "label": "Vente d'industrie stratégique (Trahison)"},
|
| 18 |
+
"ingerence_electorale": {"points": -30, "label": "Soupçon d'ingérence électorale / Acharnement"},
|
| 19 |
+
"atteinte_libertes": {"points": 60, "label": "Atteinte grave aux libertés publiques"},
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
@staticmethod
|
| 23 |
+
def calculate_score(casseroles):
|
| 24 |
+
"""
|
| 25 |
+
Calculate the total score based on a list of casserole keys.
|
| 26 |
+
"""
|
| 27 |
+
total_score = 0
|
| 28 |
+
details = []
|
| 29 |
+
|
| 30 |
+
for item in casseroles:
|
| 31 |
+
if item in CorruptoCalculator.CRITERIA:
|
| 32 |
+
crit = CorruptoCalculator.CRITERIA[item]
|
| 33 |
+
total_score += crit["points"]
|
| 34 |
+
details.append(crit)
|
| 35 |
+
|
| 36 |
+
return total_score, details
|
| 37 |
+
|
| 38 |
+
@staticmethod
|
| 39 |
+
def get_grade(score):
|
| 40 |
+
"""
|
| 41 |
+
Convert score to a grade (A-G) and color.
|
| 42 |
+
"""
|
| 43 |
+
if score <= 10:
|
| 44 |
+
return "A", "#006400" # Dark Green
|
| 45 |
+
elif score <= 25:
|
| 46 |
+
return "B", "#32CD32" # Green
|
| 47 |
+
elif score <= 45:
|
| 48 |
+
return "C", "#FFFF00" # Yellow
|
| 49 |
+
elif score <= 70:
|
| 50 |
+
return "D", "#FFA500" # Orange
|
| 51 |
+
elif score <= 100:
|
| 52 |
+
return "E", "#FF4500" # Red
|
| 53 |
+
elif score <= 150:
|
| 54 |
+
return "F", "#8B0000" # Dark Red
|
| 55 |
+
else:
|
| 56 |
+
return "G", "#000000" # Black
|
| 57 |
+
|
| 58 |
+
@staticmethod
|
| 59 |
+
def generate_politician_report(name_query, politicians_db, api_token=None):
|
| 60 |
+
"""
|
| 61 |
+
Search for a politician by name (case-insensitive) and return their full report.
|
| 62 |
+
Returns None if not found.
|
| 63 |
+
"""
|
| 64 |
+
name_query = name_query.lower().strip()
|
| 65 |
+
found_name = None
|
| 66 |
+
found_data = None
|
| 67 |
+
is_live = False
|
| 68 |
+
ai_analysis = None
|
| 69 |
+
|
| 70 |
+
# 1. Exact/Partial Match in DB
|
| 71 |
+
for name, data in politicians_db.items():
|
| 72 |
+
if name_query == name.lower():
|
| 73 |
+
found_name = name
|
| 74 |
+
found_data = data
|
| 75 |
+
break
|
| 76 |
+
if name_query in name.lower(): # Simple partial match
|
| 77 |
+
found_name = name
|
| 78 |
+
found_data = data
|
| 79 |
+
# Keep searching for exact match, otherwise take the first partial
|
| 80 |
+
|
| 81 |
+
# 2. Fallback to Live Investigation OR Force Live if Token Present
|
| 82 |
+
live_casseroles = []
|
| 83 |
+
|
| 84 |
+
# Always run live investigation if we have a token (to get fresh AI analysis)
|
| 85 |
+
# OR if we didn't find the person in the DB.
|
| 86 |
+
if not found_data or api_token:
|
| 87 |
+
search_name = found_name if found_name else name_query.title()
|
| 88 |
+
live_casseroles, ai_analysis = LiveInvestigator.investigate(search_name, api_token)
|
| 89 |
+
|
| 90 |
+
if not found_data:
|
| 91 |
+
found_name = search_name
|
| 92 |
+
found_data = {
|
| 93 |
+
"party": "Inconnu (Web)",
|
| 94 |
+
"casseroles": []
|
| 95 |
+
}
|
| 96 |
+
is_live = True
|
| 97 |
+
else:
|
| 98 |
+
# If found in DB but we ran live search, mark as "Live Update"
|
| 99 |
+
is_live = True
|
| 100 |
+
|
| 101 |
+
# Merge casseroles (Static + Live)
|
| 102 |
+
# We use a set to avoid duplicates if the same key is found in both
|
| 103 |
+
all_casseroles = set(found_data["casseroles"]) | set(live_casseroles)
|
| 104 |
+
|
| 105 |
+
# --- PERSISTENCE: Save results to DB ---
|
| 106 |
+
if found_name:
|
| 107 |
+
update_data = {
|
| 108 |
+
"party": found_data["party"],
|
| 109 |
+
"casseroles": list(all_casseroles)
|
| 110 |
+
}
|
| 111 |
+
if ai_analysis:
|
| 112 |
+
update_data["ai_analysis"] = ai_analysis
|
| 113 |
+
|
| 114 |
+
database.update_politician(found_name, update_data)
|
| 115 |
+
# ---------------------------------------
|
| 116 |
+
|
| 117 |
+
# 3. Calculate Score
|
| 118 |
+
score, details = CorruptoCalculator.calculate_score(list(all_casseroles))
|
| 119 |
+
|
| 120 |
+
# --- AI PENALTY INTEGRATION ---
|
| 121 |
+
if ai_analysis and ai_analysis.get('gravite', 0) > 0:
|
| 122 |
+
try:
|
| 123 |
+
gravite = float(ai_analysis['gravite'])
|
| 124 |
+
penalty = int(gravite * 10) # Multiplier x10
|
| 125 |
+
score += penalty
|
| 126 |
+
details.append({
|
| 127 |
+
"label": f"🤖 Verdict IA : {ai_analysis.get('type', 'Affaire détectée')}",
|
| 128 |
+
"points": penalty
|
| 129 |
+
})
|
| 130 |
+
except (ValueError, TypeError):
|
| 131 |
+
pass # Ignore if gravity is not a number
|
| 132 |
+
# ------------------------------
|
| 133 |
+
|
| 134 |
+
grade, color = CorruptoCalculator.get_grade(score)
|
| 135 |
+
|
| 136 |
+
# 4. Build Report
|
| 137 |
+
return {
|
| 138 |
+
"name": found_name,
|
| 139 |
+
"party": found_data["party"],
|
| 140 |
+
"score": score,
|
| 141 |
+
"grade": grade,
|
| 142 |
+
"color": color,
|
| 143 |
+
"casseroles_details": details,
|
| 144 |
+
"casseroles_count": len(details),
|
| 145 |
+
"is_live": is_live,
|
| 146 |
+
"ai_analysis": ai_analysis
|
| 147 |
+
}
|