Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: listrr response being treated as a dict #979

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/program/apis/listrr_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ def get_items_from_Listrr(self, content_type, content_lists) -> list[MediaItem]
url = f"api/List/{content_type}/{list_id}/ReleaseDate/Descending/{page}"
response = self.request_handler.execute(HttpMethod.GET, url)
data = response.data
total_pages = data.get("pages", 1)
for item in data.get("items", []):
imdb_id = item.get("imDbId")
total_pages = getattr(data, "pages", 1)
for item in getattr(data, "items", []):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use direct attribute access instead of getattr.

Using getattr with constant attribute values is not safer than direct attribute access. The static analysis tool also flags this as B009.

Apply this diff to use direct attribute access:

-                    total_pages = getattr(data, "pages", 1)
-                    for item in getattr(data, "items", []):
+                    total_pages = data.pages if hasattr(data, "pages") else 1
+                    for item in data.items if hasattr(data, "items") else []:

Committable suggestion skipped: line range outside the PR's diff.

imdb_id = getattr(item,"imDbId")
if imdb_id:
unique_ids.add(imdb_id)
elif content_type == "Movies" and item.get("tmDbId"):
imdb_id = self.trakt_api.get_imdbid_from_tmdb(item["tmDbId"])
elif content_type == "Movies" and getattr(item, "tmDbId"):
imdb_id = self.trakt_api.get_imdbid_from_tmdb(getattr(item, "tmDbId"))
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use direct attribute access with proper error handling.

Since we're working with SimpleNamespace objects, we should use direct attribute access with proper error handling to catch potential AttributeError exceptions.

-        imdb_id = getattr(item,"imDbId")
-        if imdb_id:
-            unique_ids.add(imdb_id)
-        elif content_type == "Movies" and getattr(item, "tmDbId"):
-            imdb_id = self.trakt_api.get_imdbid_from_tmdb(getattr(item, "tmDbId"))
+        try:
+            if item.imDbId:
+                unique_ids.add(item.imDbId)
+            elif content_type == "Movies" and item.tmDbId:
+                imdb_id = self.trakt_api.get_imdbid_from_tmdb(item.tmDbId)
+                if imdb_id:
+                    unique_ids.add(imdb_id)
+        except AttributeError:
+            # Skip items that don't have the expected attributes
+            continue
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
imdb_id = getattr(item,"imDbId")
if imdb_id:
unique_ids.add(imdb_id)
elif content_type == "Movies" and item.get("tmDbId"):
imdb_id = self.trakt_api.get_imdbid_from_tmdb(item["tmDbId"])
elif content_type == "Movies" and getattr(item, "tmDbId"):
imdb_id = self.trakt_api.get_imdbid_from_tmdb(getattr(item, "tmDbId"))
try:
if item.imDbId:
unique_ids.add(item.imDbId)
elif content_type == "Movies" and item.tmDbId:
imdb_id = self.trakt_api.get_imdbid_from_tmdb(item.tmDbId)
if imdb_id:
unique_ids.add(imdb_id)
except AttributeError:
# Skip items that don't have the expected attributes
continue
🧰 Tools
🪛 Ruff (0.8.2)

60-60: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)


63-63: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)


64-64: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidemarcoli its got a point :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

if imdb_id:
unique_ids.add(imdb_id)
except HTTPError as e:
Expand Down
Loading