Last active
October 27, 2023 19:50
-
-
Save MichaelMackus/a81050bed80f099e4ae02bed8b8b63e9 to your computer and use it in GitHub Desktop.
Relist all items on traderie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Relist on traderie. You need to login via Chrome browser. Outputs next relist time in command line. | |
# | |
# Requires selenium, seleniumbase, and chromedriver | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.action_chains import ActionChains | |
from selenium.common.exceptions import InvalidCookieDomainException | |
from seleniumbase import SB | |
from os.path import expanduser | |
import pickle | |
import time | |
import re | |
LOGIN_PAGE = 'https://traderie.com/login' | |
HOME_PAGE = 'https://traderie.com/' | |
GAME_PAGE = 'https://traderie.com/diablo2resurrected' | |
#PROFILE_DIR = expanduser('~') + r'\AppData\Local\Google\Chrome\User Data\Default' # uncomment to re-use cookies from chrome profile dir | |
class NoProfileFoundException(Exception): | |
pass | |
def is_profile_link(element): | |
if element is None: | |
return False | |
prop = element.get_property('href') | |
return prop and prop.find('/profile') != -1 | |
def is_relist_button(element): | |
return element.text == "Relist" | |
def find_profile_link(driver): | |
driver.get(GAME_PAGE) | |
for element in driver.find_elements(By.CLASS_NAME, 'profile-link'): | |
if is_profile_link(element): | |
return element | |
return None | |
def match_relist_time(s): | |
return s and re.match('(\d*) (hour|minute)', s) | |
def relist_time_minutes(s): | |
m = match_relist_time(s) | |
n = int(m[1]) | |
if m[2] == 'hour': | |
return n * 60 | |
else: | |
return n | |
def is_relist_time_sooner(new_time, original_time): | |
if not match_relist_time(original_time): | |
return True | |
if not match_relist_time(new_time): | |
return False | |
# compare integer | |
minutes_original = relist_time_minutes(original_time) | |
minutes_new = relist_time_minutes(new_time) | |
return minutes_new < minutes_original | |
def find_load_button(driver): | |
elements = driver.find_elements(By.CSS_SELECTOR, '.see-all-btn-bar button') | |
for element in elements: | |
if element.text == 'Load More': | |
return element | |
return None | |
if __name__ == '__main__': | |
try: | |
args = '--user-data-dir=' + PROFILE_DIR | |
except NameError: | |
args = None | |
with SB(uc=True, headed=True, chromium_arg=args) as driver: | |
# check for login | |
element = find_profile_link(driver) | |
if element is None: | |
driver.get(LOGIN_PAGE) | |
# wait for homepage URL | |
while True: | |
if driver.get_current_url() == HOME_PAGE: | |
break | |
time.sleep(1) | |
element = find_profile_link(driver) | |
# navigate to user's profile | |
try: | |
if element and is_profile_link(element): | |
profile_url = element.get_property('href') | |
driver.get(profile_url + '?orderBy=posted-asc') # order trades by oldest first | |
time.sleep(1) | |
else: | |
raise NoProfileFoundException() | |
except NameError: | |
raise NoProfileFoundException() | |
# relist trades | |
while True: | |
relist_count = 0 | |
elements = driver.find_elements(By.CSS_SELECTOR, '.listing-action-bar button') | |
for element in elements: | |
if is_relist_button(element): | |
driver.execute_script('arguments[0].scrollIntoView();', element) | |
element.click() | |
relist_count += 1 | |
time.sleep(1) | |
# paginate | |
load_button = find_load_button(driver) | |
if not load_button: | |
break | |
driver.execute_script('arguments[0].scrollIntoView();', load_button) | |
load_button.click() | |
time.sleep(1) | |
# get next relist time | |
relist_time = None | |
elements = driver.find_elements(By.CSS_SELECTOR, '.listing-action-bar button') | |
for element in elements: | |
if match_relist_time(element.text) and is_relist_time_sooner(element.text, relist_time): | |
relist_time = element.text | |
if relist_count > 0: | |
print("Relisted", relist_count, "item(s)") | |
else: | |
print("No items to relist") | |
if relist_time: | |
now = time.localtime() | |
print("Current time is %s. Next relist in %s." % (time.strftime("%I:%M %p", now), relist_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment