Skip to content

Commit 33e39a0

Browse files
committed
sync-tennis-stats: Add support for country remapping
Change-Id: I92044e1d151df39b83503fecf78973fdcf2bcc41
1 parent 61695f9 commit 33e39a0

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

majavahbot/tasks/task_sync_tennis_stats.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,34 @@ class Ranking:
3232
tied: bool
3333

3434

35+
def remap_country(
36+
name: str, country: Optional[str], overrides: Dict[str, Dict[str, Any]]
37+
) -> str:
38+
# Turn 'Bar, Foo' into 'Foo Bar'
39+
overrides_key = " ".join(name.split(", ", 1)[::-1])
40+
if overrides_key in overrides and "country" in overrides[overrides_key]:
41+
return overrides[overrides_key]["country"]
42+
43+
if not country:
44+
return ""
45+
46+
return country.lstrip(" (").rstrip(")")
47+
48+
3549
class SyncTennisStatsTask(Task):
3650
def __init__(self, number, name, site, family):
3751
super().__init__(number, name, site, family)
3852
self.register_task_configuration("User:MajavahBot/ATP rankings updater")
3953
self.merge_task_configuration(
4054
enable=True,
4155
summary="Bot: Updating rankings data",
56+
overrides_page="Module:ATP rankings/data/overrides.json",
4257
singles_result="Module:ATP rankings/data/singles.json",
4358
)
4459

45-
def remap_country(self, name: str, country: Optional[str]) -> str:
46-
# TODO: do something for Russia etc where the official stats don't have a country
47-
48-
if not country:
49-
return ""
50-
51-
return country.lstrip(" (").rstrip(")")
52-
53-
def download_and_parse(self, url: str) -> Tuple[List[Ranking], Optional[str]]:
60+
def download_and_parse(
61+
self, url: str, overrides: Dict[str, Dict[str, Any]]
62+
) -> Tuple[List[Ranking], Optional[str]]:
5463
f = None
5564
try:
5665
with NamedTemporaryFile(delete=False) as f:
@@ -84,8 +93,8 @@ def download_and_parse(self, url: str) -> Tuple[List[Ranking], Optional[str]]:
8493
players.append(
8594
Ranking(
8695
name=match.group("name"),
87-
country=self.remap_country(
88-
match.group("name"), match.group("country")
96+
country=remap_country(
97+
match.group("name"), match.group("country"), overrides
8998
),
9099
rank=int(match.group("rank")),
91100
tied=match.group("tied").strip() != "",
@@ -95,8 +104,10 @@ def download_and_parse(self, url: str) -> Tuple[List[Ranking], Optional[str]]:
95104

96105
return players, update_date
97106

98-
def process_pdf(self, url: str, target_page: str):
99-
players, update_date = self.download_and_parse(url)
107+
def process_pdf(
108+
self, url: str, target_page: str, overrides: Dict[str, Dict[str, Any]]
109+
):
110+
players, update_date = self.download_and_parse(url, overrides)
100111
LOGGER.info("Formatting rankings for the required on-wiki format")
101112

102113
per_country: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
@@ -148,7 +159,15 @@ def run(self):
148159
LOGGER.error("Disabled in configuration")
149160
return
150161

151-
self.process_pdf(SINGLES_PDF_URL, self.get_task_configuration("singles_result"))
162+
overrides = json.loads(
163+
self.get_mediawiki_api()
164+
.get_page(self.get_task_configuration("overrides_page"))
165+
.get()
166+
)
167+
168+
self.process_pdf(
169+
SINGLES_PDF_URL, self.get_task_configuration("singles_result"), overrides
170+
)
152171

153172

154173
task_registry.add_task(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
from majavahbot.tasks.task_sync_tennis_stats import remap_country
4+
5+
OVERRIDES = {
6+
"Foo Bar": {
7+
"article": "Foo Bar (that Tennis player)",
8+
},
9+
"Foo Baz": {
10+
"country": "NO",
11+
},
12+
}
13+
14+
15+
@pytest.mark.parametrize(
16+
"country, expected",
17+
[
18+
[None, ""],
19+
["", ""],
20+
["YES", "YES"],
21+
[" (YES)", "YES"],
22+
],
23+
)
24+
def test_remap_country_simple(country: str, expected: str):
25+
assert remap_country("Does Not Matter", country, {}) == expected
26+
27+
28+
def test_remap_country_override():
29+
assert remap_country("Baz, Foo", " (YES)", OVERRIDES) == "NO"

0 commit comments

Comments
 (0)