Patch together the election command for 2024
This commit is contained in:
parent
ae968be9de
commit
a8e43fff03
1 changed files with 30 additions and 43 deletions
|
@ -45,55 +45,42 @@ class ElectionCog(commands.Cog):
|
|||
self.log = logging.getLogger("jimmy.cogs.election")
|
||||
|
||||
def process_soup(self, soup: BeautifulSoup) -> dict[str, list[int]] | None:
|
||||
good_soup = soup.find(attrs={"data-testid": "election-banner-results-bar"})
|
||||
if not good_soup:
|
||||
good_soups = list(soup.find_all(attrs={"data-testid": "election-banner-results-bar"}))
|
||||
if not good_soups:
|
||||
return
|
||||
|
||||
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)
|
||||
good_soup = list(good_soups)[1]
|
||||
|
||||
results: dict[str, list[int]] = {}
|
||||
for child_ul in good_soup.children:
|
||||
child_ul: BeautifulSoup
|
||||
span = child_ul.find("span", recursive=False)
|
||||
if not span:
|
||||
self.log.warning("%r did not have a 'span' element.", child_ul)
|
||||
for child_li in good_soup.children:
|
||||
try:
|
||||
party, extra = child_li.get_text().strip().split(":", 1)
|
||||
seats, extra = extra.split(",", 1)
|
||||
seats = int(seats.split()[0])
|
||||
change = -1
|
||||
except ValueError:
|
||||
self.log.error("failed to parse %r", child_li)
|
||||
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(",", "")
|
||||
groups = SPAN_REGEX.match(text)
|
||||
if groups:
|
||||
groups = groups.groupdict()
|
||||
else:
|
||||
self.log.warning(
|
||||
"Found span element (%r), however resolved text (%r) did not match regex.",
|
||||
span, text
|
||||
)
|
||||
continue
|
||||
# text = span.get_text().replace(",", "")
|
||||
# groups = SPAN_REGEX.match(text)
|
||||
# if groups:
|
||||
# groups = groups.groupdict()
|
||||
# else:
|
||||
# self.log.warning(
|
||||
# "Found span element (%r), however resolved text (%r) did not match regex.",
|
||||
# span, text
|
||||
# )
|
||||
# continue
|
||||
|
||||
results[str(groups["party"]).strip()] = [
|
||||
int(groups["councillors"].strip()),
|
||||
int(groups["net"].strip()) * MULTI[groups["net_change"]],
|
||||
int(find_colour(child_ul.next["class"][0])[1:], base=16)
|
||||
]
|
||||
# results[str(groups["party"]).strip()] = [
|
||||
# int(groups["councillors"].strip()),
|
||||
# int(groups["net"].strip()) * MULTI[groups["net_change"]],
|
||||
# int(find_colour(child_ul.next["class"][0])[1:], base=16)
|
||||
# ]
|
||||
return results
|
||||
|
||||
@commands.slash_command(name="election")
|
||||
|
|
Loading…
Reference in a new issue