Skip to content

Commit 61990a3

Browse files
committed
Fixed outdated Spotify API code
1 parent 105fc4f commit 61990a3

File tree

2 files changed

+150
-97
lines changed

2 files changed

+150
-97
lines changed

src/io/github/rowak/nanoleafdesktop/spotify/SpotifyPlayer.java

Lines changed: 136 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@
1616

1717
import javax.imageio.ImageIO;
1818

19+
import org.json.JSONObject;
20+
21+
import com.github.kevinsawicki.http.HttpRequest;
1922
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
2023
import com.wrapper.spotify.SpotifyApi;
2124
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
2225
import com.wrapper.spotify.model_objects.miscellaneous.AudioAnalysis;
26+
import com.wrapper.spotify.model_objects.miscellaneous.AudioAnalysisMeasure;
27+
import com.wrapper.spotify.model_objects.miscellaneous.AudioAnalysisMeta;
28+
import com.wrapper.spotify.model_objects.miscellaneous.AudioAnalysisSection;
29+
import com.wrapper.spotify.model_objects.miscellaneous.AudioAnalysisSegment;
30+
import com.wrapper.spotify.model_objects.miscellaneous.AudioAnalysisTrack;
2331
import com.wrapper.spotify.model_objects.miscellaneous.CurrentlyPlaying;
2432
import com.wrapper.spotify.model_objects.specification.AlbumSimplified;
2533
import com.wrapper.spotify.model_objects.specification.ArtistSimplified;
2634
import com.wrapper.spotify.model_objects.specification.Image;
2735
import com.wrapper.spotify.model_objects.specification.Track;
2836
import com.wrapper.spotify.requests.data.player.GetUsersCurrentlyPlayingTrackRequest;
29-
import com.wrapper.spotify.requests.data.tracks.GetAudioAnalysisForTrackRequest;
3037

