File size: 1,749 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
from venv import logger
from login import WeBookLogin
from book import WeBookBooking
from utils import WeBookBase # Import WeBookBase for cleanup
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

def main():
    profile_dir = "./webook_profile"
    
    # Instantiate login and perform login
    login = WeBookLogin(
        email=os.getenv('WEBBOOK_EMAIL'),
        password=os.getenv('WEBBOOK_PASSWORD'),
        profile_dir=profile_dir
    )
    
    # login() now returns the page object on success, or False on failure
    page = login.login() 
    
    if page: # Check if login was successful and returned a page object
        logger.info("Login successful, proceeding with booking.")
        # Instantiate booking with the existing page object
        booking = WeBookBooking(page=page, profile_dir=profile_dir)
        event_url = "https://webook.com/en/events/mdlbeast-beast-house-ec/book"
        booking.book_event(event_url)
        
        # Perform final cleanup using the context/playwright from the login object
        # The page object is part of the context, so closing context is enough.
        if login.context: # Check if context was created by login
             login.context.close()
        if login.playwright: # Check if playwright was started by login
             login.playwright.stop()
             logger.info("Browser closed after booking.")

    else:
        logger.error("Login failed. Cannot proceed with booking.")
        # Cleanup should already be handled within login() on failure, but a final check
        if login.context:
             login.context.close()
        if login.playwright:
             login.playwright.stop()


if __name__ == "__main__":
    main()