Skip to content

Commit

Permalink
Handle keyerror
Browse files Browse the repository at this point in the history
  • Loading branch information
sidward35 committed Aug 27, 2020
1 parent dae9df9 commit 9f16ca1
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions bin/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@ def get_basic_info(stock):

quote = requests.get('https://finnhub.io/api/v1/quote?symbol='+ticker+'&token=brqivm7rh5rc4v2pmq8g')
quote_json = json.loads(str(quote.json()).replace('\'','"').replace('None','"None"').replace('True','"True"').replace('False','"False"'))
output += " ticker="+ticker+" current_price="+str(quote_json["c"])+" high_price="+str(quote_json["h"])+" low_price="+str(quote_json["l"])+" open_price="+str(quote_json["o"])+" prev_close_price="+str(quote_json["pc"])
output += " ticker="+ticker+" current_price="+str(quote_json.get('c'))+" high_price="+str(quote_json.get('h'))+" low_price="+str(quote_json.get('l'))+" open_price="+str(quote_json.get('o'))+" prev_close_price="+str(quote_json.get('pc'))

fins = requests.get('https://finnhub.io/api/v1/stock/metric?symbol='+ticker+'&metric=all&token=brqivm7rh5rc4v2pmq8g')
fins_json = json.loads(str(fins.json()).replace('\'','"').replace('None','"None"').replace('True','"True"').replace('False','"False"'))
fins_json = fins_json["metric"]
output += " 52WeekHigh="+str(fins_json["52WeekHigh"])+" 52WeekLow="+str(fins_json["52WeekLow"])+" beta="+str(fins_json["beta"])+" 52WeekPriceReturnDaily="+str(fins_json["52WeekPriceReturnDaily"])+" 26WeekPriceReturnDaily="+str(fins_json["26WeekPriceReturnDaily"])+" 13WeekPriceReturnDaily="+str(fins_json["13WeekPriceReturnDaily"])+" bookValuePerShareAnnual="+str(fins_json["bookValuePerShareAnnual"])+" bookValuePerShareQuarterly="+str(fins_json["bookValuePerShareQuarterly"])+" bookValueShareGrowth5Y="+str(fins_json["bookValueShareGrowth5Y"])+" capitalSpendingGrowth5Y="+str(fins_json["capitalSpendingGrowth5Y"])+" cashFlowPerShareAnnual="+str(fins_json["cashFlowPerShareAnnual"])+" epsGrowthQuarterlyYoy="+str(fins_json["epsGrowthQuarterlyYoy"])+" marketCapitalization="+str(fins_json["marketCapitalization"])+" netProfitMargin5Y="+str(fins_json["netProfitMargin5Y"])+" netProfitMarginAnnual="+str(fins_json["netProfitMarginAnnual"])+" peNormalizedAnnual="+str(fins_json["peNormalizedAnnual"])+" priceRelativeToSP50052Week="+str(fins_json["priceRelativeToS&P50052Week"])+" priceRelativeToSP50026Week="+str(fins_json["priceRelativeToS&P50026Week"])+" priceRelativeToSP50013Week="+str(fins_json["priceRelativeToS&P50013Week"])+" priceRelativeToSP5004Week="+str(fins_json["priceRelativeToS&P5004Week"])+" revenueGrowth3Y="+str(fins_json["revenueGrowth3Y"])+" revenueGrowth5Y="+str(fins_json["revenueGrowth5Y"])+" revenueGrowthQuarterlyYoy="+str(fins_json["revenueGrowthQuarterlyYoy"])+" roi5Y="+str(fins_json["roi5Y"])+" roiAnnual="+str(fins_json["roiAnnual"])
output += " 52WeekHigh="+str(fins_json.get('52WeekHigh'))+" 52WeekLow="+str(fins_json.get('52WeekLow'))+" beta="+str(fins_json.get('beta'))+" 52WeekPriceReturnDaily="+str(fins_json.get('52WeekPriceReturnDaily'))+" 26WeekPriceReturnDaily="+str(fins_json.get('26WeekPriceReturnDaily'))+" 13WeekPriceReturnDaily="+str(fins_json.get('13WeekPriceReturnDaily'))+" bookValuePerShareAnnual="+str(fins_json.get('bookValuePerShareAnnual'))+" bookValuePerShareQuarterly="+str(fins_json.get('bookValuePerShareQuarterly'))+" bookValueShareGrowth5Y="+str(fins_json.get('bookValueShareGrowth5Y'))+" capitalSpendingGrowth5Y="+str(fins_json.get('capitalSpendingGrowth5Y'))+" cashFlowPerShareAnnual="+str(fins_json.get('cashFlowPerShareAnnual'))+" ebitdAnnual="+str(fins_json.get('ebitdAnnual'))+" epsGrowthQuarterlyYoy="+str(fins_json.get('epsGrowthQuarterlyYoy'))+" marketCapitalization="+str(fins_json.get('marketCapitalization'))+" netIncomeCommonAnnual="+str(fins_json.get('netIncomeCommonAnnual'))+" netProfitMargin5Y="+str(fins_json.get('netProfitMargin5Y'))+" netProfitMarginAnnual="+str(fins_json.get('netProfitMarginAnnual'))+" peNormalizedAnnual="+str(fins_json.get('peNormalizedAnnual'))+" priceRelativeToSP50052Week="+str(fins_json.get('priceRelativeToS&P50052Week'))+" priceRelativeToSP50026Week="+str(fins_json.get('priceRelativeToS&P50026Week'))+" priceRelativeToSP50013Week="+str(fins_json.get('priceRelativeToS&P50013Week'))+" priceRelativeToSP5004Week="+str(fins_json.get('priceRelativeToS&P5004Week'))+" revenueAnnual="+str(fins_json.get('revenueAnnual'))+" revenueGrowth3Y="+str(fins_json.get('revenueGrowth3Y'))+" revenueGrowth5Y="+str(fins_json.get('revenueGrowth5Y'))+" revenueGrowthQuarterlyYoy="+str(fins_json.get('revenueGrowthQuarterlyYoy'))+" roi5Y="+str(fins_json.get('roi5Y'))+" roiAnnual="+str(fins_json.get('roiAnnual'))

