Fix async prompt in sfp.search_for_resource

This commit is contained in:
Ben 2024-02-02 22:04:03 +01:00
parent ce0459c832
commit b0a9fecde5
Signed by: ben
GPG key ID: 0F54A7ED232D3319
2 changed files with 29 additions and 15 deletions

View file

@ -28,7 +28,7 @@ def main(debug: bool, refetch: bool, ignore_factories: list[str], search: str):
with SatisfactoryPlus(ignore_factories=ignore_factories, debug=debug) as data_provider: with SatisfactoryPlus(ignore_factories=ignore_factories, debug=debug) as data_provider:
if resource is None: if resource is None:
ret = data_provider.search_for_resource(session=session, search=search, prompt=click_prompt) ret = data_provider.search_for_resource(session=session, search=search, prompt=click_prompt).result()
if ret is None: if ret is None:
return return
else: else:

View file

@ -1,3 +1,4 @@
from concurrent.futures import Future
from contextlib import AbstractContextManager from contextlib import AbstractContextManager
from datetime import datetime from datetime import datetime
from typing import Optional, Callable from typing import Optional, Callable
@ -51,7 +52,7 @@ class SatisfactoryPlus(RecipeProvider, AbstractContextManager):
def search_for_resource( def search_for_resource(
self, session: AlchemySession, search: str, prompt: Callable self, session: AlchemySession, search: str, prompt: Callable
) -> tuple[Resource, bool] | None: ) -> Future[tuple[Resource, bool] | None]:
browser = self._init_browser() browser = self._init_browser()
browser.get("https://wiki.kyrium.space/") browser.get("https://wiki.kyrium.space/")
search_bar = browser.find_element(By.CSS_SELECTOR, "nav input[placeholder='Search for an item...']") search_bar = browser.find_element(By.CSS_SELECTOR, "nav input[placeholder='Search for an item...']")
@ -60,9 +61,21 @@ class SatisfactoryPlus(RecipeProvider, AbstractContextManager):
search_button = browser.find_element(By.CSS_SELECTOR, "nav button[type='submit']") search_button = browser.find_element(By.CSS_SELECTOR, "nav button[type='submit']")
search_button.click() search_button.click()
choices = browser.find_elements(By.CSS_SELECTOR, "body > div > .container:nth-child(1) a.items-center") choices = browser.find_elements(By.CSS_SELECTOR, "body > div > .container:nth-child(1) a.items-center")
ret = Future()
def process_link_html_elem(link_html_elem):
alt_resource_label = link_html_elem.find_element(By.TAG_NAME, "img").get_attribute("alt")
resource = session.scalars(Resource.by_label(alt_resource_label)).one_or_none()
if resource:
ret.set_result((resource, True))
else:
resource_fetch_url = self._normalize_url(href=link_html_elem.get_attribute("href"))
ret.set_result((Resource(label=alt_resource_label, uri=resource_fetch_url), False))
if not choices: if not choices:
print("No wiki entries found for this result") print("No wiki entries found for this result")
return None ret.set_result(None)
elif len(choices) > 1: elif len(choices) > 1:
default_choice = 1 default_choice = 1
@ -73,20 +86,21 @@ class SatisfactoryPlus(RecipeProvider, AbstractContextManager):
options[idx + 1] = name options[idx + 1] = name
if name.casefold() == search.casefold(): if name.casefold() == search.casefold():
default_choice = idx + 1 default_choice = idx + 1
user_choice = prompt(options=options, text="Chose a recipe to continue…", default=default_choice)
if user_choice is None:
return None
link_html_elem = choices[user_choice - 1]
else:
link_html_elem = choices[0]
alt_resource_label = link_html_elem.find_element(By.TAG_NAME, "img").get_attribute("alt") def user_choice_cb(fut: Future):
resource = session.scalars(Resource.by_label(alt_resource_label)).one_or_none() user_choice = fut.result()
if resource: if user_choice is None:
return resource, True ret.set_result(None)
else:
process_link_html_elem(link_html_elem=choices[user_choice - 1])
prompt(options=options, text="Chose a recipe to continue…", default=default_choice).add_done_callback(
user_choice_cb
)
else: else:
resource_fetch_url = self._normalize_url(href=link_html_elem.get_attribute("href")) process_link_html_elem(link_html_elem=choices[0])
return Resource(label=alt_resource_label, uri=resource_fetch_url), False
return ret
def update_resource_recipes(self, session: AlchemySession, resource: Resource) -> Resource: def update_resource_recipes(self, session: AlchemySession, resource: Resource) -> Resource:
assert resource.uri, "Resource.uri not set" assert resource.uri, "Resource.uri not set"