From c71adee9fa463e823fccb5cac48d3cfb742ba84d Mon Sep 17 00:00:00 2001 From: davidemarcoli Date: Sun, 13 Oct 2024 18:04:21 +0200 Subject: [PATCH] feat: add cache check when manually adding torrent chore: remove not needed state transition --- src/controllers/items.py | 54 +++++++++++++++++++++++++++------ src/program/state_transition.py | 3 -- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/controllers/items.py b/src/controllers/items.py index 52f191257..088ba0a13 100644 --- a/src/controllers/items.py +++ b/src/controllers/items.py @@ -35,6 +35,7 @@ from RTN import Torrent from sqlalchemy import and_, func, or_, select from sqlalchemy.exc import NoResultFound +from program.indexers.trakt import create_item_from_imdb_id from utils.logger import logger from utils.torrent import get_type_and_infohash @@ -248,25 +249,59 @@ async def add_item_manually(request: Request, imdb_id: str = None, input: str = if not infohash: raise HTTPException(status_code=400, detail="No valid input provided") - - with db.Session() as session: + + downloader: Downloader = request.app.program.services.get(Downloader) + with db.Session() as session: + item = create_item_from_imdb_id(imdb_id) + item.requested_by = "user" + item.requested_at = datetime.now() + + needed_media = get_needed_media(item) + cached_streams = downloader.get_cached_streams([infohash], needed_media) + + if len(cached_streams) == 0: + session.rollback() + raise HTTPException( + status_code=400, + detail=f"The selected torrent is not cached for {item.log_string}", + ) + + raw_title = f"Manual Torrent for {imdb_id}" torrent = Torrent( raw_title=raw_title, infohash=infohash, data=ParsedData(raw_title=raw_title), ) - item = MediaItem( - {"imdb_id": imdb_id, "requested_by": "riven", "requested_at": datetime.now(), "state": States.Requested} - ) stream = Stream(torrent) session.add(stream) - session.commit() item.streams = [stream] - request.app.program.em.add_item(item) + item.active_stream = cached_streams[0] + session.add(item) + session.commit() + + try: + downloader.download(item, item.active_stream) + except Exception as e: + logger.error(f"Failed to download {item.log_string}: {e}") + if item.active_stream.get("infohash", None): + downloader._delete_and_reset_active_stream(item) + session.rollback() + raise HTTPException( + status_code=500, detail=f"Failed to download {item.log_string}: {e}" + ) from e - return {"message": f"Added {imdb_id} manually to the database (format was {type})"} + session.commit() + request.app.program.em.add_event(Event("Symlinker", item)) + + return { + "success": True, + "message": f"Added {imdb_id} manually to the database (format was {type})", + "item_id": item._id, + "torrent_id": input, + } + @router.get( "/{id}", @@ -423,7 +458,8 @@ class SetTorrentRDResponse(BaseModel): def add_torrent(request: Request, id: int, magnet: str) -> SetTorrentRDResponse: torrent_id = "" try: - torrent_id = add_torrent_magnet(magnet) + _, infohash = get_type_and_infohash(magnet) + torrent_id = add_torrent(infohash) except Exception: raise HTTPException(status_code=500, detail="Failed to add torrent.") from None diff --git a/src/program/state_transition.py b/src/program/state_transition.py index f81a9a2c9..eeb6018ac 100644 --- a/src/program/state_transition.py +++ b/src/program/state_transition.py @@ -58,9 +58,6 @@ def process_event(existing_item: MediaItem | None, emitted_by: Service, item: Me updated_item = item = existing_item if existing_item.last_state == States.Completed: return existing_item, None, [] - elif existing_item.streams: - next_service = Downloader - items_to_submit = [existing_item] elif not emitted_by == Scraping and Scraping.can_we_scrape(existing_item): items_to_submit = [existing_item] elif item.type == "show":