File size: 4,920 Bytes
f959aff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import time # Import the time module
from utils import WeBookBase, logger

class WeBookBooking(WeBookBase):
    def __init__(self, page, profile_dir=None):
        super().__init__(profile_dir, page=page)
        if not self.page:
             raise ValueError("A Playwright page object must be provided to WeBookBooking")

    def book_event(self, event_url):
        try:
            logger.info(f"Navigating to event booking page: {event_url}")
            self.page.goto(event_url, wait_until='networkidle')
            
            self._handle_cookies()
            
            # Locate and click the '+' button for the first ticket offer
            # Locate the button containing the specific '+' SVG icon
            try:
                # We are looking for a button that contains an SVG with the specific path
                plus_button = self.page.locator('button:has(svg path[d="M7 1C7 0.447715 6.55228 0 6 0C5.44772 0 5 0.447715 5 1V5H1C0.447715 5 0 5.44772 0 6C0 6.55228 0.447715 7 1 7H5V11C5 11.5523 5.44772 12 6 12C6.55228 12 7 11.5523 7 11V7H11C11.5523 7 12 6.55228 12 6C12 5.44772 11.5523 5 11 5H7V1Z"])').first
                
                if plus_button.is_visible():
                    plus_button.click()
                    logger.info("Clicked '+' button (SVG) for the first ticket offer")

                
                    # Wait for the main summary/payment button to become visible
                    summary_button = self.page.locator('[data-testid="ticketing_tickets_go_to_summary_button"]')
                    summary_button.wait_for(state='visible', timeout=5000) # Wait up to 5 seconds

                else:
                    logger.warning("Could not find the '+' button (SVG) for ticket selection.")
                    return False # Exit if ticket selection failed

            except Exception as click_e:
                logger.error(f"Failed to click '+' button (SVG) or wait for summary button: {str(click_e)}")
                return False # Exit on error

            # Locate and click the main summary/payment button using its data-testid
            if summary_button.is_visible():
                summary_button.click()
                logger.info("Clicked main summary/payment button (using data-testid)")

                # --- New Step: Wait for and click the "Skip to Payment" button ---
                # Use a more specific selector for the Skip to checkout button
                skip_button = self.page.get_by_role("button", name="Skip to checkout")
                try:
                    skip_button.wait_for(state='visible', timeout=5000) # Wait up to 5 seconds for the skip button
                    if skip_button.is_visible():
                        skip_button.click()
                        logger.info("Clicked 'Skip to Payment' button")
                    else:
                        logger.warning("'Skip to Payment' button not visible after waiting.")
                        return False
                except Exception as skip_e:
                     logger.error(f"Failed to wait for or click 'Skip to Payment' button: {str(skip_e)}")
                     return False # Exit on error during skip button click
                # --- End New Step ---

            else:
                 logger.warning("Main summary/payment button not visible after waiting.")
                 return False # Exit if summary button is not visible

            # Wait for and click the terms checkbox
            terms_checkbox = self.page.locator('[data-testid="ticketing_summary_resell_terms_checkbox"]')
            if terms_checkbox.is_visible():
                terms_checkbox.click()
                logger.info("Accepted terms and conditions")
            else:
                 logger.warning("Could not find the terms checkbox.") # Might be critical depending on site
                 # Consider returning False here if accepting terms is mandatory
                 # return False
                 
            

            # Add final step to click the Confirm & pay button
            confirm_button = self.page.locator('[data-testid="ticketing_summary_proceed_to_payment"]')
            try:
                confirm_button.wait_for(state='visible', timeout=5000)
                if confirm_button.is_visible():
                    confirm_button.click()
                    logger.info("Clicked 'Confirm & pay' button")
                else:
                    logger.warning("'Confirm & pay' button not visible after waiting.")
                    return False
            except Exception as confirm_e:
                logger.error(f"Failed to wait for or click 'Confirm & pay' button: {str(confirm_e)}")
                return False
            
            time.sleep(330)

            return True
            
        except Exception as e:
            logger.error(f"Booking process failed: {str(e)}")
            return False