Fix update etf holdings

This commit is contained in:
2025-04-12 11:25:50 -04:00
parent 0126d9d863
commit ce90d2060e
4 changed files with 39 additions and 9 deletions

View File

@@ -1321,14 +1321,13 @@ namespace MarketData.Helper
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Request:{0} failed with status {1}",httpNetResponse.Request,httpNetResponse.StatusCode));
return null;
}
List<String> sections = Sections.GetAllItemsInSections(httpNetResponse.ResponseString,"table");
if(null == sections || 1!=sections.Count)
{
etfHoldings=TryParseYahooFinanceETFHoldings(etfSymbol,httpNetResponse.ResponseString);
if(null==etfHoldings)
{
MDTrace.WriteLine(LogLevel.DEBUG,"GetETFHoldings: Unable to interpret response.");
MDTrace.WriteLine(LogLevel.DEBUG,"GetETFHoldings: Unable to interpret the response string.");
return null;
}
return etfHoldings;
@@ -1375,11 +1374,13 @@ namespace MarketData.Helper
}
}
// <section data-testid="top-holdings" class="yf-1hj9jti">
private static ETFHoldings TryParseYahooFinanceETFHoldings(String etfSymbol,String responseString)
{
ETFHoldings etfHoldings = new ETFHoldings();
try
{
WriteToDisk(responseString,$"{etfSymbol}_etfholdings.txt");
DateTime modified = DateTime.Now;
int groupBy=3;
List<String> sections = Sections.GetAllItemsInSections(responseString,"section");
@@ -1405,13 +1406,17 @@ namespace MarketData.Helper
else
{
ETFHolding etfHolding = new ETFHolding();
if(index+2>=spans.Count())continue;
List<String> subSectionsSymbol = Sections.GetSections(spans[index]);
List<String> subSectionsPercentOfAssets = Sections.GetSections(spans[index+2]);
etfHolding.ETFSymbol = etfSymbol;
etfHolding.HoldingSymbolShareClass = null;
etfHolding.HoldingCompanyName = Utility.BetweenString(spans[index+1],">","<");
etfHolding.HoldingSymbol = etfHolding.HoldingSymbolShareClass = Utility.BetweenString(spans[index],">","<");
if(null!=subSectionsSymbol && subSectionsSymbol.Count>0)etfHolding.HoldingSymbol = subSectionsSymbol.Where(x => !String.IsNullOrEmpty(x)).FirstOrDefault();
if (null == etfHolding.HoldingSymbol || "N/A".Equals(etfHolding.HoldingSymbol) || "".Equals(etfHolding.HoldingSymbol)) continue;
etfHolding.PercentOfAssets = FeedParser.ParseValue(Utility.BetweenString(spans[index+2],">","<"));
etfHolding.PercentOfAssets = FeedParser.ParseValue(subSectionsPercentOfAssets.Where(x => !String.IsNullOrEmpty(x)).FirstOrDefault());
etfHolding.Modified = modified;
if(FeedParser.IsNumeric(etfHolding.HoldingSymbol))continue;
etfHoldings.Add(etfHolding);
}
}
@@ -1419,7 +1424,7 @@ namespace MarketData.Helper
}
catch(Exception exception)
{
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[TryParseYahooFinanceETFHoldings] Exception: {0}",exception.ToString()));
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[TryParseYahooFinanceETFHoldings] Symbol:{0} Exception:{1}",etfSymbol,exception.ToString()));
return null;
}
}