15app.get("/shared/*", c => serveFile(c.req.path, import.meta.url));
16
17// API endpoint to fetch today's matches
18app.get("/api/matches", async (c) => {
19 try {
20 const today = getTodayDateString();
21
22 // Fetch live matches from multiple sources
23 const matches = await fetchTodaysMatches();
24
25 const response: ApiResponse = {
30 return c.json(response);
31 } catch (error) {
32 console.error('Error fetching matches:', error);
33 return c.json({
34 matches: [],
35 lastUpdated: new Date().toISOString(),
36 error: 'Failed to fetch matches'
37 }, 500);
38 }
39});
40
41// API endpoint to fetch matches by date
42app.get("/api/matches/:date", async (c) => {
43 try {
44 const date = c.req.param('date');
45 const matches = await fetchMatchesByDate(date);
46
47 const response: ApiResponse = {
52 return c.json(response);
53 } catch (error) {
54 console.error('Error fetching matches by date:', error);
55 return c.json({
56 matches: [],
57 lastUpdated: new Date().toISOString(),
58 error: 'Failed to fetch matches for date'
59 }, 500);
60 }
67 const sendUpdate = async () => {
68 try {
69 const matches = await fetchTodaysMatches();
70 const liveMatches = matches.filter(m => m.status === 'live');
71
104});
105
106// Function to fetch matches by specific date
107async function fetchMatchesByDate(date: string): Promise<Match[]> {
108 try {
109 // Validate date format
113
114 const matches = await Promise.allSettled([
115 fetchFromFootballAPIByDate(date),
116 fetchFromSportsAPIByDate(date)
117 ]);
118
133 );
134 } catch (error) {
135 console.error('Error fetching matches by date:', error);
136 return [];
137 }
138}
139
140async function fetchFromFootballAPIByDate(date: string): Promise<Match[]> {
141 try {
142 const response = await fetch(`https://api.football-data.org/v4/matches?dateFrom=${date}&dateTo=${date}`, {
143 headers: {
144 'X-Auth-Token': Deno.env.get('FOOTBALL_DATA_API_KEY') || 'demo'
170}
171
172async function fetchFromSportsAPIByDate(date: string): Promise<Match[]> {
173 try {
174 const response = await fetch(`https://www.thesportsdb.com/api/v1/json/3/eventsday.php?d=${date}&s=Soccer`);
175
176 if (!response.ok) {
197}
198
199// Function to fetch today's matches from multiple sources
200async function fetchTodaysMatches(): Promise<Match[]> {
201 try {
202 // Try multiple free APIs for live sports data
203 const matches = await Promise.allSettled([
204 fetchFromFootballAPI(),
205 fetchFromSportsAPI(),
206 fetchFromOpenLigaDB()
207 ]);
208
229 );
230 } catch (error) {
231 console.error('Error in fetchTodaysMatches:', error);
232 return getMockMatches();
233 }
234}
235
236// Fetch from football-data.org (free tier)
237async function fetchFromFootballAPI(): Promise<Match[]> {
238 try {
239 const today = new Date().toISOString().split('T')[0];
240
241 // Using free tier without API key (limited data)
242 const response = await fetch(`https://api.football-data.org/v4/matches?dateFrom=${today}&dateTo=${today}`, {
243 headers: {
244 'X-Auth-Token': Deno.env.get('FOOTBALL_DATA_API_KEY') || 'demo'
270}
271
272// Fetch from free sports API
273async function fetchFromSportsAPI(): Promise<Match[]> {
274 try {
275 const today = new Date().toISOString().split('T')[0];
276
277 // Using free sports API
278 const response = await fetch(`https://www.thesportsdb.com/api/v1/json/3/eventsday.php?d=${today}&s=Soccer`);
279
280 if (!response.ok) {
301}
302
303// Fetch from OpenLigaDB (German football)
304async function fetchFromOpenLigaDB(): Promise<Match[]> {
305 try {
306 const today = new Date();
308
309 // Get current Bundesliga matches
310 const response = await fetch(`https://api.openligadb.de/getmatchdata/bl1/${year}`);
311
312 if (!response.ok) {
409
410 // Inject initial data to avoid extra round-trips
411 const initialMatches = await fetchTodaysMatches();
412 const initialData = {
413 matches: initialMatches,
427});
428
429export default app.fetch;