Skip to content

Commit 92da8ba

Browse files
Copilotmarceljungle
andcommitted
Refactor: Move URL construction logic from use case to infrastructure layer
Co-authored-by: marceljungle <44347173+marceljungle@users.noreply.github.com>
1 parent fa83d6c commit 92da8ba

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

red_plex/infrastructure/rest/gazelle/gazelle_api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,20 @@ def add_to_collage(self, collage_id: str,
378378
logger.error('Error adding groups %s to collage %s: %s', group_ids_str, collage_id, e)
379379
return None
380380

381+
def get_torrent_group_url(self, group_id: str) -> str:
382+
"""
383+
Get the full URL for a torrent group page.
384+
385+
Args:
386+
group_id: The ID of the torrent group
387+
388+
Returns:
389+
Full URL to the torrent group page
390+
"""
391+
# Get the base URL (remove '/ajax.php?action=' suffix to get clean base)
392+
base_url = self.base_url.replace('/ajax.php?action=', '')
393+
return f"{base_url}/torrents.php?id={group_id}"
394+
381395
@staticmethod
382396
def _normalize_string(text: str) -> str:
383397
"""

red_plex/use_case/show_missing/show_missing_use_case.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ def execute(self, collage_id: str) -> ShowMissingResponse:
8181
missing_groups=[]
8282
)
8383

84-
# Get the base URL from config for links
85-
base_url = self._get_base_url(site)
86-
8784
# Fetch details for each missing group
8885
missing_groups = []
8986
for group_id in sorted(missing_group_ids):
@@ -92,7 +89,7 @@ def execute(self, collage_id: str) -> ShowMissingResponse:
9289
if torrent_group:
9390
artists = torrent_group.artists if torrent_group.artists else ["Unknown Artist"]
9491
album_name = torrent_group.album_name or "Unknown Album"
95-
torrent_url = f"{base_url}/torrents.php?id={group_id}"
92+
torrent_url = self.gazelle_api.get_torrent_group_url(str(group_id))
9693

9794
missing_groups.append(MissingGroupInfo(
9895
group_id=group_id,
@@ -106,7 +103,7 @@ def execute(self, collage_id: str) -> ShowMissingResponse:
106103
group_id=group_id,
107104
artist_names=["Unknown Artist"],
108105
album_name="Details not available",
109-
torrent_url=f"{base_url}/torrents.php?id={group_id}"
106+
torrent_url=self.gazelle_api.get_torrent_group_url(str(group_id))
110107
))
111108
except Exception as e: # pylint: disable=W0718
112109
logger.warning("Failed to fetch details for group %s: %s", group_id, e)
@@ -115,7 +112,7 @@ def execute(self, collage_id: str) -> ShowMissingResponse:
115112
group_id=group_id,
116113
artist_names=["Unknown Artist"],
117114
album_name="Error fetching details",
118-
torrent_url=f"{base_url}/torrents.php?id={group_id}"
115+
torrent_url=self.gazelle_api.get_torrent_group_url(str(group_id))
119116
))
120117

121118
return ShowMissingResponse(
@@ -124,17 +121,3 @@ def execute(self, collage_id: str) -> ShowMissingResponse:
124121
site=site,
125122
missing_groups=missing_groups
126123
)
127-
128-
def _get_base_url(self, site: str) -> str:
129-
"""Get the base URL for the site from config."""
130-
try:
131-
# pylint: disable=C0415
132-
from red_plex.infrastructure.config.config import load_config
133-
config_data = load_config()
134-
site_config = config_data.site_configurations.get(site.upper())
135-
if site_config:
136-
return site_config.base_url.rstrip('/')
137-
return f"https://{site}.example.com" # fallback
138-
except Exception as e: # pylint: disable=W0718
139-
logger.warning("Failed to load config for URL construction: %s", e)
140-
return f"https://{site}.example.com" # fallback

0 commit comments

Comments
 (0)