Spaces:
Paused
Paused
Commit ·
58c1896
1
Parent(s): 3f85f7b
Create app.py
Browse files- selimium_try.py +17 -11
- setup.sh +3 -0
selimium_try.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
from playwright.sync_api import sync_playwright
|
| 2 |
import json
|
| 3 |
import time
|
|
|
|
| 4 |
|
| 5 |
COOKIE_FILE = "cookies.json"
|
| 6 |
|
| 7 |
|
| 8 |
-
def load_cookies(context):
|
| 9 |
"""Loads cookies from a file if available."""
|
| 10 |
try:
|
| 11 |
with open(COOKIE_FILE, "r") as f:
|
| 12 |
cookies = json.load(f)
|
|
|
|
| 13 |
context.add_cookies(cookies)
|
| 14 |
print("✅ Cookies loaded successfully.")
|
| 15 |
except FileNotFoundError:
|
|
@@ -39,15 +41,22 @@ def is_logged_in_youtube(page):
|
|
| 39 |
|
| 40 |
def login_to_youtube(page):
|
| 41 |
"""Logs into YouTube manually if needed."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
page.goto("https://accounts.google.com/signin/v2/identifier?service=youtube")
|
| 43 |
|
| 44 |
# Enter email
|
| 45 |
-
page.fill("input[type='email']",
|
| 46 |
page.click("button:has-text('Next')")
|
| 47 |
time.sleep(3)
|
| 48 |
|
| 49 |
# Enter password
|
| 50 |
-
page.fill("input[type='password']",
|
| 51 |
page.click("button:has-text('Next')")
|
| 52 |
time.sleep(5)
|
| 53 |
|
|
@@ -58,23 +67,20 @@ def login_to_youtube(page):
|
|
| 58 |
print("❌ Login failed.")
|
| 59 |
return False
|
| 60 |
|
|
|
|
| 61 |
def main():
|
| 62 |
with sync_playwright() as p:
|
| 63 |
-
browser = p.chromium.launch(headless=
|
| 64 |
context = browser.new_context()
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
load_cookies(context)
|
| 68 |
|
| 69 |
-
page = context.new_page()
|
| 70 |
result=is_logged_in_youtube(page)
|
| 71 |
|
| 72 |
-
# if not is_logged_in_youtube(page):
|
| 73 |
-
# if login_to_youtube(page):
|
| 74 |
-
# save_cookies(context)
|
| 75 |
-
|
| 76 |
|
| 77 |
browser.close()
|
|
|
|
| 78 |
return result
|
| 79 |
|
| 80 |
|
|
|
|
| 1 |
from playwright.sync_api import sync_playwright
|
| 2 |
import json
|
| 3 |
import time
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
COOKIE_FILE = "cookies.json"
|
| 7 |
|
| 8 |
|
| 9 |
+
def load_cookies(context, page):
|
| 10 |
"""Loads cookies from a file if available."""
|
| 11 |
try:
|
| 12 |
with open(COOKIE_FILE, "r") as f:
|
| 13 |
cookies = json.load(f)
|
| 14 |
+
page.goto("https://www.youtube.com", wait_until="domcontentloaded") # Ensure page is loaded
|
| 15 |
context.add_cookies(cookies)
|
| 16 |
print("✅ Cookies loaded successfully.")
|
| 17 |
except FileNotFoundError:
|
|
|
|
| 41 |
|
| 42 |
def login_to_youtube(page):
|
| 43 |
"""Logs into YouTube manually if needed."""
|
| 44 |
+
email = os.getenv("YOUTUBE_EMAIL")
|
| 45 |
+
password = os.getenv("YOUTUBE_PASSWORD")
|
| 46 |
+
|
| 47 |
+
if not email or not password:
|
| 48 |
+
print("❌ Missing login credentials! Set YOUTUBE_EMAIL and YOUTUBE_PASSWORD.")
|
| 49 |
+
return False
|
| 50 |
+
|
| 51 |
page.goto("https://accounts.google.com/signin/v2/identifier?service=youtube")
|
| 52 |
|
| 53 |
# Enter email
|
| 54 |
+
page.fill("input[type='email']", email)
|
| 55 |
page.click("button:has-text('Next')")
|
| 56 |
time.sleep(3)
|
| 57 |
|
| 58 |
# Enter password
|
| 59 |
+
page.fill("input[type='password']", password)
|
| 60 |
page.click("button:has-text('Next')")
|
| 61 |
time.sleep(5)
|
| 62 |
|
|
|
|
| 67 |
print("❌ Login failed.")
|
| 68 |
return False
|
| 69 |
|
| 70 |
+
|
| 71 |
def main():
|
| 72 |
with sync_playwright() as p:
|
| 73 |
+
browser = p.chromium.launch(headless=False, args=["--no-sandbox"]) # Use --no-sandbox for Hugging Face Spaces
|
| 74 |
context = browser.new_context()
|
| 75 |
+
page = context.new_page()
|
| 76 |
|
| 77 |
+
load_cookies(context, page)
|
|
|
|
| 78 |
|
|
|
|
| 79 |
result=is_logged_in_youtube(page)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
browser.close()
|
| 83 |
+
|
| 84 |
return result
|
| 85 |
|
| 86 |
|
setup.sh
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
pip install playwright
|
| 3 |
+
playwright install --with-deps chromium
|