import requests
from bs4 import BeautifulSoup

# URL for the login page
login_page_url = 'https://login.olx.ro/?cc=eyJjYyI6MSwiZ3JvdXBzIjoiQzAwMDE6MSxDMDAwMjoxLEMwMDAzOjEsQzAwMDQ6MSxnYWQ6MSJ9&client_id=7gantjdsv7233vniq4dthhm2hh&code_challenge=6pjXEoADFtKPOvg1dEKBQrb0Ook3u1TBeQbR13UM5hc&code_challenge_method=S256&lang=ro&redirect_uri=https%3A%2F%2Fwww.olx.ro%2Fd%2Fcallback%2F&st=eyJzbCI6IjE5MDdlNWQxYmM3eDRjYzYyOTM4IiwicyI6IjE5MWRkOTI2ZmZmeDc3OTI4M2ExIn0%3D&state=c3RZWGJxYzRIV1lWWk9LNlZiZUJOVi1QUFpWSTFUR0VKRDBfY1M2MmZNTg%3D%3D'  # Replace with the actual URL

# Start a session to persist cookies across requests
session = requests.Session()

# Get the login page to fetch any hidden form data (e.g., CSRF token)
login_page_response = session.get(login_page_url)
soup = BeautifulSoup(login_page_response.content, 'html.parser')

# If there's a CSRF token or other hidden fields, extract it here (if necessary)
# csrf_token = soup.find('input', {'name': 'csrf_token'})['value']

# Login URL (usually found in the form's action attribute)
login_url = 'https://login.olx.ro/?cc=eyJjYyI6MSwiZ3JvdXBzIjoiQzAwMDE6MSxDMDAwMjoxLEMwMDAzOjEsQzAwMDQ6MSxnYWQ6MSJ9&client_id=7gantjdsv7233vniq4dthhm2hh&code_challenge=6pjXEoADFtKPOvg1dEKBQrb0Ook3u1TBeQbR13UM5hc&code_challenge_method=S256&lang=ro&redirect_uri=https%3A%2F%2Fwww.olx.ro%2Fd%2Fcallback%2F&st=eyJzbCI6IjE5MDdlNWQxYmM3eDRjYzYyOTM4IiwicyI6IjE5MWRkOTI2ZmZmeDc3OTI4M2ExIn0%3D&state=c3RZWGJxYzRIV1lWWk9LNlZiZUJOVi1QUFpWSTFUR0VKRDBfY1M2MmZNTg%3D%3D'  # Replace with the actual login URL

# Data to submit with the login form
login_data = {
    'username': 'sebisebi135@gmail.com',  # Replace with your email
    'password': 'Topoloveni2',           # Replace with your password
    # 'csrf_token': csrf_token  # Include the CSRF token if required
}

# Send a POST request to log in
login_response = session.post(login_url, data=login_data)

# Check if login was successful (status code 200 and presence of an element from the authenticated page)
if login_response.status_code == 200 and 'Dashboard' in login_response.text:
    print("Login successful!")

    # Now you can access the protected content
    protected_page_url = 'https://www.olx.ro/d/myaccount'  # Replace with the actual URL
    protected_page = session.get(protected_page_url)
    
    # Print or process the protected page content
    print(protected_page.text)

else:
    print("Login failed!")
