File size: 1,509 Bytes
4117d38
 
762894e
4117d38
 
 
762894e
471c484
4117d38
 
 
 
4881c5a
 
 
 
 
 
4117d38
 
 
 
471c484
 
 
 
 
 
 
 
 
 
 
 
 
4117d38
 
 
762894e
 
4117d38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/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()
@router.get('/')
def getNode(background_tasks: BackgroundTasks)->str:
    @func_set_timeout(router.timeOutLimit)  # 设置超时时间
    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)