51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from flask import Flask, jsonify, redirect, url_for, send_from_directory
|
|
from flask_cors import CORS
|
|
from backend.utils.build_frontend import build_frontend
|
|
from backend.api.companies import get_companies
|
|
from backend.api.closing_price import get_closing_price_hist
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
from typing import Optional
|
|
|
|
ROOT_DIR: str = os.path.dirname(__file__)
|
|
|
|
# instantiate the app
|
|
app = Flask(__name__, static_url_path='/static', static_folder='stockingly-frontend/dist')
|
|
app.config.from_object(__name__)
|
|
|
|
# enable CORS
|
|
CORS(app, resources={r'/*': {'origins': '*'}})
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def index():
|
|
return redirect(url_for('static', filename='index.html'))
|
|
|
|
|
|
@app.route('/companies', methods=['GET'])
|
|
@app.route('/companies/<path:tickers>', methods=['GET'])
|
|
def companies(tickers: Optional[str] = None) -> object:
|
|
return jsonify(get_companies(None if tickers is None else tickers.split('/')))
|
|
|
|
|
|
@app.route('/price_history/<path:tickers>', methods=['GET'])
|
|
def price_history(tickers: Optional[str]) -> object:
|
|
return jsonify(get_closing_price_hist(tickers.split('/')))
|
|
|
|
|
|
@app.route('/companies/logos/<ticker>')
|
|
def get_company_logo(ticker: str):
|
|
logo_dir: str = os.path.join(ROOT_DIR, 'scraper', 'logos', 'logos')
|
|
logo_name: str = str(ticker).upper() + '.png'
|
|
|
|
if os.path.exists(os.path.join(logo_dir, logo_name)):
|
|
return send_from_directory(logo_dir, logo_name)
|
|
|
|
return send_from_directory(ROOT_DIR, 'company_generic.svg')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 2 or sys.argv[1] != 'no-frontend':
|
|
build_frontend()
|
|
app.run()
|