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

View File

@@ -1,4 +1,5 @@
using System.Net.Http.Json;
using System.Text.Json;
using YahooFinanceApi;
namespace NadekoBot.Modules.Searches;
@@ -16,6 +17,9 @@ public sealed class DefaultStockDataService : IStockDataService, INService
{
try
{
if (!query.IsAlphaNumeric())
return Array.Empty<StockData>();
var symbols = await Yahoo.Symbols(query)
.Fields(Field.LongName,
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);
// using var http = _httpClientFactory.CreateClient();
// var response = await http.GetFromJsonAsync<FinnHubSearchResponse>($"https://finnhub.io/api/v1/search"
// + $"?q={query}"
// + $"&token=");
//
// if (response is null)
// return Array.Empty<SymbolData>();
//
// return response.Result
// .Where(x => x.Type == "Common Stock")
// .Select(static x => new SymbolData(x.Symbol, x.Description))
// .ToList();
// }
// catch (Exception ex)
// {
// Log.Warning(ex, "Error searching stock symbol: {ErrorMessage}", ex.Message);
// return Array.Empty<SymbolData>();
// }
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}"
+ "?device=console");
var data = JsonSerializer.Deserialize<YahooFinanceSearchResponse>(res);
if (data is null or { Items: null })
return Array.Empty<SymbolData>();
return data.Items
.Where(x => x.Type == "S")
.Select(x => new SymbolData(x.Symbol, x.Name))
.ToList();
}
// 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; }
}