-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.php
120 lines (89 loc) · 3.2 KB
/
callback.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
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
<?php
//Spotify API Helper
require_once 'vendor/autoload.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');
//Checks if has pseudo cookie
if (isset($_COOKIE["pseudo"])) {
$pseudo = $_COOKIE["pseudo"];
}
else {
return 'No pseudo cookie !';
}
//SPOTIFY CALLBACK
$session = new SpotifyWebAPI\Session(
$spotify_client_id,
$spotify_client_secret,
$domainUrl.'callback.php'
);
$api = new SpotifyWebAPI\SpotifyWebAPI();
if (isset($_GET['code'])) {
// Gets access from spotify
$session->requestAccessToken($_GET['code']);
$api->setAccessToken($session->getAccessToken());
$scopes = $session->getScope();
$scopes = $session->getScope();
// Spotify logged user
$me = $api->me();
$playlists = $api->getUserPlaylists($me->id);
function fetchPlaylistId($playlists){
foreach ($playlists->items as $playlist) {
if ($playlist->name == 'Lastfm A year ago') {
return $playlist->id;
}
}
}
//LAST FM API FETCH
$lastfm_username = $pseudo ;
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);
$trackId = [];
$playlistId = '';
$playlistName = $past_year_start->format('d/m/Y');
// Search the name of the track in the Spotify catalog
foreach($obj["recenttracks"]["track"] as $track ){
$tracks = $api->search($track["name"],'track');
//var_dump('Lastfm : '. $track["artist"]["#text"]);
if (!empty($tracks->tracks->items[0]->id)){
//var_dump( $tracks->tracks->items[0]);
/*foreach ($tracks->tracks->items as $findArtist){
$i =0;
echo '<br>----------------------------<br>';
foreach ($findArtist->album->artists as $artists){
if (strcasecmp($artists->name, $track["artist"]["#text"])) {
var_dump($tracks->tracks->items[$i]->name);
}
$i++;
}
}*/
array_push($trackId, $tracks->tracks->items[0]->id);
}
}
$newPlaylist = $api->createUserPlaylist($me->id, [
'name' => 'LastFm - '.$pseudo." - ". $playlistName
]);
$playlistId = $newPlaylist->id;
// Adds the song in the users Spotify Playlist
$replaceSongsInPlaylist = $api->replaceUserPlaylistTracks($me->id,$playlistId, $trackId);
if($replaceSongsInPlaylist) {
echo 'true';
return header('Location: fetch.php?playlist=true');;
}
$playlistId = $newPlaylist->id;
} else {
// Defines what kind of information/access are required from Spotify
$options = [
'scope' => [
'user-read-email',
'user-modify-playback-state',
'playlist-modify-public',
'playlist-modify-private'
],
];
header('Location: ' . $session->getAuthorizeUrl($options));
die();
}