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

Parse player transfer history from Tfmkt endpoint #45

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
63 changes: 25 additions & 38 deletions app/services/players/transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,45 @@ class TransfermarktPlayerTransfers(TransfermarktBase):

player_id: str = None
URL: str = "https://www.transfermarkt.com/-/transfers/spieler/{player_id}"
URL_TRANSFERS: str = "https://www.transfermarkt.com/ceapi/transferHistory/list/{player_id}"

def __post_init__(self) -> None:
"""Initialize the TransfermarktPlayerTransfers class."""
self.URL = self.URL.format(player_id=self.player_id)
self.page = self.request_url_page()
self.raise_exception_if_not_found(xpath=Players.Profile.NAME)
self.transfer_history = self.make_request(url=self.URL_TRANSFERS.format(player_id=self.player_id))

def __parse_player_transfers_history(self) -> list:
def __parse_player_transfer_history(self) -> list:
"""
Parse and retrieve the transfer history of the specified player from Transfermarkt.
Parse and retrieve the transfer history of the specified player from Transfermarkt,
including the unique identifier of each transfer, source club information (ID and name),
destination club information (ID and name), transfer date, upcoming status, season, market
value at the time of transfer, and transfer fee.

Returns:
list: A list of dictionaries containing details of player transfers, including season, date, old club,
new club, market value, and transfer fee.
list: A list of dictionaries, each containing details of the player's transfer history,
"""
urls = self.get_list_by_xpath(Players.Transfers.TRANSFERS_URLS)
seasons = self.get_list_by_xpath(Players.Transfers.SEASONS)
dates = self.get_list_by_xpath(Players.Transfers.DATES)
old_clubs_urls = self.get_list_by_xpath(Players.Transfers.OLD_CLUBS_URLS)
old_clubs_names = self.get_list_by_xpath(Players.Transfers.OLD_CLUBS_NAMES)
new_clubs_urls = self.get_list_by_xpath(Players.Transfers.NEW_CLUBS_URLS)
new_clubs_names = self.get_list_by_xpath(Players.Transfers.NEW_CLUBS_NAMES)
market_values = self.get_list_by_xpath(Players.Transfers.MARKET_VALUES)
fees = self.get_list_by_xpath(Players.Transfers.FEES)

ids = [extract_from_url(url, "transfer_id") for url in urls]
old_clubs_ids = [extract_from_url(url) for url in old_clubs_urls]
new_clubs_ids = [extract_from_url(url) for url in new_clubs_urls]
transfers = self.transfer_history.json().get("transfers")

return [
{
"id": idx,
"seasonID": season,
"date": date,
"oldClubID": from_club_id,
"oldClubName": from_club_name,
"newClubID": to_club_id,
"newClubName": to_club_name,
"marketValue": market_value,
"fee": fee,
"id": extract_from_url(transfer["url"], "transfer_id"),
"from": {
"clubID": extract_from_url(transfer["from"]["href"]),
"clubName": transfer["from"]["clubName"],
},
"to": {
"clubID": extract_from_url(transfer["to"]["href"]),
"clubName": transfer["to"]["clubName"],
},
"date": transfer["date"],
"upcoming": transfer["upcoming"],
"season": transfer["season"],
"marketValue": transfer["marketValue"],
"fee": transfer["fee"],
}
for idx, season, date, from_club_id, from_club_name, to_club_id, to_club_name, market_value, fee in zip(
ids,
seasons,
dates,
old_clubs_ids,
old_clubs_names,
new_clubs_ids,
new_clubs_names,
market_values,
fees,
)
for transfer in transfers
]

def get_player_transfers(self) -> dict:
Expand All @@ -85,7 +72,7 @@ def get_player_transfers(self) -> dict:
and the timestamp of when the data was last updated.
"""
self.response["id"] = self.player_id
self.response["transfers"] = self.__parse_player_transfers_history()
self.response["transfers"] = self.__parse_player_transfer_history()
self.response["youthClubs"] = safe_split(self.get_text_by_xpath(Players.Transfers.YOUTH_CLUBS), ",")
self.response["updatedAt"] = datetime.now()

Expand Down
23 changes: 0 additions & 23 deletions app/utils/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,6 @@ class MarketValue:
RANKINGS_POSITIONS = "//span[contains(@class, 'quick-fact__content--large')]//text()"

class Transfers:
TRANSFERS_URLS = "//a[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__link']//@href"
SEASONS = "//div[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__season']//text()"
DATES = "//div[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__date']//text()"
OLD_CLUBS_URLS = (
"//div[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__old-club']"
"/a[@class='tm-player-transfer-history-grid__club-link']/@href"
)
OLD_CLUBS_NAMES = (
"//div[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__old-club']"
"/a[@class='tm-player-transfer-history-grid__club-link']/text()"
)
NEW_CLUBS_URLS = (
"//div[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__new-club']"
"/a[@class='tm-player-transfer-history-grid__club-link']/@href"
)
NEW_CLUBS_NAMES = (
"//div[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__new-club']"
"/a[@class='tm-player-transfer-history-grid__club-link']/text()"
)
MARKET_VALUES = (
"//div[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__market-value']//text()"
)
FEES = "//div[@class='grid__cell grid__cell--center tm-player-transfer-history-grid__fee']//text()"
YOUTH_CLUBS = "//div[@data-viewport='Jugendvereine']//div//text()"

class Stats:
Expand Down
15 changes: 10 additions & 5 deletions tests/players/test_players_transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ def test_get_player_transfers(player_id, len_greater_than_0, regex_integer, rege
"transfers": [
{
"id": And(str, len_greater_than_0, regex_integer),
"seasonID": And(str, len_greater_than_0),
"season": And(str, len_greater_than_0),
"date": And(str, len_greater_than_0, regex_date_mmm_dd_yyyy),
"oldClubID": And(str, len_greater_than_0, regex_integer),
"oldClubName": And(str, len_greater_than_0),
"newClubID": And(str, len_greater_than_0, regex_integer),
"newClubName": And(str, len_greater_than_0),
"from": {
"clubID": And(str, len_greater_than_0, regex_integer),
"clubName": And(str, len_greater_than_0),
},
"to": {
"clubID": And(str, len_greater_than_0, regex_integer),
"clubName": And(str, len_greater_than_0),
},
"upcoming": bool,
Optional("marketValue"): And(str, len_greater_than_0, regex_market_value),
Optional("fee"): And(str, len_greater_than_0),
},
Expand Down
Loading