115 lines
2.2 KiB
Python
115 lines
2.2 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
|
||
|
|
import csv
|
||
|
|
import mysql.connector
|
||
|
|
|
||
|
|
# ==========================================
|
||
|
|
# CONFIGURATION
|
||
|
|
# ==========================================
|
||
|
|
|
||
|
|
CSV_FILE = "/app/isin.csv"
|
||
|
|
|
||
|
|
# Pays associé au fichier CSV
|
||
|
|
PAYS = "France"
|
||
|
|
|
||
|
|
MYSQL = {
|
||
|
|
"host": "db",
|
||
|
|
"port": 3306,
|
||
|
|
"user": "root",
|
||
|
|
"password": "sysadm-1963",
|
||
|
|
"database": "bourse"
|
||
|
|
}
|
||
|
|
|
||
|
|
# ==========================================
|
||
|
|
# CONNEXION MYSQL
|
||
|
|
# ==========================================
|
||
|
|
|
||
|
|
try:
|
||
|
|
db = mysql.connector.connect(**MYSQL)
|
||
|
|
print("Connexion MySQL OK")
|
||
|
|
except Exception as e:
|
||
|
|
print("Erreur MySQL :", e)
|
||
|
|
exit(1)
|
||
|
|
|
||
|
|
cursor = db.cursor(dictionary=True)
|
||
|
|
|
||
|
|
# ==========================================
|
||
|
|
# CHARGEMENT DU CSV
|
||
|
|
# ==========================================
|
||
|
|
|
||
|
|
print("Chargement du CSV...")
|
||
|
|
|
||
|
|
csv_data = {}
|
||
|
|
|
||
|
|
with open(CSV_FILE, newline="", encoding="utf-8-sig") as f:
|
||
|
|
|
||
|
|
reader = csv.reader(f, delimiter=";")
|
||
|
|
|
||
|
|
for row in reader:
|
||
|
|
|
||
|
|
if len(row) < 3:
|
||
|
|
continue
|
||
|
|
|
||
|
|
isin = row[0].strip().upper()
|
||
|
|
ticker = row[2].strip().upper()
|
||
|
|
|
||
|
|
if ticker:
|
||
|
|
csv_data[(ticker, PAYS.upper())] = isin
|
||
|
|
|
||
|
|
print(f"{len(csv_data)} ISIN chargés.")
|
||
|
|
|
||
|
|
# ==========================================
|
||
|
|
# LECTURE DE LA TABLE
|
||
|
|
# ==========================================
|
||
|
|
|
||
|
|
cursor.execute("""
|
||
|
|
SELECT
|
||
|
|
id,
|
||
|
|
ticker,
|
||
|
|
pays,
|
||
|
|
isin
|
||
|
|
FROM les_actions
|
||
|
|
""")
|
||
|
|
|
||
|
|
lignes = cursor.fetchall()
|
||
|
|
|
||
|
|
print(f"{len(lignes)} actions lues.")
|
||
|
|
|
||
|
|
# ==========================================
|
||
|
|
# MISE A JOUR
|
||
|
|
# ==========================================
|
||
|
|
|
||
|
|
sql_update = """
|
||
|
|
UPDATE les_actions
|
||
|
|
SET isin = %s
|
||
|
|
WHERE id = %s
|
||
|
|
"""
|
||
|
|
|
||
|
|
update_data = []
|
||
|
|
|
||
|
|
for ligne in lignes:
|
||
|
|
|
||
|
|
ticker = (ligne["ticker"] or "").strip().upper()
|
||
|
|
pays = (ligne["pays"] or "").strip().upper()
|
||
|
|
|
||
|
|
cle = (ticker, pays)
|
||
|
|
|
||
|
|
if cle in csv_data:
|
||
|
|
|
||
|
|
update_data.append((
|
||
|
|
csv_data[cle],
|
||
|
|
ligne["id"]
|
||
|
|
))
|
||
|
|
|
||
|
|
print(f"{len(update_data)} lignes à mettre à jour.")
|
||
|
|
|
||
|
|
if update_data:
|
||
|
|
cursor.executemany(sql_update, update_data)
|
||
|
|
db.commit()
|
||
|
|
|
||
|
|
print("===================================")
|
||
|
|
print(f"{len(update_data)} ISIN mis à jour.")
|
||
|
|
print("===================================")
|
||
|
|
|
||
|
|
cursor.close()
|
||
|
|
db.close()
|