uu_pixiv_api / getNode.py
rogerxavier's picture
Update getNode.py
1279d0f verified
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from server.decoder import decode_url_to_configs
from server.utils import config,headers
import random
from fastapi import APIRouter,BackgroundTasks
from randomSubscribeUrl import subscription_link_list,getRandomSubscribeUrl,delete_invalid_url_in_txt
router = APIRouter()
@router.get('/')
def getNode(background_tasks: BackgroundTasks)->str:
# 返回一个可用的订阅链接信息
SubscribeUrlList = subscription_link_list(router.fileName)
# 直接用现成的认为可用然后直接返回,之后再删不可用的
randomSubscribeUrl= getRandomSubscribeUrl(SubscribeUrlList)
NodeList = dump_configs(randomSubscribeUrl)
NodeStr = RandomNode(NodeList)
#background_tasks.add_task(delete_invalid_url_in_txt, router.fileName)
return NodeStr
def RandomNode(NodeList:list)->str:
#返回一个节点信息(优先trojan和vmess,因为ss存活辣鸡)
# 检查列表中是否存在以'trojan'或'vmess'开头的项
trojan_vmess_nodes = [node for node in NodeList if node.startswith('trojan') or node.startswith('vmess')]
# 如果存在以'trojan'或'vmess'开头的项,则从这些项中随机选择一个返回
if trojan_vmess_nodes:
return random.choice(trojan_vmess_nodes)
else:
# 否则从整个列表中随机选择一个返回
return random.choice(NodeList)
def dump_configs(url:str)->list:
#返回全部节点信息的列表
configs = decode_url_to_configs(url)
return configs