From 468f7184e4309f7e92d83fb576a5daa5488c11da Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Mon, 8 May 2023 12:16:06 +0200 Subject: [PATCH] Implemented backend /companies route --- .vscode/settings.json | 2 +- backend/api/companies.py | 16 ++++ scraper/top100_extractor.py | 177 +++++++++++++++++------------------- stockingly.py | 8 ++ 4 files changed, 109 insertions(+), 94 deletions(-) create mode 100644 backend/api/companies.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 9ee86e7..ba77eac 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "[python]": { - "editor.defaultFormatter": "ms-python.autopep8" + "editor.defaultFormatter": "ms-python.python" }, "python.formatting.provider": "none" } \ No newline at end of file diff --git a/backend/api/companies.py b/backend/api/companies.py new file mode 100644 index 0000000..649c27c --- /dev/null +++ b/backend/api/companies.py @@ -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') \ No newline at end of file diff --git a/scraper/top100_extractor.py b/scraper/top100_extractor.py index 9b5477b..55e66e4 100644 --- a/scraper/top100_extractor.py +++ b/scraper/top100_extractor.py @@ -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' +] \ No newline at end of file diff --git a/stockingly.py b/stockingly.py index 39698a7..3ed9e56 100644 --- a/stockingly.py +++ b/stockingly.py @@ -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()