diff --git a/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/app.py b/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/app.py index 84b52615a..99276e827 100644 --- a/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/app.py +++ b/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/app.py @@ -1,15 +1,16 @@ -from flask import Flask, jsonify +from flask import Flask, jsonify, request from flask_cors import CORS +from random import uniform import time -from utils import load_historical_data +from utils import load_data app = Flask(__name__) CORS(app) start_time = time.time() -historical_data = load_historical_data() +historical_data = load_data() @app.route('/exchange_rate/') @@ -17,16 +18,14 @@ def get_stock_data(symbol): if symbol not in list(historical_data.keys()): return jsonify("Invalid symbol") current_time = time.time() + last_value = historical_data[symbol].Close[-1] step = (int(current_time * 10) - int(start_time * 10) ) % len(historical_data[symbol]) - try: - return jsonify({ - 'symbol': 'USD', - 'rate': float(historical_data[symbol][step]), - 'timestamp': current_time - }) - except: - return "Server Error" + return jsonify({ + 'symbol': 'USD', + 'rate': last_value * (1 + uniform(0.05, -0.05) + step * 0.0005), + 'timestamp': current_time + }) @app.route('/stocks_list') diff --git a/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/requirements.txt b/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/requirements.txt index d58c8a9d8..7860e127b 100644 --- a/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/requirements.txt +++ b/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/requirements.txt @@ -6,3 +6,9 @@ itsdangerous==2.1.2 Jinja2==3.1.4 MarkupSafe==2.1.3 Werkzeug==3.0.3 +numpy==1.26.4 +pandas==2.2.2 +python-dateutil==2.9.0.post0 +pytz==2024.1 +six==1.16.0 +tzdata==2024.1 diff --git a/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/utils.py b/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/utils.py index 67acc76c0..65cbdcf81 100644 --- a/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/utils.py +++ b/subjects/mobile-dev/stock-market/resources/mock-stock-data-server/utils.py @@ -1,5 +1,6 @@ import os import csv +import pandas as pd def load_historical_data(directory_path='./sample-stocks/'): @@ -19,6 +20,21 @@ def load_historical_data(directory_path='./sample-stocks/'): return historical_data +def load_data(directory_path='./sample-stocks/'): + historical_data = {} + + file_list = [filename for filename in os.listdir( + directory_path) if filename.endswith(".csv")] + for filename in file_list: + symbol = filename.replace(".csv", "") + file_path = os.path.join(directory_path, filename) + historical_data[symbol] = pd.read_csv( + file_path, index_col=0, parse_dates=True) + + return historical_data + + if __name__ == "__main__": - result = load_historical_data() + result = load_data() print(f'keys: {result.keys()}') + print(result["AE"].Close[-1])