Ejemplo n.º 1
0
        public RetornoAlbunsSpotify.RootObject ObterListaAlbuns(TokenSpotify token, string genero)
        {
            RetornoAlbunsSpotify.RootObject results = null;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://api.spotify.com/v1/");
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.access_token);
                string url = $"browse/categories/{genero}/playlists?limit=50";

                HttpResponseMessage response = null;
                try
                {
                    response = client.GetAsync(url).Result;
                }
                catch (Exception ex)
                {
                    if (response == null)
                    {
                        response = new HttpResponseMessage();
                    }
                    response.StatusCode   = HttpStatusCode.InternalServerError;
                    response.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
                }

                if (response.IsSuccessStatusCode)
                {
                    string responseBody = response.Content.ReadAsStringAsync().Result;

                    results = JsonConvert.DeserializeObject <RetornoAlbunsSpotify.RootObject>(responseBody);
                }
            }

            return(results);
        }
Ejemplo n.º 2
0
        private void AdicionarAlbunsSpotify(ApplicationDbContext context)
        {
            SpotifyService spotify = new SpotifyService();
            TokenSpotify   token   = spotify.ObterTokenSpotify();

            if (token != null && !string.IsNullOrWhiteSpace(token.access_token))
            {
                string[] generos = new string[] { "pop", "rock", "classical", "jazz" };

                foreach (string genero in generos)
                {
                    RetornoAlbunsSpotify.RootObject albunsSpotify = spotify.ObterListaAlbuns(token, genero);

                    if (albunsSpotify != null && albunsSpotify.playlists != null)
                    {
                        string generoBanco = string.Empty;
                        switch (genero)
                        {
                        case "pop":
                            generoBanco = "POP";
                            break;

                        case "classical":
                            generoBanco = "CLASSIC";
                            break;

                        case "rock":
                            generoBanco = "ROCK";
                            break;

                        default:
                            generoBanco = "MPB";
                            break;
                        }

                        if (albunsSpotify.playlists.items.Any())
                        {
                            foreach (var item in albunsSpotify.playlists.items)
                            {
                                Disco disco = new Disco()
                                {
                                    Genero = generoBanco,
                                    Nome   = item.name
                                };

                                Random  randNum    = new Random();
                                decimal valorVenda = randNum.Next(10, 100);
                                double  centavos   = randNum.NextDouble();
                                disco.PrecoVenda = valorVenda + decimal.Round((decimal)centavos, 2);

                                context.CatalogoDiscos.Add(disco);
                            }

                            context.SaveChanges();
                        }
                    }
                }
            }
        }