Пример #1
0
        private KomBankRatesLine Parse(string json)
        {
            try
            {
                var result = new KomBankRatesLine()
                {
                    Bank = KomBankE.Bps.ToString().ToUpper()
                };
                var bpsRootObject = (BpsRootObject)JsonConvert.DeserializeObject(json, typeof(BpsRootObject));
                if (bpsRootObject == null)
                {
                    return(null);
                }
                DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                result.StartedFrom = start.AddMilliseconds(bpsRootObject.date).ToLocalTime();

                var usdRate = bpsRootObject.rates.list.First(r => r.iso == "USD");
                result.UsdA = usdRate.buy;
                result.UsdB = usdRate.sale;
                var eurRate = bpsRootObject.rates.list.First(r => r.iso == "EUR");
                result.EurA = eurRate.buy;
                result.EurB = eurRate.sale;
                var rubRate = bpsRootObject.rates.list.First(r => r.iso == "RUB");
                result.RubA = rubRate.buy;
                result.RubB = rubRate.sale;

                result.LastCheck = DateTime.Now;
                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine($@"{e.Message} in Bps parser");
                return(null);
            }
        }
Пример #2
0
        private KomBankRatesLine Parse(string json)
        {
            var alfaRoot = JsonConvert.DeserializeObject <AlfaRoot>(json);

            if (alfaRoot == null)
            {
                return(null);
            }

            var result = new KomBankRatesLine()
            {
                Bank        = BankTitle,
                LastCheck   = DateTime.Now,
                StartedFrom = alfaRoot.initialItems[0].currenciesData[0].date,
                UsdA        = alfaRoot.initialItems[0].currenciesData[0].value.exchangeRate[0].purchase.value,
                UsdB        = alfaRoot.initialItems[0].currenciesData[0].value.exchangeRate[0].sell.value,

                EurA = alfaRoot.initialItems[0].currenciesData[0].value.exchangeRate[1].purchase.value,
                EurB = alfaRoot.initialItems[0].currenciesData[0].value.exchangeRate[1].sell.value,

                RubA = alfaRoot.initialItems[0].currenciesData[0].value.exchangeRate[2].purchase.value,
                RubB = alfaRoot.initialItems[0].currenciesData[0].value.exchangeRate[2].sell.value,

                EurUsdA = alfaRoot.initialItems[0].currenciesData[0].value.conversionRate[0].purchase.value,
                EurUsdB = alfaRoot.initialItems[0].currenciesData[0].value.conversionRate[0].sell.value,

                RubEurA = alfaRoot.initialItems[0].currenciesData[0].value.conversionRate[1].purchase.value,
                RubEurB = alfaRoot.initialItems[0].currenciesData[0].value.conversionRate[1].sell.value,

                RubUsdA = alfaRoot.initialItems[0].currenciesData[0].value.conversionRate[2].purchase.value,
                RubUsdB = alfaRoot.initialItems[0].currenciesData[0].value.conversionRate[2].sell.value,
            };

            return(result);
        }
Пример #3
0
        private KomBankRatesLine Parse(string json)
        {
            var bnbRoot = JsonConvert.DeserializeObject <Dictionary <int, Object> >(json);

            if (bnbRoot == null)
            {
                return(null);
            }
            var imBankingRates  = JsonConvert.DeserializeObject <Dictionary <int, ImBankingCurrency> >(bnbRoot[8].ToString());
            var convertionRates = JsonConvert.DeserializeObject <Dictionary <int, BnbCurrency> >(bnbRoot[7].ToString());

            var result = new KomBankRatesLine
            {
                Bank      = BankTitle,
                LastCheck = DateTime.Now,
                UsdA      = imBankingRates[32].UF_BUY,
                UsdB      = imBankingRates[32].UF_SALE,
                EurA      = imBankingRates[33].UF_BUY,
                EurB      = imBankingRates[33].UF_SALE,
                RubA      = imBankingRates[34].UF_BUY,
                RubB      = imBankingRates[34].UF_SALE,

                EurUsdA = convertionRates[310].UF_BUY,
                EurUsdB = convertionRates[310].UF_SALE,
                RubUsdA = convertionRates[308].UF_BUY,
                RubUsdB = convertionRates[308].UF_SALE,
                RubEurA = convertionRates[309].UF_BUY,
                RubEurB = convertionRates[309].UF_SALE,
            };

            return(result);
        }
