Exemple #1
0
def log_in(login_url, username, password):
    print('Step 1 :Logging In to Booking System')
    print()
    #print('=====================================')
    #print('Booking a court on',desired_date,'at',desired_time)
    #print()
    #print('Logging in with Credentials:')
    #print('Username ='******'Password ='******'=====================================')
    #print()
    browser.go(login_url)
    if browser.find_link("Logout"):
        print("Already logged in!")
    else:
        login_form = browser.form("1")
        username_field = browser.form_field(login_form, "username")
        password_field = browser.form_field(login_form, "password")
        username_field.value = username
        password_field.value = password
        browser.submit()
        if "denied" in browser.html.lower(
        ):  # error : TypeError: 'str' object is not callable : removed () after html - solved !
            raise Exception("Login failed!")
        else:
            print('Logged in successfully')
            print()
Exemple #2
0
def cancel_tennis_courts_to_avoid_fees():
    #++++++++++++++++++++++++++++++++++++++++++++++++
    import datetime
    cancel_date = datetime.date.today() + timedelta(days=0)
    #++++++++++++++++++++++++++++++++++++++++++++++++
    print("Cancelling courts without opponents on {}".format(
        cancel_date.strftime('%A %d %B %Y')))
    log_in(login_url, username, password)
    my_bookings_url = login_url + '/my_bookings.asp'
    browser.go(my_bookings_url)
    soup = BeautifulSoup(browser.html, 'html.parser')  # removed () after html
    bookings_table = soup.findAll("table", {"class": "form_table my_bookings"})
    cancelled_count = 0
    for booking in bookings_table:
        #rows = get_booking_table_rows()
        if int(cancel_date.strftime('%d')) < 10:
            cancel_date_to_use = cancel_date.strftime('%d')  #str '09'
            cancel_date_to_use = cancel_date_to_use.replace('0', '')  #str '9'
        else:
            cancel_date_to_use = cancel_date.strftime('%d')
        if 'select opponent(s)' in str(booking) and cancel_date_to_use in str(
                booking):
            date = str(bookings_table[3]).split()[7] + ' ' + str(
                bookings_table[3]).split()[8] + ' ' + str(
                    bookings_table[3]).split()[9]
            time = str(bookings_table[3]).split()[39]
            court = str(bookings_table[3]).split()[17] + ' ' + str(
                bookings_table[3]).split()[18].replace('</a></td>', '')
            print('Unfilled booking found for', court, 'on', date, 'at', time)
            print()
            try:
                cancel_link = 'https://blackheath-ltc.mycourts.co.uk/bookings_cancel.asp?bid=' + str(
                    booking).split()[31].replace('id="cancel', '').replace(
                        '"', '')
                browser.go(cancel_link)
                print('Cancelled', court, 'on', date, 'at', time)
            except Exception as e:
                print("Failed to cancel court {} {}".format(e, booking))
            cancelled_count = cancelled_count + 1
    if cancelled_count == 0:
        print("No courts to cancel")
Exemple #3
0
def go(*args, **kwargs):
    # replace the default ``go`` to make the additional
    # arguments that our custom browser provides available.
    browser = get_browser()
    browser.go(*args, **kwargs)
    return browser.get_url()
Exemple #4
0
def go(*args, **kwargs):
    # replace the default ``go`` to make the additional
    # arguments that our custom browser provides available.
    browser = get_browser()
    browser.go(*args, **kwargs)
    return browser.get_url()
Exemple #5
0
def test():
    # reset
    twill.commands.reset_browser()

    # get the current browser obj.
    browser = twill.get_browser()
    assert browser is twill.commands.browser

    # check the 'None' value of return code
    assert browser.get_code() is None

    # no forms, right?
    try:
        browser.submit()
    except Exception:
        pass

    try:
        import warnings
        warnings.filterwarnings('ignore')
        assert browser is twill.get_browser_state() # deprecated
        warnings.resetwarnings()
    except DeprecationWarning:
        pass

    old_err, sys.stderr = sys.stderr, StringIO()
    try:
        try:
            browser.go('http://') # what's a good "nowhere"?!?
            assert 0, "shouldn't get here"
        except:
            pass
    finally:
        sys.stderr = old_err

    try:
        twill.commands.exit()
        assert 0, "shouldn't get here"
    except SystemExit:
        pass

    try:
        twill.commands.reset_browser()
        twill.commands.showhistory()
        twill.commands.tidy_ok()
        twill.commands.show()
        assert 0, "shouldn't get here!" # no page!
    except TwillAssertionError:
        pass

    twill.commands.debug('http', '1')
    twill.commands.debug('http', '0')
    twill.commands.debug('http', '+')
    twill.commands.debug('http', '-')
    
    twill.commands.debug('commands', '0')
    twill.commands.debug('commands', '1')
    try:
        twill.commands.debug('nada', '1')
        assert 0
    except:
        pass

    twill.commands.config()
    twill.commands.config('readonly_controls_writeable')
    twill.commands.config('use_tidy')
    twill.commands.config('require_tidy')
    
    twill.commands.config('readonly_controls_writeable', 1)
    twill.commands.config('use_tidy', 1)
    twill.commands.config('require_tidy', 0)
    
    twill.commands.config('require_tidy', "on")

    twill.commands.run("print('hello')")
