import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Function to send email
def send_email(text):
    textMsg = 'Test - CSK Vs RCB Tickets button text -' + text

    mailId = 'cookbook.videos@gmail.com'
    mailPassword = 'shjw awws xcbk rjgz'
    rcvMailID = ['cookbook.videos@gmail.com', 'aswinb4u@gmail.com', 'mails2ttp@gmail.com']

    server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
    server.login(mailId, mailPassword)

    msg = MIMEMultipart()
    msg['From'] = mailId
    msg['To'] = ', '.join(rcvMailID)
    msg['Subject'] = textMsg

    msg.attach(MIMEText(textMsg, 'plain'))  # Change to 'html' for HTML content
    server.sendmail(mailId, rcvMailID, msg.as_string())
    server.quit()

# Function to check if email has been sent
def check_email_status():
    try:
        # Open the status file
        with open('email_status.txt', 'r') as file:
            status = file.read().strip()
            return status == 'sent'
    except FileNotFoundError:
        return False

# Function to update email status
def update_email_status():
    with open('email_status.txt', 'w') as file:
        file.write('sent')

def check_email_status_1():
    try:
        # Open the status file
        with open('email_status_1.txt', 'r') as file:
            status = file.read().strip()
            return status == 'sent'
    except FileNotFoundError:
        return False

# Function to update email status
def update_email_status_1():
    with open('email_status_1.txt', 'w') as file:
        file.write('sent')

# URL of the webpage
url = "https://shop.royalchallengers.com/ticket"

# Send a GET request to the URL
response = requests.get(url)

# Parse the HTML content
soup = BeautifulSoup(response.content, "html.parser")

# Find the element containing the text "Chennai Super Kings"
team_element = soup.find("p", text="Chennai Super Kings")

# Find the next sibling div containing the button element
next_div = team_element.find_next("div", class_="chakra-stack")

# Find the button element within the next div
button_element = next_div.find("button", class_="chakra-button")

# Extract the text from the button element
button_text = button_element.text.strip()

# Check if email has been sent
if button_text != "SOLD OUT" and not check_email_status():
    send_email(button_text)
    update_email_status()




