Skip to content

Commit

Permalink
fix: add parent object into stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaisberg authored and Gaisberg committed Jul 27, 2024
1 parent e57d06c commit 16c1ceb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/program/db/db_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from program.media.item import Episode, MediaItem, Movie, Season, Show
from program.media.stream import Stream
from program.types import Event
from sqlalchemy import func, select
from sqlalchemy.orm import joinedload
Expand Down Expand Up @@ -61,15 +62,19 @@ def _get_item_from_db(session, item: MediaItem):
match type:
case "movie":
r = session.execute(select(Movie).where(MediaItem.imdb_id==item.imdb_id).options(joinedload("*"))).unique().scalar_one()
r.streams = session.execute(select(Stream).where(Stream.parent_id==item._id).options(joinedload("*"))).unique().scalars().all()
return r
case "show":
r = session.execute(select(Show).where(MediaItem.imdb_id==item.imdb_id).options(joinedload("*"))).unique().scalar_one()
r.streams = session.execute(select(Stream).where(Stream.parent_id==item._id).options(joinedload("*"))).unique().scalars().all()
return r
case "season":
r = session.execute(select(Season).where(Season._id==item._id).options(joinedload("*"))).unique().scalar_one()
r.streams = session.execute(select(Stream).where(Stream.parent_id==item._id).options(joinedload("*"))).unique().scalars().all()
return r
case "episode":
r = session.execute(select(Episode).where(Episode._id==item._id).options(joinedload("*"))).unique().scalar_one()
r.streams = session.execute(select(Stream).where(Stream.parent_id==item._id).options(joinedload("*"))).unique().scalars().all()
return r
case _:
logger.error(f"_get_item_from_db Failed to create item from type: {type}")
Expand All @@ -93,7 +98,7 @@ def _run_thread_with_db_item(fn, service, program, input_item: MediaItem | None)
pass
item = _get_item_from_db(session, item)

#session.merge(item)
# session.merge(item)
for res in fn(item):
if isinstance(res, list):
all_media_items = True
Expand Down
5 changes: 3 additions & 2 deletions src/program/media/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ class Stream(db.Model):
blacklisted: Mapped[bool] = mapped_column(sqlalchemy.Boolean, nullable=False)

parent_id: Mapped[int] = mapped_column(sqlalchemy.ForeignKey("MediaItem._id"))
parent = relationship("MediaItem", back_populates="streams", cascade="all, delete-orphan", single_parent=True)
parent: Mapped["MediaItem"] = relationship(lazy=False, back_populates="streams", foreign_keys="Stream.parent_id")

def __init__(self, torrent: Torrent):
def __init__(self, torrent: Torrent, parent):
self.raw_title = torrent.raw_title
self.infohash = torrent.infohash
self.parsed_title = torrent.data.parsed_title
self.rank = torrent.rank
self.lev_ratio = torrent.lev_ratio
self.blacklisted = False
self.parent = parent

def __hash__(self):
return self.infohash
Expand Down
2 changes: 1 addition & 1 deletion src/program/scrapers/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _parse_results(item: MediaItem, results: Dict[str, str]) -> Dict[str, Stream
torrents = sort_torrents(torrents)
torrents_dict = {}
for torrent in torrents.values():
torrents_dict[torrent.infohash] = Stream(torrent)
torrents_dict[torrent.infohash] = Stream(torrent, item)
return torrents_dict

return {}

0 comments on commit 16c1ceb

Please sign in to comment.