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: api manual scraping fixes #915

Merged
merged 4 commits into from
Dec 5, 2024
Merged

fix: api manual scraping fixes #915

merged 4 commits into from
Dec 5, 2024

Conversation

dreulavelle
Copy link
Member

@dreulavelle dreulavelle commented Nov 27, 2024

Pull Request Check List

Resolves: #issue-number-here

  • Added tests for changed code.
  • Updated documentation for changed code.

Description:

Summary by CodeRabbit

  • New Features
    • Enhanced file creation capabilities with support for additional file types ("show" and "season") and an optional file size limit.
    • Improved handling of torrent identifiers, allowing for both integer and string types.
    • Added support for the "alldebrid" downloader option in the media scraping functionality.
  • Bug Fixes
    • Refined error handling for missing identifiers in torrent management.
    • Improved clarity in error messages related to session handling.
  • Documentation
    • Updated type definitions for improved clarity and type safety in torrent-related data structures.

Copy link
Contributor

coderabbitai bot commented Nov 27, 2024

Walkthrough

The changes in this pull request involve modifications to the create method of the DebridFile class, enhancing its parameters to support additional file types and an optional file size limit. The DownloaderBase class has also been updated to allow for more versatile input types, particularly concerning torrent identifiers. Additionally, the scrape.py file has undergone changes to improve type safety and error handling while refining the management of torrent-related data structures.

Changes

File Path Change Summary
src/program/services/downloaders/models.py - Updated create method to include limit_filesize parameter and expanded filetype options to include "show" and "season". Logic adjusted to validate file sizes based on the new parameter.
src/program/services/downloaders/shared.py - Modified add_torrent, select_files, get_torrent_info, and delete_torrent methods to enhance input types for torrent_id and clarify return types.
src/routers/secure/scrape.py - Updated StartSessionResponse and ScrapingSession classes to use more specific types for torrent_info and containers. Adjusted scrape_item function to simplify stream processing and improved error handling across several functions.

Possibly related PRs

Suggested reviewers

  • Gaisberg
  • davidemarcoli

🐇 In a world of streams and files,
A rabbit hops with joyful smiles.
With types so clear and limits set,
Our downloads now are the best yet!
So let us cheer, with hearts so light,
For changes made, our code's delight! 🐰✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

src/program/services/downloaders/models.py Outdated Show resolved Hide resolved
src/program/services/downloaders/models.py Outdated Show resolved Hide resolved
src/program/services/downloaders/models.py Outdated Show resolved Hide resolved
src/routers/secure/scrape.py Outdated Show resolved Hide resolved
@dreulavelle dreulavelle marked this pull request as ready for review December 3, 2024 07:05
@dreulavelle dreulavelle requested a review from Gaisberg December 3, 2024 07:05
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (6)
src/routers/secure/scrape.py (3)

279-282: Handle exceptions more specifically for better error tracing

Catching a general Exception may mask underlying issues. Consider catching specific exceptions related to downloader operations to provide more precise error handling and messaging.


310-310: Potential inconsistency in HTTP status code

Raising a 500 Internal Server Error when no torrent ID is found may not accurately reflect the nature of the error. Consider using a 404 Not Found or 400 Bad Request status code to provide a more precise indication.

Apply this fix:

- raise HTTPException(status_code=500, detail="No torrent ID found")
+ raise HTTPException(status_code=404, detail="No torrent ID found")

339-339: Clarify error status when item ID is missing

Similar to the previous comment, using a 500 Internal Server Error may not accurately represent the issue when item_id is not found. A 404 Not Found would be more appropriate.

Apply this fix:

- raise HTTPException(status_code=500, detail="No item ID found")
+ raise HTTPException(status_code=404, detail="No item ID found")
src/program/services/downloaders/shared.py (1)

44-55: Clarify return type and document possible values in add_torrent

Since add_torrent can return either int or str, consider documenting the specific scenarios or downloader implementations that return each type to improve clarity for users of the method.

src/program/services/downloaders/models.py (2)

60-63: Use not in for clearer string membership testing

The condition not "sample" in filename.lower() is more idiomatically expressed using not in.

Apply this diff:

