Repository navigation
Expand file tree
/
Copy pathnlm_auth.py
More file actions
172 lines (134 loc) · 5.67 KB
/
Copy pathnlm_auth.py
File metadata and controls
172 lines (134 loc) · 5.67 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
"""
NotebookLM GitHub Sync — Setup Script
Authenticates with NotebookLM and configures GitHub secrets in one step.
Run this once to set up, and again whenever your session expires.
Usage:
python nlm_auth.py
python nlm_auth.py --repo owner/repo # target a specific GitHub repo
Without --repo, secrets go to the repo of the current folder, so run it inside a
clone of the repo you want to sync.
"""
import argparse
import json
import os
import subprocess
import sys
from pathlib import Path
CLI_VERSION = "0.15.5"
def run(cmd, capture=False, check=False):
return subprocess.run(cmd, shell=True, capture_output=capture, text=True, check=check)
def profile_dir():
return Path.home() / ".notebooklm-mcp-cli" / "profiles" / "default"
def install_cli():
# Must match the version pinned in action.yml, or the cookies/metadata
# written here may not parse in CI.
result = run("nlm --version", capture=True)
if result.returncode != 0 or CLI_VERSION not in result.stdout + result.stderr:
print(f"Installing notebooklm-mcp-cli=={CLI_VERSION}...")
run(f"pip install notebooklm-mcp-cli=={CLI_VERSION}", check=True)
print()
def authenticate():
print("Opening browser for Google authentication...")
print("Log in with the Google account that has access to your NotebookLM notebook.\n")
result = run("nlm login")
if result.returncode != 0:
print("Error: nlm login failed. Ensure notebooklm-mcp-cli is installed and retry.")
sys.exit(1)
print()
def read_auth():
d = profile_dir()
cookies_path = d / "cookies.json"
metadata_path = d / "metadata.json"
if not cookies_path.exists():
print(f"Error: cookies.json not found at {d}")
print("Authentication may have failed. Re-run nlm_auth.py and complete the browser login.")
sys.exit(1)
cookies = cookies_path.read_text()
metadata = metadata_path.read_text() if metadata_path.exists() else "{}"
return cookies, metadata
def get_notebook_id():
print("Find your NOTEBOOK_ID:")
print(" 1. Go to https://notebooklm.google.com")
print(" 2. Open your notebook")
print(" 3. Copy the ID from the URL: notebooklm.google.com/notebook/<THIS_PART>\n")
notebook_id = input("Paste your NOTEBOOK_ID: ").strip()
if not notebook_id:
print("Error: NOTEBOOK_ID cannot be empty.")
sys.exit(1)
return notebook_id
def set_secret(name, value, repo_flag):
cmd = ["gh", "secret", "set", name]
if repo_flag:
cmd += ["-R", repo_flag]
result = subprocess.run(cmd, input=value, text=True, capture_output=True)
return result
def set_secrets_via_gh(cookies, metadata, notebook_id, repo_flag):
secrets = [
("NOTEBOOKLM_COOKIES", cookies),
("NOTEBOOKLM_METADATA", metadata),
("NOTEBOOK_ID", notebook_id),
]
failed = False
for name, value in secrets:
r = set_secret(name, value, repo_flag)
if r.returncode != 0:
print(f"gh secret set failed for {name}: {r.stderr.strip()}")
failed = True
if failed:
print("Check that you're authenticated with `gh auth login`.")
return False
return True
def target_repo(repo_flag):
if repo_flag:
return repo_flag
r = run("gh repo view --json nameWithOwner -q .nameWithOwner", capture=True)
return r.stdout.strip() if r.returncode == 0 else None
def print_manual_instructions(cookies, metadata, notebook_id):
print("\n" + "=" * 60)
print("Add these secrets to your GitHub repo:")
print("Repo → Settings → Secrets and variables → Actions → New repository secret")
print("=" * 60)
print("\nSecret name: NOTEBOOKLM_COOKIES")
print("Secret value:")
print(cookies)
print("\nSecret name: NOTEBOOKLM_METADATA")
print("Secret value:")
print(metadata)
print("\nSecret name: NOTEBOOK_ID")
print(f"Secret value: {notebook_id}")
print("=" * 60)
def main():
parser = argparse.ArgumentParser(description="Set up NotebookLM GitHub sync authentication.")
parser.add_argument("--repo", help="GitHub repo (owner/repo) to set secrets on. Defaults to the repo of the current folder.")
args = parser.parse_args()
if sys.version_info < (3, 11):
print(f"Error: notebooklm-mcp-cli {CLI_VERSION} needs Python 3.11+, this is {sys.version.split()[0]}.")
sys.exit(1)
print("NotebookLM GitHub Sync — Setup\n")
install_cli()
authenticate()
cookies, metadata = read_auth()
notebook_id = get_notebook_id()
gh_available = run("gh --version", capture=True).returncode == 0
repo = target_repo(args.repo) if gh_available else None
if gh_available and not repo:
print("\nNo GitHub repo found here. Run this inside a clone of the repo you want to sync,")
print("or pass --repo owner/repo. Copy these secrets manually instead:")
print_manual_instructions(cookies, metadata, notebook_id)
elif gh_available:
print(f"\nSetting GitHub secrets on {repo} via gh CLI...")
if set_secrets_via_gh(cookies, metadata, notebook_id, repo):
print("✓ NOTEBOOKLM_COOKIES set")
print("✓ NOTEBOOKLM_METADATA set")
print("✓ NOTEBOOK_ID set")
print("\nSetup complete. Push a .md file to trigger the sync, or run the workflow manually.")
else:
print_manual_instructions(cookies, metadata, notebook_id)
else:
print("\ngh CLI not found — copy these secrets manually:")
print_manual_instructions(cookies, metadata, notebook_id)
print("\nOr install the GitHub CLI (https://cli.github.com) and re-run nlm_auth.py.")
print()
if __name__ == "__main__":
main()