Skip to content

Commit 9b56ed3

Browse files
committed
feat: lets make spotify work live
1 parent 1a8907a commit 9b56ed3

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

Diff for: app/Console/Commands/GetSpotifyActivitiesCommand.php

+18-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class GetSpotifyActivitiesCommand extends Command
1515
{
16-
protected $signature = 'spotify:activities {authorId?}';
16+
protected $signature = 'spotify:activities';
1717

1818
protected function devices(string $bearerToken)
1919
{
@@ -107,20 +107,12 @@ protected function state(string $bearerToken)
107107

108108
public function handle()
109109
{
110-
$authorId = $this->argument('authorId');
110+
$authors = DB::table('authors')->whereNotNull('spotify_user_id')->get();
111111

112-
if (!$authorId) {
113-
$authors = DB::table('authors')->whereNotNull('spotify_user_id')->get();
114-
115-
foreach ($authors as $author) {
116-
print "Aktualizacja aktywności dla autora: {$author->name}\n";
117-
$this->updateSpotifyActivity($author->id);
118-
}
119-
120-
return;
112+
foreach ($authors as $author) {
113+
print "Aktualizacja aktywności dla autora: {$author->name}\n";
114+
$this->updateSpotifyActivity($author->id);
121115
}
122-
123-
$this->updateSpotifyActivity($authorId);
124116
}
125117

126118
protected function updateSpotifyActivity(string $authorId)
@@ -147,9 +139,20 @@ protected function updateSpotifyActivity(string $authorId)
147139
return;
148140
}
149141

150-
$bearerToken = $spotifyUser->access_token;
151-
152142
try {
143+
$bearerToken = $spotifyUser->access_token;
144+
$refreshedAt = $spotifyUser->token_refreshed_at;
145+
146+
if (empty($refreshedAt) || Carbon::parse($refreshedAt)->addSeconds(1800)->isPast()) {
147+
$response = SpotifyService::getRefreshToken(env('SPOTIFY_CLIENT_ID'), env('SPOTIFY_CLIENT_SECRET'), $spotifyUser->refresh_token);
148+
149+
$spotifyUser->update([
150+
'access_token' => $response->access_token,
151+
'token_refreshed_at' => Carbon::now(),
152+
'scope' => $response->scope,
153+
]);
154+
}
155+
153156
$devices = $this->devices($bearerToken);
154157
$state = $this->state($bearerToken);
155158
$history = $this->history($bearerToken);
@@ -169,9 +172,6 @@ protected function updateSpotifyActivity(string $authorId)
169172
'player' => $player,
170173
];
171174
} catch (Throwable $t) {
172-
// TODO refresh token and retry once!
173-
// SpotifyService::getRefreshToken(env('SPOTIFY_CLIENT_ID'), env('SPOTIFY_CLIENT_SECRET'), $spotifyUser->refresh_token);
174-
175175
print $t->getMessage();
176176
logger()->error($t->getMessage());
177177
return;

Diff for: app/Http/Controllers/Spotify/SpotifyController.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ public function handleCallback()
4141
die('Błąd autoryzacji');
4242
}
4343

44-
session(['spotify_token' => $token->access_token]);
45-
4644
$spotifyUser = SpotifyService::getCurrentUser($token->access_token);
4745

46+
if (empty($spotifyUser)) {
47+
die('Błąd autoryzacji');
48+
}
49+
4850
$existingUser = SpotifyUser::where('user_id', $spotifyUser->id)->first();
4951

5052
if ($existingUser) {
@@ -53,7 +55,7 @@ public function handleCallback()
5355
'refresh_token' => $token->refresh_token,
5456
'scope' => $token->scope,
5557
'followers' => $spotifyUser->followers->total,
56-
'avatar_url' => $spotifyUser->images[0]->url,
58+
'avatar_url' => $spotifyUser->images[0]->url ?? '',
5759
'type' => $spotifyUser->type,
5860
'token_refreshed_at' => null,
5961
]);
@@ -68,7 +70,7 @@ public function handleCallback()
6870
'refresh_token' => $token->refresh_token,
6971
'scope' => $token->scope,
7072
'followers' => $spotifyUser->followers->total,
71-
'avatar_url' => $spotifyUser->images[0]->url,
73+
'avatar_url' => $spotifyUser->images[0]->url ?? '',
7274
'type' => $spotifyUser->type,
7375
'token_refreshed_at' => null,
7476
]);

Diff for: app/Models/Spotify/SpotifyUser.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @property int $followers
1616
* @property string $avatar_url
1717
* @property string $type
18+
* @property string $token_refreshed_at
1819
*/
1920
class SpotifyUser extends Model
2021
{

Diff for: app/Services/Spotify/SpotifyService.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,18 @@ public static function getTrack(string $bearerToken, string $trackId)
100100
return json_decode($response);
101101
}
102102

103-
public static function authorizeWithSpotify(string $clientId, string $state, string $redirectUri): RedirectResponse
103+
public static function authorizeWithSpotify(string $clientId, string $state, string $redirectUri, array $scope = []): RedirectResponse
104104
{
105-
$scope = implode(' ', [
106-
'user-read-recently-played', // poprzednio grany utwór
107-
'user-read-playback-state', // stan odtwarzania
108-
]);
105+
if (empty($scope)) {
106+
$scope = [
107+
'user-read-private',
108+
'user-read-email',
109+
'user-read-recently-played',
110+
'user-read-playback-state',
111+
];
112+
}
113+
114+
$scope = implode(' ', $scope);
109115

110116
$url = "https://accounts.spotify.com/authorize?response_type=code&client_id=$clientId&scope=$scope&state=$state&redirect_uri=$redirectUri";
111117

0 commit comments

Comments
 (0)