Links to other leagues added

This commit is contained in:
Lucas Patenaude
2024-04-04 22:29:18 -06:00
parent 3983d00caf
commit ba6cd1ee7c
2 changed files with 47 additions and 45 deletions

View File

@@ -58,7 +58,7 @@
}
.team img {
width: 20px; /* Set the width of the image to 100% of its container */
height: auto; /* Maintain the aspect ratio */
max-width: 20px; /* Set the width of the image to 100% of its container */
max-height: 20px; /* Maintain the aspect ratio */
margin-right: 10px;
}

View File

@@ -3,59 +3,61 @@ const axios = require('axios');
// Middleware function to fetch matches data
const fetchMatchesData = async (req, res, next) => {
try
{
try {
const today = moment().format('YYYY-MM-DD'); // Get today's date in YYYY-MM-DD format
// Subtract one day to get yesterday's date
var yesterdayUnformatted = moment().subtract(1, 'days');
const yesterdayUnformatted = moment().subtract(1, 'days');
// Format yesterday's date as YYYY-MM-DD
var yesterday = yesterdayUnformatted.format('YYYY-MM-DD');
const yesterday = yesterdayUnformatted.format('YYYY-MM-DD');
// Array of years to fetch matches data
const years = [2002, 2003, 2021];
const response = await axios({
url: 'http://api.football-data.org/v4/competitions/2021/matches',
method: 'GET',
params:
{
dateFrom: yesterday, // Set dateFrom to today's date
dateTo: today, // Set dateTo to today's date
},
headers:
{
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
},
});
// Array to store all matches data
let allMatches = [];
// Extract relevant data from the API response
const matches = response.data.matches.map(match => ({
homeTeam:
{
name: match.homeTeam.tla,
crest: match.homeTeam.crest,
},
awayTeam:
{
name: match.awayTeam.tla,
crest: match.awayTeam.crest,
},
score:
{
homeScore: match.score.fullTime.home,
awayScore: match.score.fullTime.away,
},
minute: match.status, // Set the minute of the game
}));
// Loop through each year and fetch matches data
for (const year of years) {
const response = await axios({
url: `http://api.football-data.org/v4/competitions/${year}/matches`,
method: 'GET',
params: {
dateFrom: yesterday, // Set dateFrom to yesterday's date
dateTo: today, // Set dateTo to today's date
},
headers: {
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
},
});
// Attach matches data to res.locals
res.locals.matches = matches;
// Extract relevant data from the API response
const matches = response.data.matches.map(match => ({
homeTeam: {
name: match.homeTeam.tla,
crest: match.homeTeam.crest,
},
awayTeam: {
name: match.awayTeam.tla,
crest: match.awayTeam.crest,
},
score: {
homeScore: match.score.fullTime.home,
awayScore: match.score.fullTime.away,
},
minute: match.status, // Set the minute of the game
}));
// Concatenate matches data to allMatches array
allMatches = allMatches.concat(matches);
}
// Attach all matches data to res.locals
res.locals.matches = allMatches;
next();
}
catch (error)
{
console.error('Error fetching Premier League matches:', error);
} catch (error) {
console.error('Error fetching matches data:', error);
res.locals.matches = []; // Set an empty array if there's an error
next(); // Call next middleware or route handler
}