|
@@ -9,8 +9,10 @@ import httpx
|
|
|
import yaml
|
|
import yaml
|
|
|
from config.logu import logger, get_logger
|
|
from config.logu import logger, get_logger
|
|
|
from config.settings import settings
|
|
from config.settings import settings
|
|
|
-from config.app_yaml import app_yaml, Subscription
|
|
|
|
|
from utils.sub import async_get_sub
|
|
from utils.sub import async_get_sub
|
|
|
|
|
+from database.engine import engine,get_session
|
|
|
|
|
+from sqlmodel import Session
|
|
|
|
|
+from database.models.subscription import SubscriptionManager,SubscriptFile
|
|
|
router = APIRouter()
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
@@ -22,7 +24,7 @@ class SubscriptionResponse(BaseModel):
|
|
|
provider_name: str
|
|
provider_name: str
|
|
|
updated_at: datetime
|
|
updated_at: datetime
|
|
|
proxies: List[Dict]
|
|
proxies: List[Dict]
|
|
|
-async def process_url(url: str, save_path) -> Subscription:
|
|
|
|
|
|
|
+async def process_url(url: str, save_path) -> SubscriptFile:
|
|
|
"""处理单个URL的异步任务"""
|
|
"""处理单个URL的异步任务"""
|
|
|
try:
|
|
try:
|
|
|
save_path_res = await async_get_sub(
|
|
save_path_res = await async_get_sub(
|
|
@@ -30,7 +32,7 @@ async def process_url(url: str, save_path) -> Subscription:
|
|
|
save_path,
|
|
save_path,
|
|
|
timeout=5
|
|
timeout=5
|
|
|
)
|
|
)
|
|
|
- return Subscription(
|
|
|
|
|
|
|
+ return SubscriptFile(
|
|
|
url=url,
|
|
url=url,
|
|
|
file_path=str(save_path_res),
|
|
file_path=str(save_path_res),
|
|
|
updated_at=datetime.now(),
|
|
updated_at=datetime.now(),
|
|
@@ -39,7 +41,7 @@ async def process_url(url: str, save_path) -> Subscription:
|
|
|
)
|
|
)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.error(f"更新订阅失败: {url}, 错误: {str(e)}")
|
|
logger.error(f"更新订阅失败: {url}, 错误: {str(e)}")
|
|
|
- return Subscription(
|
|
|
|
|
|
|
+ return SubscriptFile(
|
|
|
url=url,
|
|
url=url,
|
|
|
file_path="",
|
|
file_path="",
|
|
|
updated_at=datetime.now(),
|
|
updated_at=datetime.now(),
|
|
@@ -48,7 +50,7 @@ async def process_url(url: str, save_path) -> Subscription:
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
@router.post("/")
|
|
@router.post("/")
|
|
|
-async def add_subscriptions(sub: SubscriptionBatchRequest):
|
|
|
|
|
|
|
+async def add_subscriptions(sub: SubscriptionBatchRequest) -> List[SubscriptFile]:
|
|
|
"""批量更新订阅链接"""
|
|
"""批量更新订阅链接"""
|
|
|
logger.info(f"开始批量更新订阅: {sub.urls}")
|
|
logger.info(f"开始批量更新订阅: {sub.urls}")
|
|
|
# 初始化任务列表
|
|
# 初始化任务列表
|
|
@@ -59,29 +61,16 @@ async def add_subscriptions(sub: SubscriptionBatchRequest):
|
|
|
# 将每个URL的异步任务添加到任务列表中
|
|
# 将每个URL的异步任务添加到任务列表中
|
|
|
tasks.append(process_url(url, save_path))
|
|
tasks.append(process_url(url, save_path))
|
|
|
results = await asyncio.gather(*tasks)
|
|
results = await asyncio.gather(*tasks)
|
|
|
- app_yaml.subscriptions = results
|
|
|
|
|
- app_yaml.save()
|
|
|
|
|
- return results
|
|
|
|
|
|
|
+ db_results = []
|
|
|
|
|
+ db = SubscriptionManager()
|
|
|
|
|
+ for result in results:
|
|
|
|
|
+ db_results.append(
|
|
|
|
|
+ db.add_subscription_meta(result)
|
|
|
|
|
+ )
|
|
|
|
|
+ return db_results
|
|
|
|
|
|
|
|
@router.get("/")
|
|
@router.get("/")
|
|
|
async def list_subscriptions() ->List[SubscriptionResponse]:
|
|
async def list_subscriptions() ->List[SubscriptionResponse]:
|
|
|
ret = []
|
|
ret = []
|
|
|
- for sub in app_yaml.subscriptions:
|
|
|
|
|
- with open(sub.file_path, "r",encoding='utf-8') as f:
|
|
|
|
|
- sub_yaml = yaml.safe_load(f)
|
|
|
|
|
- groups = sub_yaml.get("proxy-groups", [])
|
|
|
|
|
- if not groups:
|
|
|
|
|
- continue
|
|
|
|
|
- name = groups[0].get("name", "")
|
|
|
|
|
- if not name:
|
|
|
|
|
- continue
|
|
|
|
|
- ret.append(
|
|
|
|
|
- SubscriptionResponse(
|
|
|
|
|
- file_name=Path(sub.file_path).name,
|
|
|
|
|
- provider_name=name,
|
|
|
|
|
- updated_at=sub.updated_at,
|
|
|
|
|
- proxies=sub_yaml.get("proxies", []),
|
|
|
|
|
-
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
- return ret
|
|
|
|
|
|
|
+ db = SubscriptionManager()
|
|
|
|
|
+ db_sub_models = db.get_subscription_meta()
|