SatisfactoryPlusCalculator/factorygame/calculator/SatisfactoryCalculator.py

130 lines
5.2 KiB
Python
Raw Normal View History

2024-01-28 13:12:46 +00:00
#!/usr/bin/env python3
import time
from collections import namedtuple
import click
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
Recipe = namedtuple("Recipe", ["result_count", "ingredients", "time"])
Ingredient = namedtuple("Ingredient", ["count", "name"])
@click.command()
@click.option("--result", type=str, required=True)
@click.option("--debug", is_flag=True, default=False, required=False)
def main(result: str, debug: bool):
if result:
firefox_options = Options()
if not debug:
firefox_options.add_argument("--headless")
browser = Firefox(options=firefox_options)
try:
browser.get("https://wiki.kyrium.space/")
browser.set_window_size(1600, 1015)
search_bar = browser.find_element(
By.CSS_SELECTOR, "nav input[placeholder='Search for an item...']"
)
search_bar.click()
search_bar.send_keys(result)
search_button = browser.find_element(By.CSS_SELECTOR, "nav button[type='submit']")
search_button.click()
browser.implicitly_wait(3)
choices = browser.find_elements(
By.CSS_SELECTOR, "body > div > .container:nth-child(1) a.items-center"
)
if not choices:
print("No wiki entries found for this result")
return
elif len(choices) > 1:
default_choice = 1
for choice_idx in range(1, len(choices) + 1):
recipe_choice = choices[choice_idx - 1]
name = recipe_choice.find_element(By.TAG_NAME, "img").get_attribute("alt")
if name.casefold() == result.casefold():
default_choice = choice_idx
print(f"{choice_idx}: {name}")
user_choice = input(f"Chose a recipe to continue… (default: {default_choice}) ")
if not user_choice:
user_choice = default_choice
else:
user_choice = int(user_choice)
choices[user_choice - 1].click()
else:
choices[0].click()
browser.find_element(By.CSS_SELECTOR, "button#\\3Ar1\\3A-tab-0").click()
recipes = browser.find_elements(By.CSS_SELECTOR, "#\\3Ar1\\3A-tabpanel-0 > div > div")
for recipe_idx in range(len(recipes)):
recipe = recipes[recipe_idx]
print(
"recipe",
recipe_idx,
"produced in:",
recipe.find_element(By.CSS_SELECTOR, ".flex-col > span > a").text,
)
ingredients = recipe.find_elements(
By.CSS_SELECTOR, f".flex-row > div:nth-child(1) > div:has(> a)"
)
for ingredient_idx in range(len(ingredients)):
ingredient = ingredients[ingredient_idx]
print(
"recipe",
recipe_idx,
"ingredient",
ingredient_idx,
"name:",
ingredient.find_element(By.TAG_NAME, "img").get_attribute("alt"),
)
print(
"recipe",
recipe_idx,
"ingredient",
ingredient_idx,
"count:",
ingredient.find_element(By.CSS_SELECTOR, ".text-xs:nth-child(2)").text,
)
print(
"recipe",
recipe_idx,
"ingredient",
ingredient_idx,
"time:",
ingredient.find_element(By.CSS_SELECTOR, ".text-xs:nth-child(3)").text,
)
results = recipe.find_elements(
By.CSS_SELECTOR, f".flex-row > div:nth-child(3) > div:has(> a)"
)
for result_idx in range(len(results)):
result = results[result_idx]
print(
"recipe",
recipe_idx,
"result",
result_idx,
"name:",
result.find_element(By.TAG_NAME, "img").get_attribute("alt"),
)
print(
"recipe",
recipe_idx,
"result",
result_idx,
"count:",
result.find_element(By.CSS_SELECTOR, ".text-xs:nth-child(2)").text,
)
print(
"recipe",
recipe_idx,
"result",
result_idx,
"time:",
result.find_element(By.CSS_SELECTOR, ".text-xs:nth-child(3)").text,
)
finally:
if not debug:
browser.quit()