Exemple #6
0
def test():
    # reset
    twill.commands.reset_browser()

    # get the current browser obj.
    browser = twill.get_browser()
    assert browser is twill.commands.browser

    # check the 'None' value of return code
    assert browser.get_code() is None

    # no forms, right?
    try:
        browser.submit()
    except Exception:
        pass

    try:
        import warnings
        warnings.filterwarnings('ignore')
        assert browser is twill.get_browser_state() # deprecated
        warnings.resetwarnings()
    except DeprecationWarning:
        pass

    old_err, sys.stderr = sys.stderr, StringIO()
    try:
        try:
            browser.go('http://') # what's a good "nowhere"?!?
            assert 0, "shouldn't get here"
        except:
            pass
    finally:
        sys.stderr = old_err

    try:
        twill.commands.exit()
        assert 0, "shouldn't get here"
    except SystemExit:
        pass

    try:
        twill.commands.reset_browser()
        twill.commands.showhistory()
        twill.commands.tidy_ok()
        twill.commands.show()
        assert 0, "shouldn't get here!" # no page!
    except TwillAssertionError:
        pass

    twill.commands.debug('http', '1')
    twill.commands.debug('http', '0')
    twill.commands.debug('http', '+')
    twill.commands.debug('http', '-')
    
    twill.commands.debug('commands', '0')
    twill.commands.debug('commands', '1')
    try:
        twill.commands.debug('nada', '1')
        assert 0
    except:
        pass

    twill.commands.config()
    twill.commands.config('readonly_controls_writeable')
    twill.commands.config('use_tidy')
    twill.commands.config('require_tidy')
    
    twill.commands.config('readonly_controls_writeable', 1)
    twill.commands.config('use_tidy', 1)
    twill.commands.config('require_tidy', 0)
    
    twill.commands.config('require_tidy', "on")

    twill.commands.run("print 'hello'")
Exemple #7
0
def book_squash(target_datetime):
    global booked
    log_in(login_url, username, password)
    date_str = target_datetime.strftime('%d/%m/%Y')
    time_str = target_datetime.strftime('%H%M')
    time_str_print = target_datetime.strftime('%H:%M')
    print()
    print("Step 2 : Navigating to booking sheets page")
    booking_url = login_url + '/bookings.asp'
    browser.go(booking_url)
    booking_sheet_link = browser.find_link(date_str)
    if not booking_sheet_link:
        print("Could not find booking sheet for {}!".format(date_str))
    print()
    print('Step 3 : Navigating to Booking sheet for', date_str)
    browser.follow_link(
        booking_sheet_link
    )  #https://blackheathsquashclub.mycourts.co.uk/bookings.asp?st1=600&st2=2400&d=13
    print()
    print("Step 4 : Attempting to book a squash court at {} on {}".format(
        time_str, date_str))
    soup = BeautifulSoup(browser.html, 'html.parser')  #removed () after html

    #booking_link = None
    booked = 0
    n = 1
    #Courts 1-4 : Acrylic
    courts = soup.findAll("div", {"class": "nested_column"})
    for div in courts:  # for each court
        court_name = div.find("div", {"class": "courtname"}).getText()
        print('-------------------------------------------')
        print('4.', n, ': Trying Court :', court_name)
        print('-------------------------------------------')
        for court in div.findAll(
                "div", {"class": "court_available"}):  #only avaialble courts
            if court.getText().startswith(time_str):
                #initial booking sheet - book the court
                booking_url2 = court.find(
                    "a",
                    {"class": "book_this_court"})['href'].replace(' ', '%20')
                print(
                    'Step 5. Booking the court for', time_str_print
                )  #<a class="book_this_court" href="bookings_confirm.asp?st1=600&amp;st2=2400&amp;ctid=33684&amp;dt=12 December 2020&amp;tabID=0">book now</a>
                browser.follow_link(
                    booking_url2
                )  #https://blackheathsquashclub.mycourts.co.uk/bookings_confirm.asp?st1=600&st2=2400&ctid=33684&dt=12%20December%202020&tabID=0
                #confirm booking page
                print('Step 6. Confirming the court booking for', court_name,
                      'at', time_str_print, 'on', date_str)
                soup2 = BeautifulSoup(browser.html, 'html.parser')
                if soup2.find("div", {"class": "alert"}):
                    confirm_url = soup2.find_all(
                        'div', class_='alert')[0].find_all('a')[0].get(
                            'href').replace(' ', '%20')  #class = alert
                else:
                    print()
                    print(
                        '============================================================='
                    )
                    print(
                        '%%% Unable to book - too early ? Already have a booking ? %%%'
                    )
                    print(
                        '============================================================='
                    )
                    print()
                    break
                if confirm_url:
                    browser.follow_link(confirm_url)
                    print()
                    print(
                        '======================================================================='
                    )
                    print(
                        '%%%  SUCCESS %%%  Tennis', court_name,
                        'booked for {} on {}'.format(time_str_print, date_str))
                    print(
                        '======================================================================='
                    )
                    print()
                    booked = 1
                    break
                else:
                    print(
                        'Attempt', n,
                        'Didn\'t manage to book court for {} {}'.format(
                            date_str, time_str_print),
                        '- trying the next available court')
                    n = n + 1
            if booked == 1:
                break
        if booked == 1:
            break
    if booked == 0:
        print()
        print(
            'Ran out of available courts and was not able to book any - so sorry'
        )
        print()
    """