Пример #1
0
        public ActionResult UpdateDfr()
        {
            string             url;
            HtmlNodeCollection colNode;
            string             resp;
            bool    parseResult;
            decimal rate;
            var     currencyPair = _context.CurrencyPairView;

            DateTime today = Utility.GetLocalDateTime().Date;

            var dfrList = _context.Dfr.Where(m => m.TradeDate == today);

            _context.RemoveRange(dfrList);

            foreach (var cp in currencyPair)
            {
                if (cp.CurrencyName1 == "ETH" || cp.CurrencyName1 == "UST" || cp.CurrencyName2 == "ETH" || cp.CurrencyName2 == "UST")
                {
                    string currFrom = cp.CurrencyName1 == "UST" ? "USDT" : cp.CurrencyName1;
                    string currTo   = cp.CurrencyName2 == "UST" ? "USDT" : cp.CurrencyName2;

                    url  = "https://min-api.cryptocompare.com/data/price?fsym=" + currFrom + "&tsyms=" + currTo;
                    resp = getWebResponse(url);
                    var result = Regex.Match(resp, @"\d+(\.\d+)?").Value;

                    parseResult = decimal.TryParse(result, out rate);

                    if (parseResult)
                    {
                        Dfr dfr = new Dfr();
                        dfr.TradeDate        = Utility.GetLocalDateTime().Date;
                        dfr.CurrencyPairID   = cp.id;
                        dfr.Rate             = rate;
                        dfr.DateTimeAdded    = Utility.GetLocalDateTime();
                        dfr.DateTimeModified = Utility.GetLocalDateTime();
                        _context.Add(dfr);
                    }
                }
                else
                {
                    url = "https://xecdapi.xe.com/v1/convert_from.xml/?from=" + cp.CurrencyName1 + "&to=" + cp.CurrencyName2;

                    colNode = getNodes(url, "//to/rate/mid");
                    if (colNode != null)
                    {
                        string s = colNode[0].InnerText;
                        parseResult = decimal.TryParse(s, NumberStyles.Float, CultureInfo.CreateSpecificCulture("en-US"), out rate);

                        if (parseResult)
                        {
                            Dfr dfr = new Dfr();
                            dfr.TradeDate        = Utility.GetLocalDateTime().Date;
                            dfr.CurrencyPairID   = cp.id;
                            dfr.Rate             = rate;
                            dfr.DateTimeAdded    = Utility.GetLocalDateTime();
                            dfr.DateTimeModified = Utility.GetLocalDateTime();
                            _context.Add(dfr);
                        }
                    }
                }
            }
            _context.SaveChanges();
            //var txn = _context.TxnCompleteView.Where(x => x.id == txnID).FirstOrDefault();
            return(Json(null));
        }