update file path

This commit is contained in:
2025-08-28 23:59:13 +08:00
parent f8976ee045
commit 682355231a
17 changed files with 3799 additions and 232 deletions

136
app.py
View File

@@ -3,13 +3,11 @@ from flask import Flask, render_template, request, redirect, url_for, jsonify, s
from datetime import datetime
import os
import logging
import requests
from config import COMPANY_INFO, DATA_DIR, LOG_DIR
from languages import LANGUAGES, TRANSLATIONS, DEFAULT_LANGUAGE
from futu import *
import pandas as pd
app = Flask(__name__, template_folder='pages')
app = Flask(__name__, template_folder='templates')
app.secret_key = os.urandom(24) # 设置会话密钥
# 配置日志
@@ -164,123 +162,27 @@ def serve_download_file(filename):
# 实时市场数据API端点
@app.route('/api/market-data')
def market_data():
# try:
# quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
# market_data_list = []
# ret_sub, err_message = quote_ctx.subscribe(['HK.00700'], [SubType.QUOTE], subscribe_push=False)
# # 先订阅 K 线类型。订阅成功后 OpenD 将持续收到服务器的推送False 代表暂时不需要推送给脚本
# if ret_sub == RET_OK: # 订阅成功
# ret, data = quote_ctx.get_stock_quote(['HK.00700']) # 获取订阅股票报价的实时数据
# if ret == RET_OK:
# # market_data_list.append({
# # 'symbol': data['name'], # 使用中文名称
# # 'price': float(data['last_price']),
# # 'change': float(data['last_price']),
# # 'change_percent': 0
# # })
# pass
# else:
# print('error:', data)
# else:
# print('subscription failed', err_message)
# quote_ctx.close() # 关闭当条连接OpenD 会在1分钟后自动取消相应股票相应类型的订阅
# return jsonify({
# 'status': 'success',
# 'data': market_data_list,
# 'timestamp': datetime.now().isoformat()
# })
import random
base_prices = {'上证指数': 3000, '深证成指': 10000, '中国平安': 50, '五粮液': 200}
# except ImportError:
# pass
try:
# 尝试使用akshare获取真实市场数据
import akshare as ak
import pandas as pd
market_data = []
for symbol, base_price in base_prices.items():
change_percent = random.uniform(-0.01, 0.01)
current_price = round(base_price * (1 + change_percent), 2)
change_value = round(current_price - base_price, 2)
# 获取指数实时数据
index_data = ak.stock_zh_index_spot()
index_codes = ['sh000001', 'sz399001'] # 上证指数和深证成指
index_df = index_data[index_data['code'].isin(index_codes)]
# 获取股票实时数据
stock_data = ak.stock_zh_a_spot()
stock_codes = ['sh601318', 'sz000858'] # 中国平安和五粮液
stock_df = stock_data[stock_data['code'].isin(stock_codes)]
# 合并数据
combined_data = pd.concat([index_df, stock_df], ignore_index=True)
market_data_list = []
for index, row in combined_data.iterrows():
# 处理价格字段指数使用latest股票使用price
price = row['latest'] if 'latest' in row else row['price']
change = row['change']
change_percent = row['change_percent']
market_data_list.append({
'symbol': row['name'], # 使用中文名称
'price': float(price),
'change': float(change),
'change_percent': float(change_percent)
})
return jsonify({
'status': 'success',
'data': market_data_list,
'timestamp': datetime.now().isoformat()
market_data.append({
'symbol': symbol,
'price': current_price,
'change': change_value,
'change_percent': round(change_percent * 100, 2)
})
return jsonify({
'status': 'success',
'data': market_data,
'timestamp': datetime.now().isoformat()
})
except ImportError:
# akshare未安装使用模拟数据
logging.error("akshare未安装使用模拟数据")
import random
base_prices = {'上证指数': 3000, '深证成指': 10000, '中国平安': 50, '五粮液': 200}
market_data = []
for symbol, base_price in base_prices.items():
change_percent = random.uniform(-0.01, 0.01)
current_price = round(base_price * (1 + change_percent), 2)
change_value = round(current_price - base_price, 2)
market_data.append({
'symbol': symbol,
'price': current_price,
'change': change_value,
'change_percent': round(change_percent * 100, 2)
})
return jsonify({
'status': 'success',
'data': market_data,
'timestamp': datetime.now().isoformat()
})
except Exception as e:
logging.error(f"获取市场数据时发生错误: {str(e)}")
# 出错时使用模拟数据
import random
base_prices = {'上证指数': 3000, '深证成指': 10000, '中国平安': 50, '五粮液': 200}
market_data = []
for symbol, base_price in base_prices.items():
change_percent = random.uniform(-0.01, 0.01)
current_price = round(base_price * (1 + change_percent), 2)
change_value = round(current_price - base_price, 2)
market_data.append({
'symbol': symbol,
'price': current_price,
'change': change_value,
'change_percent': round(change_percent * 100, 2)
})
return jsonify({
'status': 'success',
'data': market_data,
'timestamp': datetime.now().isoformat()
})
if __name__ == '__main__':
serve(app, host='0.0.0.0', port=778)