Implemented backend /companies route

This commit is contained in:
Claudio Maggioni 2023-05-08 12:16:06 +02:00
parent 1579fa6794
commit 468f7184e4
4 changed files with 109 additions and 94 deletions

View File

@ -1,6 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
"editor.defaultFormatter": "ms-python.python"
},
"python.formatting.provider": "none"
}

16
backend/api/companies.py Normal file
View File

@ -0,0 +1,16 @@
import os
import pandas as pd
import numpy as np
from scraper.top100_extractor import programming_crime_list
COMPANIES_CSV_PATH: str = 'scraper/companies.csv'
def get_companies(root_dir: str) -> list[dict]:
"""
reads the companies.csv file and returns it as a JSON-ifiable object
to return to the frontend.
"""
df = pd.read_csv(os.path.join(root_dir, COMPANIES_CSV_PATH), index_col='ticker')
tickers = pd.Series(programming_crime_list)
df = df.loc[df.index.isin(tickers), :]
return df.reset_index().replace({ np.nan: None }).to_dict('records')

View File

@ -1,102 +1,93 @@
programming_crime_list = [
'AAPL',
'MSFT',
'AMZN',
'GOOGL',
'META',
'BRK-A',
'TSLA',
'JPM',
'JNJ',
'V',
'PG',
'MA',
'NVDA',
'UNH',
'HD',
'BAC',
'DIS',
'PYPL',
'KO',
'INTC',
'VZ',
'ADBE',
'NFLX',
'CRM',
'PFE',
'MRK',
'CMCSA',
'T',
'ABT',
'PEP',
'XOM',
'CVX',
'WMT',
'CSCO',
'MDT',
'ABBV',
'WFC',
'NEE',
'TMUS',
'MCD',
'TMO',
'ABT',
'ACN',
'AVGO',
'NKE',
'TGT',
'UNP',
'HON',
'DHR',
'ORCL',
'LLY',
'FIS',
'COST',
'LOW',
'UPS',
'AMGN',
'MMM',
'TXN',
'BA',
'BMY',
'PM',
'IBM',
'GILD',
'ANTM',
'LMT',
'ADBE',
'AMAT',
'SPGI',
'RTX',
'CAT',
'CVS',
'MO',
'LIN',
'GE',
'CHTR',
'DUK',
'GS',
'CME',
'ISRG',
'SPG',
'FDX',
'BDX',
'CCI',
'DE',
'CCI',
'BIDU',
'GS',
'AMGN',
'AMZN',
'ANTM',
'APD',
'MCO',
'MMC',
'NSC',
'USB',
'AVGO',
'BA',
'BAC',
'BDX',
'BIDU',
'BMY',
'CAT',
'CCI',
'CHTR',
'CMCSA',
'CME',
'COST',
'CRM',
'CSCO',
'CSX',
'LRCX',
'SCHW',
'CVS',
'CVX',
'D',
'BDX',
'EXC',
'SO',
'DE',
'DHR',
'DIS',
'DUK',
'BDX',
'EXC'
]
'EXC',
'FDX',
'FIS',
'GE',
'GILD',
'GOOGL',
'GS',
'HD',
'HON',
'IBM',
'INTC',
'ISRG',
'JNJ',
'JPM',
'KO',
'LIN',
'LLY',
'LMT',
'LOW',
'LRCX',
'MA',
'MCD',
'MCO',
'MDT',
'MMC',
'MMM',
'MO',
'MRK',
'MSFT',
'NEE',
'NFLX',
'NKE',
'NSC',
'NVDA',
'ORCL',
'PEP',
'PFE',
'PG',
'PM',
'PYPL',
'SCHW',
'SO',
'SPG',
'SPGI',
'T',
'TGT',
'TMO',
'TMUS',
'TSLA',
'TXN',
'UNH',
'UNP',
'UPS',
'USB',
'V',
'VZ',
'WFC',
'WMT',
'XOM'
]

View File

@ -1,10 +1,13 @@
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
import os
import subprocess
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__)
@ -18,6 +21,11 @@ def index():
return redirect(url_for('static', filename='index.html'))
@app.route('/companies', methods=['GET'])
def companies() -> object:
return jsonify(get_companies(ROOT_DIR))
if __name__ == '__main__':
build_frontend()
app.run()