Skip to content

Commit

Permalink
Merge pull request #5 from Rectify-Sideproject/3-adding-the-songs-to-…
Browse files Browse the repository at this point in the history
…a-newly-created-playlist

3 adding the songs to a newly created playlist
  • Loading branch information
marvellous10 authored Jul 30, 2024
2 parents 31cece4 + 3064dec commit 38503e7
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 1 deletion.
Binary file added backend/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added backend/__pycache__/settings.cpython-312.pyc
Binary file not shown.
Binary file added backend/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file added createplaylist/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added createplaylist/__pycache__/admin.cpython-312.pyc
Binary file not shown.
Binary file added createplaylist/__pycache__/apps.cpython-312.pyc
Binary file not shown.
Binary file added createplaylist/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file added createplaylist/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file added createplaylist/__pycache__/views.cpython-312.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions createplaylist/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
path('search/', SearchSong.as_view(), name='spotify_search'),
path('getcurrentsong/', GetCurrentSong.as_view(), name='getcurrentsong'),
path('createplaylist/', CreatePlaylist.as_view(), name='createplaylist'),
path('addplaylist/', CreatePlaylistOnSpotify.as_view(), name='addplaylist'),
]
45 changes: 44 additions & 1 deletion createplaylist/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,47 @@ def post(self, request, format=None, *args, **kwargs):

class CreatePlaylistOnSpotify(APIView):
def post(self, request, format=None, *args, **kwargs):
pass
#{Daniel's} addition of adding playlist feature.
#Simply create a new playlist and add tracks to that playlist's ID
tracks = request.data['tracks']
user_id = request.data['id']
scope='playlist-modify-public'
sp = spotifyconnect(scope=scope)

#Get first word in song title
song_title = request.data['song_title']
song = song_title.split(' ')[0]

playlist_name = f"Rectify playlist from {song}"
desc = f"Playlist created from rectify for the song {song}"

create_playlist = sp.user_playlist_create(user_id, name=playlist_name, public=True,collaborative=True, description=desc)

# We need playlists ID for adding songs to playlist.
playlist_id = create_playlist['id'] # Based on what was show in the Response when testing for create_playlist

# FEATURE ENHANCEMENT: MAKE SURE ADDED SONGS ISNT IN PLAYLIST ACCORDING TO ID --- LATER REQUEST ---


tracks_id = []

for id in range(len(tracks)):
tracks_id.append(tracks[id]['track_id'])


try:
sp.user_playlist_add_tracks(user_id, playlist_id=playlist_id, tracks=tracks_id) #Line to add songs to the playlist
except:
return Response(
{
"Error": "An error occurred"
},
status=status.HTTP_406_NOT_ACCEPTABLE
)
return Response(
{
"Playlist": "Added songs to playlist"
},
status=status.HTTP_201_CREATED
)

0 comments on commit 38503e7

Please sign in to comment.