SHARE
TWEET

Untitled

a guest Aug 25th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. import win32com.client as comclt
  4. import time
  5. import datetime
  6. from selenium.common.exceptions import TimeoutException
  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 re
  11. import os
  12.  
  13. ts = time.time()
  14. st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
  15. wsh= comclt.Dispatch("WScript.Shell")
  16.  
  17. base_url = 'https://www.reservecalifornia.com/CaliforniaWebHome/'
  18. rxp_premium = '^Premium Campsite \#M\d{1,3}'
  19. rxp_site = 'Santa Miguel.*'
  20. attr_available = 'blue_brd_box'
  21.  
  22. timeout_delay = 3 # seconds
  23. # search options
  24. start_date = '02/03/2020'
  25. num_nights = 3
  26. def open_baseurl(url):
  27.     chrome_driver = r'C:\bin\chromedriver.exe'
  28.     chrome_profile = os.path.join(os.getenv('LOCALAPPDATA'), 'Google', 'Chrome', 'User Data')
  29.     chrome_options = webdriver.chrome.options.Options()
  30.     chrome_options.add_argument('--ignore-certificate-errors')
  31.     chrome_options.add_argument('--ignore-certificate-errors-spki-list')
  32.     chrome_options.add_argument('--ignore-ssl-errors')
  33.     # chrome_options.add_argument('--user-data-dir={0}'.format(chrome_profile))
  34.     driver = webdriver.Chrome(executable_path=chrome_driver, options=chrome_options)
  35.     driver.implicitly_wait(3) # seconds
  36.     driver.get("https://www.reservecalifornia.com/CaliforniaWebHome/")
  37.     return driver
  38.  
  39.  
  40. def reserve(driver, start_date, num_nights):
  41.     park = driver.find_element_by_id('txtSearchparkautocomplete')
  42.     park.send_keys('Carpinteria SB')
  43.     time.sleep(0.5)
  44.     park.send_keys(Keys.TAB)
  45.     park.send_keys(Keys.TAB)
  46.     arrival = driver.find_element_by_id('mainContent_txtArrivalDate')
  47.     arrival.click()
  48.     arrival.click()
  49.     arrival.clear()
  50.     arrival.send_keys(start_date)
  51.     nights = driver.find_element_by_id('ddlHomeNights')
  52.     nights.click()
  53.     nights.send_keys(str(num_nights))
  54.     nights.click()
  55.     rental = driver.find_element_by_id('ddl_homeCategories')
  56.     rental.send_keys('camp')
  57.     rental.click()
  58.     equip = driver.find_element_by_id('ddl_homeCampingUnit')
  59.     equip.click()
  60.     equip.send_keys('trail')
  61.     equip.click()
  62.     driver.find_element_by_link_text("Search").click()
  63.     return driver
  64.    
  65.  
  66. def select_facility(rxp_site):
  67.     try:
  68.         element = WebDriverWait(driver, timeout_delay).until(EC.presence_of_element_located((By.ID, 'number_6')))
  69.     except TimeoutException:
  70.         print('timeout?')
  71.     xpath_rows = "//div[@id='number_6']/div[@class='table_data_box table-responsive']/table/tbody/tr"
  72.     rows = driver.find_elements_by_xpath(xpath_rows)
  73.     for row in rows:
  74.         m = re.match(rxp_site, row.text)
  75.         if m:
  76.             for r in row.find_elements_by_xpath('.//following-sibling::tr'):
  77.                 if re.match('Premium Campsite', r.text):
  78.                     premium_link = r.find_element_by_tag_name('a')
  79.     premium_link.click()
  80.     return driver
  81.  
  82.  
  83. def get_premium_rows():
  84.     premium_rows = []
  85.     table = driver.find_element_by_id('mainContent_ugReservationGrid_tableGridParent').find_element_by_xpath('.//child::table/tbody')
  86.     rows = table.find_elements_by_css_selector('tr.unitdata')
  87.     for row in rows:
  88.         link = row.find_element_by_xpath(".//child::td[@class='first_td_new']/a")
  89.         site_title = link.get_attribute('title')
  90.         if re.match(rxp_premium, site_title):
  91.             ancestor_row = link.find_element_by_xpath(".//ancestor::tr[@class='unitdata']")
  92.             premium_rows.append(ancestor_row)
  93.     return premium_rows
  94.  
  95.  
  96. driver = open_baseurl(base_url)
  97. driver = reserve(driver, start_date, num_nights)
  98. driver = select_facility(rxp_site)
  99. site_list = get_premium_rows()
  100.  
  101. print('Available sites: ', len(sites))
  102. for p in sites:
  103.     print(p['sitename'], ':', len(p['slots']))
  104.  
  105. slots = site.find_elements_by_xpath(".//child::td[contains(@class, 'brd_box')]")
  106. slot_list = []
  107. for i in range((len(slots) - 1)):
  108.     slot_isvalid = False
  109.     if (i + num_nights) > len(slots):
  110.         print('ignoring site')
  111.         break
  112.     for j in range(i, (i + num_nights)):
  113.         print(j)
  114.         if slots[j].get_attribute('class') != 'blue_brd_box':
  115.             break
  116.         else:
  117.             slot_isvalid = True
  118.     if slot_isvalid is True:
  119.         slot_list.append(slots[i])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top