mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-28 03:39:34 +02:00
fix a few bugs and make everything a bit more reusable
This commit is contained in:
@@ -16,12 +16,11 @@ class Quality(Enum):
|
|||||||
unknown = 5
|
unknown = 5
|
||||||
|
|
||||||
|
|
||||||
# TODO: make system to detect quality more sophisticated
|
|
||||||
class QualityMixin:
|
class QualityMixin:
|
||||||
title: str
|
title: str
|
||||||
|
|
||||||
@property
|
|
||||||
@computed_field
|
@computed_field
|
||||||
|
@property
|
||||||
def quality(self) -> Quality:
|
def quality(self) -> Quality:
|
||||||
high_quality_pattern = r'\b(4k|4K)\b'
|
high_quality_pattern = r'\b(4k|4K)\b'
|
||||||
medium_quality_pattern = r'\b(1080p|1080P)\b'
|
medium_quality_pattern = r'\b(1080p|1080P)\b'
|
||||||
@@ -39,7 +38,6 @@ class QualityMixin:
|
|||||||
else:
|
else:
|
||||||
return Quality.unknown
|
return Quality.unknown
|
||||||
|
|
||||||
|
|
||||||
class TorrentMixin:
|
class TorrentMixin:
|
||||||
torrent_id: UUID | None = Field(default=None, foreign_key="torrent.id")
|
torrent_id: UUID | None = Field(default=None, foreign_key="torrent.id")
|
||||||
|
|
||||||
@@ -53,4 +51,4 @@ class Torrent(SQLModel, QualityMixin, table=True):
|
|||||||
@property
|
@property
|
||||||
@computed_field
|
@computed_field
|
||||||
def torrent_filepath(self) -> str:
|
def torrent_filepath(self) -> str:
|
||||||
return f"{self.id}.torrent"
|
return f"{self.id}.torrent"
|
||||||
@@ -13,6 +13,7 @@ def search(query: str | Season) -> list[IndexerQueryResult]:
|
|||||||
|
|
||||||
if isinstance(query, Season):
|
if isinstance(query, Season):
|
||||||
query = query.show.name + " s" + query.number.__str__()
|
query = query.show.name + " s" + query.number.__str__()
|
||||||
|
log.debug(f"Searching for Season {query}")
|
||||||
|
|
||||||
for indexer in indexers:
|
for indexer in indexers:
|
||||||
results.extend(indexer.get_search_results(query))
|
results.extend(indexer.get_search_results(query))
|
||||||
|
|||||||
@@ -1,15 +1,33 @@
|
|||||||
import re
|
import re
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
import pydantic
|
||||||
from pydantic import BaseModel, computed_field
|
from pydantic import BaseModel, computed_field
|
||||||
|
|
||||||
from database.torrents import QualityMixin, Torrent
|
from database.torrents import QualityMixin, Torrent
|
||||||
|
|
||||||
|
|
||||||
class IndexerQueryResult(BaseModel, QualityMixin):
|
class IndexerQueryResult(BaseModel, QualityMixin):
|
||||||
|
id: UUID = pydantic.Field(default_factory=uuid4)
|
||||||
title: str
|
title: str
|
||||||
download_url: str
|
_download_url: str
|
||||||
seeders: int
|
seeders: int
|
||||||
flags: list[str]
|
flags: set[str]
|
||||||
|
|
||||||
|
@computed_field
|
||||||
|
@property
|
||||||
|
def season(self) -> set[int]:
|
||||||
|
pattern = r"\b[sS](\d+)\b"
|
||||||
|
matches = re.findall(pattern, self.title, re.IGNORECASE)
|
||||||
|
if matches.__len__() == 2:
|
||||||
|
result = set()
|
||||||
|
for i in range(int(matches[0]), int(matches[1]) + 1):
|
||||||
|
result.add(i)
|
||||||
|
elif matches.__len__() == 1:
|
||||||
|
result = {int(matches[0])}
|
||||||
|
else:
|
||||||
|
result = {}
|
||||||
|
return result
|
||||||
|
|
||||||
def __gt__(self, other) -> bool:
|
def __gt__(self, other) -> bool:
|
||||||
if self.quality.value != other.quality.value:
|
if self.quality.value != other.quality.value:
|
||||||
@@ -35,22 +53,6 @@ class IndexerQueryResult(BaseModel, QualityMixin):
|
|||||||
return Torrent(torrent_title=self.title)
|
return Torrent(torrent_title=self.title)
|
||||||
|
|
||||||
|
|
||||||
@computed_field
|
|
||||||
@property
|
|
||||||
def season(self) -> list[int]:
|
|
||||||
pattern = r"\b[sS](\d+)\b"
|
|
||||||
matches = re.findall(pattern, self.title, re.IGNORECASE)
|
|
||||||
if matches.__len__() == 2:
|
|
||||||
result = []
|
|
||||||
for i in range(int(matches[0]), int(matches[1]) + 1):
|
|
||||||
result.append(i)
|
|
||||||
elif matches.__len__() == 1:
|
|
||||||
result = [int(matches[0])]
|
|
||||||
else:
|
|
||||||
result = []
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
class GenericIndexer(object):
|
class GenericIndexer(object):
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
|||||||
@@ -40,14 +40,13 @@ class Prowlarr(GenericIndexer):
|
|||||||
log.debug("torrent result: " + result.__str__())
|
log.debug("torrent result: " + result.__str__())
|
||||||
result_list.append(
|
result_list.append(
|
||||||
IndexerQueryResult(
|
IndexerQueryResult(
|
||||||
download_url=result['downloadUrl'],
|
_download_url=result['downloadUrl'],
|
||||||
title=result['sortTitle'],
|
title=result['sortTitle'],
|
||||||
seeders=result['seeders'],
|
seeders=result['seeders'],
|
||||||
flags=[]
|
flags=result['indexerFlags'],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return result_list
|
return result_list
|
||||||
else:
|
else:
|
||||||
print(f'Error: {response.status_code}')
|
print(f'Error: {response.status_code}')
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|||||||
@@ -102,22 +102,6 @@ def add_season(db: SessionDependency, season_id: UUID):
|
|||||||
"""
|
"""
|
||||||
season = db.get(Season, season_id)
|
season = db.get(Season, season_id)
|
||||||
season.requested = True
|
season.requested = True
|
||||||
|
|
||||||
# if season.requested == True and season.torrent_status is None:
|
|
||||||
# torrents = indexer.search(season.show.name + " " + season.number.__str__())
|
|
||||||
# log.info("Found torrents: " + pprint.pformat(json.dumps(torrents, default=str)))
|
|
||||||
# torrents.sort()
|
|
||||||
# log.info("Found torrents: " + pprint.pformat(json.dumps(torrents, default=str)))
|
|
||||||
#
|
|
||||||
# torrent_filepath = torrents[0].download()
|
|
||||||
# season.torrent_filepath = torrent_filepath
|
|
||||||
#
|
|
||||||
# db.add(season)
|
|
||||||
# db.commit()
|
|
||||||
# db.refresh(season)
|
|
||||||
# log.info("Selected Torrent: " + pprint.pformat(json.dumps(torrents[0], default=str)))
|
|
||||||
# season = dowloadClients.client.download(torrent=season)
|
|
||||||
|
|
||||||
db.add(season)
|
db.add(season)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(season)
|
db.refresh(season)
|
||||||
@@ -139,19 +123,27 @@ def delete_season(db: SessionDependency, show_id: UUID, season: int):
|
|||||||
return season
|
return season
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{show_id}/torrent", status_code=status.HTTP_200_OK, dependencies=[Depends(
|
@router.get("/{show_id}/{season_id}/torrent", status_code=status.HTTP_200_OK, dependencies=[Depends(
|
||||||
auth.get_current_user)],
|
auth.get_current_user)],
|
||||||
response_model=list[IndexerQueryResult])
|
response_model=list[IndexerQueryResult])
|
||||||
def get_season_torrents(db: SessionDependency, show_id: UUID, season_number: int):
|
def get_season_torrents(db: SessionDependency, show_id: UUID, season_id: UUID):
|
||||||
season = db.exec(select(Season).where(Season.show_id == show_id).where(Season.number == season_number)).first()
|
season = db.get(Season, season_id)
|
||||||
|
|
||||||
torrents = indexer.search(season)
|
if season is None:
|
||||||
|
return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={"message": "Season not found"})
|
||||||
|
|
||||||
|
torrents: list[IndexerQueryResult] = indexer.search(season)
|
||||||
result = []
|
result = []
|
||||||
for torrent in torrents:
|
for torrent in torrents:
|
||||||
if season_number in torrent.season:
|
if season.number in torrent.season:
|
||||||
result.append(torrent)
|
result.append(torrent)
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
if len(result) == 0:
|
||||||
|
return result
|
||||||
result.sort()
|
result.sort()
|
||||||
log.info(f"Found {torrents.__len__()} torrents for show {season.show.name} season {season_number}, of which "
|
|
||||||
|
log.info(f"Found {torrents.__len__()} torrents for show {season.show.name} season {season.number}, of which "
|
||||||
f"{result.__len__()} torrents fit the query")
|
f"{result.__len__()} torrents fit the query")
|
||||||
log.debug(f"unfiltered torrents: \n{pprint.pformat(torrents)}\nfiltered torrents: \n{pprint.pformat(result)}")
|
log.debug(f"unfiltered torrents: \n{pprint.pformat(torrents)}\nfiltered torrents: \n{pprint.pformat(result)}")
|
||||||
return result
|
return result
|
||||||
@@ -169,9 +161,13 @@ def download_seasons_torrent(db: SessionDependency, show_id: UUID, torrent: Inde
|
|||||||
).first()
|
).first()
|
||||||
)
|
)
|
||||||
|
|
||||||
filepath = torrent.download()
|
torrent = torrent.download()
|
||||||
|
|
||||||
status = dowloadClients.client.download(Torrent())
|
dowloadClients.client.download(Torrent)
|
||||||
|
|
||||||
|
for season in seasons:
|
||||||
|
season.requested = True
|
||||||
|
season.torrent_id = torrent.id
|
||||||
|
|
||||||
return seasons
|
return seasons
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user