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]": { "[python]": {
"editor.defaultFormatter": "ms-python.autopep8" "editor.defaultFormatter": "ms-python.python"
}, },
"python.formatting.provider": "none" "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 = [ programming_crime_list = [
'AAPL', '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', 'ABBV',
'WFC', 'ABT',
'NEE',
'TMUS',
'MCD',
'TMO',
'ACN', 'ACN',
'AVGO', 'ADBE',
'NKE',
'TGT',
'UNP',
'HON',
'DHR',
'ORCL',
'LLY',
'FIS',
'COST',
'LOW',
'UPS',
'AMGN',
'MMM',
'TXN',
'BA',
'BMY',
'PM',
'IBM',
'GILD',
'ANTM',
'LMT',
'AMAT', 'AMAT',
'SPGI', 'AMGN',
'RTX', 'AMZN',
'CAT', 'ANTM',
'CVS',
'MO',
'LIN',
'GE',
'CHTR',
'DUK',
'GS',
'CME',
'ISRG',
'SPG',
'FDX',
'BDX',
'CCI',
'DE',
'CCI',
'BIDU',
'GS',
'APD', 'APD',
'MCO', 'AVGO',
'MMC', 'BA',
'NSC', 'BAC',
'USB', 'BDX',
'BIDU',
'BMY',
'CAT',
'CCI',
'CHTR',
'CMCSA',
'CME',
'COST',
'CRM',
'CSCO',
'CSX', 'CSX',
'LRCX', 'CVS',
'SCHW', 'CVX',
'D', 'D',
'BDX', 'DE',
'EXC', 'DHR',
'SO', 'DIS',
'DUK', '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 import Flask, jsonify, redirect, url_for, send_from_directory
from flask_cors import CORS from flask_cors import CORS
from backend.utils.build_frontend import build_frontend from backend.utils.build_frontend import build_frontend
from backend.api.companies import get_companies
import os import os
import subprocess import subprocess
ROOT_DIR: str = os.path.dirname(__file__)
# instantiate the app # instantiate the app
app = Flask(__name__, static_url_path='/static', static_folder='stockingly-frontend/dist') app = Flask(__name__, static_url_path='/static', static_folder='stockingly-frontend/dist')
app.config.from_object(__name__) app.config.from_object(__name__)
@ -18,6 +21,11 @@ def index():
return redirect(url_for('static', filename='index.html')) 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__': if __name__ == '__main__':
build_frontend() build_frontend()
app.run() app.run()