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
150081b2
Commit
150081b2
authored
2 years ago
by
Benoît Harrault
Browse files
Options
Downloads
Patches
Plain Diff
Add a "mix daily mixes" button
parent
509782a9
No related branches found
No related tags found
1 merge request
!35
Resolve "Add a "mix daily mixes" button"
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
public/index.php
+19
-1
19 additions, 1 deletion
public/index.php
spotify/lib.php
+59
-7
59 additions, 7 deletions
spotify/lib.php
template.php
+30
-0
30 additions, 0 deletions
template.php
with
108 additions
and
8 deletions
public/index.php
+
19
−
1
View file @
150081b2
...
@@ -77,6 +77,7 @@ $templateData = [
...
@@ -77,6 +77,7 @@ $templateData = [
$generatePlaylistFromPlaylist
=
isset
(
$_GET
[
'generate-playlist-from-playlist'
]);
$generatePlaylistFromPlaylist
=
isset
(
$_GET
[
'generate-playlist-from-playlist'
]);
$generatePlaylistFromSelectedTopArtists
=
isset
(
$_GET
[
'generate-playlist-top-artists'
]);
$generatePlaylistFromSelectedTopArtists
=
isset
(
$_GET
[
'generate-playlist-top-artists'
]);
$generatePlaylistFromRandomTopArtists
=
isset
(
$_GET
[
'generate-playlist-random-top-artists'
]);
$generatePlaylistFromRandomTopArtists
=
isset
(
$_GET
[
'generate-playlist-random-top-artists'
]);
$generateQuickPlaylistFromDailyMixes
=
isset
(
$_GET
[
'generate-quick-playlist-from-daily-mixes'
]);
$showPlaylistContent
=
isset
(
$_GET
[
'show-playlist'
]);
$showPlaylistContent
=
isset
(
$_GET
[
'show-playlist'
]);
$action
=
''
;
$action
=
''
;
...
@@ -86,6 +87,8 @@ if ($generatePlaylistFromPlaylist) {
...
@@ -86,6 +87,8 @@ if ($generatePlaylistFromPlaylist) {
$action
=
'generate-playlist-from-selected-top-artists'
;
$action
=
'generate-playlist-from-selected-top-artists'
;
}
elseif
(
$generatePlaylistFromRandomTopArtists
)
{
}
elseif
(
$generatePlaylistFromRandomTopArtists
)
{
$action
=
'generate-playlist-from-random-top-artists'
;
$action
=
'generate-playlist-from-random-top-artists'
;
}
elseif
(
$generateQuickPlaylistFromDailyMixes
)
{
$action
=
'generate-playlist-from-daily-mixes'
;
}
elseif
(
$showPlaylistContent
)
{
}
elseif
(
$showPlaylistContent
)
{
$action
=
'show-playlist-content'
;
$action
=
'show-playlist-content'
;
}
else
{
}
else
{
...
@@ -152,6 +155,7 @@ switch ($action) {
...
@@ -152,6 +155,7 @@ switch ($action) {
);
);
$templateData
[
'infos'
][]
=
getCreatedPlaylistInformationMessage
(
$api
,
$newPlaylist
);
$templateData
[
'infos'
][]
=
getCreatedPlaylistInformationMessage
(
$api
,
$newPlaylist
);
break
;
break
;
case
'generate-playlist-from-selected-top-artists'
:
case
'generate-playlist-from-selected-top-artists'
:
$selectedArtists
=
$_GET
[
'selected-artist'
];
$selectedArtists
=
$_GET
[
'selected-artist'
];
printLog
(
'Will create new playlist with filterted recommendations from artists: '
.
join
(
', '
,
$selectedArtists
)
.
'".'
);
printLog
(
'Will create new playlist with filterted recommendations from artists: '
.
join
(
', '
,
$selectedArtists
)
.
'".'
);
...
@@ -193,6 +197,7 @@ switch ($action) {
...
@@ -193,6 +197,7 @@ switch ($action) {
);
);
$templateData
[
'infos'
][]
=
getCreatedPlaylistInformationMessage
(
$api
,
$newPlaylist
);
$templateData
[
'infos'
][]
=
getCreatedPlaylistInformationMessage
(
$api
,
$newPlaylist
);
break
;
break
;
case
'generate-playlist-from-random-top-artists'
:
case
'generate-playlist-from-random-top-artists'
:
$countInTopArtists
=
random_int
(
4
,
6
);
$countInTopArtists
=
random_int
(
4
,
6
);
$countInLessTopArtists
=
random_int
(
4
,
6
);
$countInLessTopArtists
=
random_int
(
4
,
6
);
...
@@ -242,14 +247,27 @@ switch ($action) {
...
@@ -242,14 +247,27 @@ switch ($action) {
);
);
$templateData
[
'infos'
][]
=
getCreatedPlaylistInformationMessage
(
$api
,
$newPlaylist
);
$templateData
[
'infos'
][]
=
getCreatedPlaylistInformationMessage
(
$api
,
$newPlaylist
);
break
;
break
;
case
'generate-playlist-from-daily-mixes'
:
$generateLongPlaylist
=
isset
(
$_GET
[
'check-quick-create-long-playlist'
]);
printLog
(
'Generate playlist from Daily Mixes (long playlist :'
.
(
$generateLongPlaylist
?
'yes'
:
'no'
)
.
')'
);
$newPlaylist
=
createPlaylistFromDailyMixesTracks
(
$api
,
$generateLongPlaylist
?
$tracksCountInLongPlaylist
:
$tracksCountInShortPlaylist
);
$templateData
[
'infos'
][]
=
getCreatedPlaylistInformationMessage
(
$api
,
$newPlaylist
);
break
;
case
'show-playlist-content'
:
case
'show-playlist-content'
:
printLog
(
'Get content of playlist'
);
printLog
(
'Get content of playlist'
);
$playlistId
=
$_GET
[
'id'
];
$playlistId
=
$_GET
[
'id'
];
$playlist
=
$api
->
getPlaylist
(
$playlistId
);
$playlist
=
$api
->
getPlaylist
(
$playlistId
);
$templateData
[
'infos'
][]
=
getPlaylistInformationMessage
(
$api
,
$playlist
);
$templateData
[
'infos'
][]
=
getPlaylistInformationMessage
(
$api
,
$playlist
);
$templateData
[
'playlist'
]
=
createDisplayablePlaylist
(
$playlist
);
$templateData
[
'playlist'
]
=
createDisplayablePlaylist
(
$playlist
);
break
;
break
;
default
:
default
:
$templateData
[
'topArtists'
]
=
$api
->
getMyTop
(
'artists'
,
[
'limit'
=>
$topArtistsCount
,
'time_range'
=>
'short_term'
])
->
items
;
$templateData
[
'topArtists'
]
=
$api
->
getMyTop
(
'artists'
,
[
'limit'
=>
$topArtistsCount
,
'time_range'
=>
'short_term'
])
->
items
;
$templateData
[
'playlists'
]
=
$api
->
getUserPlaylists
(
$user
[
'id'
],
[
'limit'
=>
$playlistsCount
])
->
items
;
$templateData
[
'playlists'
]
=
$api
->
getUserPlaylists
(
$user
[
'id'
],
[
'limit'
=>
$playlistsCount
])
->
items
;
...
...
This diff is collapsed.
Click to expand it.
spotify/lib.php
+
59
−
7
View file @
150081b2
...
@@ -30,18 +30,18 @@ function createEmptyPlaylist($api, $playlistName = 'Auto playlist')
...
@@ -30,18 +30,18 @@ function createEmptyPlaylist($api, $playlistName = 'Auto playlist')
return
$playlist
;
return
$playlist
;
}
}
function
pickRandomTracksFromArray
(
$
recommendedT
rackIds
,
$count
=
0
)
function
pickRandomTracksFromArray
(
$
t
rackIds
,
$count
=
0
)
{
{
$
recommendedT
rackIds
=
array_keys
(
$
recommendedT
rackIds
);
$
t
rackIds
=
\
array_keys
(
$
t
rackIds
);
printLog
(
'=> Got '
.
count
(
$
recommendedT
rackIds
)
.
' unique tracks.'
);
printLog
(
'=> Got '
.
\
count
(
$
t
rackIds
)
.
' unique tracks.'
);
shuffle
(
$
recommendedT
rackIds
);
\
shuffle
(
$
t
rackIds
);
if
(
!
$count
)
{
if
(
!
$count
)
{
return
$
recommendedT
rackIds
;
return
$
t
rackIds
;
}
}
$pickedTrackIds
=
array_slice
(
$
recommendedT
rackIds
,
0
,
$count
);
$pickedTrackIds
=
\
array_slice
(
$
t
rackIds
,
0
,
$count
);
printLog
(
'Keep '
.
count
(
$pickedTrackIds
)
.
' random tracks.'
);
printLog
(
'Keep '
.
\
count
(
$pickedTrackIds
)
.
' random tracks.'
);
return
$pickedTrackIds
;
return
$pickedTrackIds
;
}
}
...
@@ -89,6 +89,37 @@ function createPlaylistWithRandomTracks($api, $trackIds, $count = 50)
...
@@ -89,6 +89,37 @@ function createPlaylistWithRandomTracks($api, $trackIds, $count = 50)
return
$api
->
getPlaylist
(
$playlist
->
id
);
return
$api
->
getPlaylist
(
$playlist
->
id
);
}
}
function
createPlaylistFromDailyMixesTracks
(
$api
,
$count
=
50
)
{
// Get daily mix playlists from user library
$userPlaylists
=
getAllPlaylistsFromUserId
(
$api
,
$api
->
me
()
->
id
);
$dailyMixPlaylistIds
=
[];
foreach
(
$userPlaylists
as
$userPlaylist
)
{
$regex
=
'/^Daily Mix [1-6]$/'
;
if
(
\preg_match
(
$regex
,
$userPlaylist
->
name
))
{
$dailyMixPlaylistIds
[]
=
$userPlaylist
->
id
;
}
}
// Get all tracks in daily mix playlists
$trackIds
=
[];
foreach
(
$dailyMixPlaylistIds
as
$dailyMixPlaylistId
)
{
$dailyMixPlaylist
=
$api
->
getPlaylist
(
$dailyMixPlaylistId
);
$tracks
=
$dailyMixPlaylist
->
tracks
->
items
;
foreach
(
$tracks
as
$track
)
{
$trackId
=
$track
->
track
->
id
;
$trackIds
[
$trackId
]
=
$trackId
;
}
}
$pickedTrackIds
=
pickRandomTracksFromArray
(
$trackIds
,
$count
);
$playlist
=
createEmptyPlaylist
(
$api
,
'Daily Mixes'
);
$api
->
replacePlaylistTracks
(
$playlist
->
id
,
$pickedTrackIds
);
return
$api
->
getPlaylist
(
$playlist
->
id
);
}
function
getCreatedPlaylistInformationMessage
(
$api
,
$playlist
)
function
getCreatedPlaylistInformationMessage
(
$api
,
$playlist
)
{
{
$link
=
'<a href="'
.
generateShowPlaylistUrl
(
$playlist
->
id
)
.
'">'
.
$playlist
->
name
.
'</a>'
;
$link
=
'<a href="'
.
generateShowPlaylistUrl
(
$playlist
->
id
)
.
'">'
.
$playlist
->
name
.
'</a>'
;
...
@@ -138,3 +169,24 @@ function createDisplayablePlaylist($playlist)
...
@@ -138,3 +169,24 @@ function createDisplayablePlaylist($playlist)
return
$output
;
return
$output
;
}
}
function
getAllPlaylistsFromUserId
(
$api
,
$userId
)
{
$playlists
=
[];
$batchSize
=
50
;
$offset
=
0
;
while
((
$offset
===
0
)
or
(
\count
(
$batch
->
items
)
===
$batchSize
))
{
$batch
=
$api
->
getUserPlaylists
(
$userId
,
[
'limit'
=>
$batchSize
,
'offset'
=>
$offset
,
]);
foreach
(
$batch
->
items
as
$playlist
)
{
$playlists
[]
=
$playlist
;
}
$offset
+=
$batchSize
;
}
return
$playlists
;
}
This diff is collapsed.
Click to expand it.
template.php
+
30
−
0
View file @
150081b2
...
@@ -54,6 +54,36 @@
...
@@ -54,6 +54,36 @@
<?php
}
?>
<?php
}
?>
<?php
if
(
count
(
$templateData
[
'topArtists'
]))
{
?>
<?php
if
(
count
(
$templateData
[
'topArtists'
]))
{
?>
<form
class=
"clearfix mt-2"
>
<div
class=
"row"
>
<div
class=
"col-md-12"
>
<legend>
Quick playlist generator:
</legend>
</div>
</div>
<div
class=
"row"
>
<div
class=
"col-md-8 col-sm-12"
>
<div>
<input
type=
"checkbox"
class=
"custom-control-input"
id=
"check-quick-create-long-playlist"
name=
"check-quick-create-long-playlist"
value=
"long-playlist"
checked
>
<label
class=
"custom-control-label"
for=
"check-quick-create-long-playlist"
>
Generate a long playlist (x2)
</label>
</div>
</div>
<div
class=
"col-md-4 col-sm-12"
>
<div
class=
"btn-group float-end"
role=
"group"
aria-label=
"Action buttons"
>
<button
name=
"generate-quick-playlist-from-daily-mixes"
type=
"submit"
class=
"btn btn-primary"
>
🎶 Mix daily mixes!
</button>
</div>
</div>
</div>
</form>
<form
class=
"clearfix mt-2"
>
<form
class=
"clearfix mt-2"
>
<div
class=
"row"
>
<div
class=
"row"
>
<div
class=
"col-md-8 col-sm-12"
>
<div
class=
"col-md-8 col-sm-12"
>
...
...
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