Skip to content

Commit

Permalink
feat: add cache check when manually adding torrent
Browse files Browse the repository at this point in the history
chore: remove not needed state transition
  • Loading branch information
davidemarcoli committed Oct 18, 2024
1 parent 7bdcc15 commit c71adee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
54 changes: 45 additions & 9 deletions src/controllers/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}",
Expand Down Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions src/program/state_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit c71adee

Please sign in to comment.