Fix async prompt in sfp.search_for_resource
This commit is contained in:
parent
ce0459c832
commit
b0a9fecde5
|
@ -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:
|
||||
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:
|
||||
return
|
||||
else:
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
from concurrent.futures import Future
|
||||
from contextlib import AbstractContextManager
|
||||
from datetime import datetime
|
||||
from typing import Optional, Callable
|
||||
|
@ -51,7 +52,7 @@ class SatisfactoryPlus(RecipeProvider, AbstractContextManager):
|
|||
|
||||
def search_for_resource(
|
||||
self, session: AlchemySession, search: str, prompt: Callable
|
||||
) -> tuple[Resource, bool] | None:
|
||||
) -> Future[tuple[Resource, bool] | None]:
|
||||
browser = self._init_browser()
|
||||
browser.get("https://wiki.kyrium.space/")
|
||||
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.click()
|
||||
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:
|
||||
print("No wiki entries found for this result")
|
||||
return None
|
||||
ret.set_result(None)
|
||||
|
||||
elif len(choices) > 1:
|
||||
default_choice = 1
|
||||
|
@ -73,20 +86,21 @@ class SatisfactoryPlus(RecipeProvider, AbstractContextManager):
|
|||
options[idx + 1] = name
|
||||
if name.casefold() == search.casefold():
|
||||
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")
|
||||
resource = session.scalars(Resource.by_label(alt_resource_label)).one_or_none()
|
||||
if resource:
|
||||
return resource, True
|
||||
def user_choice_cb(fut: Future):
|
||||
user_choice = fut.result()
|
||||
if user_choice is None:
|
||||
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:
|
||||
resource_fetch_url = self._normalize_url(href=link_html_elem.get_attribute("href"))
|
||||
return Resource(label=alt_resource_label, uri=resource_fetch_url), False
|
||||
process_link_html_elem(link_html_elem=choices[0])
|
||||
|
||||
return ret
|
||||
|
||||
def update_resource_recipes(self, session: AlchemySession, resource: Resource) -> Resource:
|
||||
assert resource.uri, "Resource.uri not set"
|
||||
|
|
Loading…
Reference in a new issue