Пример #4
0
 public bool IsDifferent(KomBankRatesLine anotherLine)
 {
     return(anotherLine == null ||
            !UsdA.Equals(anotherLine.UsdA) || !UsdB.Equals(anotherLine.UsdB) ||
            !EurA.Equals(anotherLine.EurA) || !EurB.Equals(anotherLine.EurB) ||
            !RubA.Equals(anotherLine.RubA) || !RubB.Equals(anotherLine.RubB) ||
            !EurUsdA.Equals(anotherLine.EurUsdA) || !EurUsdB.Equals(anotherLine.EurUsdB) ||
            !RubUsdA.Equals(anotherLine.RubUsdA) || !RubUsdB.Equals(anotherLine.RubUsdB) ||
            !RubEurA.Equals(anotherLine.RubEurA) || !RubEurB.Equals(anotherLine.RubEurB));
 }
Пример #5
0
        private KomBankRatesLine Parse(string table)
        {
            var rates = new KomBankRatesLine()
            {
                Bank      = BankTitle,
                LastCheck = DateTime.Now,
            };

            var index = table.IndexOf("rates-table__tbody", StringComparison.Ordinal);

            if (GetOneCurrency(table, index, "usd", out var usdBuy, out var usdSell))
            {
                rates.UsdA = usdBuy;
                rates.UsdB = usdSell;
            }

            if (GetOneCurrency(table, index, "USD/RUB", out var usdRubBuy, out var usdRubSell))
            {
                rates.RubUsdA = usdRubBuy;
                rates.RubUsdB = usdRubSell;
            }

            if (GetOneCurrency(table, index, "eur", out var eurBuy, out var eurSell))
            {
                rates.EurA = eurBuy;
                rates.EurB = eurSell;
            }


            if (GetOneCurrency(table, index, "EUR/USD", out var eurUsdBuy, out var eurUsdSell))
            {
                rates.EurUsdA = eurUsdBuy;
                rates.EurUsdB = eurUsdSell;
            }

            if (GetOneCurrency(table, index, "rub", out var rubBuy, out var rubSell))
            {
                rates.RubA = rubBuy;
                rates.RubB = rubSell;
            }

            if (GetOneCurrency(table, index, "EUR/RUB", out var rubEurBuy, out var rubEurSell))
            {
                rates.RubEurA = rubEurBuy;
                rates.RubEurB = rubEurSell;
            }

            if (GetTimeFrom(table, index, out var timeFrom))
            {
                rates.StartedFrom = timeFrom;
            }

            return(rates);
        }
Пример #6
0
        public async Task <KomBankRatesLine> GetRatesLineAsync()
        {
            var page = await((HttpWebRequest)WebRequest.Create(Url))
                       .InitializeForKombanks()
                       .GetDataAsync();

            if (string.IsNullOrEmpty(page))
            {
                return(null);
            }

            var rates = (BelgazOneRate[])JsonConvert.DeserializeObject(page, typeof(BelgazOneRate[]));

            if (rates == null)
            {
                return(null);
            }

            var result = new KomBankRatesLine()
            {
                Bank = KomBankE.Bgpb.ToString().ToUpper(),
            };
            DateTime date = _startDate.AddMilliseconds(rates[0].D).ToLocalTime();

            result.StartedFrom = date;
            result.LastCheck   = DateTime.Now;

            result.EurA = rates.Where(r => r.T == "PNN" && r.C == "EUR").Max(l => l.V);
            result.EurB = rates.Where(r => r.T == "SNN" && r.C == "EUR").Min(l => l.V);

            result.UsdA = rates.Where(r => r.T == "PNN" && r.C == "USD").Max(l => l.V);
            result.UsdB = rates.Where(r => r.T == "SNN" && r.C == "USD").Min(l => l.V);

            result.RubA = rates.Where(r => r.T == "PNN" && r.C == "RUB").Max(l => l.V);
            result.RubB = rates.Where(r => r.T == "SNN" && r.C == "RUB").Min(l => l.V);

            result.EurUsdA = rates.First(r => r.T == "ENN" && r.C == "EUR" && r.Bc == "USD").V;
            result.EurUsdB = rates.First(r => r.T == "ENN" && r.C == "USD" && r.Bc == "EUR").V;

            result.RubUsdB = rates.First(r => r.T == "ENN" && r.C == "RUB" && r.Bc == "USD").V;
            result.RubUsdA = rates.First(r => r.T == "ENN" && r.C == "USD" && r.Bc == "RUB").V;

            result.RubEurB = rates.First(r => r.T == "ENN" && r.C == "RUB" && r.Bc == "EUR").V;
            result.RubEurA = rates.First(r => r.T == "ENN" && r.C == "EUR" && r.Bc == "RUB").V;

            return(result);
        }
