-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
93 lines (76 loc) · 3.8 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
async function getMatchData() {
try {
const response = await fetch("https://api.cricapi.com/v1/cricScore?apikey=a80b2e33-6764-4af7-8797-3a8dcc0e88ac");
const data = await response.json();
if (data.status !== "success") return;
const matchesList = data.data;
// Clear existing match data
document.getElementById("news").innerHTML = '';
if (!matchesList) return [];
// Filter out only the live matches
const liveMatches = matchesList.filter(match => match.ms === "live" && match.series === "Indian Premier League 2024");
if (liveMatches.length === 0) {
const matchesElement = document.getElementById("matches");
matchesElement.innerHTML = '<div class="team-stats" style="display: flex;font-size:1.6em; justify-content: center; align-items: flex-start; height: 100vh; padding-top: 20vh;">No live ipl matches</div>';
return; // Optionally return if needed, or continue execution
}
const relevantData = liveMatches.map(match => {
const { id, dateTimeGMT, t1, t2, t1s, t2s, t1img, t2img, series, status } = match;
return `<div class="match-name">${series}</div>
<div class="match-details">
<div class="team-details">
<span class="team-name">
<img class="team-img" src="${t1img}" />
${t1}</span>
<span class="team-stats">${t1s || "N/A"}</span>
</div>
<div class="team-details">
<span class="team-name">
<img class="team-img" src="${t2img}" />
${t2}</span>
<span class="team-stats">${t2s || "N/A"}</span>
</div>
<div class="match-status">${status}</div>
</div>`;
});
const matchesElement = document.getElementById("matches");
matchesElement.innerHTML = relevantData.map(match => `<li>${match}</li>`).join('');
return relevantData;
} catch (e) {
console.error(e);
}
}
async function getNewsData() {
try {
const response = await fetch("https://api.webz.io/newsApiLite?token=69529245-f932-4221-a082-9394ed9eebdc&q=Bitcoin");
const data = await response.json();
if (data.posts && data.posts.length > 0) {
const posts = data.posts.map(post => ({
title: post.thread.title,
url: post.thread.url,
site: post.thread.site,
date: post.thread.published
}));
const newsElement = document.getElementById("news");
// Clear existing match data
document.getElementById("matches").innerHTML = '';
// Display news data
newsElement.innerHTML = posts.map(post => `
<li>
<span class="team-stats" style="color: white;">${post.title}</span><br>
<a style="color: green;" href="${post.url}" target="_blank">${post.site}</a>
<span style="color: white; display: block; text-align: right;">${new Date(post.date).toLocaleDateString('en-US', { weekday: 'short', day: '2-digit', month: 'short', year: 'numeric' })}</span>
</li>
`).join('');
} else {
console.log("No posts found.");
}
} catch (error) {
console.error("Error fetching news data:", error);
}
}
document.getElementById("newsBtn").addEventListener("click", getNewsData);
document.getElementById("matchesBtn").addEventListener("click", getMatchData);
document.getElementById("refreshBtn").addEventListener("click", getMatchData);
// Initial load - show matches by default
getMatchData();