-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.php
85 lines (59 loc) · 2.26 KB
/
fetch.php
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
<?php
//Contains API Key for Last.fm and Spotify
//include('config.php');
// import from env variables
$lastfm_api_key = getenv('LASTFM_API_KEY');
$spotify_client_id = getenv('SPOTIFY_CLIENT_ID');
$spotify_client_secret = getenv('SPOTIFY_CLIENT_SECRET');
function fetchTracks($lastfm_api_key, $lastfm_username) {
// Creates a the last year date
include('inc/date.php');
$url ='http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='.$lastfm_username.'&from='.$start.'&extended=0&to='.$stop.'&api_key='.$lastfm_api_key.'&format=json';
$json = file_get_contents($url);
$obj = json_decode($json, true);
// If no last fm tracks found
if($obj["recenttracks"]["track"] == NULL) {
print 'No track found for this date :( or incorrect pseudo';
}
else{
print '<div id="tracks">';
print '<h2>'.$past_year_start->format('d/m/Y').'</h2>';
foreach($obj["recenttracks"]["track"] as $track ){
print '<div class="track uk-card uk-card-default uk-card-body uk-width-1-2@m uk-animation-slide-top"><div class="uk-card-title">';
// Convert lastfm date timestamp to hh-mm date
$trackDate = new DateTime();
$trackDate -> setTimestamp($track["date"]["uts"]);
print $trackDate -> format('H:i');
print '</div>';
print '<img src="'.$track["image"][2]["#text"].'">';
print $track["name"].'<br>';
print $track["artist"]["#text"].'<br>';
print $track["album"]['#text'];
print '</div><br>';
}
print '</div>';
}
}
// Checks if pseudo and years have been sent trought index.php
if(isset($_POST["pseudo"]) && isset($_POST["years"])){
setcookie("pseudo", $_POST["pseudo"]);
$lastfm_username = $_POST["pseudo"];
fetchTracks($lastfm_api_key ,$lastfm_username);
}
// Checks if the cookies have been set
elseif(isset($_COOKIE["pseudo"]) && isset($_COOKIE["years"])){
$lastfm_username = $_COOKIE["pseudo"];
if (!empty($_GET["playlist"])) {
if ($_GET["playlist"] == 'true'){
echo 'Playlist succesfully generated';
}
}
fetchTracks($lastfm_api_key ,$lastfm_username);
}
else {
print 'Please enter a username';
}
include('header.html');
?>
</body>
</html>