forked from lucaspatenaude/ScoreSpot
New folder for middleware created. File structure reorganized
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
// Convert finished matches to "FT"
|
||||
const convert_time = (req, res, next) => {
|
||||
try {
|
||||
// Access matches data from res.locals
|
||||
const matches = res.locals.matches;
|
||||
|
||||
// Loop through matches and convert "FINISHED" to "FT" for minute
|
||||
const convertedMatches = matches.map(match => {
|
||||
if (match.minute === "FINISHED") {
|
||||
match.minute = "FT";
|
||||
}
|
||||
else if (match.minute === "IN_PLAY") {
|
||||
match.minute = "IP"
|
||||
}
|
||||
else if (match.minute === "TIMED") {
|
||||
match.minute = "TM";
|
||||
}
|
||||
return match;
|
||||
});
|
||||
|
||||
// Update res.locals with converted matches
|
||||
res.locals.matches = convertedMatches;
|
||||
|
||||
// Proceed to the next middleware/route handler
|
||||
next();
|
||||
} catch (error) {
|
||||
// If an error occurs, log it and proceed to the next middleware/route handler
|
||||
console.error('Error converting finished matches:', error);
|
||||
next();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = convert_time;
|
||||
@@ -0,0 +1,66 @@
|
||||
const moment = require('moment');
|
||||
const axios = require('axios');
|
||||
|
||||
// Middleware function to fetch matches data
|
||||
const fetchMatchesData = async (req, res, next) => {
|
||||
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
|
||||
const yesterdayUnformatted = moment().subtract(4, 'days');
|
||||
|
||||
// Format yesterday's date as YYYY-MM-DD
|
||||
const yesterday = yesterdayUnformatted.format('YYYY-MM-DD');
|
||||
|
||||
// Array of years to fetch matches data
|
||||
const league_ids = [2021]; /* Readd , 2002, 2014, 2019, 2015, 2013 */
|
||||
|
||||
// Array to store all matches data
|
||||
let allMatches = [];
|
||||
|
||||
// Loop through each year and fetch matches data
|
||||
for (const league_id of league_ids) {
|
||||
const response = await axios({
|
||||
url: `http://api.football-data.org/v4/competitions/2021/matches`, /* Resinsert ${league_id} for 2021 */
|
||||
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
|
||||
},
|
||||
});
|
||||
|
||||
// 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 matches data:', error);
|
||||
res.locals.matches = []; // Set an empty array if there's an error
|
||||
next(); // Call next middleware or route handler
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = fetchMatchesData;
|
||||
Reference in New Issue
Block a user