diff --git a/CHANGELOG.md b/CHANGELOG.md index 847d342c1..752624a41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog. ## Unreleased +## [4.1.5] - 11.05.2022 + ### Changed - `.clubdesc ` will now have a nicer response @@ -12,7 +14,8 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog. ### Fixed - `.give` DM will once again show an amount -- Fixed an issue with filters not working and with custom reactions no longer being able to override commands. +- Fixed an issue with filters not working and with custom reactions no longer being able to override commands. +- Fixed `.stock` command ## [4.1.4] - 06-05-2022 diff --git a/src/NadekoBot/Modules/Searches/Crypto/DefaultStockDataService.cs b/src/NadekoBot/Modules/Searches/Crypto/DefaultStockDataService.cs index 5122acbc1..dee5c27b8 100644 --- a/src/NadekoBot/Modules/Searches/Crypto/DefaultStockDataService.cs +++ b/src/NadekoBot/Modules/Searches/Crypto/DefaultStockDataService.cs @@ -1,5 +1,9 @@ -using System.Text.Json; -using YahooFinanceApi; +using CsvHelper; +using CsvHelper.Configuration; +using Google.Protobuf.WellKnownTypes; +using System.Globalization; +using System.Net.Http.Json; +using System.Text.Json; namespace NadekoBot.Modules.Searches; @@ -17,19 +21,14 @@ public sealed class DefaultStockDataService : IStockDataService, INService if (!query.IsAlphaNumeric()) return default; - var symbols = await Yahoo.Symbols(query) - .Fields(Field.LongName, - Field.Symbol, - Field.RegularMarketPrice, - Field.RegularMarketPreviousClose, - Field.MarketCap, - Field.FiftyDayAverageChangePercent, - Field.TwoHundredDayAverageChangePercent, - Field.AverageDailyVolume10Day, - Field.FullExchangeName) - .QueryAsync(); + using var http = _httpClientFactory.CreateClient(); + var data = await http.GetFromJsonAsync( + $"https://query1.finance.yahoo.com/v7/finance/quote?symbols={query}"); - var symbol = symbols.Values.FirstOrDefault(); + if (data is null) + return default; + + var symbol = data.QuoteResponse.Result.FirstOrDefault(); if (symbol is null) return default; @@ -79,11 +78,25 @@ public sealed class DefaultStockDataService : IStockDataService, INService .ToList(); } + private static CsvConfiguration _csvConfig = new(CultureInfo.InvariantCulture) + { + PrepareHeaderForMatch = args => args.Header.Humanize(LetterCasing.Title) + }; + public async Task> GetCandleDataAsync(string query) { - var candles = await Yahoo.GetHistoricalAsync(query, DateTime.Now.Subtract(30.Days())); + using var http = _httpClientFactory.CreateClient(); + await using var resStream = await http.GetStreamAsync( + $"https://query1.finance.yahoo.com/v7/finance/download/{query}" + + $"?period1={DateTime.UtcNow.Subtract(30.Days()).ToTimestamp().Seconds}" + + $"&period2={DateTime.UtcNow.ToTimestamp().Seconds}" + + "&interval=1d"); - return candles + using var textReader = new StreamReader(resStream); + using var csv = new CsvReader(textReader, _csvConfig); + var records = csv.GetRecords().ToArray(); + + return records .Map(static x => new CandleData(x.Open, x.Close, x.High, x.Low, x.Volume)); } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/Crypto/_Common/QuoteResponse.cs b/src/NadekoBot/Modules/Searches/Crypto/_Common/QuoteResponse.cs new file mode 100644 index 000000000..8af438a90 --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Crypto/_Common/QuoteResponse.cs @@ -0,0 +1,42 @@ +using System.Text.Json.Serialization; + +namespace NadekoBot.Modules.Searches; + +public class QuoteResponse +{ + public class ResultModel + { + [JsonPropertyName("longName")] + public string LongName { get; set; } + + [JsonPropertyName("regularMarketPrice")] + public double RegularMarketPrice { get; set; } + + [JsonPropertyName("regularMarketPreviousClose")] + public double RegularMarketPreviousClose { get; set; } + + [JsonPropertyName("fullExchangeName")] + public string FullExchangeName { get; set; } + + [JsonPropertyName("averageDailyVolume10Day")] + public int AverageDailyVolume10Day { get; set; } + + [JsonPropertyName("fiftyDayAverageChangePercent")] + public double FiftyDayAverageChangePercent { get; set; } + + [JsonPropertyName("twoHundredDayAverageChangePercent")] + public double TwoHundredDayAverageChangePercent { get; set; } + + [JsonPropertyName("marketCap")] + public long MarketCap { get; set; } + + [JsonPropertyName("symbol")] + public string Symbol { get; set; } + } + + [JsonPropertyName("result")] + public List Result { get; set; } + + [JsonPropertyName("error")] + public object Error { get; set; } +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/Crypto/_Common/YahooFinanceCandleData.cs b/src/NadekoBot/Modules/Searches/Crypto/_Common/YahooFinanceCandleData.cs new file mode 100644 index 000000000..e95853104 --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Crypto/_Common/YahooFinanceCandleData.cs @@ -0,0 +1,12 @@ +namespace NadekoBot.Modules.Searches; + +public class YahooFinanceCandleData +{ + public DateTime Date { get; set; } + public decimal Open { get; set; } + public decimal High { get; set; } + public decimal Low { get; set; } + public decimal Close { get; set; } + public decimal AdjClose { get; set; } + public long Volume { get; set; } +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/Crypto/_Common/YahooQueryModel.cs b/src/NadekoBot/Modules/Searches/Crypto/_Common/YahooQueryModel.cs new file mode 100644 index 000000000..f3091971a --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Crypto/_Common/YahooQueryModel.cs @@ -0,0 +1,9 @@ +using System.Text.Json.Serialization; + +namespace NadekoBot.Modules.Searches; + +public class YahooQueryModel +{ + [JsonPropertyName("quoteResponse")] + public QuoteResponse QuoteResponse { get; set; } +} \ No newline at end of file diff --git a/src/NadekoBot/NadekoBot.csproj b/src/NadekoBot/NadekoBot.csproj index 902c03340..188a33954 100644 --- a/src/NadekoBot/NadekoBot.csproj +++ b/src/NadekoBot/NadekoBot.csproj @@ -26,6 +26,7 @@ + @@ -81,7 +82,6 @@ - diff --git a/src/NadekoBot/Services/Impl/StatsService.cs b/src/NadekoBot/Services/Impl/StatsService.cs index 36602c748..b7aaf6884 100644 --- a/src/NadekoBot/Services/Impl/StatsService.cs +++ b/src/NadekoBot/Services/Impl/StatsService.cs @@ -7,7 +7,7 @@ namespace NadekoBot.Services; public sealed class StatsService : IStatsService, IReadyExecutor, INService { - public const string BOT_VERSION = "4.1.4"; + public const string BOT_VERSION = "4.1.5"; public string Author => "Kwoth#2452";