refactor: 移除交易时间检查逻辑并优化数据获取流程
- 删除 SpiderManager 和 WaveformWidget 中的 is_trading_time 方法及相关检查 - 在 BackendWorker 中添加截图获取步骤 - 更新波形图显示逻辑,移除非交易时间相关提示
This commit is contained in:
7
main.py
7
main.py
@@ -109,7 +109,12 @@ class BackendWorker(QObject):
|
|||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 1. 爬取评论
|
# 1. 先尝试获取截图
|
||||||
|
logger.info("开始获取截图")
|
||||||
|
self.status_update.emit("正在获取截图...")
|
||||||
|
self.fetch_sse_screenshot()
|
||||||
|
|
||||||
|
# 2. 爬取评论
|
||||||
logger.info("开始爬取评论")
|
logger.info("开始爬取评论")
|
||||||
self.status_update.emit("正在爬取评论...")
|
self.status_update.emit("正在爬取评论...")
|
||||||
self.fetch_count += 1
|
self.fetch_count += 1
|
||||||
|
|||||||
24
spider.py
24
spider.py
@@ -159,26 +159,6 @@ class SpiderManager:
|
|||||||
|
|
||||||
return comments
|
return comments
|
||||||
|
|
||||||
def is_trading_time(self) -> bool:
|
|
||||||
"""判断当前是否为交易时间"""
|
|
||||||
from datetime import datetime, time
|
|
||||||
|
|
||||||
current_time = datetime.now().time()
|
|
||||||
|
|
||||||
# 上午交易时间: 9:30-11:30
|
|
||||||
morning_start = time(9, 30)
|
|
||||||
morning_end = time(11, 30)
|
|
||||||
|
|
||||||
# 下午交易时间: 13:00-15:00
|
|
||||||
afternoon_start = time(13, 0)
|
|
||||||
afternoon_end = time(15, 0)
|
|
||||||
|
|
||||||
# 判断是否在交易时间内
|
|
||||||
is_trading = ((morning_start <= current_time <= morning_end) or
|
|
||||||
(afternoon_start <= current_time <= afternoon_end))
|
|
||||||
|
|
||||||
logger.debug(f"当前时间 {current_time.strftime('%H:%M')} 是否为交易时间: {is_trading}")
|
|
||||||
return is_trading
|
|
||||||
|
|
||||||
def _fetch_sse_with_selenium(self, url: str) -> Optional[str]:
|
def _fetch_sse_with_selenium(self, url: str) -> Optional[str]:
|
||||||
"""使用 Selenium 获取页面内容"""
|
"""使用 Selenium 获取页面内容"""
|
||||||
@@ -228,10 +208,6 @@ class SpiderManager:
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import re
|
import re
|
||||||
|
|
||||||
if not self.is_trading_time():
|
|
||||||
logger.info("当前为非交易时间,跳过股票数据爬取")
|
|
||||||
return {}
|
|
||||||
|
|
||||||
sse_url = "https://hq.sinajs.cn/list=sh000001"
|
sse_url = "https://hq.sinajs.cn/list=sh000001"
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 62 KiB |
@@ -89,37 +89,10 @@ class WaveformWidget(QWidget):
|
|||||||
logger.error(f"时间转换错误: {time_str}, 错误: {e}")
|
logger.error(f"时间转换错误: {time_str}, 错误: {e}")
|
||||||
return total_width / 2 # 默认返回中间位置
|
return total_width / 2 # 默认返回中间位置
|
||||||
|
|
||||||
def is_trading_time(self, time_str: str) -> bool:
|
|
||||||
"""判断是否为交易时间"""
|
|
||||||
try:
|
|
||||||
current_time = datetime.strptime(time_str, "%H:%M").time()
|
|
||||||
|
|
||||||
# 上午交易时间: 9:30-11:30
|
|
||||||
morning_start = time(9, 30)
|
|
||||||
morning_end = time(11, 30)
|
|
||||||
|
|
||||||
# 下午交易时间: 13:00-15:00
|
|
||||||
afternoon_start = time(13, 0)
|
|
||||||
afternoon_end = time(15, 0)
|
|
||||||
|
|
||||||
# 判断是否在交易时间内
|
|
||||||
is_trading = ((morning_start <= current_time <= morning_end) or
|
|
||||||
(afternoon_start <= current_time <= afternoon_end))
|
|
||||||
|
|
||||||
logger.debug(f"时间 {time_str} 是否为交易时间: {is_trading}")
|
|
||||||
return is_trading
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"时间判断错误: {time_str}, 错误: {e}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
def add_data_point(self, time_str: str, value: float):
|
def add_data_point(self, time_str: str, value: float):
|
||||||
"""添加数据点"""
|
"""添加数据点"""
|
||||||
# 检查是否为交易时间
|
|
||||||
if not self.is_trading_time(time_str):
|
|
||||||
logger.info(f"非交易时间 {time_str},跳过数据点添加")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 如果是第一个数据点,设置基准值
|
# 如果是第一个数据点,设置基准值
|
||||||
if not self.data_points:
|
if not self.data_points:
|
||||||
self.base_value = value
|
self.base_value = value
|
||||||
@@ -154,12 +127,6 @@ class WaveformWidget(QWidget):
|
|||||||
if not self.data_points:
|
if not self.data_points:
|
||||||
self._draw_no_data_message(painter, width, height)
|
self._draw_no_data_message(painter, width, height)
|
||||||
return
|
return
|
||||||
|
|
||||||
# 检查最后一个数据点的时间是否为交易时间
|
|
||||||
last_time = self.data_points[-1][0] if self.data_points else ""
|
|
||||||
if last_time and not self.is_trading_time(last_time):
|
|
||||||
self._draw_non_trading_message(painter, width, height)
|
|
||||||
return
|
|
||||||
|
|
||||||
# 绘制网格和坐标轴
|
# 绘制网格和坐标轴
|
||||||
self._draw_grid(painter, width, height)
|
self._draw_grid(painter, width, height)
|
||||||
@@ -264,24 +231,12 @@ class WaveformWidget(QWidget):
|
|||||||
painter.setFont(font)
|
painter.setFont(font)
|
||||||
|
|
||||||
# 绘制提示信息
|
# 绘制提示信息
|
||||||
message = "等待交易时间数据..."
|
message = "等待数据..."
|
||||||
text_rect = painter.fontMetrics().boundingRect(message)
|
text_rect = painter.fontMetrics().boundingRect(message)
|
||||||
x = (width - text_rect.width()) // 2
|
x = (width - text_rect.width()) // 2
|
||||||
y = height // 2
|
y = height // 2
|
||||||
|
|
||||||
painter.drawText(x, y, message)
|
painter.drawText(x, y, message)
|
||||||
|
|
||||||
# 绘制交易时间说明
|
|
||||||
font.setPointSize(10)
|
|
||||||
font.setBold(False)
|
|
||||||
painter.setFont(font)
|
|
||||||
|
|
||||||
info = "交易时间: 9:30-11:30, 13:00-15:00"
|
|
||||||
info_rect = painter.fontMetrics().boundingRect(info)
|
|
||||||
x_info = (width - info_rect.width()) // 2
|
|
||||||
y_info = y + 30
|
|
||||||
|
|
||||||
painter.drawText(x_info, y_info, info)
|
|
||||||
|
|
||||||
def _draw_non_trading_message(self, painter: QPainter, width: int, height: int):
|
def _draw_non_trading_message(self, painter: QPainter, width: int, height: int):
|
||||||
"""绘制非交易时间提示信息或截图"""
|
"""绘制非交易时间提示信息或截图"""
|
||||||
|
|||||||
Reference in New Issue
Block a user