private Task Check_Currencies_CMC() { return(Task.Run(async() => { if (string.IsNullOrEmpty(CMCAPIKEY)) { return; } var failCounter = 0; while (failCounter < 10) { _ = AwaitOnline(Services.CMC); var ctr = 0; try { Currencies_CMC_Fiat.Clear(); Currencies_CMC_Crypto.Clear(); using (WebClient client = new()) { client.Headers.Add("X-CMC_PRO_API_KEY", CMCAPIKEY); client.Headers.Add("Accepts", "application/json"); { var json = client.DownloadString("https://pro-api.coinmarketcap.com/v1/fiat/map"); var currencies = JsonConvert.DeserializeObject <JSON_CMC_Currencies>(json); foreach (var fiat in currencies.data) { if (!Currencies_CMC_Fiat.Contains(fiat.symbol)) { ctr++; Currencies_CMC_Fiat.Add(fiat.symbol); } } } Currencies_CMC_Fiat = Currencies_CMC_Fiat.OrderBy(x => x).ToList(); } using (WebClient client = new()) { client.Headers.Add("X-CMC_PRO_API_KEY", CMCAPIKEY); client.Headers.Add("Accepts", "application/json"); { var json = client.DownloadString("https://pro-api.coinmarketcap.com/v1/cryptocurrency/map"); var currencies = JsonConvert.DeserializeObject <JSON_CMC_Currencies>(json); foreach (var crypto in currencies.data) { if (!Currencies_CMC_Crypto.Contains(crypto.symbol)) { ctr++; Currencies_CMC_Crypto.Add(crypto.symbol); } } } Currencies_CMC_Crypto = Currencies_CMC_Crypto.OrderBy(x => x).ToList(); } log.Information($"Found {ctr} Currencies at CMC."); Dispatcher.Invoke(() => { ComboBox_ReferenceCurrency.ItemsSource = Currencies_CMC_Fiat.Intersect(Currencies_Fixer); if (REFERENCECURRENCY != null) { var idx = Currencies_CMC_Fiat.IndexOf(REFERENCECURRENCY); if (idx != -1) { ComboBox_ReferenceCurrency.SelectedIndex = idx; } } }); break; } catch (Exception ex) when(ex.Message.Contains("401")) { log.Error("Querying CMC currencies: Unauthorized."); break; } catch (Exception ex) { failCounter++; log.Error($"Querying CMC currencies: {ex.Short()}"); await Task.Delay(5000 * failCounter); } } })); }
private async void Query_CMC(bool reference_change = false) { if (string.IsNullOrEmpty(CMCAPIKEY)) { return; } await Task.Run(() => { _ = cmcQuery.WaitOne(2500); }); await Task.Run(async() => { try { _ = AwaitOnline(Services.CMC); Activity(); var reference = await ReferenceCurrency_Get(); using (WebClient client = new()) { client.Headers.Add("X-CMC_PRO_API_KEY", CMCAPIKEY); client.Headers.Add("Accepts", "application/json"); var currencies = Currencies.ToArray().Where(x => Currencies_CMC_Crypto.Contains(x)); if (!reference_change) { var remove = Change.Where(x => DateTime.Now - x.Date < new TimeSpan(0, 5, 0)); if (remove.Any()) { currencies = currencies.Where(x => !remove.Any(y => y.Base == x)); } } var symbols = string.Join(",", currencies); if (string.IsNullOrEmpty(symbols)) { return; } var url = $"https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol={symbols}&convert={reference}"; var json = client.DownloadString(url); json = json.Replace("\"quote\":{\"" + $"{reference}" + "\"", "\"quote\":{\"Currency\""); var cmc = JsonConvert.DeserializeObject <JSON_CMC_Change>(json); Dispatcher.Invoke(() => { foreach (var item in cmc.data) { Change temp = new() { Quote = reference, Base = item.Value.symbol, Change1h = Math.Round(item.Value.quote.currency.percent_change_1h, 2), Change24h = Math.Round(item.Value.quote.currency.percent_change_24h, 2), Change7d = Math.Round(item.Value.quote.currency.percent_change_7d, 2), Change30d = Math.Round(item.Value.quote.currency.percent_change_30d, 2), Date = DateTime.Now }; var entry = Change.SingleOrDefault(x => x.Base == temp.Base); lock (lock_currencyChange) { if (entry == default) { Change.Add(temp); } else { _ = Change.Remove(entry); Change.Add(temp); } } } }); if (reference_change) { log.Information($"CMC: Added {cmc.data.Count} currencies for new reference currency [{reference}]."); } } } catch (Exception ex) when(ex.Message.Contains("408")) { log.Error($"CMC Query: General Timeout"); } catch (Exception ex) when(ex.Message.Contains("504")) { log.Error($"CMC Query: Gateway Timeout"); } catch (Exception ex) when(ex.Message.Contains("404")) { log.Error("CMC Query: Not found"); } catch (Exception ex) when(ex.Message.Contains("500")) { log.Error("CMC Query: Internal Server Error"); } catch (Exception ex) when(ex.Message.Contains("406")) { log.Error($"Query unavailable at CMC."); } catch (Exception ex) when(ex.Message.Contains("403")) { log.Error($"Query unavailable at CMC."); } catch (Exception ex) { log.Error($"CMC Query: {ex.Short()}"); } }); _ = cmcQuery.Set(); }
private async void Query_Fixer(bool reference_change = false) { if (fixerQuery || string.IsNullOrEmpty(FIXERAPIKEY)) { return; } else { fixerQuery = true; } await Task.Run(async() => { try { _ = AwaitOnline(Services.Fixer); Activity(); var reference = await ReferenceCurrency_Get(); var currencies = Currencies.Where(x => Currencies_Fixer.Contains(x) && !Currencies_CMC_Crypto.Contains(x) && x != reference); var symbol = string.Join(",", currencies.ToArray()); var dates = new string[] { "latest", (DateTime.Now - new TimeSpan(1, 0, 0, 0)).ToString("yyyy-MM-dd"), (DateTime.Now - new TimeSpan(7, 0, 0, 0)).ToString("yyyy-MM-dd"), (DateTime.Now - new TimeSpan(30, 0, 0, 0)).ToString("yyyy-MM-dd") }; var query = new JSON_Fixer[4]; using (WebClient webclient = new()) { for (var i = 0; i < 4; i++) { var json = webclient.DownloadString($"http://data.fixer.io/api/{dates[i]}?access_key={FIXERAPIKEY}&base=EUR&symbols={symbol + (reference == "EUR" ? null : $",{reference}")}"); query[i] = JsonConvert.DeserializeObject <JSON_Fixer>(json); } } if (query.Any(x => x.Succeess == false && x?.error?.Code == "104")) { log.Information("Fixer.io requests exceeded. [Pair]"); return; } RemovePreviousReferenceCurrency(); foreach (var currency in currencies) { Change temp; if (reference == "EUR") { temp = new Change() { Quote = reference, Base = currency, Change1h = 0, Change24h = Math.Round(((query[1].Rates[currency] / query[0].Rates[currency]) - 1) * 100, 2), Change7d = Math.Round(((query[2].Rates[currency] / query[0].Rates[currency]) - 1) * 100, 2), Change30d = Math.Round(((query[3].Rates[currency] / query[0].Rates[currency]) - 1) * 100, 2), Date = DateTime.Now }; } else { temp = new Change() { Quote = reference, Base = currency, Change1h = 0, Change24h = Math.Round(((query[1].Rates[currency] / query[0].Rates[currency] / (query[1].Rates[reference] / query[0].Rates[reference])) - 1) * 100, 2), Change7d = Math.Round(((query[2].Rates[currency] / query[0].Rates[currency] / (query[2].Rates[reference] / query[0].Rates[reference])) - 1) * 100, 2), Change30d = Math.Round(((query[3].Rates[currency] / query[0].Rates[currency] / (query[3].Rates[reference] / query[0].Rates[reference])) - 1) * 100, 2), Date = DateTime.Now }; } UpdateEntry(temp); } if (reference_change) { log.Information($"Fixer.io: Added {currencies.Count()} currencies for new reference currency [{reference}]."); } async void RemovePreviousReferenceCurrency() { var ref_entry = Change.SingleOrDefault(x => x.Base == reference); await Dispatcher.InvokeAsync(() => { if (ref_entry != default) { _ = Change.Remove(ref_entry); } }); } async void UpdateEntry(Change temp) { var entry = Change.SingleOrDefault(x => x.Base == temp.Base); await Dispatcher.InvokeAsync(() => { lock (lock_currencyChange) { if (entry == default) { Change.Add(temp); } else { _ = Change.Remove(entry); Change.Add(temp); } } }); } } catch (Exception ex) when(ex.Message.Contains("408")) { log.Error($"Error in Fixer.io Query: General Timeout"); }