This commit is contained in:
Flatlogic Bot 2025-10-26 11:27:54 +00:00
parent 7ba71298ae
commit 9caf555a86
6 changed files with 335 additions and 0 deletions

118
api/.php Normal file
View File

@ -0,0 +1,118 @@
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/../db/config.php';
function call_api($endpoint, $params = []) {
$url = API_FOOTBALL_ENDPOINT . $endpoint;
if (!empty($params)) {
$url .= '?'.http_build_query($params);
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'x-rapidapi-host: v3.football.api-sports.io',
'x-rapidapi-key: ' . API_FOOTBALL_KEY
]);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode != 200) {
error_log("API call failed for endpoint $endpoint with code $httpcode and response: $response");
return null;
}
return json_decode($response, true);
}
function import_leagues() {
$league_ids = [135, 136, 39, 40, 140, 141, 78, 79, 61, 62, 2, 3, 137];
$pdo = db();
$stmt = $pdo->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
}
}

5
api/config.php Normal file
View File

@ -0,0 +1,5 @@
<?php
// api/config.php
define('API_KEY', '7723f82472mshb50fe0dc46b0dcap12f861jsnc6c25a316ec8');
define('API_FOOTBALL_ENDPOINT', 'https://v3.football.api-sports.io/');
?>

122
api/handler.php Normal file
View File

@ -0,0 +1,122 @@
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/../db/config.php';
function call_api($endpoint, $params = []) {
$url = API_FOOTBALL_ENDPOINT . $endpoint;
if (!empty($params)) {
$url .= '?'.http_build_query($params);
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'x-rapidapi-host: v3.football.api-sports.io',
'x-rapidapi-key: ' . API_KEY
]);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP Code: $httpcode\n";
if ($httpcode != 200) {
$error_message = "API call failed for endpoint $endpoint with code $httpcode and response: $response";
error_log($error_message);
echo $error_message . "\n";
return null;
}
return json_decode($response, true);
}
function import_leagues() {
$league_ids = [135, 136, 39, 40, 140, 141, 78, 79, 61, 62, 2, 3, 137];
$pdo = db();
$stmt = $pdo->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
}
}

16
api/import.php Normal file
View File

@ -0,0 +1,16 @@
<?php
set_time_limit(300); // Increase execution time limit
require_once __DIR__ . '/handler.php';
echo "<pre>";
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 "</pre>";

52
db/schema.sql Normal file
View File

@ -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;

22
db/setup.php Normal file
View File

@ -0,0 +1,22 @@
<?php
require_once __DIR__ . '/config.php';
try {
// Connect to MySQL without specifying a database
$pdo = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => 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());
}