Пример #7
0
        private static KomBankRatesLine Parse(string page)
        {
            var jsonStr = page.Replace("&quot;", "\"");

            var pos = jsonStr.IndexOf("<v-conversion :data-source=\"");

            pos = pos + 28;
            var posEnd = jsonStr.IndexOf("</v-conversion>") - 1;

            var section = jsonStr.Substring(pos, posEnd - pos - 1);

            var ratesPos = section.IndexOf("\"ibank\":{");

            ratesPos += 8;
            var   ratesPosEnd   = section.IndexOf(",\"offices\":{");
            var   jsonRates     = section.Substring(ratesPos, ratesPosEnd - ratesPos);
            Root1 exchangeRates = JsonConvert.DeserializeObject <Root1>(jsonRates);

            var   convertionPos   = section.IndexOf("\"rates\":{");
            var   jsonConvertions = "{" + section.Substring(convertionPos, section.Length - convertionPos - 1);
            Root2 convertionRates = JsonConvert.DeserializeObject <Root2>(jsonConvertions);

            var rates = new KomBankRatesLine()
            {
                Bank        = KomBankE.Bveb.ToString().ToUpper(),
                StartedFrom = GetStartedFrom(exchangeRates.timetext),
                LastCheck   = DateTime.Now
            };

            rates.UsdA = exchangeRates.rates[0].buy_rate.value;
            rates.UsdB = exchangeRates.rates[0].sale_rate.value;

            rates.EurA = exchangeRates.rates[1].buy_rate.value;
            rates.EurB = exchangeRates.rates[1].sale_rate.value;

            rates.RubA    = convertionRates.rates.RUB.purchase.BYN * 100;
            rates.RubB    = convertionRates.rates.RUB.sell.BYN * 100;
            rates.EurUsdA = convertionRates.rates.EUR.purchase.USD;
            rates.EurUsdB = convertionRates.rates.EUR.sell.USD;
            rates.RubUsdA = convertionRates.rates.USD.purchase.RUB;
            rates.RubUsdB = convertionRates.rates.USD.sell.RUB;
            rates.RubEurA = convertionRates.rates.EUR.purchase.RUB;
            rates.RubEurB = convertionRates.rates.EUR.sell.RUB;

            return(rates);
        }
