[GH-ISSUE #267] How to run in command line? #194

Closed
opened 2026-02-27 19:26:32 +03:00 by kerem · 4 comments
Owner

Originally created by @Egregius on GitHub (Aug 10, 2023).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/267

Is there a way to use this in command line?
I'd like to create a script where my playlists get shuffled during the night.
I have the script working in a browser but would like to execute it by cron.

$session->requestAccessToken($_GET['code']);
$api->setAccessToken($session->getAccessToken());
$playlists = $api->getUserPlaylists('egregiusspotify', ['limit' => 50]);
foreach ($playlists->items as $playlist) {
$nrs=array_fill(0, $playlist->tracks->total, null);
while (count($nrs)>0) {
$nr=array_rand($nrs, 1);
unset($nrs[$nr]);
$api->reorderPlaylistTracks($playlist->id, [
'range_start' => $nr,
'insert_before' => 0,
]);
usleep(40000);
}
}

Thanks for your ideas.

Originally created by @Egregius on GitHub (Aug 10, 2023). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/267 Is there a way to use this in command line? I'd like to create a script where my playlists get shuffled during the night. I have the script working in a browser but would like to execute it by cron. > $session->requestAccessToken($_GET['code']); $api->setAccessToken($session->getAccessToken()); $playlists = $api->getUserPlaylists('egregiusspotify', ['limit' => 50]); foreach ($playlists->items as $playlist) { $nrs=array_fill(0, $playlist->tracks->total, null); while (count($nrs)>0) { $nr=array_rand($nrs, 1); unset($nrs[$nr]); $api->reorderPlaylistTracks($playlist->id, [ 'range_start' => $nr, 'insert_before' => 0, ]); usleep(40000); } } Thanks for your ideas.
kerem 2026-02-27 19:26:32 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Aug 12, 2023):

Hey!
I'd use the refresh token returned when requesting an access token (retrievable with $session->getRefreshToken()) to request a new access token each time the script is run. The refresh token will change each time so be sure to store it between runs.

Hope this helps you out!

<!-- gh-comment-id:1675896505 --> @jwilsson commented on GitHub (Aug 12, 2023): Hey! I'd use the refresh token returned when requesting an access token (retrievable with `$session->getRefreshToken()`) to request a new access token each time the script is run. The refresh token will change each time so be sure to store it between runs. Hope this helps you out!
Author
Owner

@Egregius commented on GitHub (Aug 14, 2023):

Great, thanks!
Think I got it working.

$api = new SpotifyWebAPI\SpotifyWebAPI();
$stmt = $db->query("SELECT refreshToken FROM token;");
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) $token=$row;
$session->refreshAccessToken($token['refreshToken']);
$accessToken = $session->getAccessToken();
$refreshToken = $session->getRefreshToken();
$api->setAccessToken($accessToken);
if ($refreshToken!=$token['refreshToken']) {
lg('New refresh token '.$refreshToken);
$stmt = $db->prepare("UPDATE token SET refreshToken = :refreshToken;");
$opts = array(':refreshToken'=>$refreshToken);
$stmt->execute($opts);
}

Now need to figure out how to handle 'too many requests' errors. Already have a 1.5 second sleep between each request.

<!-- gh-comment-id:1676743780 --> @Egregius commented on GitHub (Aug 14, 2023): Great, thanks! Think I got it working. > $api = new SpotifyWebAPI\SpotifyWebAPI(); $stmt = $db->query("SELECT refreshToken FROM token;"); while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) $token=$row; $session->refreshAccessToken($token['refreshToken']); $accessToken = $session->getAccessToken(); $refreshToken = $session->getRefreshToken(); $api->setAccessToken($accessToken); if ($refreshToken!=$token['refreshToken']) { lg('New refresh token '.$refreshToken); $stmt = $db->prepare("UPDATE token SET refreshToken = :refreshToken;"); $opts = array(':refreshToken'=>$refreshToken); $stmt->execute($opts); } Now need to figure out how to handle 'too many requests' errors. Already have a 1.5 second sleep between each request.
Author
Owner

@jwilsson commented on GitHub (Aug 14, 2023):

Awesome!

Sounds like the auto_retry option could be of help here, it'll automatically sleep the amount of time needed.

<!-- gh-comment-id:1676941891 --> @jwilsson commented on GitHub (Aug 14, 2023): Awesome! Sounds like the [`auto_retry` option](https://github.com/jwilsson/spotify-web-api-php/blob/a75f78d5c27a71eeae4682e6299a83dbb67a6093/docs/examples/setting-options.md#auto_retry) could be of help here, it'll automatically sleep the amount of time needed.
Author
Owner

@Egregius commented on GitHub (Aug 16, 2023):

Looks good and stable now. Thank you for your responses!
Finally a true shuffle for my huge playlists 👯‍♂️

<!-- gh-comment-id:1680707839 --> @Egregius commented on GitHub (Aug 16, 2023): Looks good and stable now. Thank you for your responses! Finally a true shuffle for my huge playlists 👯‍♂️
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#194
No description provided.