Esempio n. 1
0
        public JsonResult NowPlaying()
        {
            Dictionary <string, string> nowPlaying = new Dictionary <string, string>();

            if (User.Identity.IsAuthenticated)
            {
                string accessToken = HttpContext.GetTokenAsync("access_token").Result;

                try
                {
                    // For usage visit: https://github.com/JohnnyCrazy/SpotifyAPI-NET/
                    SpotifyWebAPI spotifyAPI = new SpotifyWebAPI
                    {
                        AccessToken = accessToken,
                        TokenType   = "Bearer"
                    };

                    PrivateProfile  profile = spotifyAPI.GetPrivateProfileAsync().Result;
                    PlaybackContext context = spotifyAPI.GetPlayback();
                    if (!profile.HasError())
                    {
                        nowPlaying.Add("SpotifyName", profile.DisplayName);
                        nowPlaying.Add("SpotifyAvatar", profile.Images.FirstOrDefault().Url);
                        nowPlaying.Add("SpotifyProfileUrl", "https://open.spotify.com/user/" + profile.Id);
                    }

                    if (context.Item != null && (context.IsPlaying))
                    {
                        nowPlaying.Add("SongName", context.Item.Name);
                        nowPlaying.Add("SongUrl", context.Item.Href);
                        nowPlaying.Add("ArtistName", context.Item.Artists.FirstOrDefault().Name);
                        nowPlaying.Add("ArtistUrl", context.Item.Artists.FirstOrDefault().Href);
                        nowPlaying.Add("NowPlaying", nowPlaying["SongName"] + " by " + nowPlaying["ArtistName"]);
                        nowPlaying.Add("AlbumArt", context.Item.Album.Images.FirstOrDefault().Url);
                        nowPlaying.Add("AlbumUrl", context.Item.Album.ExternalUrls["spotify"]);
                        nowPlaying.Add("IsPlaying", "true");
                    }
                    else
                    {
                        nowPlaying.Add("SongName", null);
                        nowPlaying.Add("SongUrl", null);
                        nowPlaying.Add("ArtistName", null);
                        nowPlaying.Add("ArtistUrl", null);
                        nowPlaying.Add("NowPlaying", null);
                        nowPlaying.Add("AlbumArt", null);
                        nowPlaying.Add("AlbumUrl", null);
                        nowPlaying.Add("IsPlaying", "false");
                    }
                    GeniusService geniusService = new GeniusService(nowPlaying["SongName"], nowPlaying["ArtistName"]);
                    nowPlaying.Add("Lyrics", geniusService.Lyrics);
                    nowPlaying.Add("LyricsContribution", geniusService.Url);
                }
                catch (Exception ex)
                {
                    // show the users some error
                }
            }
            return(new JsonResult(nowPlaying));
        }
Esempio n. 2
0
 public GeniusCommands(
     IPrefixService prefixService,
     LastFMService lastFmService,
     UserService userService,
     GeniusService geniusService)
 {
     this._prefixService = prefixService;
     this._userService   = userService;
     this._geniusService = geniusService;
     this._lastFmService = lastFmService;
     this._embed         = new EmbedBuilder()
                           .WithColor(DiscordConstants.LastFMColorRed);
     this._embedFooter = new EmbedFooterBuilder();
 }