From 4bb08f2ccfcab4435d79c48b423517f14a19b11a Mon Sep 17 00:00:00 2001 From: HCha Date: Wed, 9 Jul 2025 14:38:44 +0800 Subject: [PATCH 1/3] 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. --- nodeseek_sign.py | 55 ++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/nodeseek_sign.py b/nodeseek_sign.py index 39dfbf0..775296e 100644 --- a/nodeseek_sign.py +++ b/nodeseek_sign.py @@ -245,7 +245,7 @@ def sign(ns_cookie, ns_random): # ---------------- 查询签到收益统计函数 ---------------- def get_signin_stats(ns_cookie, days=30): - """查询本月的签到收益统计""" + """查询前days天内的签到收益统计""" if not ns_cookie: return None, "无有效Cookie" @@ -261,13 +261,15 @@ def get_signin_stats(ns_cookie, days=30): 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) - # 获取多页数据以确保覆盖本月所有数据 + # 计算查询开始时间:当前时间减去指定天数 + 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 +281,19 @@ 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.replace(tzinfo=None) + + utc_offset) + 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.replace(tzinfo=None) + + utc_offset) + if record_time_shanghai >= query_start_time: all_records.append(record) break else: @@ -296,30 +302,37 @@ 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.replace(tzinfo=None) + + utc_offset) - # 只统计本月的签到收益 - if (record_time_shanghai >= current_month_start and - "签到收益" in description and "鸡腿" in description): + # 只统计指定天数内的签到收益 + if (record_time_shanghai >= query_start_time and + "签到收益" in description and "鸡腿" in description): signin_records.append({ 'amount': amount, 'date': record_time_shanghai.strftime('%Y-%m-%d'), '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, "查询成功" From cc84684db1b92aa812e51ac738b0675319257c08 Mon Sep 17 00:00:00 2001 From: HCha Date: Wed, 9 Jul 2025 14:45:38 +0800 Subject: [PATCH 2/3] 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. --- nodeseek_sign.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/nodeseek_sign.py b/nodeseek_sign.py index 775296e..9847937 100644 --- a/nodeseek_sign.py +++ b/nodeseek_sign.py @@ -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 @@ -258,9 +259,8 @@ 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 + shanghai_tz = ZoneInfo("Asia/Shanghai") + now_shanghai = datetime.now(shanghai_tz) # 计算查询开始时间:当前时间减去指定天数 query_start_time = now_shanghai - timedelta(days=days) @@ -284,15 +284,13 @@ def get_signin_stats(ns_cookie, days=30): # 检查最后一条记录的时间,如果超出查询范围就停止 last_record_time = datetime.fromisoformat( records[-1][3].replace('Z', '+00:00')) - last_record_time_shanghai = (last_record_time.replace(tzinfo=None) - + utc_offset) + 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) + record_time_shanghai = record_time.astimezone(shanghai_tz) if record_time_shanghai >= query_start_time: all_records.append(record) break @@ -308,8 +306,7 @@ def get_signin_stats(ns_cookie, days=30): 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_shanghai = record_time.astimezone(shanghai_tz) # 只统计指定天数内的签到收益 if (record_time_shanghai >= query_start_time and From f2bc001c748e487bf773d73d98e2929159d78b8c Mon Sep 17 00:00:00 2001 From: HCha Date: Wed, 9 Jul 2025 14:55:47 +0800 Subject: [PATCH 3/3] fix: Prevent invalid value for days in get_signin_stats --- nodeseek_sign.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nodeseek_sign.py b/nodeseek_sign.py index 9847937..00f5e1d 100644 --- a/nodeseek_sign.py +++ b/nodeseek_sign.py @@ -249,7 +249,10 @@ 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",