-
Notifications
You must be signed in to change notification settings - Fork 48
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
Add TransfermarktPlayerInjuries service #49
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
32338ca
Added /players/{player_id}/injuries endpoint
bjethwan 6ecb9ca
Make isort and ruff happy :)
felipeall 7a94286
Update TransfermarktPlayerInjuries service
felipeall 206962a
Add docstrings
felipeall 89e2ed2
Add unit tests
felipeall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from typing import List, Optional | ||
from xml.etree import ElementTree | ||
|
||
from app.services.base import TransfermarktBase | ||
from app.utils.utils import clean_response, extract_from_url, trim | ||
from app.utils.xpath import Players | ||
|
||
|
||
@dataclass | ||
class TransfermarktPlayerInjuries(TransfermarktBase): | ||
player_id: str = None | ||
URL: str = "https://www.transfermarkt.com/player/verletzungen/spieler/{player_id}/plus/1/page/{page_number}" | ||
page_number: int = 1 | ||
|
||
def __post_init__(self): | ||
self.URL = self.URL.format(player_id=self.player_id, page_number=self.page_number) | ||
self.page = self.request_url_page() | ||
self.raise_exception_if_not_found(xpath=Players.Profile.URL) | ||
|
||
def __parse_player_injuries(self) -> Optional[List[dict]]: | ||
injuries: ElementTree = self.page.xpath(Players.Injuries.RESULTS) | ||
player_injuries = [] | ||
|
||
for injury in injuries: | ||
season = trim(injury.xpath(Players.Injuries.SEASONS)) | ||
injury_type = trim(injury.xpath(Players.Injuries.INJURY)) | ||
date_from = trim(injury.xpath(Players.Injuries.FROM)) | ||
date_until = trim(injury.xpath(Players.Injuries.UNTIL)) | ||
days = trim(injury.xpath(Players.Injuries.DAYS)) | ||
games_missed = trim(injury.xpath(Players.Injuries.GAMES_MISSED)) | ||
games_missed_clubs_urls = injury.xpath(Players.Injuries.GAMES_MISSED_CLUBS_URLS) | ||
games_missed_clubs_ids = [extract_from_url(club_url) for club_url in games_missed_clubs_urls] | ||
|
||
player_injuries.append( | ||
{ | ||
"season": season, | ||
"injury": injury_type, | ||
"from": date_from, | ||
"until": date_until, | ||
"days": days, | ||
"gamesMissed": games_missed, | ||
"gamesMissedClubs": games_missed_clubs_ids, | ||
}, | ||
) | ||
|
||
return player_injuries | ||
|
||
def get_player_injuries(self) -> dict: | ||
self.response["id"] = self.player_id | ||
self.response["pageNumber"] = self.page_number | ||
self.response["lastPageNumber"] = self.get_last_page_number() | ||
self.response["injuries"] = self.__parse_player_injuries() | ||
self.response["updatedAt"] = datetime.now() | ||
|
||
return clean_response(self.response) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
TransfermarktPlayerInjuries
class is well-structured and follows the inheritance pattern fromTransfermarktBase
. The use of utility functions and XPath expressions from other modules is consistent with the application's design. The methods__post_init__
,__parse_player_injuries
, andget_player_injuries
are clearly defined and seem to follow the intended logic for fetching and parsing player injury data.However, there are a few points to consider:
The
player_id
attribute is initialized asNone
by default (line 13), which could lead to a malformed URL if not provided. It might be safer to requireplayer_id
as a mandatory parameter during initialization to avoid potential runtime errors.The
__parse_player_injuries
method is prefixed with double underscores, which in Python indicates a name mangling to create a private version of the variable or method. If this method is intended to be used only within this class, this is fine. However, if it is meant to be accessed from outside, consider renaming it with a single underscore to indicate it is a protected member of the class.The
get_player_injuries
method updates theself.response
dictionary directly (lines 51-55). Ensure thatself.response
is defined in the base class or within this class to avoid anAttributeError
.The
datetime.now()
method used in line 55 does not include timezone information. It's recommended to usedatetime.now(timezone.utc)
to have timezone-aware timestamps, which are generally more robust for applications that may run in different time zones or that store data that could be accessed from multiple time zones.The
clean_response
utility function is used to return the response (line 57). Ensure that this function is thoroughly tested, especially since it's handling the output of the service which could be critical for the application's functionality.Error handling is not visible in the provided hunk. It's important to ensure that there are proper error handling mechanisms in place, especially for network calls and XPath queries which can fail or return unexpected results.
The use of XPath queries (lines 23-34) assumes that the structure of the HTML from
transfermarkt.com
will not change. If the website's layout changes, these queries may break. It's advisable to have a fallback mechanism or a monitoring system to alert when the data extraction process fails.The method
get_last_page_number
is called without being defined within this class (line 53). It's assumed to be a method from the base classTransfermarktBase
. Ensure that this method exists and is correctly implemented in the base class.The
extract_from_url
utility function is used to extract club IDs from URLs (line 34). Ensure that this function is robust and can handle any edge cases or unexpected URL formats.The
self.request_url_page()
method is called in the__post_init__
method (line 19). Ensure that this method includes proper exception handling for HTTP errors or connection issues.Overall, the class seems to be well-integrated with the rest of the application, and the methods are consistent with the PR's objectives. The above points are mainly for ensuring robustness and maintainability of the code.