transfermarkt-api icon indicating copy to clipboard operation
transfermarkt-api copied to clipboard

Transfer API Improvements

Open Emmett-H opened this issue 11 months ago • 2 comments

First of all, thank you for the hard work on this project, Transfermarkt is an incredible source of up to date data and I'm intending on self hosting this and using it for a wider project soon.

During my testing I noticed the transfers endpoint was a bit broken, returning 500s when it came to different types of loans and loan fees etc. I believe there was some HTML tags getting parsed into the responses when only an integer was expected. There was also no way to differentiate between the different types of transfer, I feel like this is quite an important distinction and it would be really useful for me going forward. I have implemented some changes to the transfer response to make different transfer types obvious.

Please let me know if theres any issues or corrections you would like me to do. Thanks again.

Initial Issues

500 Error with HTML in Fee Values: The API was failing with a 500 error when processing transfers with special fee values like "Loan fee" or "End of loan" because it was receiving HTML-formatted strings that couldn't be converted to numbers. Missing Transfer Information: Important transfer context like loan returns and loan transfers was being lost due to validation errors, as the schema expected numeric values for fees. Inconsistent Data Representation: There was no standardised way to represent different types of transfers (permanent, loans, end of loans, free transfers). Steps Taken to Fix the Issues HTML Cleaning: Added BeautifulSoup to parse and clean HTML tags from fee values Extracted the meaningful text content from the HTML-formatted strings

Loan handling: Added logic to identify and preserve special text values like "End of loan" and "loan transfer" Created a system to properly handle these values without causing validation errors

Fee Value Parsing: Implemented proper parsing of currency values with different formats (€, k, m suffixes) Converted string fee values to integers for consistent representation Made the fee field truly optional, only including it when a value is present

Additional Fields Added

Transfer Type Field:

Added a new transferType field to explicitly categorize transfers

Implemented four standard transfer types:

permanent: Regular transfers with fees loan: Temporary transfers, with or without fees end_of_loan: Returns to parent clubs after loan periods free_transfer: Transfers without fees, including youth promotions Schema Updates: Updated the schema to include the new transferType field with appropriate validation Ensured the schema properly handles optional fee values

Test Improvements:

Updated tests to verify that all transfers have appropriate transfer types

These improvements ensure the API now provides comprehensive and accurate transfer information for all players, regardless of their transfer history complexity, while maintaining a consistent and well-structured response format.

Summary by CodeRabbit

  • New Features
    • Player transfers now display a clearly defined transfer type, categorizing each transfer as permanent, loan, end-of-loan, or free transfer.
    • Enhanced transfer fee processing improves the accuracy of displayed transfer details.
    • Robust data validation guarantees consistent and reliable transfer information for a smoother user experience.

Emmett-H avatar Mar 05 '25 22:03 Emmett-H

Walkthrough

The pull request introduces enhancements to the player transfer modules. A new attribute, transfer_type, is added to the player transfer schema to explicitly categorize transfer types. In the transfer service, two new private methods are implemented to clean HTML fee values and determine transfer types during fee processing. Additionally, the testing suite is updated to validate the new transferType field, ensuring it adheres to the specified allowed values.

Changes

File(s) Summary of Changes
app/schemas/players/transfers.py Added transfer_type: Literal["permanent", "loan", "end_of_loan", "free_transfer"] attribute to the PlayerTransfer class and the corresponding Literal import.
app/services/players/transfers.py Introduced two private methods: __clean_html_value for stripping HTML and extracting fee and transfer type, and __process_fee_and_type to integrate this processing; updated __parse_player_transfer_history to use the new processing logic.
tests/players/test_players_transfers.py Updated schema validation for transferType by adding the Or function to enforce specific string values and added assertions to ensure all transfers include a non-null transferType.

Sequence Diagram(s)

sequenceDiagram
    participant T as Test
    participant TP as TransfermarktPlayerTransfers
    T->>TP: Call get_player_transfers()
    TP->>TP: __parse_player_transfer_history()
    TP->>TP: __process_fee_and_type(fee_value)
    TP->>TP: __clean_html_value(html_value)
    TP-->>TP: Return cleaned fee and transfer type
    TP-->>T: Return processed transfer data

Poem

Hey, I'm a bunny, hopping with glee,
Code fields now dance, so fresh and free.
Transfer types are clear as a sunny day,
Fees get cleaned in a smart new way.
I nibble on changes with a joyful play!
🐰✨

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this 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.

coderabbitai[bot] avatar Mar 05 '25 22:03 coderabbitai[bot]

Apologies created this a week ago and have just closed it. Needed to use the main branch in my fork for a few things. Let me know your thoughts when you get a chance to have a look @felipeall, TIA 🙏

Emmett-H avatar Mar 05 '25 22:03 Emmett-H