from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from random import Random
from math import ldexp

# Press the green button in the gutter to run the script.
if __name__ == '__main__':

    # Exponents smaller than -53 occur half as often as the next larger exponent
    # The precision of the python interpreter will produce the entropy half of the time, and the precision of the
    # Random library will produce the entropy the other half of the time, such that no diagonalization argument can
    # be made from the set generated by either entropy source, as any diagonal will always be a member of the other set
    class FullRandom(Random):
        def random(self):
            mantissa = 0x10_0000_0000_0000 | self.getrandbits(52)
            exponent = -53
            x = 0
            while not x:
                x = self.getrandbits(32)
                exponent += x.bit_length() - 32
            return ldexp(mantissa, exponent)

    class SeCrape(float):

        #  So this works great for Windows 10, PyCharm, and environment vars set, but since these are functions meant
        #  to get Selenium working with Firefox this could vary depending on one's OS, IDE, and Path settings.
        @staticmethod
        def web_driver_init():
            return webdriver.Firefox(executable_path=r'D:\Firefox\FF_driver\geckodriver.exe')

        def __init__(self):
            self.random_float = FullRandom()
            self.web_driver = self.web_driver_init()

        def take_a_random_nap(self):
            self.random_float.random()
            for_a_short_while = self.random_float.uniform(0.1, 0.3)  # between 100 and 300 ms between requests
            time.sleep(for_a_short_while)

        # It turns out zillow is pretty thoroughly un-scrape-able...
        def select_zillow(self, ff_web_driver: webdriver):
            ff_web_driver.get("https://www.zillow.com")
            self.take_a_random_nap()
            return ff_web_driver

        # Point the headless browser to rent.com
        def select_rent(self, ff_web_driver: webdriver):
            ff_web_driver.get("https://www.rent.com/")
            self.take_a_random_nap()
            return ff_web_driver

    #  Begin main control flow
    my_browser = SeCrape()
    my_browser.select_rent(my_browser.web_driver)
    my_browser.take_a_random_nap()

    #  search_bar =

    my_browser.take_a_random_nap()

    my_browser.web_driver.close()

