File size: 2,543 Bytes
b5289a3
 
 
58c1896
b5289a3
 
 
 
58c1896
b5289a3
 
 
 
58c1896
b5289a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58c1896
 
 
 
 
 
 
b5289a3
 
 
58c1896
b5289a3
 
 
 
58c1896
b5289a3
 
 
 
 
 
 
 
 
 
58c1896
b5289a3
 
6cde2cf
 
b5289a3
58c1896
b5289a3
58c1896
b5289a3
 
 
 
 
58c1896
b5289a3
f17d41b
 
 
 
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
from playwright.sync_api import sync_playwright
import json
import time
import os

COOKIE_FILE = "cookies.json"


def load_cookies(context, page):
    """Loads cookies from a file if available."""
    try:
        with open(COOKIE_FILE, "r") as f:
            cookies = json.load(f)
            page.goto("https://www.youtube.com", wait_until="domcontentloaded")  # Ensure page is loaded
            context.add_cookies(cookies)
            print("✅ Cookies loaded successfully.")
    except FileNotFoundError:
        print("⚠️ No cookies found. Logging in manually.")


def save_cookies(context):
    """Saves cookies to a file."""
    cookies = context.cookies()
    with open(COOKIE_FILE, "w") as f:
        json.dump(cookies, f)
        print("✅ Cookies saved successfully.")


def is_logged_in_youtube(page):
    """Checks if the user is logged into YouTube."""
    page.goto("https://www.youtube.com", wait_until="domcontentloaded")
    time.sleep(2)  # Wait for elements to load
    try:
        page.wait_for_selector("button#avatar-btn", timeout=5000)
        print("✅ You are already logged in to YouTube!")
        return True
    except:
        print("❌ You are NOT logged in to YouTube.")
        return False


def login_to_youtube(page):
    """Logs into YouTube manually if needed."""
    email = os.getenv("YOUTUBE_EMAIL")
    password = os.getenv("YOUTUBE_PASSWORD")

    if not email or not password:
        print("❌ Missing login credentials! Set YOUTUBE_EMAIL and YOUTUBE_PASSWORD.")
        return False

    page.goto("https://accounts.google.com/signin/v2/identifier?service=youtube")

    # Enter email
    page.fill("input[type='email']", email)
    page.click("button:has-text('Next')")
    time.sleep(3)

    # Enter password
    page.fill("input[type='password']", password)
    page.click("button:has-text('Next')")
    time.sleep(5)

    if is_logged_in_youtube(page):
        print("✅ Login successful.")
        return True
    else:
        print("❌ Login failed.")
        return False


def main():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False,
            executable_path="/home/user/.cache/ms-playwright/chromium/chrome-linux/chrome", args=["--no-sandbox"])  # Use --no-sandbox for Hugging Face Spaces
        context = browser.new_context()
        page = context.new_page()

        load_cookies(context, page)

        result=is_logged_in_youtube(page)


        browser.close()

        return result


if __name__ == "__main__":
    main()