@@ -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+
3549class 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
154173task_registry .add_task (
0 commit comments