-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop_five.php
More file actions
283 lines (236 loc) · 9.28 KB
/
Copy pathtop_five.php
File metadata and controls
283 lines (236 loc) · 9.28 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
<?php
require_once './includes/config.php';
require_once './includes/navbar.php';
$pdo = new PDO(
"mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME,
DB_USER,
DB_PASS
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
function getTopArtists($pdo, $startDate, $endDate, $limit = 5)
{
$query = "SELECT artist_name, artist_uri, SUM(seconds_played) / 60 AS minutes_listened
FROM artists
WHERE artist_name IS NOT NULL
AND TRIM(artist_name) != ''";
$params = [];
if (!empty($startDate) && !empty($endDate)) {
$query .= " AND played_at BETWEEN :start_date AND :end_date";
$params[':start_date'] = $startDate . " 00:00:00";
$params[':end_date'] = $endDate . " 23:59:59";
}
$query .= " GROUP BY artist_name, artist_uri
ORDER BY minutes_listened DESC
LIMIT :limit";
$stmt = $pdo->prepare($query);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getTopSongs($pdo, $startDate, $endDate, $limit = 5)
{
$query = "SELECT song, song_uri, SUM(seconds_played) / 60 AS minutes_listened
FROM playbacks
WHERE 1=1";
$params = [];
if (!empty($startDate) && !empty($endDate)) {
$query .= " AND played_at BETWEEN :start_date AND :end_date";
$params[':start_date'] = $startDate . " 00:00:00";
$params[':end_date'] = $endDate . " 23:59:59";
}
$query .= " GROUP BY song, song_uri
ORDER BY minutes_listened DESC
LIMIT :limit";
$stmt = $pdo->prepare($query);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getTopPopularSongs($pdo, $startDate, $endDate, $limit = 5)
{
$query = "SELECT song, song_uri, MAX(track_popularity) AS track_popularity, SUM(seconds_played) / 60 AS minutes_listened
FROM playbacks
WHERE song_uri IS NOT NULL";
$params = [];
if (!empty($startDate) && !empty($endDate)) {
$query .= " AND played_at BETWEEN :start_date AND :end_date";
$params[':start_date'] = $startDate . " 00:00:00";
$params[':end_date'] = $endDate . " 23:59:59";
}
$query .= " GROUP BY song, song_uri
ORDER BY track_popularity DESC
LIMIT :limit";
$stmt = $pdo->prepare($query);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getTopNicheSongs($pdo, $startDate, $endDate, $limit = 5)
{
$query = "SELECT
song,
AVG(track_popularity) AS track_popularity,
(SELECT song_uri
FROM playbacks AS p2
WHERE p2.song = p1.song
ORDER BY ABS(track_popularity - (SELECT AVG(track_popularity) FROM playbacks WHERE song = p1.song))
LIMIT 1) AS song_uri,
SUM(seconds_played) / 60 AS minutes_listened
FROM playbacks AS p1
WHERE track_popularity > 0
AND track_popularity IS NOT NULL
AND song_uri IS NOT NULL";
$params = [];
if (!empty($startDate) && !empty($endDate)) {
$query .= " AND played_at BETWEEN :start_date AND :end_date";
$params[':start_date'] = $startDate . " 00:00:00";
$params[':end_date'] = $endDate . " 23:59:59";
}
$query .= " GROUP BY song
ORDER BY track_popularity ASC
LIMIT :limit";
$stmt = $pdo->prepare($query);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function spotifyUriToLink($uri) {
// Check if the URI is null or empty
if (empty($uri)) {
return '#'; // Return a fallback link or placeholder
}
// Ensure the URI has the correct format (spotify:artist:id or spotify:track:id)
$parts = explode(':', $uri);
if (count($parts) === 3) {
$type = $parts[1];
$id = $parts[2];
return "https://open.spotify.com/$type/$id";
}
// If URI is not in expected format, return a placeholder
return '#';
}
$defaultStartDate = date('Y-m-d', strtotime('-365 days'));
$defaultEndDate = date('Y-m-d');
$startDate = isset($_GET['start_date']) && !empty($_GET['start_date']) ? $_GET['start_date'] : $defaultStartDate;
$endDate = isset($_GET['end_date']) && !empty($_GET['end_date']) ? $_GET['end_date'] : $defaultEndDate;
$topArtists = getTopArtists($pdo, $startDate, $endDate);
$topSongs = getTopSongs($pdo, $startDate, $endDate);
$topPopularSongs = getTopPopularSongs($pdo, $startDate, $endDate);
$topNicheSongs = getTopNicheSongs($pdo, $startDate, $endDate);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Top Five</title>
<link rel="stylesheet" href="./assets/css/top_five.css">
<link rel="stylesheet" href="./assets/css/navbar.css">
</head>
<body>
<header>
<div class="navbar">
<div class="menu">Menu</div>
<div class="nav-links">
<?php renderNavbar("top_five.php"); ?>
</div>
</div>
</header>
<div class="main-content">
<!-- Filter Form -->
<form method="get" action="top_five.php" style="margin-bottom: 20px; display: flex; gap: 15px; align-items: center;">
<div>
<label for="start_date" style="color: #b3b3b3; font-weight: bold;">Start Date:</label>
<input type="date" id="start_date" name="start_date" value="<?= htmlspecialchars($startDate) ?>" class="date-input">
</div>
<div>
<label for="end_date" style="color: #b3b3b3; font-weight: bold;">End Date:</label>
<input type="date" id="end_date" name="end_date" value="<?= htmlspecialchars($endDate) ?>" class="date-input">
</div>
<button type="submit" class="btn-submit">Filter</button>
<!-- Reset Button -->
<button type="button" onclick="window.location.href='top_five.php';" class="btn-reset">Reset</button>
</form>
<h1>Top Five</h1>
<div class="top-content">
<div class="top-row">
<div class="top-column top-artists">
<h2>Top Artists</h2>
<ul>
<?php foreach ($topArtists as $artist): ?>
<li>
<a href="<?= spotifyUriToLink($artist['artist_uri']) ?>" class="spotify-link" target="_blank">
<?= htmlspecialchars($artist['artist_name']) ?>
</a>
(<?= ceil($artist['minutes_listened']) ?> minutes)
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="top-column top-songs">
<h2>Top Songs</h2>
<ul>
<?php foreach ($topSongs as $song): ?>
<li>
<a href="./search.php?song=<?= formatsonguri($song['song_uri']) ?>" class="spotify-link">
<?= htmlspecialchars($song['song']) ?>
</a>
(<?= ceil($song['minutes_listened']) ?> minutes)
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<div class="top-row">
<div class="top-column top-popular-songs">
<h2>Top Popular Songs</h2>
<ul>
<?php foreach ($topPopularSongs as $song): ?>
<li>
<?php if (isset($song['song_uri'])): ?>
<a href="./search.php?song=<?= formatsonguri($song['song_uri']) ?>" class="spotify-link">
<?= htmlspecialchars($song['song']) ?>
</a>
<?php else: ?>
<?= htmlspecialchars($song['song']) ?>
<?php endif; ?>
(Popularity Score: <?= htmlspecialchars($song['track_popularity']) ?>)
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="top-column top-niche-songs">
<h2>Top Niche Songs</h2>
<ul>
<?php foreach ($topNicheSongs as $song): ?>
<li>
<?php if (isset($song['song_uri'])): ?>
<a href="./search.php?song=<?= formatsonguri($song['song_uri']) ?>" class="spotify-link">
<?= htmlspecialchars($song['song']) ?>
</a>
<?php else: ?>
<?= htmlspecialchars($song['song']) ?>
<?php endif; ?>
(Popularity Score: <?= number_format($song['track_popularity'], 0, '.', '') ?>)
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</div>
<script src="./assets/js/script.js"></script>
</body>
</html>