Skip to content

Commit 6ff8d35

Browse files
committed
Moved image retrieval to public Player#currentCoverImage (#182)
1 parent a9cdbdc commit 6ff8d35

1 file changed

Lines changed: 55 additions & 41 deletions

File tree

  • core/src/main/java/xyz/gianlu/librespot/player

core/src/main/java/xyz/gianlu/librespot/player/Player.java

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import okhttp3.Response;
1010
import okhttp3.ResponseBody;
1111
import org.apache.log4j.Logger;
12-
import org.jetbrains.annotations.Contract;
1312
import org.jetbrains.annotations.NotNull;
1413
import org.jetbrains.annotations.Nullable;
1514
import org.jetbrains.annotations.Range;
@@ -662,6 +661,51 @@ public PlayableId currentPlayableId() {
662661
return state.getCurrentPlayable();
663662
}
664663

664+
@Nullable
665+
public byte[] currentCoverImage() throws IOException {
666+
Metadata.Track track = currentTrack();
667+
Metadata.Episode episode = currentEpisode();
668+
Metadata.ImageGroup group = null;
669+
if (track != null) {
670+
if (track.hasAlbum() && track.getAlbum().hasCoverGroup())
671+
group = track.getAlbum().getCoverGroup();
672+
} else if (episode != null) {
673+
if (episode.hasCoverImage())
674+
group = episode.getCoverImage();
675+
} else {
676+
throw new IllegalStateException();
677+
}
678+
679+
ImageId image = null;
680+
if (group == null) {
681+
PlayableId id = state.getCurrentPlayable();
682+
if (id == null) return null;
683+
684+
Map<String, String> metadata = state.metadataFor(id);
685+
for (String key : ImageId.IMAGE_SIZES_URLS) {
686+
if (metadata.containsKey(key)) {
687+
image = ImageId.fromUri(metadata.get(key));
688+
break;
689+
}
690+
}
691+
} else {
692+
image = ImageId.biggestImage(group);
693+
}
694+
695+
if (image == null)
696+
return null;
697+
698+
try (Response resp = session.client().newCall(new Request.Builder()
699+
.url("http://open.spotify.com/image/" + image.hexId()).build())
700+
.execute()) {
701+
ResponseBody body;
702+
if (resp.code() == 200 && (body = resp.body()) != null)
703+
return body.bytes();
704+
else
705+
throw new IOException(String.format("Bad response code. {id: %s, code: %d}", image.hexId(), resp.code()));
706+
}
707+
}
708+
665709
/**
666710
* @return The current position of the player or {@code -1} if unavailable (most likely if it's playing an episode).
667711
*/
@@ -766,7 +810,7 @@ private void send(@NotNull String type, @NotNull String code, @Nullable byte[] p
766810

767811
if (payload != null) {
768812
out.write(String.format("<item><type>%s</type><code>%s</code><length>%d</length>\n<data encoding=\"base64\">%s</data></item>\n", type, code,
769-
payload.length, new String(Base64.getEncoder().encode(payload),StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8));
813+
payload.length, new String(Base64.getEncoder().encode(payload), StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8));
770814
} else {
771815
out.write(String.format("<item><type>%s</type><code>%s</code><length>0</length></item>\n", type, code).getBytes(StandardCharsets.UTF_8));
772816
}
@@ -790,51 +834,21 @@ private class EventsDispatcher {
790834
}
791835
}
792836

793-
@Contract("null, null -> fail")
794-
private void sendImage(@Nullable Metadata.Track track, @Nullable Metadata.Episode episode) {
795-
Metadata.ImageGroup group = null;
796-
if (track != null) {
797-
if (track.hasAlbum() && track.getAlbum().hasCoverGroup())
798-
group = track.getAlbum().getCoverGroup();
799-
} else if (episode != null) {
800-
if (episode.hasCoverImage())
801-
group = episode.getCoverImage();
802-
} else {
803-
throw new IllegalStateException();
804-
}
805-
806-
ImageId image = null;
807-
if (group == null) {
808-
PlayableId id = state.getCurrentPlayable();
809-
if (id == null) return;
810-
811-
Map<String, String> metadata = state.metadataFor(id);
812-
for (String key : ImageId.IMAGE_SIZES_URLS) {
813-
if (metadata.containsKey(key)) {
814-
image = ImageId.fromUri(metadata.get(key));
815-
break;
816-
}
817-
}
818-
} else {
819-
image = ImageId.biggestImage(group);
837+
private void sendImage() {
838+
byte[] image;
839+
try {
840+
image = currentCoverImage();
841+
} catch (IOException ex) {
842+
LOGGER.warn("Failed downloading image.", ex);
843+
return;
820844
}
821845

822846
if (image == null) {
823847
LOGGER.warn("No image found in metadata.");
824848
return;
825849
}
826850

827-
try (Response resp = session.client().newCall(new Request.Builder()
828-
.url("http://open.spotify.com/image/" + image.hexId()).build())
829-
.execute()) {
830-
ResponseBody body;
831-
if (resp.code() == 200 && (body = resp.body()) != null)
832-
metadataPipe.safeSend(MetadataPipe.TYPE_SSNC, MetadataPipe.CODE_PICT, body.bytes());
833-
else
834-
LOGGER.warn(String.format("Failed downloading image. {id: %s, code: %d}", image.hexId(), resp.code()));
835-
} catch (IOException ex) {
836-
LOGGER.warn("Failed downloading image.", ex);
837-
}
851+
metadataPipe.safeSend(MetadataPipe.TYPE_SSNC, MetadataPipe.CODE_PICT, image);
838852
}
839853

840854
private void sendProgress() {
@@ -939,7 +953,7 @@ void metadataAvailable() {
939953
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_ASAR, artist);
940954

941955
sendProgress();
942-
sendImage(track, episode);
956+
sendImage();
943957
}
944958
}
945959

0 commit comments

Comments
 (0)