Skip to content

Commit

Permalink
fix: duplicate item after scraping for media that isn't in the databa…
Browse files Browse the repository at this point in the history
…se already (#834)
  • Loading branch information
davidemarcoli authored Nov 1, 2024
1 parent d426a8a commit 4d7ac8d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/program/db/db_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ def get_item(session, media_item_id, item_type):
session.expunge(item)
yield item


def get_media_item_by_imdb_id(imdb_id: str):
"""Retrieve a MediaItem by its IMDb ID."""
from program.media.item import MediaItem

with db.Session() as session:
return session.execute(
select(MediaItem)
.where(MediaItem.imdb_id == imdb_id)
).unique().scalar_one_or_none()

def get_parent_ids(media_item_ids: list[int]):
"""Retrieve the _ids of MediaItems of type 'movie' or 'show' by a list of MediaItem _ids."""
from program.media.item import MediaItem
Expand Down
31 changes: 13 additions & 18 deletions src/routers/secure/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlalchemy import select
from sqlalchemy.orm import selectinload

from program.db.db_functions import get_media_items_by_ids
import program.db.db_functions as db_functions
from program.downloaders import Downloader
from program.downloaders.shared import hash_from_uri
from program.media.item import Episode, MediaItem, Season, Show
Expand Down Expand Up @@ -252,9 +252,12 @@ async def start_manual_session(

if imdb_id:
prepared_item = MediaItem({"imdb_id": imdb_id})
item = next(TraktIndexer().run(prepared_item))
if db_functions.ensure_item_exists_in_db(prepared_item):
item = db_functions.get_media_item_by_imdb_id(imdb_id)
else:
item = next(TraktIndexer().run(prepared_item))
else:
item = next(get_media_items_by_ids([item_id]), None)
item = next(db_functions.get_media_items_by_ids([item_id]), None)

if not item:
raise HTTPException(status_code=404, detail="Item not found")
Expand Down Expand Up @@ -325,22 +328,14 @@ def manual_update_attributes(request: Request, session_id, data: Union[Container

if str(session.item_id).startswith("tt"):
prepared_item = MediaItem({"imdb_id": session.item_id})
item = next(TraktIndexer().run(prepared_item))
db_session.add(item)
db_session.commit()
if db_functions.ensure_item_exists_in_db(prepared_item):
item = db_functions.get_media_item_by_imdb_id(session.item_id)
else:
item = next(TraktIndexer().run(prepared_item))
db_functions.store_item(item)
item = db_functions.get_media_item_by_imdb_id(session.item_id)
else:
item: MediaItem = (
db_session.execute(
select(MediaItem)
.where(MediaItem._id == session.item_id)
.options(
selectinload(Show.seasons)
.selectinload(Season.episodes)
)
)
.unique()
.scalar_one_or_none()
)
item = db_functions.get_item_from_db(session.item_id)

if not item:
raise HTTPException(status_code=404, detail="Item not found")
Expand Down

0 comments on commit 4d7ac8d

Please sign in to comment.