3138
import io.github.rowak.nanoleafapi.Aurora;
3239
import io.github.rowak.nanoleafapi.Color;
@@ -52,6 +59,7 @@ public class SpotifyPlayer
5259
{
5360
private boolean running, playing, usingDefaultPalette;
5461
private int progress, sensitivity, audioOffset;
62+
private int previousHue, previousSat, previousBri;
5563
private String previousEffect;
5664
private Timer effectTimer, spotifyActionTimer;
5765
private SpotifyApi spotifyApi;
@@ -232,6 +240,12 @@ private void saveCurrentEffect()
232240
try
233241
{
234242
previousEffect = auroras[0].effects().getCurrentEffectName();
243+
if (previousEffect != null && previousEffect.equals("*Solid*"))
244+
{
245+
previousHue = auroras[0].state().getHue();
246+
previousSat = auroras[0].state().getSaturation();
247+
previousBri = auroras[0].state().getBrightness();
248+
}
235249
}
236250
catch (StatusCodeException sce)
237251
{
@@ -244,10 +258,20 @@ private void loadPreviousEffect()
244258
{
245259
try
246260
{
247-
auroras[0].effects().setEffect(previousEffect);
261+
if (previousEffect != null && !previousEffect.equals("*Dynamic*") && !previousEffect.equals("*Solid*"))
262+
{
263+
auroras[0].effects().setEffect(previousEffect);
264+
}
265+
else if (previousEffect.equals("*Solid*"))
266+
{
267+
auroras[0].state().setHue(previousHue);
268+
auroras[0].state().setSaturation(previousSat);
269+
auroras[0].state().setBrightness(previousBri);
270+
}
248271
}
249-
catch (StatusCodeException | NullPointerException scenpe)
272+
catch (StatusCodeException | NullPointerException e)
250273
{
274+
e.printStackTrace();
251275
new TextDialog(panel.getFocusCycleRootAncestor(),
252276
"The previous effect could not be loaded.").setVisible(true);
253277
}
@@ -268,10 +292,10 @@ private void init()
268292
updateTrackInfoText();
269293
updateTrackProgressText();
270294

271-
java.awt.Color[] newpalette = getAlbumImagePalette();
272-
setPalette(convertPalette(newpalette));
273-
System.out.println(Arrays.asList(newpalette));
274-
panel.setPalette(newpalette);
295+
// java.awt.Color[] newpalette = getAlbumImagePalette();
296+
// setPalette(convertPalette(newpalette));
297+
// System.out.println(Arrays.asList(newpalette));
298+
// panel.setPalette(newpalette);
275299
}
276300
catch (SpotifyWebApiException swe)
277301
{
@@ -415,58 +439,58 @@ private String getArtists()
415439
return str;
416440
}
417441

418-
private BufferedImage getAlbumImage() throws IOException
419-
{
420-
BufferedImage img = null;
421-
if (currentAlbum.getImages().length > 0)
422-
{
423-
Image raw = currentAlbum.getImages()[1];
424-
img = ImageIO.read(new URL(raw.getUrl()));
425-
}
426-
return img;
427-
}
428-
429-
private java.awt.Color[] getAlbumImagePalette() throws StatusCodeException, IOException
430-
{
431-
Panel[][] rows = PanelTableSort.getRows(auroras[0].panelLayout().getPanelsRotated());
432-
BufferedImage img = getAlbumImage();
433-
Set<java.awt.Color> colors = new HashSet<java.awt.Color>();
434-
if (img != null)
435-
{
436-
final int VERTICAL_SEPARATOR = img.getHeight()/rows.length;
437-
for (int i = 0; i < rows.length; i++)
438-
{
439-
int captureY = VERTICAL_SEPARATOR*i + VERTICAL_SEPARATOR/2;
440-
441-
for (int j = 0; j < rows[i].length; j++)
442-
{
443-
final int HORIZONTAL_SEPARATOR = img.getWidth()/rows[i].length;
444-
int captureX = HORIZONTAL_SEPARATOR*j + HORIZONTAL_SEPARATOR/2;
445-
446-
try
447-
{
448-
if (img.getSubimage(captureX, captureY, 1, 1) != null)
449-
{
450-
java.awt.Color c = new java.awt.Color(img.getRGB(captureX, captureY));
451-
if (!c.equals(java.awt.Color.BLACK))
452-
{
453-
colors.add(c);
454-
}
455-
}
456-
}
457-
catch (RasterFormatException rfe)
458-
{
459-
// catch, but ignore
460-
}
461-
}
462-
}
463-
}
464-
else
465-
{
466-
System.out.println("ERROR: Invalid album image");
467-
}
468-
return colors.toArray(new java.awt.Color[]{});
469-
}
442+
// private BufferedImage getAlbumImage() throws IOException
443+
// {
444+
// BufferedImage img = null;
445+
// if (currentAlbum.getImages().length > 0)
446+
// {
447+
// Image raw = currentAlbum.getImages()[1];
448+
// img = ImageIO.read(new URL(raw.getUrl()));
449+
// }
450+
// return img;
451+
// }
452+
//
453+
// private java.awt.Color[] getAlbumImagePalette() throws StatusCodeException, IOException
454+
// {
455+
// Panel[][] rows = PanelTableSort.getRows(auroras[0].panelLayout().getPanelsRotated());
456+
// BufferedImage img = getAlbumImage();
457+
// Set<java.awt.Color> colors = new HashSet<java.awt.Color>();
458+
// if (img != null)
459+
// {
460+
// final int VERTICAL_SEPARATOR = img.getHeight()/rows.length;
461+
// for (int i = 0; i < rows.length; i++)
462+
// {
463+
// int captureY = VERTICAL_SEPARATOR*i + VERTICAL_SEPARATOR/2;
464+
//
465+
// for (int j = 0; j < rows[i].length; j++)
466+
// {
467+
// final int HORIZONTAL_SEPARATOR = img.getWidth()/rows[i].length;
468+
// int captureX = HORIZONTAL_SEPARATOR*j + HORIZONTAL_SEPARATOR/2;
469+
//
470+
// try
471+
// {
472+
// if (img.getSubimage(captureX, captureY, 1, 1) != null)
473+
// {
474+
// java.awt.Color c = new java.awt.Color(img.getRGB(captureX, captureY));
475+
// if (!c.equals(java.awt.Color.BLACK))
476+
// {
477+
// colors.add(c);
478+
// }
479+
// }
480+
// }
481+
// catch (RasterFormatException rfe)
482+
// {
483+
// // catch, but ignore
484+
// }
485+
// }
486+
// }
487+
// }
488+
// else
489+
// {
490+
// System.out.println("ERROR: Invalid album image");
491+
// }
492+
// return colors.toArray(new java.awt.Color[]{});
493+
// }
470494

471495
private Direction getDirectionFromStr(String str)
472496
{
@@ -508,19 +532,19 @@ private void checkTrackStateChange()
508532
updateTrackInfoText();
509533
updateTrackProgressText();
510534

511-
if (usingDefaultPalette)
512-
{
513-
try
514-
{
515-
java.awt.Color[] newpalette = getAlbumImagePalette();
516-
setPalette(convertPalette(newpalette));
517-
panel.setPalette(newpalette);
518-
}
519-
catch (StatusCodeException e)
520-
{
521-
e.printStackTrace();
522-
}
523-
}
535+
// if (usingDefaultPalette)
536+
// {
537+
// try
538+
// {
539+
// java.awt.Color[] newpalette = getAlbumImagePalette();
540+
// setPalette(convertPalette(newpalette));
541+
// panel.setPalette(newpalette);
542+
// }
543+
// catch (StatusCodeException e)
544+
// {
545+
// e.printStackTrace();
546+
// }
547+
// }
524548
}
525549

526550
float progressDiff = Math.abs(current.getProgress_ms() - progress);
@@ -565,15 +589,50 @@ private CurrentlyPlaying getCurrentlyPlaying()
565589
final GetUsersCurrentlyPlayingTrackRequest trackRequest = spotifyApi
566590
.getUsersCurrentlyPlayingTrack()
567591
.build();
568-
return trackRequest.execute();
592+
CurrentlyPlaying curr = trackRequest.execute();
593+
return curr;
569594
}
570595

596+
/*
597+
* This is a very hacky solution to a very annoying problem. The Spotify Java
598+
* API library I'm using doesn't properly handle JSON exceptions, so when
599+
* Spotify made a very small change to the JSON format, it caused the
600+
* AudioAnalysis object creation to fail.
601+
*
602+
* This solution handles the JSON format change.
603+
*/
571604
private AudioAnalysis getTrackAnalysis(String trackId)
572-
throws SpotifyWebApiException, IOException
573605
{
574-
final GetAudioAnalysisForTrackRequest trackAnalysisRequest = spotifyApi
575-
.getAudioAnalysisForTrack(trackId)
606+
HttpRequest req = HttpRequest.get("https://api.spotify.com/v1/audio-analysis/" + trackId);
607+
req.header("Content-Type", "application/json");
608+
req.header("Accept", "application/json");
609+
req.header("Authorization", "Bearer " + spotifyApi.getAccessToken());
610+
JSONObject json = new JSONObject(req.body());
611+
json.getJSONObject("meta").put("status_code", req.code());
612+
AudioAnalysisMeta aamet = new AudioAnalysisMeta.JsonUtil().createModelObject(json.getJSONObject("meta").toString());
613+
AudioAnalysisTrack aat = new AudioAnalysisTrack.JsonUtil().createModelObject(json.getJSONObject("track").toString());
614+
AudioAnalysisMeasure[] aamba = new AudioAnalysisMeasure.JsonUtil().createModelObjectArray(json.getJSONArray("bars").toString());
615+
AudioAnalysisMeasure[] aambe = new AudioAnalysisMeasure.JsonUtil().createModelObjectArray(json.getJSONArray("beats").toString());
616+
AudioAnalysisSection[] aasec = new AudioAnalysisSection.JsonUtil().createModelObjectArray(json.getJSONArray("sections").toString());
617+
AudioAnalysisSegment[] aaseg = new AudioAnalysisSegment.JsonUtil().createModelObjectArray(json.getJSONArray("segments").toString());
618+
AudioAnalysisMeasure[] aamta = new AudioAnalysisMeasure.JsonUtil().createModelObjectArray(json.getJSONArray("tatums").toString());
619+
return new AudioAnalysis.Builder()
620+
.setMeta(aamet)
621+
.setTrack(aat)
622+
.setBars(aamba)
623+
.setBeats(aambe)
624+
.setSections(aasec)
625+
.setSegments(aaseg)
626+
.setTatums(aamta)
576627
.build();
577-
return trackAnalysisRequest.execute();
578628
}
629+
630+
// private AudioAnalysis getTrackAnalysis(String trackId)
631+
// throws SpotifyWebApiException, IOException
632+
// {
633+
// final GetAudioAnalysisForTrackRequest trackAnalysisRequest = spotifyApi
634+
// .getAudioAnalysisForTrack(trackId)
635+
// .build();
636+
// return trackAnalysisRequest.execute();
637+
// }
579638
}

src/io/github/rowak/nanoleafdesktop/ui/panel/SpotifyPanel.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -549,31 +549,25 @@ private void loadUserSettings()
549549
PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH);
550550

551551
String lastSensitivity = manager.getProperty("spotifySensitivity");
552-
if (lastSensitivity != null)
552+
try
553553
{
554-
try
555-
{
556-
sensitivity = Integer.parseInt(lastSensitivity);
557-
sensitivitySlider.setValue(sensitivity);
558-
}
559-
catch (NumberFormatException nfe)
560-
{
561-
sensitivity = DEFAULT_SENSITIVITY;
562-
}
554+
sensitivity = Integer.parseInt(lastSensitivity);
555+
}
556+
catch (NumberFormatException | NullPointerException e)
557+
{
558+
sensitivity = DEFAULT_SENSITIVITY;
563559
}
560+
sensitivitySlider.setValue(sensitivity);
564561
String lastAudioOffset = manager.getProperty("spotifyAudioOffset");
565-
if (lastAudioOffset != null)
562+
try
566563
{
567-
try
568-
{
569-
audioOffset = Integer.parseInt(lastAudioOffset);
570-
audioOffsetSlider.setValue(audioOffset);
571-
}
572-
catch (NumberFormatException nfe)
573-
{
574-
audioOffset = DEFAULT_AUDIO_OFFSET;
575-
}
564+
audioOffset = Integer.parseInt(lastAudioOffset);
565+
}
566+
catch (NumberFormatException | NullPointerException e)
567+
{
568+
audioOffset = DEFAULT_AUDIO_OFFSET;
576569
}
570+
audioOffsetSlider.setValue(audioOffset);
577571
}
578572

579573
private void setProperty(String key, Object value)

0 commit comments

Comments
 (0)