Advertisement
Guest User

Untitled

a guest
Jul 24th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. #
  2. # SCANNER
  3. import ast
  4. from datetime import datetime
  5. from selenium import webdriver
  6. from selenium.webdriver.chrome.options import Options
  7. from selenium.webdriver.common.by import By
  8. from selenium.webdriver.support.ui import WebDriverWait
  9. from selenium.webdriver.support import expected_conditions as EC
  10. import configparser
  11. import sys
  12. from time import sleep
  13. from time import gmtime, strftime, time
  14. import os
  15. from GPCommon import *
  16.  
  17. GET_WINDOW_ID_JS = "return $(window.Poker.tableId)[0];"
  18.  
  19.  
  20. def main():
  21. num_param = len(sys.argv)
  22. if num_param < 2:
  23. print("Usage python3 GPScanner.py config_file")
  24. sys.exit(1)
  25. else:
  26. config_file = sys.argv[1:]
  27.  
  28. config = configparser.ConfigParser()
  29. config.read(config_file)
  30. gp_user = config[GP_CONN_SECTION]['Username']
  31. gp_pass = config[GP_CONN_SECTION]['Password']
  32. chrome_profile = config['MISC']['ChromeProfile']
  33. limits_to_scan = str(config['FEATURES']['LIMITS']).split(',')
  34. max_tables = int(config['FEATURES']['MAX_TABLES'])
  35. max_seats = ast.literal_eval(config['FEATURES']['MAX_SEATS'])
  36. new_tables_timeout = int(config['FEATURES']['NEW_TABLES_TIMEOUT'])
  37.  
  38. if not os.path.exists(SCAN_DIR):
  39. os.makedirs(SCAN_DIR)
  40.  
  41. hands_file = open("{0}/scanned_seatmap_all".format(SCAN_DIR), 'a')
  42.  
  43. # configure chrome options
  44. chrome_options = Options()
  45. chrome_options.add_argument(chrome_profile);
  46. chrome_options.add_argument("disable-infobars");
  47. driver = webdriver.Chrome(chrome_options=chrome_options)
  48. driver.get(GP_URL)
  49. wait = WebDriverWait(driver, WAIT_EL_TIMEOUT);
  50.  
  51. # login
  52. elem = wait.until(EC.presence_of_element_located((By.NAME, EL_NAME_EMAIL)))
  53. elem.send_keys(gp_user)
  54. elem = driver.find_element_by_name(EL_NAME_PASSWORD)
  55. sleep(1)
  56. elem.send_keys(gp_pass)
  57. driver.find_element_by_xpath(LOGIN_BUTTON_XPATH).click()
  58.  
  59. wait.until(EC.frame_to_be_available_and_switch_to_it(EL_MAIN_FRAME))
  60. wait.until(EC.presence_of_element_located((By.LINK_TEXT, EL_TEXT_SWITCH_CURRENCY))).click()
  61.  
  62. total_hands = 0
  63.  
  64. # load javascript
  65. with open('js/scanner/scanHands.js', 'r') as script_file:
  66. scan_script = script_file.read()
  67.  
  68. with open('js/common/getHandFromStorage.js', 'r') as script_file:
  69. get_hand_script = script_file.read()
  70.  
  71. with open('js/scanner/getListOfStuckHands.js', 'r') as script_file:
  72. get_list_of_stuck_hands_script = script_file.read()
  73.  
  74. # last processed hand index for window
  75. window_processed_index = dict()
  76. is_cleaned_up = False
  77. is_switch_needed = False
  78. open_tables = []
  79. is_first_time = True
  80. check_for_new_tables_time = -1
  81. while 1:
  82. try:
  83. print('{0} Hands logged {1}. Current tables: {2}'.format(datetime.now().strftime('%H:%M:%S'), total_hands, len(driver.window_handles) - 1))
  84.  
  85. # iterate over opened poker tables
  86. for index, window_handle in enumerate(driver.window_handles):
  87.  
  88. # no need to do it for main lobby
  89. if index == 0:
  90. if time() - new_tables_timeout < check_for_new_tables_time:
  91. print("{0} seconds for new table check...".format(check_for_new_tables_time - time() + new_tables_timeout))
  92. continue
  93.  
  94. driver.switch_to.window(window_handle)
  95. wait.until(EC.frame_to_be_available_and_switch_to_it(EL_MAIN_FRAME))
  96.  
  97. # parsing tables
  98. elem = driver.find_element_by_class_name("table-list-item-container")
  99. tables = elem.find_elements_by_xpath("*")
  100. for table in tables:
  101. table_name = table.find_element_by_class_name('table-name').text
  102. blinds = table.find_element_by_class_name('blinds').text
  103. (seated, total_seats) = table.find_element_by_class_name('seated').text.split('/')
  104.  
  105. # in case limit is reached there is no reason to go further in this for
  106. if len(open_tables) >= max_tables:
  107. break
  108.  
  109. if table_name not in open_tables and NL_LIMITS[blinds] in limits_to_scan and \
  110. int(seated) > 1 and int(total_seats) <= max_seats[NL_LIMITS[blinds]]:
  111. table.click()
  112. sleep(1)
  113. if table_name not in open_tables:
  114. open_tables.append(table_name)
  115.  
  116. check_for_new_tables_time = time()
  117. # max_tables = max_tables + 3
  118. # print("Max tables: {0}".format(max_tables))
  119. continue
  120.  
  121. # in case it's new window or switch is needed due to old window was closed
  122. if is_switch_needed or (window_handle and window_handle not in window_processed_index):
  123.  
  124. # switch to it
  125. driver.switch_to.window(window_handle)
  126.  
  127. # do all other stuff just in case window is new
  128. if not is_switch_needed:
  129. # I know it is horrible, but there is no way around it as main lobby has different local storage
  130. # for some reason, so we need to open a table to get to correct local storage
  131. if not is_cleaned_up:
  132. clean_up_local_storage(driver, get_list_of_stuck_hands_script, hands_file)
  133. is_cleaned_up = True
  134.  
  135. # execute main js
  136. driver.execute_script(scan_script)
  137. # get window id and set first processed hand to 0
  138. window_processed_index[window_handle] = (driver.execute_script(GET_WINDOW_ID_JS), 0, driver.title);
  139. if driver.title not in open_tables:
  140. open_tables.append(driver.title)
  141. print("New window added ({0}). Title:{0}".format(len(open_tables),driver.title))
  142.  
  143. # switch not needed anymore
  144. is_switch_needed = False
  145.  
  146. (window_id, hand_index, title) = window_processed_index[window_handle]
  147. last_hand = driver.execute_script(get_hand_script, window_id, hand_index)
  148.  
  149. # if hand was not empty increment last hand counter
  150. if last_hand:
  151. hand_index += 1
  152. window_processed_index[window_handle] = (window_id, hand_index, title)
  153.  
  154. # save to file
  155. hands_file.write("{0}\n".format(last_hand))
  156. hands_file.flush()
  157. total_hands = total_hands + 1
  158.  
  159. # window handles cleanup, need list conversion as in Python3 items will return iterator and
  160. # it will be impossible to delete items from dict
  161. for window_handle, (window_id, _, title) in list(window_processed_index.items()):
  162. if window_handle not in driver.window_handles:
  163. open_tables.remove(title)
  164. print("---------Table {0} was closed".format(title))
  165. del window_processed_index[window_handle]
  166.  
  167. # print("Open tables: {0}".format(open_tables))
  168. # print("Window processed: {0}".format(window_processed_index))
  169.  
  170. except Exception as e:
  171. print(str(e))
  172. if "no such window: target window already closed" in str(e):
  173. is_switch_needed = True
  174. except:
  175. print("Didn't get that exception type")
  176. sleep(10)
  177.  
  178. driver.quit()
  179. hands_file.close()
  180.  
  181.  
  182. def clean_up_local_storage(driver, script, hands_file):
  183. hands = driver.execute_script(script)
  184. print("Found {0} stuck hands".format(hands.count("\n")))
  185. hands_file.write(hands)
  186. hands_file.flush()
  187.  
  188.  
  189. def current_time():
  190. return strftime("%Y%m%d%H%M", gmtime())
  191.  
  192.  
  193. if __name__ == '__main__':
  194. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement