@@ -502,11 +502,11 @@ bool SpotifyArduino::transferPlayback(const char *deviceId, bool play)
502
502
503
503
int SpotifyArduino::getCurrentlyPlaying (processCurrentlyPlaying currentlyPlayingCallback, const char *market)
504
504
{
505
- char command[50 ] = SPOTIFY_CURRENTLY_PLAYING_ENDPOINT;
505
+ char command[75 ] = SPOTIFY_CURRENTLY_PLAYING_ENDPOINT;
506
506
if (market[0 ] != 0 )
507
507
{
508
508
char marketBuff[15 ];
509
- sprintf (marketBuff, " ? market=%s" , market);
509
+ sprintf (marketBuff, " & market=%s" , market);
510
510
strcat (command, marketBuff);
511
511
}
512
512
@@ -538,8 +538,9 @@ int SpotifyArduino::getCurrentlyPlaying(processCurrentlyPlaying currentlyPlaying
538
538
CurrentlyPlaying current;
539
539
540
540
// Apply Json Filter: https://arduinojson.org/v6/example/filter/
541
- StaticJsonDocument<320 > filter;
541
+ StaticJsonDocument<464 > filter;
542
542
filter[" is_playing" ] = true ;
543
+ filter[" currently_playing_type" ] = true ;
543
544
filter[" progress_ms" ] = true ;
544
545
filter[" context" ][" uri" ] = true ;
545
546
@@ -561,6 +562,16 @@ int SpotifyArduino::getCurrentlyPlaying(processCurrentlyPlaying currentlyPlaying
561
562
filter_item_album_images_0[" width" ] = true ;
562
563
filter_item_album_images_0[" url" ] = true ;
563
564
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
+
564
575
// Allocate DynamicJsonDocument
565
576
DynamicJsonDocument doc (bufferSize);
566
577
@@ -578,63 +589,131 @@ int SpotifyArduino::getCurrentlyPlaying(processCurrentlyPlaying currentlyPlaying
578
589
#endif
579
590
JsonObject item = doc[" item" ];
580
591
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 ())
583
601
{
584
- numArtists = SPOTIFY_MAX_NUM_ARTISTS ;
602
+ current. contextUri = doc[ " context " ][ " uri " ]. as < const char *>() ;
585
603
}
586
- current.numArtists = numArtists;
587
-
588
- for (int i = 0 ; i < current.numArtists ; i++)
604
+ else
589
605
{
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 ;
592
607
}
593
608
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 )
603
615
{
604
- startingIndex = numImages - SPOTIFY_NUM_ALBUM_IMAGES;
605
- current.numImages = SPOTIFY_NUM_ALBUM_IMAGES;
616
+ current.currentlyPlayingType = episode;
606
617
}
607
618
else
608
619
{
609
- current.numImages = numImages ;
620
+ current.currentlyPlayingType = other ;
610
621
}
611
- #ifdef SPOTIFY_DEBUG
612
- Serial.print (F (" Num Images: " ));
613
- Serial.println (current.numImages );
614
- Serial.println (numImages);
615
- #endif
616
622
617
- for (int i = 0 ; i < current.numImages ; i++)
623
+ // If it's a song/track
624
+ if (current.currentlyPlayingType == track)
618
625
{
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;
624
632
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
+ }
627
638
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 *>();
629
641
630
- current.progressMs = doc[" progress_ms" ].as <long >();
631
- current.durationMs = item[" duration_ms" ].as <long >();
642
+ JsonArray images = item[" album" ][" images" ];
632
643
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
+ }
638
717
}
639
718
640
719
currentlyPlayingCallback (current);
0 commit comments