Пример #8
0
        private KomBankRatesLine ParseJson(string page)
        {
            AlfaApiRoot list   = JsonConvert.DeserializeObject <AlfaApiRoot>(page);
            var         result = new KomBankRatesLine();

            result.Bank = BankTitle;
//            result.StartedFrom = DateTime.Now; // v 1.0.1
            result.StartedFrom = list.rates.First().date;

            var usdRate = list.rates.First(r => r.buyCode == 933 && r.sellCode == 840);

            result.UsdA = usdRate.sellRate;
            result.UsdB = usdRate.buyRate;

            var eurRate = list.rates.First(r => r.buyCode == 933 && r.sellCode == 978);

            result.EurA = eurRate.sellRate;
            result.EurB = eurRate.buyRate;

            var rubRate = list.rates.First(r => r.buyCode == 933 && r.sellCode == 643);

            result.RubA = rubRate.sellRate;
            result.RubB = rubRate.buyRate;

            var eurUsdRate = list.rates.First(r => r.buyCode == 840 && r.sellCode == 978);

            result.EurUsdA = eurUsdRate.sellRate;
            result.EurUsdB = eurUsdRate.buyRate;

            var rubUsdRate = list.rates.First(r => r.buyCode == 643 && r.sellCode == 840);

            result.RubUsdA = rubUsdRate.sellRate;
            result.RubUsdB = rubUsdRate.buyRate;

            var rubEurRate = list.rates.First(r => r.buyCode == 643 && r.sellCode == 978);

            result.RubEurA = rubEurRate.sellRate;
            result.RubEurB = rubEurRate.buyRate;

            result.LastCheck = DateTime.Now;
            return(result);
        }
Пример #9
0
        private KomBankRatesLine Parse(string page)
        {
            var priorList = (RootObject)JsonConvert.DeserializeObject(page, typeof(RootObject));

            if (priorList == null || priorList.Message != "success")
            {
                return(null);
            }

            var result = new KomBankRatesLine {
                Bank = KomBankE.Prior.ToString().ToUpper()
            };

            var digitalBank = priorList.CalculatorTable.Data.Where(d => d.Channel == 3).ToList(); // 1 - нал, 2 - карточки

            var onlineRate = digitalBank.First(r => r.BaseCurrency == (int)Currency.Usd && r.RatedCurrency == (int)Currency.Byn);

            result.UsdA = onlineRate.Rate.BuyRate;
            result.UsdB = onlineRate.Rate.SellRate;

            result.StartedFrom = onlineRate.ValidFrom;
            result.LastCheck   = DateTime.Now;

            onlineRate  = digitalBank.First(r => r.BaseCurrency == (int)Currency.Eur && r.RatedCurrency == (int)Currency.Byn);
            result.EurA = onlineRate.Rate.BuyRate;
            result.EurB = onlineRate.Rate.SellRate;
            onlineRate  = digitalBank.First(r => r.BaseCurrency == (int)Currency.Rub && r.RatedCurrency == (int)Currency.Byn);
            result.RubA = onlineRate.Rate.BuyRate;
            result.RubB = onlineRate.Rate.SellRate;

            onlineRate     = digitalBank.First(r => r.BaseCurrency == (int)Currency.Usd && r.RatedCurrency == (int)Currency.Rub);
            result.RubUsdA = onlineRate.Rate.BuyRate;
            result.RubUsdB = onlineRate.Rate.SellRate;
            onlineRate     = digitalBank.First(r => r.BaseCurrency == (int)Currency.Eur && r.RatedCurrency == (int)Currency.Usd);
            result.EurUsdA = onlineRate.Rate.BuyRate;
            result.EurUsdB = onlineRate.Rate.SellRate;
            onlineRate     = digitalBank.First(r => r.BaseCurrency == (int)Currency.Eur && r.RatedCurrency == (int)Currency.Rub);
            result.RubEurA = onlineRate.Rate.BuyRate;
            result.RubEurB = onlineRate.Rate.SellRate;

            return(result);
        }
Пример #10
0
        private static KomBankRatesLine ParseTable(string table)
        {
            var rates = new KomBankRatesLine()
            {
                Bank        = KomBankE.Bib.ToString().ToUpper(),
                StartedFrom = new DateTime(2019, 1, 16, 12, 51, 0),
                LastCheck   = DateTime.Now
            };

            double usdBuy;
            double usdSell;

            if (!GetOneCurrency(table, "USD", out usdBuy, out usdSell))
            {
                return(null);
            }
            rates.UsdA = usdBuy;
            rates.UsdB = usdSell;

            double euroBuy;
            double euroSell;

            if (!GetOneCurrency(table, "EUR", out euroBuy, out euroSell))
            {
                return(null);
            }
            rates.EurA = euroBuy;
            rates.EurB = euroSell;

            double rubBuy;
            double rubSell;

            if (!GetOneCurrency(table, "RUB", out rubBuy, out rubSell))
            {
                return(null);
            }
            rates.RubA = rubBuy;
            rates.RubB = rubSell;

            return(rates);
        }
