Skip to content

Commit 4da289e

Browse files
committed
Adding support for podcasts in currently playing
#54
1 parent f0faeac commit 4da289e

File tree

2 files changed

+133
-46
lines changed

2 files changed

+133
-46
lines changed

src/SpotifyArduino.cpp

+123-44
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,11 @@ bool SpotifyArduino::transferPlayback(const char *deviceId, bool play)
502502

503503
int SpotifyArduino::getCurrentlyPlaying(processCurrentlyPlaying currentlyPlayingCallback, const char *market)
504504
{
505-
char command[50] = SPOTIFY_CURRENTLY_PLAYING_ENDPOINT;
505+
char command[75] = SPOTIFY_CURRENTLY_PLAYING_ENDPOINT;
506506
if (market[0] != 0)
507507
{
508508
char marketBuff[15];
509-
sprintf(marketBuff, "?market=%s", market);
509+
sprintf(marketBuff, "&market=%s", market);
510510
strcat(command, marketBuff);
511511
}
512512

@@ -538,8 +538,9 @@ int SpotifyArduino::getCurrentlyPlaying(processCurrentlyPlaying currentlyPlaying
538538
CurrentlyPlaying current;
539539

540540
//Apply Json Filter: https://arduinojson.org/v6/example/filter/
541-
StaticJsonDocument<320> filter;
541+
StaticJsonDocument<464> filter;
542542
filter["is_playing"] = true;
543+
filter["currently_playing_type"] = true;
543544
filter["progress_ms"] = true;
544545
filter["context"]["uri"] = true;
545546

@@ -561,6 +562,16 @@ int SpotifyArduino::getCurrentlyPlaying(processCurrentlyPlaying currentlyPlaying
561562
filter_item_album_images_0["width"] = true;
562563
filter_item_album_images_0["url"] = true;
563564

565+
// Podcast filters
566+
JsonObject filter_item_show = filter_item.createNestedObject("show");
567+
filter_item_show["name"] = true;
568+
filter_item_show["uri"] = true;
569+
570+
JsonObject filter_item_images_0 = filter_item["images"].createNestedObject();
571+
filter_item_images_0["height"] = true;
572+
filter_item_images_0["width"] = true;
573+
filter_item_images_0["url"] = true;
574+
564575
// Allocate DynamicJsonDocument
565576
DynamicJsonDocument doc(bufferSize);
566577

@@ -578,63 +589,131 @@ int SpotifyArduino::getCurrentlyPlaying(processCurrentlyPlaying currentlyPlaying
578589
#endif
579590
JsonObject item = doc["item"];
580591

581-
int numArtists = item["artists"].size();
582-
if (numArtists > SPOTIFY_MAX_NUM_ARTISTS)
592+
const char *currently_playing_type = doc["currently_playing_type"];
593+
594+
current.isPlaying = doc["is_playing"].as<bool>();
595+
596+
current.progressMs = doc["progress_ms"].as<long>();
597+
current.durationMs = item["duration_ms"].as<long>();
598+
599+
// context may be null
600+
if (!doc["context"].isNull())
583601
{
584-
numArtists = SPOTIFY_MAX_NUM_ARTISTS;
602+
current.contextUri = doc["context"]["uri"].as<const char *>();
585603
}
586-
current.numArtists = numArtists;
587-
588-
for (int i = 0; i < current.numArtists; i++)
604+
else
589605
{
590-
current.artists[i].artistName = item["artists"][i]["name"].as<const char *>();
591-
current.artists[i].artistUri = item["artists"][i]["uri"].as<const char *>();
606+
current.contextUri = NULL;
592607
}
593608

594-
current.albumName = item["album"]["name"].as<const char *>();
595-
current.albumUri = item["album"]["uri"].as<const char *>();
596-
597-
JsonArray images = item["album"]["images"];
598-
599-
// Images are returned in order of width, so last should be smallest.
600-
int numImages = images.size();
601-
int startingIndex = 0;
602-
if (numImages > SPOTIFY_NUM_ALBUM_IMAGES)
609+
// Check currently playing type
610+
if (strcmp(currently_playing_type, "track") == 0)
611+
{
612+
current.currentlyPlayingType = track;
613+
}
614+
else if (strcmp(currently_playing_type, "episode") == 0)
603615
{
604-
startingIndex = numImages - SPOTIFY_NUM_ALBUM_IMAGES;
605-
current.numImages = SPOTIFY_NUM_ALBUM_IMAGES;
616+
current.currentlyPlayingType = episode;
606617
}
607618
else
608619
{
609-
current.numImages = numImages;
620+
current.currentlyPlayingType = other;
610621
}
611-
#ifdef SPOTIFY_DEBUG
612-
Serial.print(F("Num Images: "));
613-
Serial.println(current.numImages);
614-
Serial.println(numImages);
615-
#endif
616622

617-
for (int i = 0; i < current.numImages; i++)
623+
// If it's a song/track
624+
if (current.currentlyPlayingType == track)
618625
{
619-
int adjustedIndex = startingIndex + i;
620-
current.albumImages[i].height = images[adjustedIndex]["height"].as<int>();
621-
current.albumImages[i].width = images[adjustedIndex]["width"].as<int>();
622-
current.albumImages[i].url = images[adjustedIndex]["url"].as<const char *>();
623-
}
626+
int numArtists = item["artists"].size();
627+
if (numArtists > SPOTIFY_MAX_NUM_ARTISTS)
628+
{
629+
numArtists = SPOTIFY_MAX_NUM_ARTISTS;
630+
}
631+
current.numArtists = numArtists;
624632

625-
current.trackName = item["name"].as<const char *>();
626-
current.trackUri = item["uri"].as<const char *>();
633+
for (int i = 0; i < current.numArtists; i++)
634+
{
635+
current.artists[i].artistName = item["artists"][i]["name"].as<const char *>();
636+
current.artists[i].artistUri = item["artists"][i]["uri"].as<const char *>();
637+
}
627638

628-
current.isPlaying = doc["is_playing"].as<bool>();
639+
current.albumName = item["album"]["name"].as<const char *>();
640+
current.albumUri = item["album"]["uri"].as<const char *>();
629641

630-
current.progressMs = doc["progress_ms"].as<long>();
631-
current.durationMs = item["duration_ms"].as<long>();
642+
JsonArray images = item["album"]["images"];
632643

633-
// context may be null
634-
if( ! doc["context"].isNull() ){
635-
current.contextUri = doc["context"]["uri"].as<const char *>();
636-
} else {
637-
current.contextUri = NULL;
644+
// Images are returned in order of width, so last should be smallest.
645+
int numImages = images.size();
646+
int startingIndex = 0;
647+
if (numImages > SPOTIFY_NUM_ALBUM_IMAGES)
648+
{
649+
startingIndex = numImages - SPOTIFY_NUM_ALBUM_IMAGES;
650+
current.numImages = SPOTIFY_NUM_ALBUM_IMAGES;
651+
}
652+
else
653+
{
654+
current.numImages = numImages;
655+
}
656+
#ifdef SPOTIFY_DEBUG
657+
Serial.print(F("Num Images: "));
658+
Serial.println(current.numImages);
659+
Serial.println(numImages);
660+
#endif
661+
662+
for (int i = 0; i < current.numImages; i++)
663+
{
664+
int adjustedIndex = startingIndex + i;
665+
current.albumImages[i].height = images[adjustedIndex]["height"].as<int>();
666+
current.albumImages[i].width = images[adjustedIndex]["width"].as<int>();
667+
current.albumImages[i].url = images[adjustedIndex]["url"].as<const char *>();
668+
}
669+
670+
current.trackName = item["name"].as<const char *>();
671+
current.trackUri = item["uri"].as<const char *>();
672+
}
673+
else if (current.currentlyPlayingType == episode) // Podcast
674+
{
675+
current.numArtists = 1;
676+
677+
// Save Podcast as the "track"
678+
current.trackName = item["name"].as<const char *>();
679+
current.trackUri = item["uri"].as<const char *>();
680+
681+
// Save Show name as the "artist"
682+
current.artists[0].artistName = item["show"]["name"].as<const char *>();
683+
current.artists[0].artistUri = item["show"]["uri"].as<const char *>();
684+
685+
// Leave "album" name blank
686+
char blank[1] = "";
687+
current.albumName = blank;
688+
current.albumUri = blank;
689+
690+
// Save the episode images as the "album art"
691+
JsonArray images = item["images"];
692+
// Images are returned in order of width, so last should be smallest.
693+
int numImages = images.size();
694+
int startingIndex = 0;
695+
if (numImages > SPOTIFY_NUM_ALBUM_IMAGES)
696+
{
697+
startingIndex = numImages - SPOTIFY_NUM_ALBUM_IMAGES;
698+
current.numImages = SPOTIFY_NUM_ALBUM_IMAGES;
699+
}
700+
else
701+
{
702+
current.numImages = numImages;
703+
}
704+
#ifdef SPOTIFY_DEBUG
705+
Serial.print(F("Num Images: "));
706+
Serial.println(current.numImages);
707+
Serial.println(numImages);
708+
#endif
709+
710+
for (int i = 0; i < current.numImages; i++)
711+
{
712+
int adjustedIndex = startingIndex + i;
713+
current.albumImages[i].height = images[adjustedIndex]["height"].as<int>();
714+
current.albumImages[i].width = images[adjustedIndex]["width"].as<int>();
715+
current.albumImages[i].url = images[adjustedIndex]["url"].as<const char *>();
716+
}
638717
}
639718

640719
currentlyPlayingCallback(current);

src/SpotifyArduino.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
6161
#define SPOTIFY_DEVICE_NAME_CHAR_LENGTH 80
6262
#define SPOTIFY_DEVICE_TYPE_CHAR_LENGTH 30
6363

64-
#define SPOTIFY_CURRENTLY_PLAYING_ENDPOINT "/v1/me/player/currently-playing"
64+
#define SPOTIFY_CURRENTLY_PLAYING_ENDPOINT "/v1/me/player/currently-playing?additional_types=episode"
6565

6666
#define SPOTIFY_PLAYER_ENDPOINT "/v1/me/player"
6767
#define SPOTIFY_DEVICES_ENDPOINT "/v1/me/player/devices"
@@ -93,6 +93,13 @@ enum RepeatOptions
9393
repeat_off
9494
};
9595

96+
enum SpotifyPlayingType
97+
{
98+
track,
99+
episode,
100+
other
101+
};
102+
96103
struct SpotifyImage
97104
{
98105
int height;
@@ -153,6 +160,7 @@ struct CurrentlyPlaying
153160
long progressMs;
154161
long durationMs;
155162
const char *contextUri;
163+
SpotifyPlayingType currentlyPlayingType;
156164
};
157165

158166
typedef void (*processCurrentlyPlaying)(CurrentlyPlaying currentlyPlaying);
@@ -195,7 +203,7 @@ class SpotifyArduino
195203
bool playerNavigate(char *command, const char *deviceId = "");
196204
bool seek(int position, const char *deviceId = "");
197205
bool transferPlayback(const char *deviceId, bool play = false);
198-
206+
199207
//Search
200208
int searchForSong(String query, int limit, processSearch searchCallback, SearchResult results[]);
201209

0 commit comments

Comments
 (0)