-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauthenticator.py
More file actions
73 lines (57 loc) · 2.72 KB
/
Copy pathauthenticator.py
File metadata and controls
73 lines (57 loc) · 2.72 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
import os
import re
try:
from playwright.sync_api import sync_playwright
from playwright.async_api import async_playwright
except ImportError:
sync_playwright = None
async_playwright = None
class ClaudeAuthenticator:
@staticmethod
def process(_):
if sync_playwright is None:
raise ImportError(
"Playwright is not installed. Please install it with 'pip install playwright', after that install "
"Firefox with 'playwright install firefox'")
if 'CLAUDE_COOKIE' in os.environ:
return
print("Please, authenticate to https://claude.ai/ in the Web browser.\nOpening the browser...")
with sync_playwright() as p:
browser = p.firefox.launch(headless=False, slow_mo=50)
page = browser.new_page()
page.goto('https://claude.ai/login')
while True:
cookies = page.context.cookies()
cookies_str = "; ".join([f"{cookie['name']}={cookie['value']}" for cookie in cookies])
if "sessionKey" in cookies_str:
break
os.environ["CLAUDE_COOKIE"] = cookies_str
with open('.env', 'r') as env_file:
existing_env = env_file.read()
if not re.search(r'^CLAUDE_COOKIE=', existing_env, re.MULTILINE):
with open('.env', 'a') as env_file:
env_file.write(f"\nCLAUDE_COOKIE={cookies_str}")
@staticmethod
async def process_async(_):
if async_playwright is None:
raise ImportError(
"Playwright is not installed. Please install it with 'pip install playwright', after that install "
"Firefox with 'playwright install firefox'")
if 'CLAUDE_COOKIE' in os.environ:
return
print("Please, authenticate to https://claude.ai/ in the Web browser.\nOpening the browser...")
async with async_playwright() as p:
browser = await p.firefox.launch(headless=False, slow_mo=50)
page = await browser.new_page()
await page.goto('https://claude.ai/login')
while True:
cookies = await page.context.cookies()
cookies_str = "; ".join([f"{cookie['name']}={cookie['value']}" for cookie in cookies])
if "sessionKey" in cookies_str:
break
os.environ["CLAUDE_COOKIE"] = cookies_str
with open('.env', 'r') as env_file:
existing_env = env_file.read()
if not re.search(r'^CLAUDE_COOKIE=', existing_env, re.MULTILINE):
with open('.env', 'a') as env_file:
env_file.write(f"\nCLAUDE_COOKIE={cookies_str}")