aggr = requests.get('https://finnhub.io/api/v1/scan/technical-indicator?symbol='+ticker+'&resolution=D&token=brqivm7rh5rc4v2pmq8g')
aggr_json = json.loads(str(aggr.json()).replace('\'','"').replace('None','"None"').replace('True','"True"').replace('False','"False"'))
aggr_json = aggr_json["technicalAnalysis"]
output += " signal="+str(aggr_json["signal"]).replace(" ","_")+" buy_count="+str(aggr_json["count"]["buy"])+" neutral_count="+str(aggr_json["count"]["neutral"])+" sell_count="+str(aggr_json["count"]["sell"])
output += " signal="+str(aggr_json.get('signal')).replace(" ","_")
if(aggr_json.get('count')!='None'):
output += " buy_count="+str(aggr_json["count"]["buy"])+" neutral_count="+str(aggr_json["count"]["neutral"])+" sell_count="+str(aggr_json["count"]["sell"])

senti = requests.get('https://finnhub.io/api/v1/news-sentiment?symbol='+ticker+'&token=brqivm7rh5rc4v2pmq8g')
senti_json = json.loads(str(senti.json()).replace('\'','"').replace('None','"None"').replace('True','"True"').replace('False','"False"'))
output += " newsArticleCount="+str(senti_json["buzz"]["articlesInLastWeek"])+" avgWeeklyArticleCount="+str(senti_json["buzz"]["weeklyAverage"])+" buzz="+str(senti_json["buzz"]["buzz"])+" companyNewsScore="+str(senti_json["companyNewsScore"])+" sectorAvgNewsScore="+str(senti_json["sectorAverageNewsScore"])+" bearSentiment="+str(senti_json["sentiment"]["bearishPercent"])+" bullSentiment="+str(senti_json["sentiment"]["bullishPercent"])+" sectorAvgBullishPercent="+str(senti_json["sectorAverageBullishPercent"])
if(senti_json.get('buzz')!='None'):
output += " newsArticleCount="+str(senti_json["buzz"]["articlesInLastWeek"])+" avgWeeklyArticleCount="+str(senti_json["buzz"]["weeklyAverage"])+" buzz="+str(senti_json["buzz"]["buzz"])
if(senti_json.get('sentiment')!='None'):
output += " bearSentiment="+str(senti_json["sentiment"]["bearishPercent"])+" bullSentiment="+str(senti_json["sentiment"]["bullishPercent"])
output += " companyNewsScore="+str(senti_json.get('companyNewsScore'))+" sectorAvgNewsScore="+str(senti_json.get('sectorAverageNewsScore'))+" sectorAvgBullishPercent="+str(senti_json.get('sectorAverageBullishPercent'))

export_json(output)

Expand Down

0 comments on commit 9f16ca1

Please sign in to comment.