Skip to content

Commit

Permalink
Fix diablo.trade (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisoro authored Aug 27, 2024
1 parent 2f0d5a2 commit 37ac6ff
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
!config/bnip/.gitkeep
*.bak
*.lock
*.log
*.pyc
*.pyo
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Documentation is not yet finished. For now, it should be self-explanatory. Just
Current functionality:

- Import builds from maxroll/d4builds/mobalytics
- Create profiles based off of searches for diablo.trade
- Create profiles based off of searches for diablo.trade (requires chrome)
- Complete management of your params.ini through the config tab

Each tab gives further instructions on how to use it and what kind of input it expects.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pyyaml
rapidfuzz
ruff
selenium
seleniumbase
tk
typing_extensions
webdriver-manager
2 changes: 1 addition & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

TP = concurrent.futures.ThreadPoolExecutor()

__version__ = "5.7.9"
__version__ = "5.7.10"
9 changes: 6 additions & 3 deletions src/gui/importer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from seleniumbase import Driver

from src import __version__
from src.config.loader import IniConfigLoader
Expand Down Expand Up @@ -139,12 +140,12 @@ def match_to_enum(enum_class, target_string: str, check_keys: bool = False):
return None


def retry_importer(func=None, inject_webdriver: bool = False):
def retry_importer(func=None, inject_webdriver: bool = False, uc=False):
def decorator_retry_importer(wrap_function):
@functools.wraps(wrap_function)
def wrapper(*args, **kwargs):
if inject_webdriver and "driver" not in kwargs and not args:
kwargs["driver"] = setup_webdriver()
kwargs["driver"] = setup_webdriver(uc=uc)
for _ in range(5):
try:
res = wrap_function(*args, **kwargs)
Expand Down Expand Up @@ -180,7 +181,9 @@ def save_as_profile(file_name: str, profile: ProfileModel, url: str):
LOGGER.info(f"Created profile {save_path}")


def setup_webdriver() -> ChromiumDriver:
def setup_webdriver(uc: bool = False) -> ChromiumDriver:
if uc:
return Driver(uc=uc, headless2=True)
match IniConfigLoader().general.browser:
case BrowserType.edge:
options = webdriver.EdgeOptions()
Expand Down
2 changes: 1 addition & 1 deletion src/gui/importer/d4builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class D4BuildsException(Exception):


@retry_importer(inject_webdriver=True)
def import_d4builds(driver: ChromiumDriver = None, url: str = None):
def import_d4builds(url: str, driver: ChromiumDriver = None):
url = url.strip().replace("\n", "")
if BASE_URL not in url:
LOGGER.error("Invalid url, please use a d4builds url")
Expand Down
16 changes: 9 additions & 7 deletions src/gui/importer/diablo_trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import Any
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse

import lxml.html
import seleniumbase
from pydantic import ValidationError

import src.logger
Expand All @@ -13,7 +15,6 @@
from src.dataloader import Dataloader
from src.gui.importer.common import (
format_number_as_short_string,
get_with_retry,
match_to_enum,
retry_importer,
save_as_profile,
Expand Down Expand Up @@ -50,8 +51,8 @@ class DiabloTradeException(Exception):
pass


@retry_importer
def import_diablo_trade(url: str, max_listings: int):
@retry_importer(inject_webdriver=True, uc=True)
def import_diablo_trade(url: str, max_listings: int, driver: seleniumbase.Driver = None):
url = url.strip().replace("\n", "")
if BASE_URL not in url:
LOGGER.error("Invalid url, please use a diablo.trade filter url")
Expand All @@ -62,11 +63,12 @@ def import_diablo_trade(url: str, max_listings: int):
while True:
api_url = _construct_api_url(listing_url=url, cursor=cursor)
try:
r = get_with_retry(url=api_url)
except ConnectionError:
LOGGER.error("Can't fetch listings, saving current data")
driver.default_get(url=api_url)
source = lxml.html.fromstring(driver.get_page_source())
data = json.loads(source.text_content().strip())
except Exception:
LOGGER.exception("Can't fetch listings, saving current data")
break
data = r.json()
if not (listings := data["data"]):
LOGGER.debug("Reached end")
break
Expand Down
2 changes: 1 addition & 1 deletion tests/gui/importer/test_diablo_trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@pytest.mark.parametrize("url", URLS)
@pytest.mark.requests
@pytest.mark.selenium
def test_import_diablo_trade(url: str, mock_ini_loader: MockerFixture, mocker: MockerFixture):
Dataloader() # need to load data first or the mock will make it impossible
mocker.patch("builtins.open", new=mocker.mock_open())
Expand Down

0 comments on commit 37ac6ff

Please sign in to comment.