2023-05-03 08:54:36 +00:00
|
|
|
from flask import Flask, jsonify, redirect, url_for, send_from_directory
|
2023-05-03 08:07:34 +00:00
|
|
|
from flask_cors import CORS
|
2023-05-03 08:54:36 +00:00
|
|
|
from backend.utils.build_frontend import build_frontend
|
2023-05-08 10:16:06 +00:00
|
|
|
from backend.api.companies import get_companies
|
2023-05-20 21:49:12 +00:00
|
|
|
from backend.api.closing_price import get_closing_price_hist
|
2023-05-03 08:54:36 +00:00
|
|
|
import os
|
2023-05-20 21:49:12 +00:00
|
|
|
import sys
|
2023-05-03 08:54:36 +00:00
|
|
|
import subprocess
|
2023-05-21 14:16:18 +00:00
|
|
|
from typing import Optional
|
2023-05-03 08:07:34 +00:00
|
|
|
|
2023-05-08 10:16:06 +00:00
|
|
|
ROOT_DIR: str = os.path.dirname(__file__)
|
|
|
|
|
2023-05-03 08:07:34 +00:00
|
|
|
# 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': '*'}})
|
|
|
|
|
2023-05-03 08:54:36 +00:00
|
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def index():
|
|
|
|
return redirect(url_for('static', filename='index.html'))
|
|
|
|
|
2023-05-03 08:07:34 +00:00
|
|
|
|
2023-05-08 10:16:06 +00:00
|
|
|
@app.route('/companies', methods=['GET'])
|
2023-05-21 14:16:18 +00:00
|
|
|
@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('/')))
|
2023-05-08 10:16:06 +00:00
|
|
|
|
|
|
|
|
2023-05-21 14:16:18 +00:00
|
|
|
@app.route('/price_history/<path:tickers>', methods=['GET'])
|
|
|
|
def price_history(tickers: Optional[str]) -> object:
|
|
|
|
return jsonify(get_closing_price_hist(tickers.split('/')))
|
2023-05-20 21:49:12 +00:00
|
|
|
|
|
|
|
|
2023-05-15 09:50:07 +00:00
|
|
|
@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')
|
|
|
|
|
|
|
|
|
2023-05-03 08:07:34 +00:00
|
|
|
if __name__ == '__main__':
|
2023-05-20 21:49:12 +00:00
|
|
|
if len(sys.argv) < 2 or sys.argv[1] != 'no-frontend':
|
|
|
|
build_frontend()
|
2023-05-03 08:54:36 +00:00
|
|
|
app.run()
|