Compare commits

..

4 Commits

Author SHA1 Message Date
yowiv
a360ec2053 Merge pull request #35 from HChaZZY/main
feat: 改进签到统计功能,支持自定义查询天数
2025-07-09 15:16:18 +08:00
f2bc001c74 fix: Prevent invalid value for days in get_signin_stats 2025-07-09 14:55:47 +08:00
cc84684db1 fix(timezone): Use zoneinfo for accurate timezone conversion
Replaced manual UTC+8 offset calculation with the `zoneinfo` library to ensure correct handling of timezone-aware datetimes, particularly for the Shanghai timezone.
2025-07-09 14:45:38 +08:00
4bb08f2ccf 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.
2025-07-09 14:38:44 +08:00

View File

@@ -4,6 +4,7 @@ import os
import time
import json
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from curl_cffi import requests
from yescaptcha import YesCaptchaSolver, YesCaptchaSolverError
from turnstile_solver import TurnstileSolver, TurnstileSolverError
@@ -245,10 +246,13 @@ def sign(ns_cookie, ns_random):
# ---------------- 查询签到收益统计函数 ----------------
def get_signin_stats(ns_cookie, days=30):
"""查询本月的签到收益统计"""
"""查询前days天内的签到收益统计"""
if not ns_cookie:
return None, "无有效Cookie"
if days <= 0:
days = 1
headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
'origin': "https://www.nodeseek.com",
@@ -258,16 +262,17 @@ def get_signin_stats(ns_cookie, days=30):
try:
# 使用UTC+8时区上海时区
utc_offset = timedelta(hours=8)
now_utc = datetime.utcnow()
now_shanghai = now_utc + utc_offset
current_month_start = datetime(now_shanghai.year, now_shanghai.month, 1)
shanghai_tz = ZoneInfo("Asia/Shanghai")
now_shanghai = datetime.now(shanghai_tz)
# 获取多页数据以确保覆盖本月所有数据
# 计算查询开始时间:当前时间减去指定天数
query_start_time = now_shanghai - timedelta(days=days)
# 获取多页数据以确保覆盖指定天数内的所有数据
all_records = []
page = 1
while page <= 10: # 最多查询10页防止无限循环
while page <= 20: # 最多查询20页防止无限循环
url = f"https://www.nodeseek.com/api/account/credit/page-{page}"
response = requests.get(url, headers=headers, impersonate="chrome110")
data = response.json()
@@ -279,15 +284,17 @@ def get_signin_stats(ns_cookie, days=30):
if not records:
break
# 检查最后一条记录的时间,如果超出本月范围就停止
last_record_time = datetime.fromisoformat(records[-1][3].replace('Z', '+00:00'))
last_record_time_shanghai = last_record_time.replace(tzinfo=None) + utc_offset
if last_record_time_shanghai < current_month_start:
# 只添加在本月范围内的记录
# 检查最后一条记录的时间,如果超出查询范围就停止
last_record_time = datetime.fromisoformat(
records[-1][3].replace('Z', '+00:00'))
last_record_time_shanghai = last_record_time.astimezone(shanghai_tz)
if last_record_time_shanghai < query_start_time:
# 只添加在查询范围内的记录
for record in records:
record_time = datetime.fromisoformat(record[3].replace('Z', '+00:00'))
record_time_shanghai = record_time.replace(tzinfo=None) + utc_offset
if record_time_shanghai >= current_month_start:
record_time = datetime.fromisoformat(
record[3].replace('Z', '+00:00'))
record_time_shanghai = record_time.astimezone(shanghai_tz)
if record_time_shanghai >= query_start_time:
all_records.append(record)
break
else:
@@ -296,15 +303,16 @@ def get_signin_stats(ns_cookie, days=30):
page += 1
time.sleep(0.5)
# 筛选本月签到收益记录
# 筛选指定天数内的签到收益记录
signin_records = []
for record in all_records:
amount, balance, description, timestamp = record
record_time = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
record_time_shanghai = record_time.replace(tzinfo=None) + utc_offset
record_time = datetime.fromisoformat(
timestamp.replace('Z', '+00:00'))
record_time_shanghai = record_time.astimezone(shanghai_tz)
# 只统计本月的签到收益
if (record_time_shanghai >= current_month_start and
# 只统计指定天数内的签到收益
if (record_time_shanghai >= query_start_time and
"签到收益" in description and "鸡腿" in description):
signin_records.append({
'amount': amount,
@@ -312,14 +320,19 @@ def get_signin_stats(ns_cookie, days=30):
'description': description
})
# 生成时间范围描述
period_desc = f"{days}"
if days == 1:
period_desc = "今天"
if not signin_records:
return {
'total_amount': 0,
'average': 0,
'days_count': 0,
'records': [],
'period': f"{now_shanghai.strftime('%Y年%m月')}"
}, "查询成功,但没有找到本月签到记录"
'period': period_desc,
}, f"查询成功,但没有找到{period_desc}签到记录"
# 统计数据
total_amount = sum(record['amount'] for record in signin_records)
@@ -331,7 +344,7 @@ def get_signin_stats(ns_cookie, days=30):
'average': average,
'days_count': days_count,
'records': signin_records,
'period': f"{now_shanghai.strftime('%Y年%m月')}"
'period': period_desc
}
return stats, "查询成功"