-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_manager.py
More file actions
83 lines (67 loc) · 3.24 KB
/
Copy pathsession_manager.py
File metadata and controls
83 lines (67 loc) · 3.24 KB
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
"""Session management for YC Startup School authentication."""
import json
from pathlib import Path
from playwright.sync_api import BrowserContext, Page
from rich.console import Console
console = Console()
class SessionManager:
"""Manages browser session persistence and authentication."""
def __init__(self, storage_path: Path):
"""Initialize session manager."""
self.storage_path = storage_path
def session_exists(self) -> bool:
"""Check if saved session exists."""
return self.storage_path.exists()
def save_session(self, context: BrowserContext):
"""Save browser session to file."""
storage_state = context.storage_state()
with open(self.storage_path, 'w') as f:
json.dump(storage_state, f, indent=2)
console.print(f"[green]✓[/green] Session saved to {self.storage_path}")
def load_session(self) -> dict:
"""Load saved session from file."""
if not self.session_exists():
raise FileNotFoundError(f"Session file not found: {self.storage_path}")
with open(self.storage_path, 'r') as f:
return json.load(f)
def delete_session(self):
"""Delete saved session file."""
if self.storage_path.exists():
self.storage_path.unlink()
console.print(f"[yellow]Session deleted: {self.storage_path}[/yellow]")
def authenticate(self, page: Page, context: BrowserContext):
"""
Interactive authentication flow.
Opens YC Startup School login page and waits for user to log in.
"""
console.print("\n[bold cyan]Authentication Required[/bold cyan]")
console.print("1. A browser window will open")
console.print("2. Log into YC Startup School manually")
console.print("3. Press Enter in this terminal when logged in\n")
# Navigate to YC login page with redirect to cofounder matching
login_url = 'https://account.ycombinator.com/?continue=https%3A%2F%2Fwww.startupschool.org%2Fcofounder-matching'
console.print(f"[dim]Opening: {login_url}[/dim]\n")
page.goto(login_url, timeout=60000)
# Wait for user confirmation
input("Press Enter when you have logged in...")
# Save session
self.save_session(context)
console.print("[green]✓[/green] Authentication complete!")
def validate_session(self, page: Page) -> bool:
"""
Validate that the session is still active.
Returns True if session is valid, False if expired.
"""
try:
# Navigate to cofounder matching page
page.goto('https://www.startupschool.org/cofounder-matching', timeout=30000)
# Check if we're redirected to login (session expired)
current_url = page.url
if 'login' in current_url.lower() or 'signin' in current_url.lower():
return False
# Check for common authenticated elements
page.wait_for_load_state('networkidle', timeout=10000)
return True
except Exception as e:
console.print(f"[yellow]Session validation error: {e}[/yellow]")
return False