|
| 1 | +"""Use case for showing missing torrent groups in local collections.""" |
| 2 | + |
| 3 | +from red_plex.infrastructure.db.local_database import LocalDatabase |
| 4 | +from red_plex.infrastructure.logger.logger import logger |
| 5 | +from red_plex.infrastructure.rest.gazelle.gazelle_api import GazelleAPI |
| 6 | +from red_plex.use_case.show_missing.show_missing_response import ( |
| 7 | + ShowMissingResponse, MissingGroupInfo |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +# pylint: disable=R0903 |
| 12 | +class ShowMissingUseCase: |
| 13 | + """Use case for finding missing torrent groups in local collections.""" |
| 14 | + |
| 15 | + def __init__(self, db: LocalDatabase, gazelle_api: GazelleAPI): |
| 16 | + """Initialize the use case with required dependencies.""" |
| 17 | + self.db = db |
| 18 | + self.gazelle_api = gazelle_api |
| 19 | + |
| 20 | + # pylint: disable=R0914 |
| 21 | + def execute(self, collage_id: str) -> ShowMissingResponse: |
| 22 | + """ |
| 23 | + Execute the show missing use case. |
| 24 | +
|
| 25 | + Args: |
| 26 | + collage_id: The external ID of the collage to check |
| 27 | +
|
| 28 | + Returns: |
| 29 | + ShowMissingResponse with the results |
| 30 | + """ |
| 31 | + # Get the local collection by external_id |
| 32 | + local_collection = self.db.get_collage_collection_by_external_id(collage_id) |
| 33 | + if not local_collection: |
| 34 | + return ShowMissingResponse( |
| 35 | + success=False, |
| 36 | + collage_name="", |
| 37 | + site="", |
| 38 | + missing_groups=[], |
| 39 | + error_message=( |
| 40 | + f"No local collection found for collage ID {collage_id}. " |
| 41 | + f"You may need to convert it first using " |
| 42 | + f"'red-plex collages convert {collage_id} --site <site>'" |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + site = local_collection.site |
| 47 | + |
| 48 | + # Get the current collage from the site |
| 49 | + try: |
| 50 | + site_collection = self.gazelle_api.get_collage(collage_id) |
| 51 | + except Exception as e: # pylint: disable=W0718 |
| 52 | + logger.error("Failed to fetch collage from site: %s", e, exc_info=True) |
| 53 | + return ShowMissingResponse( |
| 54 | + success=False, |
| 55 | + collage_name=local_collection.name, |
| 56 | + site=site, |
| 57 | + missing_groups=[], |
| 58 | + error_message=f"Failed to fetch collage {collage_id} from {site.upper()} - {e}" |
| 59 | + ) |
| 60 | + |
| 61 | + if not site_collection: |
| 62 | + return ShowMissingResponse( |
| 63 | + success=False, |
| 64 | + collage_name=local_collection.name, |
| 65 | + site=site, |
| 66 | + missing_groups=[], |
| 67 | + error_message=f"Collage {collage_id} not found on {site.upper()}" |
| 68 | + ) |
| 69 | + |
| 70 | + # Compare group IDs |
| 71 | + local_group_ids = {int(tg.id) for tg in local_collection.torrent_groups} |
| 72 | + site_group_ids = {int(tg.id) for tg in site_collection.torrent_groups} |
| 73 | + missing_group_ids = site_group_ids - local_group_ids |
| 74 | + |
| 75 | + # If no missing groups, return success with empty list |
| 76 | + if not missing_group_ids: |
| 77 | + return ShowMissingResponse( |
| 78 | + success=True, |
| 79 | + collage_name=local_collection.name, |
| 80 | + site=site, |
| 81 | + missing_groups=[] |
| 82 | + ) |
| 83 | + |
| 84 | + # Fetch details for each missing group |
| 85 | + missing_groups = [] |
| 86 | + for group_id in sorted(missing_group_ids): |
| 87 | + try: |
| 88 | + torrent_group = self.gazelle_api.get_torrent_group(str(group_id)) |
| 89 | + if torrent_group: |
| 90 | + artists = torrent_group.artists if torrent_group.artists else ["Unknown Artist"] |
| 91 | + album_name = torrent_group.album_name or "Unknown Album" |
| 92 | + torrent_url = self.gazelle_api.get_torrent_group_url(str(group_id)) |
| 93 | + |
| 94 | + missing_groups.append(MissingGroupInfo( |
| 95 | + group_id=group_id, |
| 96 | + artist_names=artists, |
| 97 | + album_name=album_name, |
| 98 | + torrent_url=torrent_url |
| 99 | + )) |
| 100 | + else: |
| 101 | + # Group details not available, but still add basic info |
| 102 | + missing_groups.append(MissingGroupInfo( |
| 103 | + group_id=group_id, |
| 104 | + artist_names=["Unknown Artist"], |
| 105 | + album_name="Details not available", |
| 106 | + torrent_url=self.gazelle_api.get_torrent_group_url(str(group_id)) |
| 107 | + )) |
| 108 | + except Exception as e: # pylint: disable=W0718 |
| 109 | + logger.warning("Failed to fetch details for group %s: %s", group_id, e) |
| 110 | + # Add basic info even if details fetch failed |
| 111 | + missing_groups.append(MissingGroupInfo( |
| 112 | + group_id=group_id, |
| 113 | + artist_names=["Unknown Artist"], |
| 114 | + album_name="Error fetching details", |
| 115 | + torrent_url=self.gazelle_api.get_torrent_group_url(str(group_id)) |
| 116 | + )) |
| 117 | + |
| 118 | + return ShowMissingResponse( |
| 119 | + success=True, |
| 120 | + collage_name=local_collection.name, |
| 121 | + site=site, |
| 122 | + missing_groups=missing_groups |
| 123 | + ) |
0 commit comments