Spaces:
Running
Running
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from server.decoder import decode_url_to_configs | |
import random | |
from fastapi import APIRouter,BackgroundTasks | |
from server.utils import subscription_link_list,getRandomSubscribeUrl,delete_invalid_url_in_txt | |
from func_timeout import func_set_timeout, FunctionTimedOut | |
router = APIRouter() | |
def getNode(background_tasks: BackgroundTasks)->str: | |
# 设置超时时间 | |
def dump_configs(url: str) -> list: | |
# 返回全部节点信息的列表 | |
configs = decode_url_to_configs(url) | |
return configs | |
# 返回一个可用的订阅链接信息 | |
SubscribeUrlList = subscription_link_list(router.fileName) | |
# 直接用现成的认为可用然后直接返回,之后再删不可用的 | |
randomSubscribeUrl= getRandomSubscribeUrl(SubscribeUrlList) | |
try: | |
NodeList = dump_configs(randomSubscribeUrl) | |
NodeStr = RandomNode(NodeList) | |
# background_tasks.add_task(delete_invalid_url_in_txt, router.fileName) | |
return NodeStr | |
except FunctionTimedOut: | |
print(f"解析订阅链接{randomSubscribeUrl} 时超时") | |
return '' | |
except Exception as e: | |
# 如果解析失败,打印错误信息并继续处理下一个订阅链接 | |
print(f"解析订阅链接 {randomSubscribeUrl} 时发生错误: {e}") | |
return '' | |
def RandomNode(NodeList:list)->str: | |
#返回一个节点信息 | |
return random.choice(NodeList) | |