From c40723d53cce5f39d70a0bdd1aee3289e8bc87f4 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Sat, 12 Aug 2023 17:21:06 +0800 Subject: [PATCH] feat: add get rss torrents api. --- backend/src/module/api/rss.py | 11 +++++++++-- backend/src/module/database/torrent.py | 3 +++ backend/src/module/rss/engine.py | 7 +++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/backend/src/module/api/rss.py b/backend/src/module/api/rss.py index e19ce2c9..aefa0d0f 100644 --- a/backend/src/module/api/rss.py +++ b/backend/src/module/api/rss.py @@ -1,7 +1,6 @@ from typing import Optional -from fastapi import APIRouter, Depends, status -from fastapi.responses import JSONResponse +from fastapi import APIRouter, Depends from .response import u_response @@ -65,3 +64,11 @@ async def refresh_rss(rss_id: int, current_user=Depends(get_current_user)): raise UNAUTHORIZED with RSSEngine() as engine, DownloadClient() as client: response = engine.refresh_rss(client, rss_id) + + +@router.get("/torrent/{rss_id}") +async def get_torrent(rss_id: int, current_user=Depends(get_current_user)): + if not current_user: + raise UNAUTHORIZED + with RSSEngine() as engine: + return engine.get_rss_torrents(rss_id) diff --git a/backend/src/module/database/torrent.py b/backend/src/module/database/torrent.py index 3ccafb14..c35dbadd 100644 --- a/backend/src/module/database/torrent.py +++ b/backend/src/module/database/torrent.py @@ -44,6 +44,9 @@ class TorrentDatabase: def search_all(self) -> list[Torrent]: return self.session.exec(select(Torrent)).all() + def search_rss(self, rss_id: int) -> list[Torrent]: + return self.session.exec(select(Torrent).where(Torrent.rss_id == rss_id)).all() + def check_new(self, torrents_list: list[Torrent]) -> list[Torrent]: new_torrents = [] old_torrents = self.search_all() diff --git a/backend/src/module/rss/engine.py b/backend/src/module/rss/engine.py index 806d79dc..4958ab97 100644 --- a/backend/src/module/rss/engine.py +++ b/backend/src/module/rss/engine.py @@ -28,6 +28,13 @@ class RSSEngine(Database): def get_combine_rss(self) -> list[RSSItem]: return self.rss.get_combine() + def get_rss_torrents(self, rss_id: int) -> list[Torrent]: + rss = self.rss.search_id(rss_id) + if rss: + return self.torrent.search_rss(rss_id) + else: + return [] + def add_rss(self, rss_link: str, name: str | None = None, combine: bool = True): if not name: with RequestContent() as req: