Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • 11-improve-new-playlist-name-and-description
  • 15-add-a-play-now-button-on-created-playlist-message
  • 20-rename-playlist-with-artists-names
  • 30-save-spotify-personal-token-for-external-use
  • 37-batch-update-recently-played-tracks
  • 40-add-more-default-playlists-in-quick-create-feature
  • 58-create-api-for-main-routes
  • 60-display-last-tracks-and-some-dump-metadata
  • master
9 results

Target

Select target project
  • web/spotify
1 result
Select Git revision
  • 11-improve-new-playlist-name-and-description
  • 15-add-a-play-now-button-on-created-playlist-message
  • 20-rename-playlist-with-artists-names
  • 30-save-spotify-personal-token-for-external-use
  • 37-batch-update-recently-played-tracks
  • 40-add-more-default-playlists-in-quick-create-feature
  • 58-create-api-for-main-routes
  • 60-display-last-tracks-and-some-dump-metadata
  • master
9 results
Show changes
Commits on Source (8)
# spotify
# Spotify playlist generator
# Register app
Create fresh playlists from selected top played artists or existing playlists.
<https://developer.spotify.com/dashboard/applications>
Playlists are generated as public Spotify playlists, with 50 tracks (~3 hours), from your personal Spotify recommendations.
App name:
Spotify playlist manager
## Generate fresh playlist form artists
Add description:
Application to help create/update playlists from other playlists or selected artists/tracks.
Something like "radios", but more personalized.
From the list of your top played artists.
Client ID:
41597dbeb0b3403caa28e7e428e0c9b2
- pick one or several artists in list
- click on "generate"
- a fresh playlist is generated with recommendations from each artist in pick ones
Client Secret:
<hidden>
You can choose if you want all recommendations, or only the ones with picked artists.
A direct button can help to randomly pick artists.
## Generate fresh playlist from existing playlists
- pick one or several playlist in list
- click on "generate"
- a fresh playlist is generated with recommendations from each track in picked playlists
You can choose if you want all recommendations, or only the ones with artists in picked playlists.
## Register app
<https://developer.spotify.com/dashboard/applications>
App name:
Spotify playlist manager
Add description:
Application to help create/update playlists from other playlists or selected artists/tracks.
Something like "radios", but more personalized.
Client ID:
41597dbeb0b3403caa28e7e428e0c9b2
Client Secret:
<hidden>
@charset "UTF-8";
.spotify-image {
display: inline-block;
text-align: center;
justify-content: center;
align-items: center;
height: 1rem;
width: 1rem;
max-height: 1rem;
max-width: 1rem;
}
.spotify-image>img {
border: solid 1px #888;
text-align: center;
max-height: 1rem;
max-width: 1rem;
margin: auto;
}
<?php
require '../spotify/spotify.php';
require '../spotify/lib.php';
if (isset($_GET['logout'])) {
$_SESSION['accessToken'] = '';
......@@ -40,7 +41,7 @@ $_SESSION['refreshToken'] = $session->getRefreshToken();
$user = $api->me();
$topArtistsCount = 48;
$topArtistsCount = 40;
$playlistsCount = 24;
$templateData = [
......@@ -104,22 +105,8 @@ if ($generatePlaylistFromPlaylist) {
$recommendedTrackIds = $filteredTrackIds;
}
$recommendedTrackIds = array_keys($recommendedTrackIds);
error_log('=> Got '.count($recommendedTrackIds).' unique recommendatations for this playlist.');
shuffle($recommendedTrackIds);
$pickedTrackIds = array_slice($recommendedTrackIds, 0, 50);
error_log('Keep '.count($pickedTrackIds).' tracks.');
$now = new DateTime();
$newPlaylistName = 'Fresh playlist ['.$now->format('Y-m-d H:i:s').']';
error_log('Create new playlist "'.$newPlaylistName.'".');
$newPlaylist = $api->createPlaylist([
'name' => $newPlaylistName,
]);
$api->replacePlaylistTracks($newPlaylist->id, $pickedTrackIds);
$playlistLink = '<a href="'.$newPlaylist->external_urls->spotify.'">'.$newPlaylistName.'</a>';
$templateData['infos'][] = 'Ok created new playlist with '.count($pickedTrackIds).' tracks: '.$playlistLink;
$newPlaylist = createPlaylistWithRandomTracks($api, $recommendedTrackIds);
$templateData['infos'][] = getCreatedPlaylistInformationMessage($api, $newPlaylist->id);
} elseif ($generatePlaylistFromArtists) {
$selectedArtists = $_GET['selected-artist'];
error_log('Will create new playlist with filterted recommendations from artists: '.join(', ', $selectedArtists).'".');
......@@ -151,22 +138,8 @@ if ($generatePlaylistFromPlaylist) {
}
}
$recommendedTrackIds = array_keys($recommendedTrackIds);
error_log('=> Got '.count($recommendedTrackIds).' unique recommendatations.');
shuffle($recommendedTrackIds);
$pickedTrackIds = array_slice($recommendedTrackIds, 0, 50);
error_log('Keep '.count($pickedTrackIds).' random tracks.');
$now = new DateTime();
$newPlaylistName = 'Fresh playlist ['.$now->format('Y-m-d H:i:s').']';
error_log('Create new playlist "'.$newPlaylistName.'".');
$newPlaylist = $api->createPlaylist([
'name' => $newPlaylistName,
]);
$api->replacePlaylistTracks($newPlaylist->id, $pickedTrackIds);
$playlistLink = '<a href="'.$newPlaylist->external_urls->spotify.'">'.$newPlaylistName.'</a>';
$templateData['infos'][] = 'Ok created new playlist with '.count($pickedTrackIds).' tracks: '.$playlistLink;
$newPlaylist = createPlaylistWithRandomTracks($api, $recommendedTrackIds);
$templateData['infos'][] = getCreatedPlaylistInformationMessage($api, $newPlaylist->id);
} else {
$templateData['topArtists'] = $api->getMyTop('artists', ['limit' => $topArtistsCount, 'time_range' => 'short_term'])->items;
$templateData['playlists'] = $api->getUserPlaylists($user->id, ['limit' => $playlistsCount])->items;
......
<?php
function generatePlaylistName() {
$now = new DateTime();
$name = 'Fresh playlist ['.$now->format('Y-m-d H:i:s').']';
return $name;
}
function createEmptyPlaylist($api) {
$name = generatePlaylistName();
error_log('Create new playlist "'.$name.'".');
$playlist = $api->createPlaylist([
'name' => $name,
]);
return $playlist;
}
function pickRandomTrackFromArray($recommendedTrackIds, $count = 50) {
$recommendedTrackIds = array_keys($recommendedTrackIds);
error_log('=> Got '.count($recommendedTrackIds).' unique recommendatations.');
shuffle($recommendedTrackIds);
$pickedTrackIds = array_slice($recommendedTrackIds, 0, 50);
error_log('Keep '.count($pickedTrackIds).' random tracks.');
return $pickedTrackIds;
}
function createPlaylistWithRandomTracks($api, $trackIds, $count = 50) {
$playlist = createEmptyPlaylist($api);
$pickedTrackIds = pickRandomTrackFromArray($trackIds);
$api->replacePlaylistTracks($playlist->id, $pickedTrackIds);
return $playlist;
}
function getCreatedPlaylistInformationMessage($api, $playlistId) {
$playlist = $api->getPlaylist($playlistId);
$link = '<a href="'.$playlist->external_urls->spotify.'">'.$playlist->name.'</a>';
$message = 'Ok created new playlist with '.count($playlist->tracks->items).' tracks: '.$link;
return $message;
}
......@@ -18,7 +18,7 @@
<div class="container-fluid">
<a class="navbar-brand" href="playlist-generator">
<?php if (count($templateData['user']->images)) { ?>
<img src="<?php echo $templateData['user']->images[0]->url; ?>" alt="" class="rounded border" alt="<?php echo $templateData['user']->display_name; ?>" style="max-height: 2rem;">
<img src="<?php echo $templateData['user']->images[0]->url; ?>" class="rounded border" alt="<?php echo $templateData['user']->display_name; ?>" style="max-height: 2rem;">
<?php } ?>
<?php echo $templateData['user']->display_name; ?>
</a>
......@@ -68,7 +68,7 @@
>
<label class="custom-control-label" for="check-top-artist-<?php echo $artist->id; ?>">
<?php if (count($artist->images)) { ?>
<img src="<?php echo $artist->images[count($artist->images) - 1]->url; ?>" style="max-height: 1rem; max-width: 1rem;" />
<span class="spotify-image"><img src="<?php echo $artist->images[count($artist->images) - 1]->url; ?>" style="max-height: 1rem; max-width: 1rem;" /></span>
<?php } ?>
<?php echo $artist->name; ?>
</label>
......@@ -116,7 +116,7 @@
>
<label class="custom-control-label" for="check-playlist-<?php echo $playlist->id; ?>">
<?php if (count($playlist->images)) { ?>
<img src="<?php echo $playlist->images[count($playlist->images) - 1]->url; ?>" style="max-height: 1rem; max-width: 1rem;" />
<span class="spotify-image"><img src="<?php echo $playlist->images[count($playlist->images) - 1]->url; ?>" style="max-height: 1rem; max-width: 1rem;" /></span>
<?php } ?>
<?php echo $playlist->name; ?>
</label>
......