diff --git a/src/Nadeko.Bot.Common/Services/IStatsService.cs b/src/Nadeko.Bot.Common/Services/IStatsService.cs index 62f727e81..a923ae367 100644 --- a/src/Nadeko.Bot.Common/Services/IStatsService.cs +++ b/src/Nadeko.Bot.Common/Services/IStatsService.cs @@ -48,4 +48,23 @@ public interface IStatsService /// Gets total amount of private memory currently in use by the bot, in Megabytes. /// double GetPrivateMemoryMegabytes(); + + GuildInfo GetGuildInfo(string name); + GuildInfo GetGuildInfo(ulong id); +} + +public record struct GuildInfo +{ + public required string Name { get; init; } + public required string IconUrl { get; init; } + public required string Owner { get; init; } + public required ulong OwnerId { get; init; } + public required ulong Id { get; init; } + public required int TextChannels { get; init; } + public required int VoiceChannels { get; init; } + public required DateTime CreatedAt { get; init; } + public required IReadOnlyList Features { get; init; } + public required IReadOnlyList Emojis { get; init; } + public required IReadOnlyList Roles { get; init; } + public int MemberCount { get; init; } } \ No newline at end of file diff --git a/src/Nadeko.Bot.Common/Services/Impl/StatsService.cs b/src/Nadeko.Bot.Common/Services/Impl/StatsService.cs index c78def0b8..fe9f40ba7 100644 --- a/src/Nadeko.Bot.Common/Services/Impl/StatsService.cs +++ b/src/Nadeko.Bot.Common/Services/Impl/StatsService.cs @@ -185,4 +185,28 @@ public sealed class StatsService : IStatsService, IReadyExecutor, INService _currentProcess.Refresh(); return _currentProcess.PrivateMemorySize64 / 1.Megabytes().Bytes; } + + public GuildInfo GetGuildInfo(string name) + => throw new NotImplementedException(); + + public GuildInfo GetGuildInfo(ulong id) + { + var g = _client.GetGuild(id); + + return new GuildInfo() + { + Id = g.Id, + IconUrl = g.IconUrl, + Name = g.Name, + Owner = g.Owner.Username, + OwnerId = g.OwnerId, + CreatedAt = g.CreatedAt.UtcDateTime, + VoiceChannels = g.VoiceChannels.Count, + TextChannels = g.TextChannels.Count, + Features = g.Features.Value.ToString().Split(","), + Emojis = g.Emotes.ToArray(), + Roles = g.Roles.OrderByDescending(x => x.Position).ToArray(), + MemberCount = g.MemberCount, + }; + } } diff --git a/src/Nadeko.Bot.Modules.Searches/Crypto/CryptoCommands.cs b/src/Nadeko.Bot.Modules.Searches/Crypto/CryptoCommands.cs index 241a2172a..68ecf0868 100644 --- a/src/Nadeko.Bot.Modules.Searches/Crypto/CryptoCommands.cs +++ b/src/Nadeko.Bot.Modules.Searches/Crypto/CryptoCommands.cs @@ -85,11 +85,11 @@ public partial class Searches .WithUrl($"https://www.tradingview.com/chart/?symbol={stock.Symbol}") .WithTitle(stock.Name) .AddField(GetText(strs.price), $"{sign} **{price}**", true) - .AddField(GetText(strs.market_cap), stock.MarketCap.ToString("C0", localCulture), true) + .AddField(GetText(strs.market_cap), stock.MarketCap, true) .AddField(GetText(strs.volume_24h), stock.DailyVolume.ToString("C0", localCulture), true) .AddField("Change", $"{change} ({changePercent})", true) - .AddField("Change 50d", $"{sign50}{change50}", true) - .AddField("Change 200d", $"{sign200}{change200}", true) + // .AddField("Change 50d", $"{sign50}{change50}", true) + // .AddField("Change 200d", $"{sign200}{change200}", true) .WithFooter(stock.Exchange); var message = await ctx.Channel.EmbedAsync(eb); diff --git a/src/Nadeko.Bot.Modules.Searches/Crypto/DefaultStockDataService.cs b/src/Nadeko.Bot.Modules.Searches/Crypto/DefaultStockDataService.cs index 9ae2766f5..b52c06889 100644 --- a/src/Nadeko.Bot.Modules.Searches/Crypto/DefaultStockDataService.cs +++ b/src/Nadeko.Bot.Modules.Searches/Crypto/DefaultStockDataService.cs @@ -1,7 +1,7 @@ -using CsvHelper; +using AngleSharp; +using CsvHelper; using CsvHelper.Configuration; using System.Globalization; -using System.Net.Http.Json; using System.Text.Json; namespace NadekoBot.Modules.Searches; @@ -22,33 +22,61 @@ public sealed class DefaultStockDataService : IStockDataService, INService return default; using var http = _httpClientFactory.CreateClient(); - var data = await http.GetFromJsonAsync( - $"https://query1.finance.yahoo.com/v7/finance/quote?symbols={query}"); - if (data is null) - return default; - - var symbol = data.QuoteResponse.Result.FirstOrDefault(); - if (symbol is null) - return default; + + var quoteHtmlPage = $"https://finance.yahoo.com/quote/{query.ToUpperInvariant()}"; + + var config = Configuration.Default.WithDefaultLoader(); + using var document = await BrowsingContext.New(config).OpenAsync(quoteHtmlPage); + var divElem = + document.QuerySelector( + "#quote-header-info > div:nth-child(2) > div > div > h1"); + var tickerName = (divElem)?.TextContent; + + var marketcap = document + .QuerySelectorAll("table") + .Skip(1) + .First() + .QuerySelector("tbody > tr > td:nth-child(2)") + ?.TextContent; + + + var volume = document.QuerySelector("td[data-test='AVERAGE_VOLUME_3MONTH-value']") + ?.TextContent; + var close= document.QuerySelector("td[data-test='PREV_CLOSE-value']") + ?.TextContent ?? "0"; + + var price = document + .QuerySelector("#quote-header-info") + ?.QuerySelector("fin-streamer[data-field='regularMarketPrice']") + ?.TextContent ?? close; + + // var data = await http.GetFromJsonAsync( + // $"https://query1.finance.yahoo.com/v7/finance/quote?symbols={query}"); + // + // if (data is null) + // return default; + + // var symbol = data.QuoteResponse.Result.FirstOrDefault(); + + // if (symbol is null) + // return default; + return new() { - Name = symbol.LongName, - Symbol = symbol.Symbol, - Price = symbol.RegularMarketPrice, - Close = symbol.RegularMarketPreviousClose, - MarketCap = symbol.MarketCap, - Change50d = symbol.FiftyDayAverageChangePercent, - Change200d = symbol.TwoHundredDayAverageChangePercent, - DailyVolume = symbol.AverageDailyVolume10Day, - Exchange = symbol.FullExchangeName + Name = tickerName, + Symbol = query, + Price = double.Parse(price, NumberStyles.Any, CultureInfo.InvariantCulture), + Close = double.Parse(close, NumberStyles.Any, CultureInfo.InvariantCulture), + MarketCap = marketcap, + DailyVolume = (long)double.Parse(volume ?? "0", NumberStyles.Any, CultureInfo.InvariantCulture), }; } - catch (Exception) + catch (Exception ex) { - // Log.Warning(ex, "Error getting stock data: {ErrorMessage}", ex.Message); + Log.Warning(ex, "Error getting stock data: {ErrorMessage}", ex.ToString()); return default; } } @@ -59,9 +87,9 @@ public sealed class DefaultStockDataService : IStockDataService, INService throw new ArgumentNullException(nameof(query)); query = Uri.EscapeDataString(query); - + using var http = _httpClientFactory.CreateClient(); - + var res = await http.GetStringAsync( "https://finance.yahoo.com/_finance_doubledown/api/resource/searchassist" + $";searchTerm={query}" @@ -71,11 +99,11 @@ public sealed class DefaultStockDataService : IStockDataService, INService if (data is null or { Items: null }) return Array.Empty(); - + return data.Items - .Where(x => x.Type == "S") - .Select(x => new SymbolData(x.Symbol, x.Name)) - .ToList(); + .Where(x => x.Type == "S") + .Select(x => new SymbolData(x.Symbol, x.Name)) + .ToList(); } private static CsvConfiguration _csvConfig = new(CultureInfo.InvariantCulture) diff --git a/src/Nadeko.Bot.Modules.Searches/Crypto/_common/StockData.cs b/src/Nadeko.Bot.Modules.Searches/Crypto/_common/StockData.cs index e18525b21..d0c191b1d 100644 --- a/src/Nadeko.Bot.Modules.Searches/Crypto/_common/StockData.cs +++ b/src/Nadeko.Bot.Modules.Searches/Crypto/_common/StockData.cs @@ -6,7 +6,7 @@ public class StockData public string Name { get; set; } public string Symbol { get; set; } public double Price { get; set; } - public long MarketCap { get; set; } + public string MarketCap { get; set; } public double Close { get; set; } public double Change50d { get; set; } public double Change200d { get; set; } diff --git a/src/NadekoBot/Bot.cs b/src/NadekoBot/Bot.cs index f30a87b5d..73c3b04f0 100644 --- a/src/NadekoBot/Bot.cs +++ b/src/NadekoBot/Bot.cs @@ -208,7 +208,6 @@ public sealed class Bot : IBot if (baseType is null) continue; - Log.Information(ft.Name); var typeReader = (TypeReader)ActivatorUtilities.CreateInstance(Services, ft); var typeArgs = baseType.GetGenericArguments(); _commandService.AddTypeReader(typeArgs[0], typeReader); diff --git a/src/NadekoBot/NadekoBot.csproj b/src/NadekoBot/NadekoBot.csproj index 9f2d495e2..b56805ca4 100644 --- a/src/NadekoBot/NadekoBot.csproj +++ b/src/NadekoBot/NadekoBot.csproj @@ -17,6 +17,7 @@ true + @@ -32,9 +33,10 @@ - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive