Exemplo n.º 1
0
        public List <MultipleAlbums.Album> CallSpotifyAPIMultipleAlbumDetails2(StopWatchResult stopWatchResult, IEnumerable <ArtistAlbums.Item> albums = null)
        {
            // can only send 20 at a time, so have to chunck if more
            //http://stackoverflow.com/a/17974/26086
            var recordsPerPage      = 20;
            var records             = albums.Count();
            int numberOfTimesToLoop = (records + recordsPerPage - 1) / recordsPerPage;

            int chunckUpTo       = 0;
            var masterListAlbums = new List <MultipleAlbums.Album>();

            for (int i = 0; i < numberOfTimesToLoop; i++)
            {
                var csvStringOfAlbumIDs = "";
                foreach (var album in albums.Skip(chunckUpTo).Take(20))
                {
                    csvStringOfAlbumIDs += album.id + ",";
                }
                csvStringOfAlbumIDs = csvStringOfAlbumIDs.TrimEnd(',');
                var            url  = String.Format("https://api.spotify.com/v1/albums/?ids={0}", csvStringOfAlbumIDs);
                var            json = CallAPI(stopWatchResult, url);
                MultipleAlbums multiAlbumDetails3 = JsonConvert.DeserializeObject <MultipleAlbums>(json);
                // map to list
                foreach (var albumx in multiAlbumDetails3.albums)
                {
                    masterListAlbums.Add(albumx);
                }
                chunckUpTo += 20;
            }
            return(masterListAlbums);
        }
Exemplo n.º 2
0
        //https://api.spotify.com/v1/artists/{id}
        public string CallSpotifyAPIArtist(string artistCode, StopWatchResult stopWatchResult)
        {
            var url = String.Format("https://api.spotify.com/v1/artists/{0}", artistCode);

            using (mp.CustomTiming("http", url)) {
                var json = CallAPI(stopWatchResult, url);
                return(json);
            }
        }
Exemplo n.º 3
0
        public APIResult CallSpotifyAPIAlbumDetails(StopWatchResult stopWatchResult, string id)
        {
            var url  = String.Format("https://api.spotify.com/v1/albums/{0}", id);
            var json = CallAPI(stopWatchResult, url);

            return(new APIResult {
                Json = json,
                Url = url
            });
        }
Exemplo n.º 4
0
        public APIResult CallSpotifyAPIArtistRelated(StopWatchResult stopWatchResult, string id)
        {
            var url = String.Format("https://api.spotify.com/v1/artists/{0}/related-artists", id);

            using (mp.CustomTiming("http", url)) {
                var json = CallAPI(stopWatchResult, url);
                return(new APIResult {
                    Json = json,
                    Url = url
                });
            }
        }
Exemplo n.º 5
0
        public APIResult CallSpotifyAPIMultipleAlbumDetails(StopWatchResult stopWatchResult, string csvListOfAlbums)
        {
            var url = String.Format("https://api.spotify.com/v1/albums/?ids={0}", csvListOfAlbums);

            using (mp.CustomTiming("http", url)) {
                var json = CallAPI(stopWatchResult, url);
                return(new APIResult {
                    Json = json,
                    Url = url
                });
            }
        }
Exemplo n.º 6
0
        public APIResult CallSpotifyAPIArtistAlbums(StopWatchResult stopWatchResult, string artistCode)
        {
            var url = String.Format("https://api.spotify.com/v1/artists/{0}/albums?country=GB&album_type=album&limit=50", artistCode);

            using (mp.CustomTiming("http", url)) {
                var json = CallAPI(stopWatchResult, url);
                return(new APIResult {
                    Json = json,
                    Url = url
                });
            }
        }
Exemplo n.º 7
0
        public APIResult CallSpotifyAPIArtistTopTracks(StopWatchResult stopWatchResult, string artistCode)
        {
            var url = String.Format("https://api.spotify.com/v1/artists/{0}/top-tracks?country=GB", artistCode);

            using (mp.CustomTiming("http", url)) {
                var json = CallAPI(stopWatchResult, url);
                return(new APIResult {
                    Json = json,
                    Url = url
                });
            }
        }
Exemplo n.º 8
0
        //public APIResult CallEchonestAPIArtistBiography(StopWatchResult stopWatchResult, string id) {
        //    var echonestAPIKey = "OMO6U4I5XEGVXYCCN ";
        //    //http://developer.echonest.com/api/v4/artist/biographies?api_key=FILDTEOIK2HBORODV&id=spotify:artist:4Z8W4fKeB5YxbusRsdQVPb
        //    var url = String.Format("http://developer.echonest.com/api/v4/artist/biographies?api_key={0}&id=spotify:artist:{1}", echonestAPIKey, id);
        //    using (mp.CustomTiming("http", url)) {
        //        var json = CallAPI(stopWatchResult, url);
        //        return new APIResult {
        //            Json = json,
        //            Url = url.Replace(echonestAPIKey, "SECRET")
        //        };
        //    }
        //}

        public static string CallAPI(StopWatchResult stopWatchResult = null, string url = "")
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();

            int    errorCount = 0;
            string text       = null;
            bool   done       = false;

            while (!done)
            {
                try {
                    var request = (HttpWebRequest)WebRequest.Create(url);
                    request.Accept = "application/json";

                    var response = (HttpWebResponse)request.GetResponse();

                    using (var sr = new StreamReader(response.GetResponseStream())) {
                        text = sr.ReadToEnd();
                    }

                    done = true;
                }
                catch (WebException ex) {
                    Thread.Sleep(200);
                    errorCount++;
                    if (errorCount == 200)
                    {
                        throw;
                    }
                }
            }

            stopWatch.Stop();
            TimeSpan ts          = stopWatch.Elapsed;
            string   elapsedTime = String.Format("{0:0}", ts.TotalMilliseconds);

            if (stopWatchResult != null)
            {
                stopWatchResult.ElapsedTime = ts;
                stopWatchResult.TimeInMs    = elapsedTime;
            }

            if (String.IsNullOrEmpty(text))
            {
                throw new InvalidOperationException();
            }
            return(text);
        }
Exemplo n.º 9
0
        private AlbumDetailsViewModel GetAlbumDetailsViewModel(string id)
        {
            var spotifyHelper   = new SpotifyHelper();
            var stopWatchResult = new StopWatchResult();
            var apiResult       = spotifyHelper.CallSpotifyAPIAlbumDetails(stopWatchResult, id);

            ViewBag.Id = id;
            var albumDetails = JsonConvert.DeserializeObject <AlbumDetails>(apiResult.Json);

            // iterate through setting vm checked property
            using (var connection = new SqlConnection(connectionString))
                using (var command = new SqlCommand(null, connection)) {
                    connection.Open();
                    command.CommandText = String.Format("SELECT TrackID FROM Tracks");
                    command.CommandType = CommandType.Text;

                    var trackIDsSelectedInDb = new List <string>();
                    using (var reader = command.ExecuteReader()) {
                        while (reader.Read())
                        {
                            var trackID = reader.GetString(reader.GetOrdinal("TrackID"));
                            trackIDsSelectedInDb.Add(trackID);
                        }
                    }

                    foreach (var trackID in trackIDsSelectedInDb)
                    {
                        var track = albumDetails.tracks.items.FirstOrDefault(x => x.id == trackID);
                        if (track != null)
                        {
                            track.Checked = true;
                        }
                    }
                }

            var vm = new AlbumDetailsViewModel {
                APIDebugList = null,
                AlbumDetails = albumDetails,
            };

            return(vm);
        }