|
|
import json |
|
|
import os |
|
|
import time |
|
|
import uuid |
|
|
from datetime import datetime |
|
|
|
|
|
import httpx |
|
|
import pytz |
|
|
|
|
|
from core.utils import refresh_token_via_rest |
|
|
from core.config import get_settings |
|
|
|
|
|
|
|
|
def show_last_20(text: str) -> str: |
|
|
return text[-20:] if len(text) > 20 else text |
|
|
|
|
|
async def daily_task(): |
|
|
|
|
|
"""异步每日任务""" |
|
|
current_time = datetime.now(pytz.timezone(get_settings().TIMEZONE)) |
|
|
print(f"执行每日任务: {current_time}") |
|
|
|
|
|
url = 'https://arcane.getmerlin.in/v1/thread/unified' |
|
|
ready_refresh_tokens = os.getenv("READY_REFRESH_TOKEN") |
|
|
if ready_refresh_tokens: |
|
|
ready_tokens_array = ready_refresh_tokens.split(',') |
|
|
for e in ready_tokens_array: |
|
|
id_token = await refresh_token_via_rest(e) |
|
|
print(f'{e} 准备签到: {id_token}') |
|
|
|
|
|
request_headers = {**get_settings().HEADERS, 'authorization': f"Bearer {id_token}"} |
|
|
|
|
|
json_data = { |
|
|
"attachments": [], |
|
|
"chatId": str(uuid.uuid4()), |
|
|
"language": "AUTO", |
|
|
"message": { |
|
|
"childId": str(uuid.uuid4()), |
|
|
"content": '介绍二叉树', |
|
|
"context": "", |
|
|
"id": str(uuid.uuid4()), |
|
|
"parentId": 'root' |
|
|
}, |
|
|
"metadata": { |
|
|
"largeContext": False, |
|
|
"merlinMagic": False, |
|
|
"proFinderMode": False, |
|
|
"webAccess": False |
|
|
}, |
|
|
"mode": "UNIFIED_CHAT", |
|
|
|
|
|
"model": 'gpt-4o-mini' |
|
|
} |
|
|
|
|
|
with httpx.Client(timeout=httpx.Timeout(30.0)) as client: |
|
|
try: |
|
|
with client.stream('POST', url, headers=request_headers, json=json_data) as response: |
|
|
if response.status_code == 200: |
|
|
current_time = datetime.now(pytz.timezone(get_settings().TIMEZONE)) |
|
|
|
|
|
print(f'{show_last_20(id_token)}在{current_time} 签到成功') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except httpx.HTTPStatusError as exc: |
|
|
print(f"HTTP 错误发生: {e}") |
|
|
raise |
|
|
|
|
|
time.sleep(30) |
|
|
|
|
|
|
|
|
async def weekly_report_task(): |
|
|
"""异步每周报告任务""" |
|
|
current_time = datetime.now(pytz.timezone(get_settings().TIMEZONE)) |
|
|
print(f"生成每周报告: {current_time}") |
|
|
|
|
|
|
|
|
async def data_cleanup_task(): |
|
|
"""异步数据清理任务""" |
|
|
current_time = datetime.now(pytz.timezone(get_settings().TIMEZONE)) |
|
|
print(f"执行数据清理: {current_time}") |
|
|
|
|
|
|