Skip to content

Commit

Permalink
Fixed issue with spark charts not retrieving proper info
Browse files Browse the repository at this point in the history
Added method to get real-time quotes
Added options to get multiple symbols for real-time quotes and spark charts
  • Loading branch information
ooples committed Jan 21, 2023
1 parent f77a7d9 commit 59e0311
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 8 deletions.
62 changes: 61 additions & 1 deletion src/Helpers/DownloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,27 @@ internal static async Task<string> DownloadSparkChartDataAsync(string symbol, Ti
}
else
{
return await DownloadRawDataAsync(BuildYahooSparkChartUrl(symbol, timeRange, timeInterval));
return await DownloadRawDataAsync(BuildYahooSparkChartUrl(new string[] { symbol }, timeRange, timeInterval));
}
}

/// <summary>
/// Downloads the spark chart json data using the chosen symbols
/// </summary>
/// <param name="symbols"></param>
/// <param name="timeRange"></param>
/// <param name="timeInterval"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
internal static async Task<string> DownloadSparkChartDataAsync(IEnumerable<string> symbols, TimeRange timeRange, TimeInterval timeInterval)
{
if (!symbols.Any())
{
throw new ArgumentException("Symbols Parameter Must Contain At Least One Symbol");
}
else
{
return await DownloadRawDataAsync(BuildYahooSparkChartUrl(symbols, timeRange, timeInterval));
}
}

Expand All @@ -186,6 +206,46 @@ internal static async Task<string> DownloadStatsDataAsync(string symbol, Country
}
}

/// <summary>
/// Downloads the real-time quote json data using the chosen symbol
/// </summary>
/// <param name="symbol"></param>
/// <param name="country"></param>
/// <param name="language"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
internal static async Task<string> DownloadRealTimeQuoteDataAsync(string symbol, Country country, Language language)
{
if (string.IsNullOrWhiteSpace(symbol))
{
throw new ArgumentException("Symbol Parameter Can't Be Empty Or Null");
}
else
{
return await DownloadRawDataAsync(BuildYahooRealTimeQuoteUrl(new string[] { symbol }, country, language));
}
}

/// <summary>
/// Downloads the real-time quote json data using the chosen symbols
/// </summary>
/// <param name="symbols"></param>
/// <param name="country"></param>
/// <param name="language"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
internal static async Task<string> DownloadRealTimeQuoteDataAsync(IEnumerable<string> symbols, Country country, Language language)
{
if (!symbols.Any())
{
throw new ArgumentException("Symbols Parameter Must Contain At Least One Symbol");
}
else
{
return await DownloadRawDataAsync(BuildYahooRealTimeQuoteUrl(symbols, country, language));
}
}

/// <summary>
/// Gets the base csv data that is used by all csv helper classes
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions src/Helpers/RealTimeQuoteHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace OoplesFinance.YahooFinanceAPI.Helpers;

internal class RealTimeQuoteHelper : YahooJsonBase
{
/// <summary>
/// Parses the raw json data for the Real-time Quote data
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonData"></param>
/// <returns></returns>
internal override IEnumerable<T> ParseYahooJsonData<T>(string jsonData)
{
var realTimeQuoteData = JsonSerializer.Deserialize<RealTimeQuoteData>(jsonData);

return realTimeQuoteData != null ? (IEnumerable<T>)realTimeQuoteData.QuoteResponse.Results : Enumerable.Empty<T>();
}
}
5 changes: 3 additions & 2 deletions src/Helpers/SparkChartHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ internal class SparkChartHelper : YahooJsonBase
/// <returns></returns>
internal override IEnumerable<T> ParseYahooJsonData<T>(string jsonData)
{
var sparkChartData = JsonSerializer.Deserialize<SparkChartData>(jsonData);
var rootObjects = JsonDocument.Parse(jsonData).RootElement.EnumerateObject();
var sparkChartData = rootObjects.Select(x => JsonSerializer.Deserialize<SparkInfo>(x.Value));

return sparkChartData != null ? Enumerable.Cast<T>(new SparkInfo[] { sparkChartData.Result }) : Enumerable.Empty<T>();
return sparkChartData != null ? (IEnumerable<T>)sparkChartData : Enumerable.Empty<T>();
}
}
36 changes: 34 additions & 2 deletions src/Helpers/UrlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ internal static Uri BuildYahooChartUrl(string symbol, TimeRange timeRange, TimeI
/// <param name="timeRange"></param>
/// <param name="timeInterval"></param>
/// <returns></returns>
internal static Uri BuildYahooSparkChartUrl(string symbol, TimeRange timeRange, TimeInterval timeInterval) =>
internal static Uri BuildYahooSparkChartUrl(IEnumerable<string> symbols, TimeRange timeRange, TimeInterval timeInterval) =>
new(string.Format(CultureInfo.InvariantCulture, $"https://query2.finance.yahoo.com/v8/finance/spark?interval=" +
$"{GetTimeIntervalString(timeInterval)}&range={GetTimeRangeString(timeRange)}&symbols={symbol}?"));
$"{GetTimeIntervalString(timeInterval)}&range={GetTimeRangeString(timeRange)}&symbols={GetSymbolsString(symbols)}"));

/// <summary>
/// Creates a url that will be used to get stats for a selected symbol
Expand All @@ -74,6 +74,38 @@ internal static Uri BuildYahooStatsUrl(string symbol, Country country, Language
new(string.Format(CultureInfo.InvariantCulture, $"https://query1.finance.yahoo.com/v11/finance/quoteSummary/{symbol}?lang={GetLanguageString(language)}" +
$"&region={GetCountryString(country)}&modules={GetModuleString(module)}"));

/// <summary>
/// Creates a url that will be used to get real-time quotes for multiple symbols
/// </summary>
/// <param name="symbolsList"></param>
/// <param name="country"></param>
/// <param name="language"></param>
/// <returns></returns>
internal static Uri BuildYahooRealTimeQuoteUrl(IEnumerable<string> symbols, Country country, Language language) =>
new(string.Format(CultureInfo.InvariantCulture, $"https://query1.finance.yahoo.com/v6/finance/quote?region=" +
$"{GetCountryString(country)}&lang={GetLanguageString(language)}&symbols={GetSymbolsString(symbols)}"));

/// <summary>
/// Returns a custom string for the symbols option
/// </summary>
/// <param name="symbolsList"></param>
/// <returns></returns>
private static string GetSymbolsString(IEnumerable<string> symbolsList)
{
var result = string.Empty;

var comma = Uri.EscapeDataString(",");
var count = symbolsList.Count();
for (int i = 0; i < count; i++)
{
var symbol = symbolsList.ElementAt(i);
// if it isn't the first element then add the encoded comma before the symbol
result += i != 0 ? comma + symbol : symbol;
}

return result;
}

/// <summary>
/// Returns a custom string for the module option
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Helpers/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
global using OoplesFinance.YahooFinanceAPI.Interfaces;
global using System.Text.Json.Serialization;
global using System.Net.Http.Json;
global using System.Text.Json;
global using System.Text.Json;
global using System.Text.Encodings.Web;
Loading

0 comments on commit 59e0311

Please sign in to comment.