Patch together the election command for 2024

This commit is contained in:
Nexus 2024-07-05 00:11:56 +01:00
parent ae968be9de
commit a8e43fff03

View file

@ -45,55 +45,42 @@ class ElectionCog(commands.Cog):
self.log = logging.getLogger("jimmy.cogs.election") self.log = logging.getLogger("jimmy.cogs.election")
def process_soup(self, soup: BeautifulSoup) -> dict[str, list[int]] | None: def process_soup(self, soup: BeautifulSoup) -> dict[str, list[int]] | None:
good_soup = soup.find(attrs={"data-testid": "election-banner-results-bar"}) good_soups = list(soup.find_all(attrs={"data-testid": "election-banner-results-bar"}))
if not good_soup: if not good_soups:
return return
good_soup = list(good_soups)[1]
css: str = "\n".join([x.get_text() for x in soup.find_all("style")])
def find_colour(style_name: str, want: str = "background-color") -> str | None:
self.log.info("Looking for style %r", style_name)
index = css.index(style_name) + len(style_name) + 1
value = ""
for char in css[index:]:
if char == "}":
break
value += char
attributes = filter(None, value.split(";"))
parsed = {}
for attr in attributes:
name, val = attr.split(":")
parsed[name] = val
self.log.info("Parsed the following attributes: %r", parsed)
if want in parsed:
self.log.info("Returning %r: %r", want, parsed[want])
return parsed[want]
self.log.warning("%r was not in attributes.", want)
results: dict[str, list[int]] = {} results: dict[str, list[int]] = {}
for child_ul in good_soup.children: for child_li in good_soup.children:
child_ul: BeautifulSoup try:
span = child_ul.find("span", recursive=False) party, extra = child_li.get_text().strip().split(":", 1)
if not span: seats, extra = extra.split(",", 1)
self.log.warning("%r did not have a 'span' element.", child_ul) seats = int(seats.split()[0])
change = -1
except ValueError:
self.log.error("failed to parse %r", child_li)
continue continue
results[party] = [seats, change, 0]
# if not span:
# self.log.warning("%r did not have a 'span' element.", child_ul)
# continue
text = span.get_text().replace(",", "") # text = span.get_text().replace(",", "")
groups = SPAN_REGEX.match(text) # groups = SPAN_REGEX.match(text)
if groups: # if groups:
groups = groups.groupdict() # groups = groups.groupdict()
else: # else:
self.log.warning( # self.log.warning(
"Found span element (%r), however resolved text (%r) did not match regex.", # "Found span element (%r), however resolved text (%r) did not match regex.",
span, text # span, text
) # )
continue # continue
results[str(groups["party"]).strip()] = [ # results[str(groups["party"]).strip()] = [
int(groups["councillors"].strip()), # int(groups["councillors"].strip()),
int(groups["net"].strip()) * MULTI[groups["net_change"]], # int(groups["net"].strip()) * MULTI[groups["net_change"]],
int(find_colour(child_ul.next["class"][0])[1:], base=16) # int(find_colour(child_ul.next["class"][0])[1:], base=16)
] # ]
return results return results
@commands.slash_command(name="election") @commands.slash_command(name="election")