Пример #11
0
        private async Task <int> Persist(KomBankRatesLine rate)
        {
            using (BanksListenerContext db = new BanksListenerContext(_dbPath))
            {
                var tid  = Thread.CurrentThread.ManagedThreadId;
                var last = await db.KomBankRates.Where(l => l.Bank == rate.Bank).OrderBy(c => c.LastCheck).LastOrDefaultAsync();

                if (last == null || last.IsDifferent(rate))
                {
                    if (rate.Bank == "BIB" || rate.Bank == "BNB")
                    {
                        rate.StartedFrom = DateTime.Now; // Bib page does not contain date from
                    }
                    if (rate.Bank == "BVEB" && rate.StartedFrom.Hour == 0 && DateTime.Now.Hour != 0)
                    {
                        rate.StartedFrom = DateTime.Now; // Bveb often returns 00:10 or 00:15 as start time
                    }
                    _logFile.AppendLine($"Thread id {tid}: {rate.Bank} new rate, usd {rate.UsdA} - {rate.UsdB},  euro {rate.EurA} - {rate.EurB},  rub {rate.RubA} - {rate.RubB}");

                    const double TOLERANCE = 0.00001;
                    if (rate.Bank == "BNB" && Math.Abs(rate.UsdA - 2.619) < TOLERANCE &&
                        Math.Abs(rate.UsdB - 2.625) < TOLERANCE &&
                        Math.Abs(rate.EurA - 3.168) < TOLERANCE &&
                        Math.Abs(rate.EurB - 3.175) < TOLERANCE)
                    {
                        return(0);
                    }

                    await db.KomBankRates.AddAsync(rate);
                }
                else
                {
                    last.LastCheck = DateTime.Now;
                    _logFile.AppendLine($"Thread id {tid}: Poller of {rate.Bank} - rate checked");
                }

                return(await db.SaveChangesAsync());
            }
        }
Пример #12
0
        private KomBankRatesLine ParseThreeTables(string page)
        {
            var result = new KomBankRatesLine {
                Bank = BankTitle
            };

            if (!FetchTable(page, Nal, out string table))
            {
                return(null);
            }
            if (!GetRates(table, "Российские рубли", out double kursA, out double kursB))
            {
                return(null);
            }
            result.RubA = kursA;
            result.RubB = kursB;

            if (!FetchTable(page, Card, out table))
            {
                return(null);
            }
            if (!GetRates(table, "Доллар США", out kursA, out kursB))
            {
                return(null);
            }
            result.UsdA = kursA;
            result.UsdB = kursB;
            if (!GetRates(table, "Eвро", out kursA, out kursB))
            {
                return(null);
            }
            result.EurA = kursA;
            result.EurB = kursB;


            if (!FetchTable(page, Conversion, out table))
            {
                return(null);
            }
            if (!GetOneRate(table, "Доллар США/Eвро", out kursA))
            {
                return(null);
            }
            result.EurUsdB = kursA;
            if (!GetOneRate(table, "Eвро/Доллар США", out kursB))
            {
                return(null);
            }
            result.EurUsdA = kursB;

            if (!GetOneRate(table, "Доллар США/Российские рубли", out kursA))
            {
                return(null);
            }
            result.RubUsdA = kursA;
            if (!GetOneRate(table, "Российские рубли/Доллар США", out kursB))
            {
                return(null);
            }
            result.RubUsdB = kursB;


            if (!GetOneRate(table, "Eвро/Российские рубли", out kursA))
            {
                return(null);
            }
            result.RubEurA = kursA;
            if (!GetOneRate(table, "Российские рубли/Eвро", out kursB))
            {
                return(null);
            }
            result.RubEurB = kursB;

            result.StartedFrom = DateTime.Now;
            result.LastCheck   = DateTime.Now;
            return(result);
        }