- if not any(filename.endswith(ext) for ext in VIDEO_EXTENSIONS) and not "sample" in filename.lower():
+ if not any(filename.endswith(ext) for ext in VIDEO_EXTENSIONS) and "sample" not in filename.lower():
🧰 Tools
🪛 Ruff (0.8.0)

60-60: Test for membership should be not in

Convert to not in

(E713)


68-70: Combine conditional statements to simplify logic

You can merge the elif and nested if conditions to streamline the code.

Apply this diff:

- elif filetype in ["show", "season", "episode"]:
-     if not (FILESIZE_EPISODE_CONSTRAINT[0] <= filesize_mb <= FILESIZE_EPISODE_CONSTRAINT[1]):
-         return None
+ elif filetype in ["show", "season", "episode"] and not (FILESIZE_EPISODE_CONSTRAINT[0] <= filesize_mb <= FILESIZE_EPISODE_CONSTRAINT[1]):
+     return None
🧰 Tools
🪛 Ruff (0.8.0)

68-69: Use a single if statement instead of nested if statements

(SIM102)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 73b5179 and e318f93.

📒 Files selected for processing (3)
  • src/program/services/downloaders/models.py (1 hunks)
  • src/program/services/downloaders/shared.py (3 hunks)
  • src/routers/secure/scrape.py (9 hunks)
🧰 Additional context used
🪛 Ruff (0.8.0)
src/program/services/downloaders/models.py

60-60: Test for membership should be not in

Convert to not in

(E713)


68-69: Use a single if statement instead of nested if statements

(SIM102)

src/routers/secure/scrape.py

285-285: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🔇 Additional comments (6)
src/routers/secure/scrape.py (3)

42-43: Good use of explicit type annotations for torrent_info and containers

Defining torrent_info as TorrentInfo and containers as Optional[List[TorrentContainer]] in StartSessionResponse enhances type safety and code clarity.


106-109: Improved type safety in ScrapingSession attributes

Adding explicit type annotations for torrent_id, torrent_info, containers, and selected_files increases code maintainability and helps prevent type-related errors.


224-225: Ensure is_cached flag accurately reflects availability

After setting stream.is_cached = bool(container and container.cached), verify that get_instant_availability correctly determines the cached status to prevent any false positives or negatives.

src/program/services/downloaders/shared.py (3)

60-66: Consistent type annotations enhance method usability

Updating select_files to accept torrent_id: Union[int, str] aligns with changes in other methods and supports flexibility across different downloader services.


71-71: Ensure consistent torrent_id types across methods

By accepting torrent_id: Union[int, str] in get_torrent_info, you maintain consistency and handle varying identifier types from different downloaders.


84-84: Consistent handling of torrent_id in delete_torrent

Accepting torrent_id as Union[int, str] ensures that the method can handle identifiers from various downloader implementations.

torrent_id: str = downloader.add_torrent(info_hash)
torrent_info: TorrentInfo = downloader.get_torrent_info(torrent_id)
container: Optional[TorrentContainer] = downloader.get_instant_availability(info_hash, item.type)
session_manager.update_session(session.id, torrent_id=torrent_id, torrent_info=torrent_info, containers=container)
except Exception as e:
background_tasks.add_task(session_manager.abort_session, session.id)
raise HTTPException(status_code=500, detail=str(e))
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Use exception chaining to preserve traceback

When re-raising an exception within an except block, use exception chaining to maintain the original traceback information.

Apply this fix:

- raise HTTPException(status_code=500, detail=str(e))
+ raise HTTPException(status_code=500, detail=str(e)) from e
📝 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
raise HTTPException(status_code=500, detail=str(e))
raise HTTPException(status_code=500, detail=str(e)) from e
🧰 Tools
🪛 Ruff (0.8.0)

285-285: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

Comment on lines +360 to +361
item.alternative_folder = session.torrent_info.alternative_filename
item.active_stream = {"infohash": session.magnet, "id": session.torrent_info.id}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure session.torrent_info is not None before accessing attributes

Accessing session.torrent_info.alternative_filename assumes that session.torrent_info is not None. Add a check to prevent potential AttributeError.

Apply this fix:

+ if session.torrent_info:
    item.alternative_folder = session.torrent_info.alternative_filename
    item.active_stream = {"infohash": session.magnet, "id": session.torrent_info.id}
