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

Add isPlaylist to response JSON #91

Merged
merged 3 commits into from
Jun 2, 2018
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
31 changes: 17 additions & 14 deletions IMPLEMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,24 @@ Authorization: youshallnotpass

Response:
```json
[
{
"track": "QAAAjQIAJVJpY2sgQXN0bGV5IC0gTmV2ZXIgR29ubmEgR2l2ZSBZb3UgVXAADlJpY2tBc3RsZXlWRVZPAAAAAAADPCAAC2RRdzR3OVdnWGNRAAEAK2h0dHBzOi8vd3d3LnlvdXR1YmUuY29tL3dhdGNoP3Y9ZFF3NHc5V2dYY1EAB3lvdXR1YmUAAAAAAAAAAA==",
"info": {
"identifier": "dQw4w9WgXcQ",
"isSeekable": true,
"author": "RickAstleyVEVO",
"length": 212000,
"isStream": false,
"position": 0,
"title": "Rick Astley - Never Gonna Give You Up",
"uri": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
{
"isPlaylist": false,
"tracks": [
{
"track": "QAAAjQIAJVJpY2sgQXN0bGV5IC0gTmV2ZXIgR29ubmEgR2l2ZSBZb3UgVXAADlJpY2tBc3RsZXlWRVZPAAAAAAADPCAAC2RRdzR3OVdnWGNRAAEAK2h0dHBzOi8vd3d3LnlvdXR1YmUuY29tL3dhdGNoP3Y9ZFF3NHc5V2dYY1EAB3lvdXR1YmUAAAAAAAAAAA==",
"info": {
"identifier": "dQw4w9WgXcQ",
"isSeekable": true,
"author": "RickAstleyVEVO",
"length": 212000,
"isStream": false,
"position": 0,
"title": "Rick Astley - Never Gonna Give You Up",
"uri": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
}
}
}
]
]
}
```

### Special notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AudioLoader implements AudioLoadResultHandler {
Expand All @@ -39,13 +40,16 @@ public class AudioLoader implements AudioLoadResultHandler {
private final AudioPlayerManager audioPlayerManager;

private List<AudioTrack> loadedItems;
private boolean isPlaylist = false;
private String playlistName = null;
private Integer selectedTrack = null;
private boolean used = false;

public AudioLoader(AudioPlayerManager audioPlayerManager) {
this.audioPlayerManager = audioPlayerManager;
}

List<AudioTrack> loadSync(String identifier) throws InterruptedException {
LoadResult loadSync(String identifier) throws InterruptedException {
if(used)
throw new IllegalStateException("This loader can only be used once per instance");

Expand All @@ -57,7 +61,7 @@ List<AudioTrack> loadSync(String identifier) throws InterruptedException {
this.wait();
}

return loadedItems;
return new LoadResult(loadedItems, isPlaylist, playlistName, selectedTrack);
}

@Override
Expand All @@ -72,6 +76,12 @@ public void trackLoaded(AudioTrack audioTrack) {

@Override
public void playlistLoaded(AudioPlaylist audioPlaylist) {
if (!audioPlaylist.isSearchResult()) {
isPlaylist = true;
playlistName = audioPlaylist.getName();
selectedTrack = audioPlaylist.getTracks().indexOf(audioPlaylist.getSelectedTrack());
}

log.info("Loaded playlist " + audioPlaylist.getName());
loadedItems = audioPlaylist.getTracks();
synchronized (this) {
Expand All @@ -98,3 +108,17 @@ public void loadFailed(FriendlyException e) {
}

}

class LoadResult {
public List<AudioTrack> tracks;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this immutable?

public boolean isPlaylist;
public String playlistName;
public Integer selectedTrack;

public LoadResult(List<AudioTrack> tracks, boolean isPlaylist, String playlistName, Integer selectedTrack) {
this.tracks = Collections.unmodifiableList(tracks);
this.isPlaylist = isPlaylist;
this.playlistName = playlistName;
this.selectedTrack = selectedTrack;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class AudioLoaderRestHandler {
Expand Down Expand Up @@ -97,10 +96,12 @@ public String getLoadTracks(HttpServletRequest request, HttpServletResponse resp
if (!isAuthorized(request, response))
return "";

JSONObject json = new JSONObject();
JSONObject playlist = new JSONObject();
JSONArray tracks = new JSONArray();
List<AudioTrack> list = new AudioLoader(audioPlayerManager).loadSync(identifier);
LoadResult result = new AudioLoader(audioPlayerManager).loadSync(identifier);

list.forEach(track -> {
result.tracks.forEach(track -> {
JSONObject object = new JSONObject();
object.put("info", trackToJSON(track));

Expand All @@ -113,7 +114,14 @@ public String getLoadTracks(HttpServletRequest request, HttpServletResponse resp
}
});

return tracks.toString();
playlist.put("name", result.playlistName);
playlist.put("selectedTrack", result.selectedTrack);

json.put("isPlaylist", result.isPlaylist);
json.put("playlistInfo", playlist);
json.put("tracks", tracks);

return json.toString();
}

@GetMapping(value = "/decodetrack", produces = "application/json")
Expand Down