Skip to content

Commit

Permalink
Replace "Export All" button for "Export Page"
Browse files Browse the repository at this point in the history
Spotify limits paginations of playlist objects to a maximum of 50 items.
Depending on how many playlists a user has, multiple calls are
required, and Spotify frequently returns "Too many request" errors for
relatively small count of requests.

This commit replaces the "Export all" button with a "Export page" button
in a attempt to force a single request to be made and avoid back pressure
from Spotify.

To be clear, whats being done here is replacing the inconvenience of not
being able to download multiple playlists simultaneously due to quota
limits with the inconvenience of manually having to click a button to
download playlists in batches a total of playlist_count / 50 times.
  • Loading branch information
htaunay committed Jul 23, 2020
1 parent e6e08ba commit f8d7150
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions exportify.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ window.Helpers = {
alert(jqXHR.responseText);
}
})
}
},
}

var PlaylistTable = React.createClass({
Expand All @@ -49,7 +49,7 @@ var PlaylistTable = React.createClass({
playlists: [],
playlistCount: 0,
nextURL: null,
prevURL: null
prevURL: null,
};
},

Expand All @@ -68,7 +68,7 @@ var PlaylistTable = React.createClass({
this.props.access_token
),
window.Helpers.apiCall(
"https://api.spotify.com/v1/users/" + userId + "/playlists",
"https://api.spotify.com/v1/users/" + userId + "/playlists?limit=50",
this.props.access_token
)
])
Expand All @@ -91,6 +91,7 @@ var PlaylistTable = React.createClass({
this.setState({
playlists: playlists,
playlistCount: response.total,
playlistOffset: response.offset,
nextURL: response.next,
prevURL: response.previous
});
Expand All @@ -102,7 +103,7 @@ var PlaylistTable = React.createClass({
},

exportPlaylists: function() {
PlaylistsExporter.export(this.props.access_token, this.state.playlistCount);
PlaylistsExporter.export(this.props.access_token, this.state.playlistOffset);
},

componentDidMount: function() {
Expand All @@ -123,7 +124,7 @@ var PlaylistTable = React.createClass({
<th style={{width: "100px"}}>Tracks</th>
<th style={{width: "120px"}}>Public?</th>
<th style={{width: "120px"}}>Collaborative?</th>
<th style={{width: "100px"}} className="text-right"><button className="btn btn-default btn-xs" type="submit" onClick={this.exportPlaylists}><span className="fa fa-file-archive-o"></span> Export All</button></th>
<th style={{width: "100px"}} className="text-right"><button className="btn btn-default btn-xs" type="submit" onClick={this.exportPlaylists}><span className="fa fa-file-archive-o"></span> Export Page</button></th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -231,11 +232,11 @@ var Paginator = React.createClass({

// Handles exporting all playlist data as a zip file
var PlaylistsExporter = {
export: function(access_token, playlistCount) {
export: function(access_token, offset) {
var playlistFileNames = [];

window.Helpers.apiCall("https://api.spotify.com/v1/me", access_token).then(function(response) {
var limit = 20;
var limit = 50;
var userId = response.id;

// Initialize requests with starred playlist
Expand All @@ -246,13 +247,11 @@ var PlaylistsExporter = {
)
];

// Add other playlists
for (var offset = 0; offset < playlistCount; offset = offset + limit) {
var url = "https://api.spotify.com/v1/users/" + userId + "/playlists";
requests.push(
window.Helpers.apiCall(url + '?offset=' + offset + '&limit=' + limit, access_token)
)
}
// Add playlists in current page
var url = "https://api.spotify.com/v1/users/" + userId + "/playlists";
requests.push(
window.Helpers.apiCall(url + '?offset=' + offset + '&limit=' + limit, access_token)
)

$.when.apply($, requests).then(function() {
var playlists = [];
Expand Down

0 comments on commit f8d7150

Please sign in to comment.