This repository has been archived on 2023-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
va-project/stockingly.py

70 lines
2.2 KiB
Python
Raw Normal View History

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
from backend.api.closing_price import get_closing_price_hist
2023-05-24 09:56:28 +00:00
from backend.api.employees import get_employees
from backend.api.assets_debts import compare_balance_sheets
2023-05-03 08:54:36 +00:00
import os
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-29 10:00:54 +00:00
from backend.api.eps import get_eps
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-24 09:56:28 +00:00
@app.route('/employees/<path:tickers>', methods=['GET'])
def employees(tickers: Optional[str]) -> object:
return jsonify(get_employees(tickers.split('/')))
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('/')))
@app.route('/assets_debts/<path:tickers>', methods=['GET'])
def assets_debts(tickers: Optional[str]) -> object:
return jsonify(compare_balance_sheets(tickers.split('/')))
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-29 10:00:54 +00:00
@app.route('/eps/<path:tickers>', methods=['GET'])
def eps(tickers: Optional[str]) -> object:
return jsonify(get_eps(tickers.split('/')))
2023-05-03 08:07:34 +00:00
if __name__ == '__main__':
if len(sys.argv) < 2 or sys.argv[1] != 'no-frontend':
build_frontend()
2023-05-03 08:54:36 +00:00
app.run()