Skip to content

Commit

Permalink
Add starter code for YahooFinanceQuoteSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhappyasthma committed May 15, 2021
1 parent c53f1a2 commit 5bf4a85
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/YahooFinance.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,64 @@ def parse_analyst_five_year_growth_rate(self, content):
if text == 'Next 5 Years (per annum)':
percentage = YahooFinanceAnalysis._parseNextPercentage(tree_iterator)
self.five_year_growth_rate = percentage.rstrip("%") if percentage else None
return True if self.five_year_growth_rate else False
return True if self.five_year_growth_rate else False


## (unofficial) API documentation: https://observablehq.com/@stroked/yahoofinance
class YahooFinanceQuoteSummary:
# Expects the ticker symbol as the first format string, and a comma-separated list
# of `QuotesummaryModules` strings for the second argument.
URL_TEMPLATE = 'https://query1.finance.yahoo.com/v10/finance/quoteSummary/{}?modules={}'

# A list of modules that can be used inside of `QUOTE_SUMMARY_URL_TEMPLATE`.
# These should be passed as a comma-separated list.
MODULES = {
"assetProfile": "assetProfile", # Company info/background
"incomeStatementHistory": "incomeStatementHistory",
"incomeStatementHistoryQuarterly": "incomeStatementHistoryQuarterly",
"balanceSheetHistory": "balanceSheetHistory", # Current cash/equivalents
"balanceSheetHistoryQuarterly": "balanceSheetHistoryQuarterly",
"cashFlowStatementHistory": "cashFlowStatementHistory",
"cashFlowStatementHistoryQuarterly": "cashFlowStatementHistoryQuarterly",
"defaultKeyStatistics": "defaultKeyStatistics",
"financialData": "financialData",
"calendarEvents": "calendarEvents", # Contains ex-dividend date
"secFilings": "secFilings", # SEC filing links
"recommendationTrend": "recommendationTrend",
"upgradeDowngradeHistory": "upgradeDowngradeHistory",
"institutionOwnership": "institutionOwnership",
"fundOwnership": "fundOwnership",
"majorDirectHolders": "majorDirectHolders",
"majorHoldersBreakdown": "majorHoldersBreakdown",
"insiderTransactions": "insiderTransactions",
"insiderHolders": "insiderHolders",
"netSharePurchaseActivity": "netSharePurchaseActivity",
"netSharePurchaseActivity": "earnings",
"earningsHistory": "earningsHistory",
"earningsTrend": "earningsTrend",
"industryTrend": "industryTrend",
"indexTrend": "indexTrend",
"sectorTrend": "sectorTrend"
}

@classmethod
def _construct_url(cls, ticker_symbol, modules):
modulesString = cls._construct_modules_string(modules)
return cls.URL_TEMPLATE.format(ticker_symbol, modulesString)

# A helper method to return a formatted modules string.
@classmethod
def _construct_modules_string(cls, modules):
modulesString = modules[0]
for index, module in enumerate(modules, start=1):
modulesString = modulesString + ',' + module
return modulesString

# Accepts the ticker symbol followed by a list of
# `YahooFinanceQuoteSummary.MODULES`.
def __init__(self, ticker_symbol, modules):
self.ticker_symbol = ticker_symbol
self.modules = modules
self.url = YahooFinanceQuote._construct_url(ticker_symbol, modules)

## TODO: Add parsing for relevant modules.

0 comments on commit 5bf4a85

Please sign in to comment.