|
1 | | -from dataclasses import dataclass, field |
| 1 | +from dataclasses import dataclass |
| 2 | +from datetime import datetime |
2 | 3 |
|
3 | | -from fastapi import HTTPException |
4 | | - |
5 | | -from app.utils.utils import ( |
6 | | - clean_response, |
7 | | - extract_from_url, |
8 | | - get_list_by_xpath, |
9 | | - get_text_by_xpath, |
10 | | - request_url_page, |
11 | | - safe_regex, |
12 | | -) |
| 4 | +from app.services.base import TransfermarktBase |
| 5 | +from app.utils.regex import REGEX_DOB |
| 6 | +from app.utils.utils import clean_response, extract_from_url, safe_regex |
13 | 7 | from app.utils.xpath import Clubs |
14 | 8 |
|
15 | 9 |
|
16 | 10 | @dataclass |
17 | | -class TransfermarktClubPlayers: |
18 | | - club_id: str |
| 11 | +class TransfermarktClubPlayers(TransfermarktBase): |
| 12 | + club_id: str = None |
19 | 13 | season_id: str = None |
20 | | - club_players: dict = field(default_factory=lambda: {}) |
21 | | - |
22 | | - def get_club_players(self) -> dict: |
23 | | - self._request_page() |
24 | | - self._update_season_id() |
25 | | - self._update_club_in_past() |
26 | | - |
27 | | - self.club_players["id"] = self.club_id |
28 | | - self.club_players["clubName"] = get_text_by_xpath(self, Clubs.Players.CLUB_NAME) |
29 | | - |
30 | | - self._check_club_found() |
31 | | - |
32 | | - self.club_players["seasonYear"] = self.season_id |
33 | | - self.club_players["players"] = self._parse_club_players() |
34 | | - |
35 | | - return clean_response(self.club_players) |
| 14 | + URL: str = "https://www.transfermarkt.com/-/kader/verein/{club_id}/saison_id/{season_id}/plus/1" |
36 | 15 |
|
37 | | - def _request_page(self) -> None: |
38 | | - club_players_url = ( |
39 | | - f"https://www.transfermarkt.com/-/kader/verein/{self.club_id}/saison_id/{self.season_id}/plus/1" |
40 | | - ) |
41 | | - self.page = request_url_page(url=club_players_url) |
| 16 | + def __post_init__(self): |
| 17 | + self.URL = self.URL.format(club_id=self.club_id, season_id=self.season_id) |
| 18 | + self.page = self.request_url_page() |
| 19 | + self.raise_exception_if_not_found(xpath=Clubs.Players.CLUB_NAME) |
| 20 | + self.__update_season_id() |
| 21 | + self.__update_past_flag() |
42 | 22 |
|
43 | | - def _update_season_id(self): |
| 23 | + def __update_season_id(self): |
44 | 24 | if self.season_id is None: |
45 | | - self.season_id = extract_from_url(get_text_by_xpath(self, Clubs.Players.CLUB_URL), "season_id") |
| 25 | + self.season_id = extract_from_url(self.get_text_by_xpath(Clubs.Players.CLUB_URL), "season_id") |
46 | 26 |
|
47 | | - def _update_club_in_past(self): |
48 | | - self.past = "Current club" in get_list_by_xpath(self, Clubs.Players.PAST_FLAG) |
| 27 | + def __update_past_flag(self): |
| 28 | + self.past = "Current club" in self.get_list_by_xpath(Clubs.Players.PAST_FLAG) |
49 | 29 |
|
50 | | - def _parse_club_players(self) -> list: |
| 30 | + def __parse_club_players(self) -> list: |
51 | 31 | page_nationalities = self.page.xpath(Clubs.Players.PAGE_NATIONALITIES) |
52 | 32 | page_players_infos = self.page.xpath(Clubs.Players.PAGE_INFOS) |
53 | 33 | page_players_signed_from = self.page.xpath( |
54 | 34 | Clubs.Players.Past.PAGE_SIGNED_FROM if self.past else Clubs.Players.Present.PAGE_SIGNED_FROM, |
55 | 35 | ) |
56 | | - |
57 | | - players_urls: list = get_list_by_xpath(self, Clubs.Players.URLS) |
58 | | - players_ids: list = [extract_from_url(url) for url in players_urls] |
59 | | - |
60 | | - players_names: list = get_list_by_xpath(self, Clubs.Players.NAMES) |
61 | | - |
62 | | - players_positions: list = get_list_by_xpath(self, Clubs.Players.POSITIONS) |
63 | | - players_dobs: list = [ |
64 | | - safe_regex(dob_age, r"^(?P<dob>.*)\s\((?P<age>\d*)\)", "dob") |
65 | | - for dob_age in get_list_by_xpath(self, Clubs.Players.DOB_AGE) |
| 36 | + players_ids = [extract_from_url(url) for url in self.get_list_by_xpath(Clubs.Players.URLS)] |
| 37 | + players_names = self.get_list_by_xpath(Clubs.Players.NAMES) |
| 38 | + players_positions = self.get_list_by_xpath(Clubs.Players.POSITIONS) |
| 39 | + players_dobs = [ |
| 40 | + safe_regex(dob_age, REGEX_DOB, "dob") for dob_age in self.get_list_by_xpath(Clubs.Players.DOB_AGE) |
66 | 41 | ] |
67 | | - players_ages: list = [ |
68 | | - safe_regex(dob_age, r"^(?P<dob>.*)\s\((?P<age>\d*)\)", "age") |
69 | | - for dob_age in get_list_by_xpath(self, Clubs.Players.DOB_AGE) |
70 | | - ] |
71 | | - players_nationalities: list = [ |
72 | | - nationality.xpath(Clubs.Players.NATIONALITIES) for nationality in page_nationalities |
| 42 | + players_ages = [ |
| 43 | + safe_regex(dob_age, REGEX_DOB, "age") for dob_age in self.get_list_by_xpath(Clubs.Players.DOB_AGE) |
73 | 44 | ] |
| 45 | + players_nationalities = [nationality.xpath(Clubs.Players.NATIONALITIES) for nationality in page_nationalities] |
74 | 46 | players_current_club = ( |
75 | | - get_list_by_xpath(self, Clubs.Players.Past.CURRENT_CLUB) if self.past else [None] * len(players_ids) |
| 47 | + self.get_list_by_xpath(Clubs.Players.Past.CURRENT_CLUB) if self.past else [None] * len(players_ids) |
76 | 48 | ) |
77 | | - players_heights: list = get_list_by_xpath( |
78 | | - self, |
| 49 | + players_heights = self.get_list_by_xpath( |
79 | 50 | Clubs.Players.Past.HEIGHTS if self.past else Clubs.Players.Present.HEIGHTS, |
80 | 51 | ) |
81 | | - players_foots: list = get_list_by_xpath( |
82 | | - self, |
| 52 | + players_foots = self.get_list_by_xpath( |
83 | 53 | Clubs.Players.Past.FOOTS if self.past else Clubs.Players.Present.FOOTS, |
84 | 54 | remove_empty=False, |
85 | 55 | ) |
86 | | - players_joined_on: list = get_list_by_xpath( |
87 | | - self, |
| 56 | + players_joined_on = self.get_list_by_xpath( |
88 | 57 | Clubs.Players.Past.JOINED_ON if self.past else Clubs.Players.Present.JOINED_ON, |
89 | 58 | ) |
90 | | - players_joined: list = ["; ".join(e.xpath(Clubs.Players.JOINED)) for e in page_players_infos] |
91 | | - players_signed_from: list = ["; ".join(e.xpath(Clubs.Players.SIGNED_FROM)) for e in page_players_signed_from] |
92 | | - players_contracts: list = get_list_by_xpath( |
93 | | - self, |
94 | | - Clubs.Players.Past.CONTRACTS if self.past else Clubs.Players.Present.CONTRACTS, |
| 59 | + players_joined = ["; ".join(e.xpath(Clubs.Players.JOINED)) for e in page_players_infos] |
| 60 | + players_signed_from = ["; ".join(e.xpath(Clubs.Players.SIGNED_FROM)) for e in page_players_signed_from] |
| 61 | + players_contracts = ( |
| 62 | + [None] * len(players_ids) if self.past else self.get_list_by_xpath(Clubs.Players.Past.CONTRACTS) |
95 | 63 | ) |
96 | | - players_marketvalues: list = get_list_by_xpath(self, Clubs.Players.MARKET_VALUES) |
97 | | - players_statuses: list = ["; ".join(e.xpath(Clubs.Players.STATUSES)) for e in page_players_infos] |
| 64 | + players_marketvalues = self.get_list_by_xpath(Clubs.Players.MARKET_VALUES) |
| 65 | + players_statuses = ["; ".join(e.xpath(Clubs.Players.STATUSES)) for e in page_players_infos] |
98 | 66 |
|
99 | 67 | return [ |
100 | 68 | { |
@@ -133,6 +101,9 @@ def _parse_club_players(self) -> list: |
133 | 101 | ) |
134 | 102 | ] |
135 | 103 |
|
136 | | - def _check_club_found(self) -> None: |
137 | | - if not self.club_players["clubName"]: |
138 | | - raise HTTPException(status_code=404, detail=f"Club Players not found for id: {self.club_id}") |
| 104 | + def get_club_players(self) -> dict: |
| 105 | + self.response["id"] = self.club_id |
| 106 | + self.response["players"] = self.__parse_club_players() |
| 107 | + self.response["updatedAt"] = datetime.now() |
| 108 | + |
| 109 | + return clean_response(self.response) |
0 commit comments