Пример #1
0
        private CurrencyListViewItemFeedData TransformInfoToFeed(CurrencyInfo currI)
        {
            string symbol             = currI.Symbol;
            string name               = currI.Name;
            var    cc                 = currI.Quotes.Where(x => x.Currency == selectedCurrency);
            CurrencyLongTermValues cq = currI.Quotes.Where(x => x.Currency == selectedCurrency)
                                        .Select(y => y.LongeTermValues.Last()).FirstOrDefault();
            decimal marketCap    = cq.marketCap;
            decimal price        = cq.price;
            decimal volume       = cq.volume24;
            decimal percentage24 = currI.Quotes.Where(x => x.Currency == selectedCurrency)
                                   .Select(y => y.percentChange24).FirstOrDefault();
            long     tokens       = currI.TotalSupply;
            DateTime dataValidity = currI.LastTimeUpdated;

            return(new CurrencyListViewItemFeedData(symbol, name, marketCap, price, volume, percentage24, tokens, dataValidity, selectedCurrency, currI.marketPairs, currI.TotalSupply));
        }
        private static async Task <IList <CurrencyInfo> > GetTop10BasedOnCurrency(CurrencyEnum selectedCurrency)
        {
            //var URL = new UriBuilder("https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest");
            var URL = new UriBuilder("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest");

            var queryString = HttpUtility.ParseQueryString(string.Empty);

            queryString["start"]   = "1";
            queryString["limit"]   = "10";
            queryString["sort"]    = "market_cap";
            queryString["convert"] = returnString(selectedCurrency);

            URL.Query = queryString.ToString();

            var client = new WebClient();

            client.Headers.Add("X-CMC_PRO_API_KEY", API_KEY);
            client.Headers.Add("Accepts", "application/json");
            string  returned = client.DownloadString(URL.ToString());
            JObject obj      = JObject.Parse(returned);
            //cathpart where data starts
            IList <JToken>       jsonData             = obj["data"].Children().ToList();
            IList <CurrencyInfo> currencyInfoToReturn = new List <CurrencyInfo>();

            foreach (JToken tokensFromJsonData in jsonData)
            {
                CurrencyInfo CurrencyInfoToReturn = new CurrencyInfo();
                CurrencyInfoToReturn.Id          = (int)tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "id").FirstOrDefault();
                CurrencyInfoToReturn.CmcRank     = (int)tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "cmc_rank").FirstOrDefault();
                CurrencyInfoToReturn.Name        = (string)tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "name").FirstOrDefault();
                CurrencyInfoToReturn.Symbol      = (string)tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "symbol").FirstOrDefault();
                CurrencyInfoToReturn.marketPairs = (long)tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "num_market_pairs").FirstOrDefault();
                JProperty ts = tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "total_supply").FirstOrDefault();
                CurrencyInfoToReturn.TotalSupply = (ts.Value.ToString() != "") ? (long)ts : 0;
                JProperty ms = tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "max_supply").FirstOrDefault();
                CurrencyInfoToReturn.MaxSupply = (ms.Value.ToString() != "") ? (long)ms : 0;
                string recievedDateToParse = ((string)tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "date_added").FirstOrDefault());
                CurrencyInfoToReturn.DateAdded       = DateTime.ParseExact(recievedDateToParse, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                recievedDateToParse                  = ((string)tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "last_updated").FirstOrDefault());
                CurrencyInfoToReturn.LastTimeUpdated = DateTime.ParseExact(recievedDateToParse, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                IList <JToken> tokensFromJsonQuote = tokensFromJsonData.Children().Select(x => x as JProperty).Where(y => y.Name == "quote").FirstOrDefault();

                CurrencyQuote quoteToReturn = new CurrencyQuote();

                foreach (JToken quoteToken in tokensFromJsonQuote)
                {
                    quoteToReturn.Currency = returnEnum((string)quoteToken.Children().Select(x => x as JProperty).FirstOrDefault().Name);

                    JToken quoteTokenValues = quoteToken.Children().First();
                    quoteToReturn.percentChange24 = (decimal)quoteTokenValues.Values().Select(x => x as JProperty).Where(y => y.Name == "percent_change_24h").FirstOrDefault();

                    CurrencyLongTermValues cLTV = new CurrencyLongTermValues();

                    string date = (string)quoteTokenValues.Values().Select(x => x as JProperty).Where(y => y.Name == "last_updated").FirstOrDefault();
                    cLTV.actualAt  = DateTime.ParseExact(date, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                    cLTV.price     = (decimal)quoteTokenValues.Values().Select(x => x as JProperty).Where(y => y.Name == "price").FirstOrDefault();
                    cLTV.volume24  = (decimal)quoteTokenValues.Values().Select(x => x as JProperty).Where(y => y.Name == "volume_24h").FirstOrDefault();
                    cLTV.marketCap = (decimal)quoteTokenValues.Values().Select(x => x as JProperty).Where(y => y.Name == "market_cap").FirstOrDefault();

                    quoteToReturn.LongeTermValues.Add(cLTV);
                    CurrencyInfoToReturn.Quotes.Add(quoteToReturn);
                }

                currencyInfoToReturn.Add(CurrencyInfoToReturn);
            }


            return(currencyInfoToReturn);
        }