-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_countries.py
More file actions
332 lines (280 loc) · 11.3 KB
/
generate_countries.py
File metadata and controls
332 lines (280 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
"""
Script pour générer countries.json pour GeoBluff.
Combine REST Countries API et World Bank API.
Usage:
python generate_countries.py
Crée countries.json avec ~195 pays contenant:
- name, capital, flag, population, area, gdp (+ autres catégories configurées)
"""
import json
import requests
import time
from pathlib import Path
from typing import Optional
def get_flag_emoji(iso2: str) -> str:
"""Convertit un code ISO2 en emoji drapeau."""
if not iso2 or len(iso2) != 2:
return "🏳️"
return "".join(chr(0x1F1E6 + ord(c) - ord('A')) for c in iso2.upper())
def fetch_rest_countries() -> dict:
"""Récupère les données depuis REST Countries API."""
print("📥 Récupération REST Countries API...")
url = "https://restcountries.com/v3.1/all"
params = {
"fields": "name,cca2,cca3,translations,capital,region,area,population,latlng",
}
headers = {"User-Agent": "GeoBluff/1.0 (+https://restcountries.com)"}
response = requests.get(url, params=params, headers=headers, timeout=60)
if response.status_code == 400:
response = requests.get(url, headers=headers, timeout=60)
response.raise_for_status()
data = response.json()
countries = {}
for country in data:
iso3 = country.get("cca3")
iso2 = country.get("cca2", "")
if not iso3:
continue
# Nom français si disponible
name_fr = country.get("translations", {}).get("fra", {}).get("common")
name = name_fr or country.get("name", {}).get("common", "")
name_en = country.get("name", {}).get("common", "")
# Capitale
capitals = country.get("capital", [])
capital = capitals[0] if capitals else ""
latlng = country.get("latlng") or []
latitude = latlng[0] if len(latlng) > 0 else None
longitude = latlng[1] if len(latlng) > 1 else None
countries[iso3] = {
"name": name,
"name_en": name_en,
"capital": capital,
"flag": get_flag_emoji(iso2),
"iso2": iso2,
"iso3": iso3,
"region": country.get("region", ""),
"area": country.get("area"),
"population": country.get("population"),
"latitude": latitude,
"longitude": longitude,
}
print(f" ✓ {len(countries)} pays")
return countries
def fetch_world_bank(indicator: str, name: str) -> dict:
"""Récupère un indicateur World Bank."""
print(f"📥 Récupération {name} (World Bank)...")
url = f"https://api.worldbank.org/v2/country/all/indicator/{indicator}"
params = {"format": "json", "per_page": 1000, "mrnev": 1}
result = {}
page = 1
while True:
params["page"] = page
try:
response = requests.get(url, params=params, timeout=30)
response.raise_for_status()
data = response.json()
if len(data) < 2 or not data[1]:
break
for item in data[1]:
if item.get("value") is not None:
iso3 = item.get("countryiso3code")
if iso3 and iso3 not in result:
result[iso3] = item["value"]
if page >= data[0].get("pages", 1):
break
page += 1
time.sleep(0.1)
except Exception as e:
print(f" ⚠ Erreur page {page}: {e}")
break
print(f" ✓ {len(result)} valeurs")
return result
# Capitales en français (principales traductions)
CAPITALES_FR = {
"AFG": "Kaboul", "DEU": "Berlin", "SAU": "Riyad", "ARE": "Abou Dabi",
"CHN": "Pékin", "JPN": "Tokyo", "KOR": "Séoul", "GBR": "Londres",
"ESP": "Madrid", "ITA": "Rome", "FRA": "Paris", "BEL": "Bruxelles",
"NLD": "Amsterdam", "CHE": "Berne", "AUT": "Vienne", "POL": "Varsovie",
"CZE": "Prague", "HUN": "Budapest", "ROU": "Bucarest", "GRC": "Athènes",
"TUR": "Ankara", "RUS": "Moscou", "UKR": "Kiev", "EGY": "Le Caire",
"MAR": "Rabat", "DZA": "Alger", "TUN": "Tunis", "LBY": "Tripoli",
"IRN": "Téhéran", "IRQ": "Bagdad", "LBN": "Beyrouth", "ISR": "Jérusalem",
"IND": "New Delhi", "PAK": "Islamabad", "BGD": "Dacca", "NPL": "Katmandou",
"VNM": "Hanoï", "THA": "Bangkok", "MYS": "Kuala Lumpur", "SGP": "Singapour",
"IDN": "Jakarta", "PHL": "Manille", "TWN": "Taipei", "HKG": "Hong Kong",
"AUS": "Canberra", "NZL": "Wellington", "USA": "Washington",
"CAN": "Ottawa", "MEX": "Mexico", "BRA": "Brasilia", "ARG": "Buenos Aires",
"CHL": "Santiago", "COL": "Bogota", "PER": "Lima", "VEN": "Caracas",
"DNK": "Copenhague", "SWE": "Stockholm", "NOR": "Oslo", "FIN": "Helsinki",
"PRT": "Lisbonne", "IRL": "Dublin", "ZAF": "Pretoria", "NGA": "Abuja",
"KEN": "Nairobi", "ETH": "Addis-Abeba", "GHA": "Accra", "SEN": "Dakar",
"CIV": "Yamoussoukro", "CMR": "Yaoundé", "AGO": "Luanda", "TZA": "Dodoma",
}
# Variantes acceptables des capitales (pour tolérance)
CAPITALE_VARIANTES = {
"Pékin": ["Pekin", "Beijing"],
"Séoul": ["Seoul"],
"Le Caire": ["Cairo", "Caire"],
"Athènes": ["Athenes", "Athens"],
"Copenhague": ["Copenhagen", "Kobenhavn"],
"Vienne": ["Vienna", "Wien"],
"Varsovie": ["Warsaw", "Warszawa"],
"Prague": ["Praha"],
"Bucarest": ["Bucharest", "Bucuresti"],
"Moscou": ["Moscow", "Moskva"],
"Kiev": ["Kyiv"],
"Téhéran": ["Tehran"],
"Bagdad": ["Baghdad"],
"Beyrouth": ["Beirut"],
"Jérusalem": ["Jerusalem"],
"Dacca": ["Dhaka"],
"Katmandou": ["Kathmandu"],
"Hanoï": ["Hanoi"],
"Mexico": ["Mexico City", "Ciudad de Mexico"],
"Brasilia": ["Brasília"],
"Lisbonne": ["Lisbon", "Lisboa"],
"Alger": ["Algiers"],
"Addis-Abeba": ["Addis Ababa"],
}
DEFAULT_CATEGORIES = [
{
"id": "population",
"label": "Population",
"source": "rest",
"field": "population",
},
{
"id": "area",
"label": "Superficie (km²)",
"source": "rest",
"field": "area",
},
{
"id": "gdp",
"label": "PIB ($)",
"source": "wb",
"indicator": "NY.GDP.MKTP.CD",
},
]
def load_categories_config(config_path: Path) -> dict:
if not config_path.exists():
return {
"enabled_categories": [c["id"] for c in DEFAULT_CATEGORIES],
"categories": DEFAULT_CATEGORIES,
}
with open(config_path, encoding="utf-8") as f:
return json.load(f)
def generate_countries_json(output: str = "countries.json"):
"""Génère le fichier countries.json."""
print("🌍 Génération de countries.json pour GeoBluff\n")
min_coverage = 150
config_path = Path(__file__).parent / "categories_config.json"
config = load_categories_config(config_path)
categories = config.get("categories", DEFAULT_CATEGORIES)
enabled = config.get("enabled_categories") or [c["id"] for c in categories]
enabled_set = set(enabled)
category_map = {c["id"]: c for c in categories if c["id"] in enabled_set}
# 1. Données de base
countries = fetch_rest_countries()
# 2. Indicateurs World Bank
wb_data = {}
disabled = []
for cat_id, cat in category_map.items():
if cat.get("source") == "wb":
indicator = cat.get("indicator")
if indicator:
wb_data[cat_id] = fetch_world_bank(indicator, cat.get("label", cat_id))
if len(wb_data[cat_id]) == 0:
disabled.append(cat_id)
elif cat.get("source") == "rest":
field = cat.get("field")
if field and not any(c.get(field) is not None for c in countries.values()):
disabled.append(cat_id)
if disabled:
for cat_id in disabled:
enabled_set.discard(cat_id)
category_map.pop(cat_id, None)
wb_data.pop(cat_id, None)
print(" ⚠ Catégories ignorées (pas de données): " + ", ".join(disabled))
# 3. Fusionner
print("\n🔄 Fusion des données...")
for iso3, c in countries.items():
# Categories depuis World Bank
for cat_id, values in wb_data.items():
if iso3 in values:
value = values[iso3]
if cat_id == "gdp":
value = int(value)
else:
value = float(value)
c[cat_id] = value
# Capitale en français
if iso3 in CAPITALES_FR:
c["capital_en"] = c["capital"]
c["capital"] = CAPITALES_FR[iso3]
else:
c["capital_en"] = c["capital"]
# Variantes de capitale
cap = c["capital"]
variantes = [cap, c["capital_en"]]
if cap in CAPITALE_VARIANTES:
variantes.extend(CAPITALE_VARIANTES[cap])
c["capital_variants"] = list(set(v for v in variantes if v))
# Categories depuis REST
for cat_id, cat in category_map.items():
if cat.get("source") == "rest":
field = cat.get("field")
if field and cat_id not in c:
c[cat_id] = c.get(field)
# 4. Appliquer la tolérance de couverture par catégorie
coverage = {
cat_id: sum(1 for c in countries.values() if c.get(cat_id) is not None)
for cat_id in enabled_set
}
low_coverage = [cat_id for cat_id, count in coverage.items() if count < min_coverage]
if low_coverage:
for cat_id in low_coverage:
enabled_set.discard(cat_id)
category_map.pop(cat_id, None)
wb_data.pop(cat_id, None)
print(
f" ⚠ Catégories ignorées (< {min_coverage} pays): "
+ ", ".join(low_coverage)
)
# 5. Filtrer pays valides
valid = [
c for c in countries.values()
if c.get("name") and all(c.get(cat_id) is not None for cat_id in enabled_set)
]
valid.sort(key=lambda x: x["name"])
# 5. Sauvegarder
print(f"\n💾 Sauvegarde de {len(valid)} pays...")
with open(output, "w", encoding="utf-8") as f:
json.dump(valid, f, ensure_ascii=False, indent=2)
# Stats
with_gdp = sum(1 for c in valid if c.get("gdp") is not None)
print(f"\n✅ {output} créé!")
print(f" - {len(valid)} pays")
if "gdp" in enabled_set:
print(f" - {with_gdp} avec PIB")
return valid
def show_categories():
"""Affiche les catégories disponibles pour le jeu."""
config_path = Path(__file__).parent / "categories_config.json"
config = load_categories_config(config_path)
categories = config.get("categories", DEFAULT_CATEGORIES)
enabled = config.get("enabled_categories") or [c["id"] for c in categories]
category_map = {c["id"]: c for c in categories if c["id"] in enabled}
print("\n📊 Catégories disponibles pour GeoBluff:")
for cat_id in enabled:
label = category_map.get(cat_id, {}).get("label", cat_id)
print(f" - {cat_id} : {label}")
if __name__ == "__main__":
countries = generate_countries_json()
# Aperçu
print("\n📋 Exemple (France):")
france = next((c for c in countries if c["iso3"] == "FRA"), None)
if france:
print(json.dumps(france, ensure_ascii=False, indent=2))
show_categories()