feat: Make sign-in stats query time-range flexible

Refactors `get_signin_stats` to query reward statistics over a customizable number of days, instead of being fixed to the current month. Defaults to 30 days.
This commit is contained in:
2025-07-09 14:38:44 +08:00
parent 33a5eda6c1
commit 4bb08f2ccf

View File

@@ -245,7 +245,7 @@ def sign(ns_cookie, ns_random):
# ---------------- 查询签到收益统计函数 ---------------- # ---------------- 查询签到收益统计函数 ----------------
def get_signin_stats(ns_cookie, days=30): def get_signin_stats(ns_cookie, days=30):
"""查询本月的签到收益统计""" """查询前days天内的签到收益统计"""
if not ns_cookie: if not ns_cookie:
return None, "无有效Cookie" return None, "无有效Cookie"
@@ -261,13 +261,15 @@ def get_signin_stats(ns_cookie, days=30):
utc_offset = timedelta(hours=8) utc_offset = timedelta(hours=8)
now_utc = datetime.utcnow() now_utc = datetime.utcnow()
now_shanghai = now_utc + utc_offset now_shanghai = now_utc + utc_offset
current_month_start = datetime(now_shanghai.year, now_shanghai.month, 1)
# 获取多页数据以确保覆盖本月所有数据 # 计算查询开始时间:当前时间减去指定天数
query_start_time = now_shanghai - timedelta(days=days)
# 获取多页数据以确保覆盖指定天数内的所有数据
all_records = [] all_records = []
page = 1 page = 1
while page <= 10: # 最多查询10页防止无限循环 while page <= 20: # 最多查询20页防止无限循环
url = f"https://www.nodeseek.com/api/account/credit/page-{page}" url = f"https://www.nodeseek.com/api/account/credit/page-{page}"
response = requests.get(url, headers=headers, impersonate="chrome110") response = requests.get(url, headers=headers, impersonate="chrome110")
data = response.json() data = response.json()
@@ -279,15 +281,19 @@ def get_signin_stats(ns_cookie, days=30):
if not records: if not records:
break break
# 检查最后一条记录的时间,如果超出本月范围就停止 # 检查最后一条记录的时间,如果超出查询范围就停止
last_record_time = datetime.fromisoformat(records[-1][3].replace('Z', '+00:00')) last_record_time = datetime.fromisoformat(
last_record_time_shanghai = last_record_time.replace(tzinfo=None) + utc_offset records[-1][3].replace('Z', '+00:00'))
if last_record_time_shanghai < current_month_start: last_record_time_shanghai = (last_record_time.replace(tzinfo=None)
# 只添加在本月范围内的记录 + utc_offset)
if last_record_time_shanghai < query_start_time:
# 只添加在查询范围内的记录
for record in records: for record in records:
record_time = datetime.fromisoformat(record[3].replace('Z', '+00:00')) record_time = datetime.fromisoformat(
record_time_shanghai = record_time.replace(tzinfo=None) + utc_offset record[3].replace('Z', '+00:00'))
if record_time_shanghai >= current_month_start: record_time_shanghai = (record_time.replace(tzinfo=None)
+ utc_offset)
if record_time_shanghai >= query_start_time:
all_records.append(record) all_records.append(record)
break break
else: else:
@@ -296,30 +302,37 @@ def get_signin_stats(ns_cookie, days=30):
page += 1 page += 1
time.sleep(0.5) time.sleep(0.5)
# 筛选本月签到收益记录 # 筛选指定天数内的签到收益记录
signin_records = [] signin_records = []
for record in all_records: for record in all_records:
amount, balance, description, timestamp = record amount, balance, description, timestamp = record
record_time = datetime.fromisoformat(timestamp.replace('Z', '+00:00')) record_time = datetime.fromisoformat(
record_time_shanghai = record_time.replace(tzinfo=None) + utc_offset timestamp.replace('Z', '+00:00'))
record_time_shanghai = (record_time.replace(tzinfo=None)
+ utc_offset)
# 只统计本月的签到收益 # 只统计指定天数内的签到收益
if (record_time_shanghai >= current_month_start and if (record_time_shanghai >= query_start_time and
"签到收益" in description and "鸡腿" in description): "签到收益" in description and "鸡腿" in description):
signin_records.append({ signin_records.append({
'amount': amount, 'amount': amount,
'date': record_time_shanghai.strftime('%Y-%m-%d'), 'date': record_time_shanghai.strftime('%Y-%m-%d'),
'description': description 'description': description
}) })
# 生成时间范围描述
period_desc = f"{days}"
if days == 1:
period_desc = "今天"
if not signin_records: if not signin_records:
return { return {
'total_amount': 0, 'total_amount': 0,
'average': 0, 'average': 0,
'days_count': 0, 'days_count': 0,
'records': [], 'records': [],
'period': f"{now_shanghai.strftime('%Y年%m月')}" 'period': period_desc,
}, "查询成功,但没有找到本月签到记录" }, f"查询成功,但没有找到{period_desc}签到记录"
# 统计数据 # 统计数据
total_amount = sum(record['amount'] for record in signin_records) total_amount = sum(record['amount'] for record in signin_records)
@@ -331,7 +344,7 @@ def get_signin_stats(ns_cookie, days=30):
'average': average, 'average': average,
'days_count': days_count, 'days_count': days_count,
'records': signin_records, 'records': signin_records,
'period': f"{now_shanghai.strftime('%Y年%m月')}" 'period': period_desc
} }
return stats, "查询成功" return stats, "查询成功"