+ else:
+   raise HTTPException(status_code=500, detail="Torrent info is missing in the session")
📝 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
item.alternative_folder = session.torrent_info.alternative_filename
item.active_stream = {"infohash": session.magnet, "id": session.torrent_info.id}
if session.torrent_info:
item.alternative_folder = session.torrent_info.alternative_filename
item.active_stream = {"infohash": session.magnet, "id": session.torrent_info.id}
else:
raise HTTPException(status_code=500, detail="Torrent info is missing in the session")

Comment on lines +380 to +381
item_episode.alternative_folder = session.torrent_info.alternative_filename
item_episode.active_stream = {"infohash": session.magnet, "id": session.torrent_info.id}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add null check for session.torrent_info before accessing

Same as previous comment, ensure session.torrent_info is not None when accessing its attributes in the loop for episodes.

Apply this fix:

+ if session.torrent_info:
    item_episode.alternative_folder = session.torrent_info.alternative_filename
    item_episode.active_stream = {"infohash": session.magnet, "id": session.torrent_info.id}
+ else:
+   raise HTTPException(status_code=500, detail="Torrent info is missing in the session")
📝 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
item_episode.alternative_folder = session.torrent_info.alternative_filename
item_episode.active_stream = {"infohash": session.magnet, "id": session.torrent_info.id}
if session.torrent_info:
item_episode.alternative_folder = session.torrent_info.alternative_filename
item_episode.active_stream = {"infohash": session.magnet, "id": session.torrent_info.id}
else:
raise HTTPException(status_code=500, detail="Torrent info is missing in the session")

Copy link
Collaborator

@Gaisberg Gaisberg left a comment

Choose a reason for hiding this comment

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

LGTM

@dreulavelle dreulavelle merged commit 42829a2 into main Dec 5, 2024
1 of 2 checks passed
@dreulavelle dreulavelle deleted the hotfix_api branch December 5, 2024 22:25
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/program/services/scrapers/mediafusion.py (1)

Line range hint 73-109: Consider enhancing downloader configuration validation.

The current implementation validates downloader settings individually. Consider implementing a more robust validation approach:

  1. Create an enum or constant for supported downloaders
  2. Add version compatibility checks
  3. Validate API key format before encryption

Example enhancement:

from enum import Enum

class SupportedDownloader(Enum):
    REAL_DEBRID = "realdebrid"
    TORBOX = "torbox"
    ALL_DEBRID = "alldebrid"

def _validate_downloader_config(self) -> tuple[str, str]:
    """Validate and return (api_key, downloader_name)"""
    for downloader in SupportedDownloader:
        config = getattr(self.app_settings.downloaders, downloader.name.lower())
        if config.enabled:
            if not self._validate_api_key_format(config.api_key):
                raise ValueError(f"Invalid API key format for {downloader.name}")
            return config.api_key, downloader.value
    raise ValueError("No supported downloader enabled")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e318f93 and c779b68.

📒 Files selected for processing (1)
  • src/program/services/scrapers/mediafusion.py (1 hunks)
🔇 Additional comments (1)
src/program/services/scrapers/mediafusion.py (1)

69-71: LGTM! Verify API compatibility.

The addition of All-Debrid support follows the established pattern and is implemented correctly. However, let's verify that "alldebrid" is the expected identifier in the API.

✅ Verification successful

"alldebrid" identifier is correctly used and consistent across the codebase

The verification confirms that:

  • The identifier "alldebrid" is consistently used in the downloader implementation (AllDebridDownloader.key = "alldebrid")
  • The API base URL is correctly defined as "api.alldebrid.com"
  • The identifier matches the test data and API response formats
  • The implementation is well-tested with comprehensive test cases
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the "alldebrid" identifier usage in the codebase

# Check for any existing usage of the "alldebrid" identifier
rg -i "alldebrid" --type py

# Check for any API endpoint definitions or documentation
fd -e yaml -e json . | xargs rg -i "alldebrid"

Length of output: 6104

@arao
Copy link

arao commented Dec 9, 2024

TorrentContainer is a new structure, it's support need to be added in UI layer as well, currently with this change only, manual scrapping does not fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants