-
Notifications
You must be signed in to change notification settings - Fork 1
/
stocknews.py
60 lines (47 loc) · 1.66 KB
/
stocknews.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask, request, Blueprint
import pyrebase
import yfinance as yf
from datetime import datetime, timedelta
stocknews = Blueprint('stocknews', __name__)
today = datetime.now()
month_ago = today - timedelta(days=30)
@stocknews.route("/api/news", methods=["GET"])
def get_general_finance_news(limit=10):
try:
news = yf.Ticker('N/A').news
return news[:limit]
except:
return 'Error getting general finance news from api'
@stocknews.route(f"/api/news/<ticker>", methods=["GET"])
def get_company_news(ticker, limit=15):
try:
stock = yf.Ticker(ticker)
stock_news = stock.news
if not stock_news :
return 'No Articles Found'
else:
return stock_news[:limit]
except:
return 'Error getting company news from api'
@stocknews.route(f"/api/news/markets/<symbol>", methods=["GET"])
def get_markets_news(symbol, limit=15):
try:
market = yf.Ticker(f"^{symbol}")
market_news = market.news
if not market_news :
return 'No Articles Found'
else:
return market_news[:limit]
except:
return 'Error getting company news from api'
@stocknews.route("/api/news/portfolio", methods=["GET"])
def get_portfolio_news():
ticker_arr = request.args.getlist("tickerArr")
limit = request.args.get("limit", 5)
if not ticker_arr:
return "No tickerArr parameter in the request", 400
news_arr = []
for stock in ticker_arr:
stock_news = get_company_news(stock)
news_arr.append(stock_news[:limit])
return news_arr