11"""
2- FPL Team Optimization API with MySQL Database Integration
2+ FPL Team Optimization API with PostgreSQL Database Integration
33=======================================================
44
55A comprehensive Fantasy Premier League team optimization API using enhanced ML models,
6- FPL-Elo-Insights data, and MySQL database for user management and data persistence.
6+ FPL-Elo-Insights data, and PostgreSQL database for user management and data persistence.
77
88Features:
99- Real-time FPL data integration
1010- Enhanced ML model with Elo-Insights data
11- - MySQL database integration
11+ - PostgreSQL database integration
1212- User authentication and management
1313- Team management with persistence
1414- Transfer suggestions
3030import time
3131import logging
3232from datetime import datetime
33- import mysql . connector
34- from mysql . connector import Error
33+ import psycopg2
34+ from psycopg2 import Error
3535import hashlib
3636import jwt
3737from functools import wraps
5151# Database Configuration from env vars
5252DB_CONFIG = {
5353 'host' : os .getenv ('DB_HOST' , 'localhost' ),
54- 'database' : os .getenv ('DB_NAME' , 'fantasy_soccer ' ),
55- 'user' : os .getenv ('DB_USER' , 'root ' ),
56- 'password' : os .getenv ('DB_PASSWORD' , 'NewPassword' ),
57- 'port' : int (os .getenv ('DB_PORT' , '3306 ' ))
54+ 'database' : os .getenv ('DB_NAME' , 'postgres ' ),
55+ 'user' : os .getenv ('DB_USER' , 'postgres ' ),
56+ 'password' : os .getenv ('DB_PASSWORD' ),
57+ 'port' : int (os .getenv ('DB_PORT' , '5432 ' ))
5858}
5959
6060# FPL API Configuration
@@ -79,12 +79,19 @@ class Config:
7979feature_columns = []
8080
8181def get_db_connection ():
82- """Get MySQL database connection"""
82+ """Get PostgreSQL database connection"""
8383 try :
84- connection = mysql .connector .connect (** DB_CONFIG )
84+ connection = psycopg2 .connect (
85+ host = DB_CONFIG ['host' ],
86+ port = DB_CONFIG ['port' ],
87+ user = DB_CONFIG ['user' ],
88+ password = DB_CONFIG ['password' ],
89+ database = DB_CONFIG ['database' ],
90+ sslmode = 'require'
91+ )
8592 return connection
8693 except Error as e :
87- logger .error (f"Error connecting to MySQL : { e } " )
94+ logger .error (f"Error connecting to PostgreSQL : { e } " )
8895 return None
8996
9097def load_ml_model ():
@@ -481,7 +488,7 @@ def add_player_to_team(current_user_id):
481488def health_check ():
482489 """Health check endpoint"""
483490 return jsonify ({
484- 'message' : 'FPL Team Optimization API with MySQL Database' ,
491+ 'message' : 'FPL Team Optimization API with PostgreSQL Database' ,
485492 'status' : 'healthy' ,
486493 'version' : '3.0.0' ,
487494 'model_loaded' : ml_model is not None ,
@@ -859,7 +866,7 @@ def internal_error(error):
859866if __name__ == '__main__' :
860867 # Load ML model on startup
861868 if load_ml_model ():
862- logger .info ("🚀 FPL Team Optimization API with MySQL starting..." )
869+ logger .info ("🚀 FPL Team Optimization API with PostgreSQL starting..." )
863870 app .run (debug = True , host = '0.0.0.0' , port = 5001 )
864871 else :
865872 logger .error ("❌ Failed to load ML model. API cannot start." )
0 commit comments