From 9caf555a86940fc5901e7a10055cbebc32734073 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sun, 26 Oct 2025 11:27:54 +0000 Subject: [PATCH] nando 2 --- api/.php | 118 ++++++++++++++++++++++++++++++++++++++++++++++ api/config.php | 5 ++ api/handler.php | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ api/import.php | 16 +++++++ db/schema.sql | 52 +++++++++++++++++++++ db/setup.php | 22 +++++++++ 6 files changed, 335 insertions(+) create mode 100644 api/.php create mode 100644 api/config.php create mode 100644 api/handler.php create mode 100644 api/import.php create mode 100644 db/schema.sql create mode 100644 db/setup.php diff --git a/api/.php b/api/.php new file mode 100644 index 0000000..8955f41 --- /dev/null +++ b/api/.php @@ -0,0 +1,118 @@ +prepare("INSERT INTO leagues (id, name, country, logo, current_season) VALUES (:id, :name, :country, :logo, :current_season) ON DUPLICATE KEY UPDATE name=:name, country=:country, logo=:logo, current_season=:current_season"); + + foreach ($league_ids as $league_id) { + $leagues_data = call_api('leagues', ['id' => $league_id]); + + if (!$leagues_data || empty($leagues_data['response'])) { + echo "Failed to fetch data for league ID: $league_id\n"; + sleep(6); + continue; + } + + foreach ($leagues_data['response'] as $league_item) { + $league = $league_item['league']; + $country = $league_item['country']; + $current_season = 2023; // Hardcoded to 2023 due to free plan limitations + + $stmt->execute([ + ':id' => $league['id'], + ':name' => $league['name'], + ':country' => $country['name'], + ':logo' => $league['logo'], + ':current_season' => $current_season + ]); + echo "Imported league: " . $league['name'] . "\n"; + if ($current_season > 0) { + import_teams_by_league($league['id'], $current_season); + } + } + sleep(6); // Wait 6 seconds between each league to respect rate limit + } +} + +function import_teams_by_league($league_id, $season) { + $teams_data = call_api('teams', ['league' => $league_id, 'season' => $season]); + + if (!$teams_data || empty($teams_data['response'])) { + echo "Failed to fetch teams for league $league_id.\n"; + return; + } + + $pdo = db(); + $stmt = $pdo->prepare("INSERT INTO teams (id, name, logo) VALUES (:id, :name, :logo) ON DUPLICATE KEY UPDATE name=:name, logo=:logo"); + + foreach ($teams_data['response'] as $team_item) { + $team = $team_item['team']; + $stmt->execute([ + ':id' => $team['id'], + ':name' => $team['name'], + ':logo' => $team['logo'] + ]); + } + echo "Imported teams for league $league_id.\n"; +} + +function import_fixtures() { + $pdo = db(); + $leagues_stmt = $pdo->query("SELECT id, current_season FROM leagues WHERE current_season > 0"); + $leagues = $leagues_stmt->fetchAll(PDO::FETCH_ASSOC); + + foreach ($leagues as $league) { + $fixtures_data = call_api('fixtures', ['league' => $league['id'], 'season' => $league['current_season'], 'from' => date('Y-m-d'), 'to' => date('Y-m-d', strtotime('+1 week'))]); + + if (!$fixtures_data || empty($fixtures_data['response'])) { + echo "No fixtures found for league {$league['id']} for the next week.\n"; + sleep(6); + continue; + } + + $stmt = $pdo->prepare("INSERT INTO events (id, league_id, home_team_id, away_team_id, event_date, status, home_score, away_score) VALUES (:id, :league_id, :home_team_id, :away_team_id, :event_date, :status, :home_score, :away_score) ON DUPLICATE KEY UPDATE event_date=:event_date, status=:status, home_score=:home_score, away_score=:away_score"); + + foreach ($fixtures_data['response'] as $fixture) { + $stmt->execute([ + ':id' => $fixture['fixture']['id'], + ':league_id' => $fixture['league']['id'], + ':home_team_id' => $fixture['teams']['home']['id'], + ':away_team_id' => $fixture['teams']['away']['id'], + ':event_date' => $fixture['fixture']['date'], + ':status' => $fixture['fixture']['status']['short'], + ':home_score' => $fixture['goals']['home'], + ':away_score' => $fixture['goals']['away'] + ]); + } + echo "Imported fixtures for league {$league['id']}.\n"; + sleep(6); // Wait 6 seconds between each league to respect rate limit + } +} diff --git a/api/config.php b/api/config.php new file mode 100644 index 0000000..62172cc --- /dev/null +++ b/api/config.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/api/handler.php b/api/handler.php new file mode 100644 index 0000000..ee74105 --- /dev/null +++ b/api/handler.php @@ -0,0 +1,122 @@ +prepare("INSERT INTO leagues (id, name, country, logo, current_season) VALUES (:id, :name, :country, :logo, :current_season) ON DUPLICATE KEY UPDATE name=:name, country=:country, logo=:logo, current_season=:current_season"); + + foreach ($league_ids as $league_id) { + $leagues_data = call_api('leagues', ['id' => $league_id]); + + if (!$leagues_data || empty($leagues_data['response'])) { + echo "Failed to fetch data for league ID: $league_id\n"; + sleep(6); + continue; + } + + foreach ($leagues_data['response'] as $league_item) { + $league = $league_item['league']; + $country = $league_item['country']; + $current_season = 2023; // Hardcoded to 2023 due to free plan limitations + + $stmt->execute([ + ':id' => $league['id'], + ':name' => $league['name'], + ':country' => $country['name'], + ':logo' => $league['logo'], + ':current_season' => $current_season + ]); + echo "Imported league: " . $league['name'] . "\n"; + if ($current_season > 0) { + import_teams_by_league($league['id'], $current_season); + } + } + sleep(6); // Wait 6 seconds between each league to respect rate limit + } +} + +function import_teams_by_league($league_id, $season) { + $teams_data = call_api('teams', ['league' => $league_id, 'season' => $season]); + + if (!$teams_data || empty($teams_data['response'])) { + echo "Failed to fetch teams for league $league_id.\n"; + return; + } + + $pdo = db(); + $stmt = $pdo->prepare("INSERT INTO teams (id, name, logo) VALUES (:id, :name, :logo) ON DUPLICATE KEY UPDATE name=:name, logo=:logo"); + + foreach ($teams_data['response'] as $team_item) { + $team = $team_item['team']; + $stmt->execute([ + ':id' => $team['id'], + ':name' => $team['name'], + ':logo' => $team['logo'] + ]); + } + echo "Imported teams for league $league_id.\n"; +} + +function import_fixtures() { + $pdo = db(); + $leagues_stmt = $pdo->query("SELECT id, current_season FROM leagues WHERE current_season > 0"); + $leagues = $leagues_stmt->fetchAll(PDO::FETCH_ASSOC); + + foreach ($leagues as $league) { + $fixtures_data = call_api('fixtures', ['league' => $league['id'], 'season' => $league['current_season'], 'from' => date('Y-m-d'), 'to' => date('Y-m-d', strtotime('+1 week'))]); + + if (!$fixtures_data || empty($fixtures_data['response'])) { + echo "No fixtures found for league {" . $league['id'] . "} for the next week.\n"; + sleep(6); + continue; + } + + $stmt = $pdo->prepare("INSERT INTO events (id, league_id, home_team_id, away_team_id, event_date, status, home_score, away_score) VALUES (:id, :league_id, :home_team_id, :away_team_id, :event_date, :status, :home_score, :away_score) ON DUPLICATE KEY UPDATE event_date=:event_date, status=:status, home_score=:home_score, away_score=:away_score"); + + foreach ($fixtures_data['response'] as $fixture) { + $stmt->execute([ + ':id' => $fixture['fixture']['id'], + ':league_id' => $fixture['league']['id'], + ':home_team_id' => $fixture['teams']['home']['id'], + ':away_team_id' => $fixture['teams']['away']['id'], + ':event_date' => $fixture['fixture']['date'], + ':status' => $fixture['fixture']['status']['short'], + ':home_score' => $fixture['goals']['home'], + ':away_score' => $fixture['goals']['away'] + ]); + } + echo "Imported fixtures for league {" . $league['id'] . ".}\n"; + sleep(6); // Wait 6 seconds between each league to respect rate limit + } +} diff --git a/api/import.php b/api/import.php new file mode 100644 index 0000000..a6c6972 --- /dev/null +++ b/api/import.php @@ -0,0 +1,16 @@ +"; +echo "Starting import process...\n"; + +// 1. Import all leagues and their teams +import_leagues(); + +// 2. Import fixtures for the upcoming week +import_fixtures(); + +echo "Import process finished.\n"; +echo ""; \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql new file mode 100644 index 0000000..4e9b734 --- /dev/null +++ b/db/schema.sql @@ -0,0 +1,52 @@ +-- Database schema for the football predictions app + +DROP TABLE IF EXISTS `predictions`; +DROP TABLE IF EXISTS `tipsters`; +DROP TABLE IF EXISTS `events`; +DROP TABLE IF EXISTS `teams`; +DROP TABLE IF EXISTS `leagues`; + +CREATE TABLE IF NOT EXISTS `leagues` ( + `id` INT PRIMARY KEY, + `name` VARCHAR(255) NOT NULL, + `country` VARCHAR(255), + `logo` VARCHAR(255), + `current_season` INT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `teams` ( + `id` INT PRIMARY KEY, + `name` VARCHAR(255) NOT NULL, + `logo` VARCHAR(255) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `events` ( + `id` INT PRIMARY KEY, + `league_id` INT NOT NULL, + `home_team_id` INT NOT NULL, + `away_team_id` INT NOT NULL, + `event_date` DATETIME NOT NULL, + `status` VARCHAR(50), + `home_score` TINYINT, + `away_score` TINYINT, + FOREIGN KEY (`league_id`) REFERENCES `leagues`(`id`), + FOREIGN KEY (`home_team_id`) REFERENCES `teams`(`id`), + FOREIGN KEY (`away_team_id`) REFERENCES `teams`(`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `tipsters` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `name` VARCHAR(255) NOT NULL, + `accuracy_kpi` FLOAT DEFAULT 0 +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `predictions` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `event_id` INT NOT NULL, + `tipster_id` INT, + `prediction_type` VARCHAR(100) NOT NULL, -- e.g., '1X2', 'Over/Under', 'GG/NG' + `prediction_value` VARCHAR(100) NOT NULL, -- e.g., '1', 'Over 2.5', 'GG' + `status` ENUM('pending', 'won', 'lost') DEFAULT 'pending', + FOREIGN KEY (`event_id`) REFERENCES `events`(`id`), + FOREIGN KEY (`tipster_id`) REFERENCES `tipsters`(`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/db/setup.php b/db/setup.php new file mode 100644 index 0000000..4f91b02 --- /dev/null +++ b/db/setup.php @@ -0,0 +1,22 @@ + PDO::ERRMODE_EXCEPTION, + ]); + + // Create the database if it doesn't exist + $pdo->exec("CREATE DATABASE IF NOT EXISTS " . DB_NAME); + + // Now connect to the specific database + $pdo = db(); + + // Create the tables + $sql = file_get_contents(__DIR__ . '/schema.sql'); + $pdo->exec($sql); + echo "Database and tables created successfully."; +} catch (PDOException $e) { + die("Database setup failed: " . $e->getMessage()); +} \ No newline at end of file