Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
370 lines (336 loc) · 18.1 KB
/
Copy pathscript.js
File metadata and controls
370 lines (336 loc) · 18.1 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// DOM elements
const viewContent = document.getElementById('view-content');
const refreshBtn = document.getElementById('refresh-btn');
const apiOptions = document.querySelectorAll('.api-option');
const viewTitle = document.querySelector('.view-title');
const chatMessages = document.getElementById('chat-messages');
const chatInput = document.getElementById('chat-input');
const sendMessageBtn = document.getElementById('send-message');
const menuToggle = document.querySelector('.menu-toggle');
const miniPlayer = document.querySelector('.mini-player');
// Current active API
let currentApi = 'dog';
// Generate random usernames for chat
const adjectives = ['Happy', 'Sunny', 'Clever', 'Brave', 'Mighty', 'Swift', 'Wise', 'Jolly'];
const nouns = ['Tiger', 'Eagle', 'Dolphin', 'Panda', 'Fox', 'Lion', 'Owl', 'Wolf'];
// Function to generate a random username
function generateUsername() {
const adj = adjectives[Math.floor(Math.random() * adjectives.length)];
const noun = nouns[Math.floor(Math.random() * nouns.length)];
const num = Math.floor(Math.random() * 1000);
return `${adj}${noun}${num}`;
}
// Store the generated username
const chatUsername = generateUsername();
// Menu toggle functionality
menuToggle.addEventListener('click', () => {
miniPlayer.classList.toggle('hidden');
});
// Fetch random dog image
async function fetchDog() {//dog.ceo
viewContent.innerHTML = '<p class="loading">Loading dog image...</p>';//loading message
try {//try-catch for error handling
const response = await fetch('https://dog.ceo/api/breeds/image/random');//fetch from dog API
const data = await response.json();//parse JSON response
if (data.status === 'success') {//check if response is successful
viewContent.innerHTML = `
<img src="${data.message}" alt="Random Dog" class="dog-img">
<button class="btn" onclick="fetchData('dog')">Another Dog</button>
`; //display dog image and button
} else {//handle unsuccessful response
throw new Error('Failed to fetch dog image');
}
} catch (error) {//catch block for errors
console.error('Failed to fetch dog image:', error);
viewContent.innerHTML = `<p class="error">Failed to fetch dog image${error.message ? ': ' + error.message : ''}</p>`;
}
}
// Fetch random cat image
async function fetchCat() {
viewContent.innerHTML = '<p class="loading">Loading cat image...</p>';
try {
const response = await fetch('https://api.thecatapi.com/v1/images/search');
const data = await response.json();
viewContent.innerHTML = `
<img src="${data[0].url}" alt="Random Cat" class="cat-img">
<button class="btn" onclick="fetchData('cat')">Another Cat</button>
`;
} catch (error) {
console.error('Failed to fetch cat image:', error);
viewContent.innerHTML = `<p class="error">Failed to fetch cat image${error.message ? ': ' + error.message : ''}</p>`;
}
}
// Fetch weather data
async function fetchWeather() {
viewContent.innerHTML = '<p class="loading">Loading weather data...</p>';
try {
// Using Open-Meteo API for Berlin
const response = await fetch('https://api.open-meteo.com/v1/forecast?latitude=13.4088&longitude=122.5615¤t=temperature_2m,relative_humidity_2m&forecast_days=1&timeformat=unixtime&wind_speed_unit=mph&temperature_unit=fahrenheit&precipitation_unit=inch');
const data = await response.json();
viewContent.innerHTML = `
<div class="weather-info">
<h3>Manila, Philippines</h3>
<p class="temp">${data.current.temperature_2m} ${data.current_units.temperature_2m}</p>
<p>Humidity level: ${data.current.relative_humidity_2m}%</p>
</div>
<button class="btn" onclick="fetchData('weather')">Refresh Weather</button>
`;
} catch (error) {
console.error('Failed to fetch weather data:', error);
viewContent.innerHTML = `<p class="error">Failed to fetch weather data${error.message ? ': ' + error.message : ''}</p>`;
}
}
// Fetch currency exchange rate
async function fetchCurrency() {
viewContent.innerHTML = '<p class="loading">Loading exchange rate...</p>';
try {
const response = await fetch('https://v6.exchangerate-api.com/v6/20a7ba2e90f93091b3ffe6d0/latest/USD');
const data = await response.json();
viewContent.innerHTML = `
<div class="currency-info">
<p>USD to PHP</p>
<p class="currency-rate">1 USD = ${data.conversion_rates.PHP} PHP</p>
<p>Last updated: ${new Date(data.time_last_update_utc).toLocaleDateString()}</p>
</div>
<button class="btn" onclick="fetchData('currency')">Refresh Rate</button>
`;
} catch (error) {
console.error('Failed to fetch exchange rate:', error);
viewContent.innerHTML = `<p class="error">Failed to fetch exchange rate${error.message ? ': ' + error.message : ''}</p>`;
}
}
// Fetch trending movies
async function fetchMovies() {
viewContent.innerHTML = '<p class="loading">Loading movie data...</p>';
try {
// Using TMDB API with the provided key
const response = await fetch(`https://api.themoviedb.org/3/trending/movie/day?api_key=67bd47cf58a4d5d38bbe49a1dc530323`);//fetch from TMDB API
const data = await response.json();//parse JSON response
let moviesHTML = `
<h3>Trending Movies Today</h3>
<ul class="movie-list">
`;//start building HTML for movie list
data.results.slice(0, 5).forEach(movie => {//limit to top 5 movies
moviesHTML += `
<li class="movie-item">
<i class="fas fa-film"></i> ${movie.title} (${new Date(movie.release_date).getFullYear()})
</li>
`;//add each movie to the list
});
moviesHTML += `
</ul>
<button class="btn" onclick="fetchData('movies')">Refresh Movies</button>
`;//close the movie list HTML
viewContent.innerHTML = moviesHTML;//set the inner HTML to display movies
} catch (error) {//catch block for errors
console.error('Failed to fetch movies:', error);
viewContent.innerHTML = `<p class="error">Failed to fetch movies. Please try again later.${error.message ? ': ' + error.message : ''}</p>`;
}
}
// Fetch GitHub user data
async function fetchGitHubUser(username = 'justinwols88'){//default username to 'justinwols88'
viewContent.innerHTML = '<p class="loading">Loading GitHub user...</p>';//loading message
try {//try-catch for error handling
const response = await fetch(`https://api.github.com/users/${username}`);//fetch from GitHub API
if (!response.ok) {//check if response is not ok
throw new Error('User not found');//throw error if user not found
}
const data = await response.json();//parse JSON response
viewContent.innerHTML = `
<div class="github-user">
<div class="github-search">
<input type="text" id="github-username" placeholder="Enter GitHub username" value="${username}">
<button onclick="fetchGitHubUser(document.getElementById('github-username').value)">Search</button>
</div>
<img src="${data.avatar_url}" alt="${data.login}" class="avatar">
<h2>${data.name || data.login}</h2>
<p>${data.bio || 'No bio available'}</p>
<div class="user-stats">
<div class="stat">
<div class="stat-number">${data.public_repos}</div>
<div class="stat-label">Repositories</div>
</div>
<div class="stat">
<div class="stat-number">${data.followers}</div>
<div class="stat-label">Followers</div>
</div>
<div class="stat">
<div class="stat-number">${data.following}</div>
<div class="stat-label">Following</div>
</div>
</div>
<div class="user-details">
${data.company ? `<div class="detail-item"><i class="fas fa-building"></i> ${data.company}</div>` : ''}
${data.location ? `<div class="detail-item"><i class="fas fa-map-marker-alt"></i> ${data.location}</div>` : ''}
${data.blog ? `<div class="detail-item"><i class="fas fa-link"></i> <a href="${data.blog}" target="_blank">Website</a></div>` : ''}
${data.twitter_username ? `<div class="detail-item"><i class="fab fa-twitter"></i> @${data.twitter_username}</div>` : ''}
</div>
<a href="${data.html_url}" target="_blank" class="profile-link">View Profile</a>
</div>
`;//display user info
} catch (error) {
console.error('Failed to fetch GitHub user:', error);//log error to console
viewContent.innerHTML = `
<div class="github-user">
<div class="github-search">
<input type="text" id="github-username" placeholder="Enter GitHub username">
<button onclick="fetchGitHubUser(document.getElementById('github-username').value)">Search</button>
</div>
<p class="error">${error.message || 'Failed to fetch user data'}</p>
<p>Please check the username and try again.</p>
</div>
`;//display error message and search box
}
}
// Fetch a random joke
async function fetchJoke() {
viewContent.innerHTML = '<p class="loading">Loading joke...</p>';
try {
const response = await fetch('https://v2.jokeapi.dev/joke/Any?type=single');
const data = await response.json();
viewContent.innerHTML = `
<p class="joke-text">${data.joke}</p>
<button class="btn" onclick="fetchData('joke')">Another Joke</button>
`;//display joke and button
} catch (error) {
console.error('Failed to fetch joke:', error);
viewContent.innerHTML = `<p class="error">Failed to fetch joke${error.message ? ': ' + error.message : ''}</p>`;//display error message
}
}
// Fetch public APIs data
async function fetchPublicAPI() {
viewContent.innerHTML = '<p class="loading">Loading API data...</p>';
try {
const response = await fetch('https://api.publicapis.org/entries?category=development&https=true');
const data = await response.json();
let apiHTML = `
<h3>Featured Public APIs</h3>
<ul class="api-list">
`;
data.entries.slice(0, 5).forEach(api => {
apiHTML += `
<li class="api-item"><i class="fas fa-plug"></i> ${api.API} - ${api.Description}</li>
`;
});
apiHTML += `
</ul>
<button class="btn" onclick="fetchData('public')">Refresh APIs</button>
`;
viewContent.innerHTML = apiHTML;
} catch (error) {
console.error('Failed to fetch API data:', error);
// Fallback to mock data if API fails
viewContent.innerHTML = `
<h3>Featured Public APIs</h3>
<ul class="api-list">
<li class="api-item"><i class="fas fa-paw"></i> Dog API - Collection of dog images</li>
<li class="api-item"><i class="fas fa-cloud-sun"></i> Open-Meteo - Weather data</li>
<li class="api-item"><i class="fas fa-money-bill-wave"></i> ExchangeRate-API - Currency exchange rates</li>
<li class="api-item"><i class="fas fa-film"></i> TMDB API - Movie database</li>
<li class="api-item"><i class="fab fa-github"></i> GitHub API - User and repo information</li>
</ul>
<button class="btn" onclick="fetchData('public')">Refresh APIs</button>
`;
}
}//end of fetchPublicAPI
// Function to fetch data based on API type
function fetchData(apiType) {
currentApi = apiType;
// Update view title
const apiTitles = {
dog: 'Random Dog API',
cat: 'Random Cat API',
weather: 'Weather API',
currency: 'Currency API',
movies: 'Movies API',
github: 'GitHub API',
joke: 'Joke API',
public: 'Public APIs'
};
viewTitle.textContent = apiTitles[apiType];
// Call the appropriate fetch function
switch(apiType) {
case 'dog':
fetchDog();
break;
case 'cat':
fetchCat();
break;
case 'weather':
fetchWeather();
break;
case 'currency':
fetchCurrency();
break;
case 'movies':
fetchMovies();
break;
case 'github':
fetchGitHubUser();
break;
case 'joke':
fetchJoke();
break;
case 'public':
fetchPublicAPI();
break;
}
}
// Set up event listeners for API options
apiOptions.forEach(option => {
option.addEventListener('click', () => {
// Remove active class from all options
apiOptions.forEach(opt => opt.classList.remove('active'));
// Add active class to clicked option
option.classList.add('active');
// Fetch data for selected API
const apiType = option.getAttribute('data-api');
fetchData(apiType);
});
});
// Set up event listener for refresh button
refreshBtn.addEventListener('click', () => {
fetchData(currentApi);
});
// Set up event listener for chat
sendMessageBtn.addEventListener('click', sendChatMessage);
chatInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendChatMessage();
}
});
// Function to send chat message
function sendChatMessage() {
const message = chatInput.value.trim();
if (message) {
const messageElement = document.createElement('div');
messageElement.classList.add('message');
messageElement.innerHTML = `<span class="user">${chatUsername}:</span> ${message}`;
chatMessages.appendChild(messageElement);
chatInput.value = '';
// Auto-scroll to the latest message
chatMessages.scrollTop = chatMessages.scrollHeight;
// Simulate occasional responses
if (Math.random() > 0.7) {
setTimeout(() => {
const responses = [
"Nice to meet you!",
"Hello there!",
"How's everyone doing?",
"This dashboard is pretty cool!",
"I love exploring these APIs"
];
const randomResponse = responses[Math.floor(Math.random() * responses.length)];
const responseElement = document.createElement('div');
responseElement.classList.add('message');
responseElement.innerHTML = `<span class="user">ChatBot:</span> ${randomResponse}`;
chatMessages.appendChild(responseElement);
chatMessages.scrollTop = chatMessages.scrollHeight;
}, 1000);
}
}
}
// Initialize with dog API
document.addEventListener('DOMContentLoaded', () => {
fetchData('dog');
});