Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # SCANNER
- import ast
- from datetime import datetime
- from selenium import webdriver
- from selenium.webdriver.chrome.options import Options
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- import configparser
- import sys
- from time import sleep
- from time import gmtime, strftime, time
- import os
- from GPCommon import *
- GET_WINDOW_ID_JS = "return $(window.Poker.tableId)[0];"
- def main():
- num_param = len(sys.argv)
- if num_param < 2:
- print("Usage python3 GPScanner.py config_file")
- sys.exit(1)
- else:
- config_file = sys.argv[1:]
- config = configparser.ConfigParser()
- config.read(config_file)
- gp_user = config[GP_CONN_SECTION]['Username']
- gp_pass = config[GP_CONN_SECTION]['Password']
- chrome_profile = config['MISC']['ChromeProfile']
- limits_to_scan = str(config['FEATURES']['LIMITS']).split(',')
- max_tables = int(config['FEATURES']['MAX_TABLES'])
- max_seats = ast.literal_eval(config['FEATURES']['MAX_SEATS'])
- new_tables_timeout = int(config['FEATURES']['NEW_TABLES_TIMEOUT'])
- if not os.path.exists(SCAN_DIR):
- os.makedirs(SCAN_DIR)
- hands_file = open("{0}/scanned_seatmap_all".format(SCAN_DIR), 'a')
- # configure chrome options
- chrome_options = Options()
- chrome_options.add_argument(chrome_profile);
- chrome_options.add_argument("disable-infobars");
- driver = webdriver.Chrome(chrome_options=chrome_options)
- driver.get(GP_URL)
- wait = WebDriverWait(driver, WAIT_EL_TIMEOUT);
- # login
- elem = wait.until(EC.presence_of_element_located((By.NAME, EL_NAME_EMAIL)))
- elem.send_keys(gp_user)
- elem = driver.find_element_by_name(EL_NAME_PASSWORD)
- sleep(1)
- elem.send_keys(gp_pass)
- driver.find_element_by_xpath(LOGIN_BUTTON_XPATH).click()
- wait.until(EC.frame_to_be_available_and_switch_to_it(EL_MAIN_FRAME))
- wait.until(EC.presence_of_element_located((By.LINK_TEXT, EL_TEXT_SWITCH_CURRENCY))).click()
- total_hands = 0
- # load javascript
- with open('js/scanner/scanHands.js', 'r') as script_file:
- scan_script = script_file.read()
- with open('js/common/getHandFromStorage.js', 'r') as script_file:
- get_hand_script = script_file.read()
- with open('js/scanner/getListOfStuckHands.js', 'r') as script_file:
- get_list_of_stuck_hands_script = script_file.read()
- # last processed hand index for window
- window_processed_index = dict()
- is_cleaned_up = False
- is_switch_needed = False
- open_tables = []
- is_first_time = True
- check_for_new_tables_time = -1
- while 1:
- try:
- print('{0} Hands logged {1}. Current tables: {2}'.format(datetime.now().strftime('%H:%M:%S'), total_hands, len(driver.window_handles) - 1))
- # iterate over opened poker tables
- for index, window_handle in enumerate(driver.window_handles):
- # no need to do it for main lobby
- if index == 0:
- if time() - new_tables_timeout < check_for_new_tables_time:
- print("{0} seconds for new table check...".format(check_for_new_tables_time - time() + new_tables_timeout))
- continue
- driver.switch_to.window(window_handle)
- wait.until(EC.frame_to_be_available_and_switch_to_it(EL_MAIN_FRAME))
- # parsing tables
- elem = driver.find_element_by_class_name("table-list-item-container")
- tables = elem.find_elements_by_xpath("*")
- for table in tables:
- table_name = table.find_element_by_class_name('table-name').text
- blinds = table.find_element_by_class_name('blinds').text
- (seated, total_seats) = table.find_element_by_class_name('seated').text.split('/')
- # in case limit is reached there is no reason to go further in this for
- if len(open_tables) >= max_tables:
- break
- if table_name not in open_tables and NL_LIMITS[blinds] in limits_to_scan and \
- int(seated) > 1 and int(total_seats) <= max_seats[NL_LIMITS[blinds]]:
- table.click()
- sleep(1)
- if table_name not in open_tables:
- open_tables.append(table_name)
- check_for_new_tables_time = time()
- # max_tables = max_tables + 3
- # print("Max tables: {0}".format(max_tables))
- continue
- # in case it's new window or switch is needed due to old window was closed
- if is_switch_needed or (window_handle and window_handle not in window_processed_index):
- # switch to it
- driver.switch_to.window(window_handle)
- # do all other stuff just in case window is new
- if not is_switch_needed:
- # I know it is horrible, but there is no way around it as main lobby has different local storage
- # for some reason, so we need to open a table to get to correct local storage
- if not is_cleaned_up:
- clean_up_local_storage(driver, get_list_of_stuck_hands_script, hands_file)
- is_cleaned_up = True
- # execute main js
- driver.execute_script(scan_script)
- # get window id and set first processed hand to 0
- window_processed_index[window_handle] = (driver.execute_script(GET_WINDOW_ID_JS), 0, driver.title);
- if driver.title not in open_tables:
- open_tables.append(driver.title)
- print("New window added ({0}). Title:{0}".format(len(open_tables),driver.title))
- # switch not needed anymore
- is_switch_needed = False
- (window_id, hand_index, title) = window_processed_index[window_handle]
- last_hand = driver.execute_script(get_hand_script, window_id, hand_index)
- # if hand was not empty increment last hand counter
- if last_hand:
- hand_index += 1
- window_processed_index[window_handle] = (window_id, hand_index, title)
- # save to file
- hands_file.write("{0}\n".format(last_hand))
- hands_file.flush()
- total_hands = total_hands + 1
- # window handles cleanup, need list conversion as in Python3 items will return iterator and
- # it will be impossible to delete items from dict
- for window_handle, (window_id, _, title) in list(window_processed_index.items()):
- if window_handle not in driver.window_handles:
- open_tables.remove(title)
- print("---------Table {0} was closed".format(title))
- del window_processed_index[window_handle]
- # print("Open tables: {0}".format(open_tables))
- # print("Window processed: {0}".format(window_processed_index))
- except Exception as e:
- print(str(e))
- if "no such window: target window already closed" in str(e):
- is_switch_needed = True
- except:
- print("Didn't get that exception type")
- sleep(10)
- driver.quit()
- hands_file.close()
- def clean_up_local_storage(driver, script, hands_file):
- hands = driver.execute_script(script)
- print("Found {0} stuck hands".format(hands.count("\n")))
- hands_file.write(hands)
- hands_file.flush()
- def current_time():
- return strftime("%Y%m%d%H%M", gmtime())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement