Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
spotify
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
web
spotify
Commits
002f4bd2
Commit
002f4bd2
authored
3 years ago
by
Benoît Harrault
Browse files
Options
Downloads
Patches
Plain Diff
Add "generate playlist from artists"
parent
72df9e94
No related branches found
No related tags found
1 merge request
!6
Resolve "Create playlist from top played artists"
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
public/index.php
+44
-0
44 additions, 0 deletions
public/index.php
spotify/spotify.php
+1
-0
1 addition, 0 deletions
spotify/spotify.php
template.php
+27
-2
27 additions, 2 deletions
template.php
with
72 additions
and
2 deletions
public/index.php
+
44
−
0
View file @
002f4bd2
...
@@ -43,6 +43,7 @@ $user = $api->me();
...
@@ -43,6 +43,7 @@ $user = $api->me();
$templateData
=
[
$templateData
=
[
'user'
=>
$user
,
'user'
=>
$user
,
'playlists'
=>
[],
'playlists'
=>
[],
'topArtists'
=>
[],
'infos'
=>
[],
'infos'
=>
[],
'errors'
=>
[],
'errors'
=>
[],
...
@@ -53,6 +54,8 @@ if (isset($_GET['playlist'])) {
...
@@ -53,6 +54,8 @@ if (isset($_GET['playlist'])) {
$selectedPlaylistId
=
$_GET
[
'playlist'
];
$selectedPlaylistId
=
$_GET
[
'playlist'
];
}
}
$generatePlaylistFromArtists
=
isset
(
$_GET
[
'generate-playlist-top-artists'
]);
if
(
$selectedPlaylistId
)
{
if
(
$selectedPlaylistId
)
{
$playlist
=
$api
->
getPlaylist
(
$selectedPlaylistId
);
$playlist
=
$api
->
getPlaylist
(
$selectedPlaylistId
);
error_log
(
'Will create new playlist from tracks in playlist '
.
$selectedPlaylistId
.
' "'
.
$playlist
->
name
.
'".'
);
error_log
(
'Will create new playlist from tracks in playlist '
.
$selectedPlaylistId
.
' "'
.
$playlist
->
name
.
'".'
);
...
@@ -88,10 +91,51 @@ if ($selectedPlaylistId) {
...
@@ -88,10 +91,51 @@ if ($selectedPlaylistId) {
'name'
=>
$newPlaylistName
,
'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
;
}
elseif
(
$generatePlaylistFromArtists
)
{
$selectedArtists
=
$_GET
[
'selected-artist'
];
error_log
(
'Will create new playlist with filterted recommendations from artists: '
.
join
(
', '
,
$selectedArtists
)
.
'".'
);
$recommendedTrackIds
=
[];
foreach
(
$selectedArtists
as
$selectedArtist
)
{
error_log
(
'Get recommendations for artist '
.
$selectedArtist
.
''
);
$recommendations
=
$api
->
getRecommendations
([
'seed_artists'
=>
$selectedArtist
,
'limit'
=>
100
]);
error_log
(
' -> Got '
.
count
(
$recommendations
->
tracks
)
.
' recommendatations.'
);
// Filter by artist, remove duplicates
foreach
(
$recommendations
->
tracks
as
$recommendedTrack
)
{
$trackArtistInWantedArtists
=
false
;
foreach
(
$recommendedTrack
->
artists
as
$trackArtist
)
{
if
(
\in_array
(
$trackArtist
->
id
,
$selectedArtists
))
{
$recommendedTrackIds
[
$recommendedTrack
->
id
]
=
1
;
}
}
}
}
$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
);
$api
->
replacePlaylistTracks
(
$newPlaylist
->
id
,
$pickedTrackIds
);
$playlistLink
=
'<a href="'
.
$newPlaylist
->
external_urls
->
spotify
.
'">'
.
$newPlaylistName
.
'</a>'
;
$playlistLink
=
'<a href="'
.
$newPlaylist
->
external_urls
->
spotify
.
'">'
.
$newPlaylistName
.
'</a>'
;
$templateData
[
'infos'
][]
=
'Ok created new playlist with '
.
count
(
$pickedTrackIds
)
.
' tracks: '
.
$playlistLink
;
$templateData
[
'infos'
][]
=
'Ok created new playlist with '
.
count
(
$pickedTrackIds
)
.
' tracks: '
.
$playlistLink
;
}
else
{
}
else
{
$templateData
[
'topArtists'
]
=
$api
->
getMyTop
(
'artists'
,
[
'limit'
=>
16
,
'time_range'
=>
'short_term'
])
->
items
;
$templateData
[
'playlists'
]
=
$api
->
getUserPlaylists
(
$user
->
id
)
->
items
;
$templateData
[
'playlists'
]
=
$api
->
getUserPlaylists
(
$user
->
id
)
->
items
;
}
}
...
...
This diff is collapsed.
Click to expand it.
spotify/spotify.php
+
1
−
0
View file @
002f4bd2
...
@@ -15,4 +15,5 @@ $SPOTIFY_REQUIRED_SCOPES = [
...
@@ -15,4 +15,5 @@ $SPOTIFY_REQUIRED_SCOPES = [
'playlist-modify-private'
,
'playlist-modify-private'
,
'playlist-modify-public'
,
'playlist-modify-public'
,
'user-read-private'
,
'user-read-private'
,
'user-top-read'
,
];
];
This diff is collapsed.
Click to expand it.
template.php
+
27
−
2
View file @
002f4bd2
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
<body
class=
"container mt-2"
>
<body
class=
"container mt-2"
>
<div
class=
"row"
>
<div
class=
"row"
>
<div
class=
"col-
2
"
>
<div
class=
"col-
3
"
>
<div
class=
"card"
>
<div
class=
"card"
>
<?php
if
(
count
(
$templateData
[
'user'
]
->
images
))
{
?>
<?php
if
(
count
(
$templateData
[
'user'
]
->
images
))
{
?>
...
@@ -32,7 +32,7 @@
...
@@ -32,7 +32,7 @@
</div>
</div>
</div>
</div>
<div
class=
"col-
10
"
>
<div
class=
"col-
9
"
>
<?php
if
(
count
(
$templateData
[
'errors'
]))
{
?>
<?php
if
(
count
(
$templateData
[
'errors'
]))
{
?>
<?php
foreach
(
$templateData
[
'errors'
]
as
$message
)
{
?>
<?php
foreach
(
$templateData
[
'errors'
]
as
$message
)
{
?>
...
@@ -46,6 +46,31 @@
...
@@ -46,6 +46,31 @@
<?php
}
?>
<?php
}
?>
<?php
}
?>
<?php
}
?>
<?php
if
(
count
(
$templateData
[
'topArtists'
]))
{
?>
<form
class=
"clearfix"
>
<legend>
Generate playlist from artists:
</legend>
<ul
class=
"list-unstyled row row-cols-4"
>
<?php
foreach
(
$templateData
[
'topArtists'
]
as
$artist
)
{
?>
<li
class=
"col"
>
<div
class=
"custom-control custom-checkbox"
>
<input
type=
"checkbox"
class=
"custom-control-input"
id=
"check-top-artist-
<?php
echo
$artist
->
id
;
?>
"
name=
"selected-artist[]"
value=
"
<?php
echo
$artist
->
id
;
?>
"
>
<label
class=
"custom-control-label"
for=
"check-top-artist-
<?php
echo
$artist
->
id
;
?>
"
>
<?php
echo
$artist
->
name
;
?>
</label>
</div>
</li>
<?php
}
?>
</ul>
<button
name=
"generate-playlist-top-artists"
type=
"submit"
class=
"btn btn-secondary float-end"
>
🎶 Generate!
</button>
</form>
<?php
}
?>
<?php
if
(
count
(
$templateData
[
'playlists'
]))
{
?>
<?php
if
(
count
(
$templateData
[
'playlists'
]))
{
?>
<h3>
Please pick a playlist to get recommendations from:
</h3>
<h3>
Please pick a playlist to get recommendations from:
</h3>
<div
class=
"list-group"
>
<div
class=
"list-group"
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment