Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saved tracks #35

Merged
merged 3 commits into from Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ def tracks_get(playlist_id):
tracks = spotify._get_playlist_tracks(playlist_id)['items']
del spotify

return jsonify(tracks['items'])
return jsonify(tracks)

@app.route('/<playlist_id>', methods=['GET'])
def playlist_get(playlist_id):
access_token = request.headers['access_token']
spotify = Spotify(access_token)

playlist = spotify.get_playlist(playlist_id)
return jsonify(playlist)


@app.route('/<playlist_id>/analysis/lyrics', methods=['GET'])
def lyrics_analysis(playlist_id):
Expand All @@ -84,7 +93,7 @@ def lyrics_analysis(playlist_id):

# select random sample of songs
max_songs=20
tracks = random.sample(tracks,k=max_songs)
tracks = random.sample(tracks,k=min(max_songs, len(tracks)))

lyrics_count = {}

Expand Down Expand Up @@ -156,6 +165,5 @@ def full_analysis(playlist_id):
return jsonify(payload)



if __name__ == '__main__':
app.run()
46 changes: 45 additions & 1 deletion lib/Spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,49 @@ def get_playlists(self):
returns: list of playlist objects
'''
playlists = self._spotify.current_user_playlists()
playlists["items"].append(self.get_playlist("saved_tracks"))
return playlists

def get_playlist(self, id):
'''
Gets information about a given playlist

returns: a playlist object
'''

if(id == "saved_tracks"):

playlist= {
"name": "Saved Tracks",
"description": "Saved Tracks",
"id": "saved_tracks",
"collaborative": False,
"external_urls": {
"spotify": "https://open.spotify.com/collection/tracks"
},
"href": "https://api.spotify.com/v1/me/tracks",
"images": [
{
"url": "https://t.scdn.co/images/3099b3803ad9496896c43f22fe9be8c4.png"
}
],
"tracks": self._spotify.current_user_saved_tracks(),
"owner": self._spotify.current_user(),
"public": None,
"snapshot_id": None,
"uri": None,
"type": "playlist",
"followers": {
"href": None,
"total": 0
}
}

else:
playlist = self._spotify.playlist(id)

return playlist

def _get_playlist_tracks(self,id):
'''
Gets the first 100 tracks inside a certain playlist
Expand All @@ -30,7 +71,10 @@ def _get_playlist_tracks(self,id):
Returns:
tracks - a list of tracks inside that playlist
'''
tracks = self._spotify.playlist_tracks(id)
if(id == "saved_tracks"):
tracks = self._spotify.current_user_saved_tracks()
else:
tracks = self._spotify.playlist_tracks(id)
return tracks

def _get_features(self,track_ids):
Expand Down
4 changes: 2 additions & 2 deletions web/src/Pages/Analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ class Analysis extends React.Component {
fetchPlaylist = async () => {
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.state.accessToken
'access_token': this.state.accessToken
}

const response = await axios.get(`https://api.spotify.com/v1/playlists/${this.state.id}`, {headers: headers})
const response = await axios.get(URL_BASE + `${this.state.id}`, {headers: headers})

if(response.status === 200) {
//console.log(response)
Expand Down
4 changes: 2 additions & 2 deletions web/src/Pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const base_url = 'https://accounts.spotify.com/authorize?'
const payload = {
client_id: '0ca7dd0007fd4ff2a34c3aab07379970',
response_type: 'code',
scope: 'playlist-read-private playlist-read-collaborative user-top-read',
scope: 'playlist-read-private playlist-read-collaborative user-top-read user-library-read',
redirect_uri: process.env.REACT_APP_REDIRECT_URI,
show_dialog: true
}
Expand Down Expand Up @@ -255,4 +255,4 @@ return(
}


export default Home
export default Home