Added search for .stock, it now supports company name search

This commit is contained in:
Kwoth
2022-02-07 08:13:55 +01:00
parent d42036845a
commit 22eabff276
5 changed files with 81 additions and 28 deletions

View File

@@ -4,6 +4,8 @@ using System.Globalization;
namespace NadekoBot.Modules.Searches; namespace NadekoBot.Modules.Searches;
// todo weighted warnings fix
// todo autoplay/fairplay
public partial class Searches public partial class Searches
{ {
public partial class CryptoCommands : NadekoSubmodule<CryptoService> public partial class CryptoCommands : NadekoSubmodule<CryptoService>
@@ -18,9 +20,6 @@ public partial class Searches
[Cmd] [Cmd]
public async partial Task Stock([Leftover]string query) public async partial Task Stock([Leftover]string query)
{ {
if (!query.IsAlphaNumeric())
return;
using var typing = ctx.Channel.EnterTypingState(); using var typing = ctx.Channel.EnterTypingState();
var stocks = await _stocksService.GetStockDataAsync(query); var stocks = await _stocksService.GetStockDataAsync(query);

View File

@@ -1,4 +1,5 @@
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text.Json;
using YahooFinanceApi; using YahooFinanceApi;
namespace NadekoBot.Modules.Searches; namespace NadekoBot.Modules.Searches;
@@ -16,6 +17,9 @@ public sealed class DefaultStockDataService : IStockDataService, INService
{ {
try try
{ {
if (!query.IsAlphaNumeric())
return Array.Empty<StockData>();
var symbols = await Yahoo.Symbols(query) var symbols = await Yahoo.Symbols(query)
.Fields(Field.LongName, .Fields(Field.LongName,
Field.Symbol, Field.Symbol,
@@ -52,32 +56,35 @@ public sealed class DefaultStockDataService : IStockDataService, INService
} }
} }
public Task<IReadOnlyCollection<SymbolData>> SearchSymbolAsync(string query) public async Task<IReadOnlyCollection<SymbolData>> SearchSymbolAsync(string query)
{ {
return Task.FromResult<IReadOnlyCollection<SymbolData>>(Array.Empty<SymbolData>()); if (string.IsNullOrWhiteSpace(query))
throw new ArgumentNullException(nameof(query));
// try query = Uri.EscapeDataString(query);
// {
// query = Uri.EscapeDataString(query); using var http = _httpClientFactory.CreateClient();
// using var http = _httpClientFactory.CreateClient();
// var response = await http.GetFromJsonAsync<FinnHubSearchResponse>($"https://finnhub.io/api/v1/search" var res = await http.GetStringAsync(
// + $"?q={query}" "https://finance.yahoo.com/_finance_doubledown/api/resource/searchassist"
// + $"&token="); + $";searchTerm={query}"
// + "?device=console");
// if (response is null)
// return Array.Empty<SymbolData>(); var data = JsonSerializer.Deserialize<YahooFinanceSearchResponse>(res);
//
// return response.Result if (data is null or { Items: null })
// .Where(x => x.Type == "Common Stock") return Array.Empty<SymbolData>();
// .Select(static x => new SymbolData(x.Symbol, x.Description))
// .ToList(); return data.Items
// } .Where(x => x.Type == "S")
// catch (Exception ex) .Select(x => new SymbolData(x.Symbol, x.Name))
// { .ToList();
// Log.Warning(ex, "Error searching stock symbol: {ErrorMessage}", ex.Message);
// return Array.Empty<SymbolData>();
// }
} }
// public async Task<IReadOnlyCollection<CandleData>> GetCandleDataAsync(string query)
// {
//
// }
} }
public record SymbolData(string Symbol, string Description); public record CandleData();

View File

@@ -0,0 +1,3 @@
namespace NadekoBot.Modules.Searches;
public record SymbolData(string Symbol, string Description);

View File

@@ -0,0 +1,19 @@
#nullable disable
using System.Text.Json.Serialization;
namespace NadekoBot.Modules.Searches;
public class YahooFinanceSearchResponse
{
[JsonPropertyName("suggestionTitleAccessor")]
public string SuggestionTitleAccessor { get; set; }
[JsonPropertyName("suggestionMeta")]
public List<string> SuggestionMeta { get; set; }
[JsonPropertyName("hiConf")]
public bool HiConf { get; set; }
[JsonPropertyName("items")]
public List<YahooFinanceSearchResponseItem> Items { get; set; }
}

View File

@@ -0,0 +1,25 @@
#nullable disable
using System.Text.Json.Serialization;
namespace NadekoBot.Modules.Searches;
public class YahooFinanceSearchResponseItem
{
[JsonPropertyName("symbol")]
public string Symbol { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("exch")]
public string Exch { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("exchDisp")]
public string ExchDisp { get; set; }
[JsonPropertyName("typeDisp")]
public string TypeDisp { get; set; }
}