ytstudio-cli 0.1.1__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/PKG-INFO +70 -2
- ytstudio_cli-0.2.0/README.md +137 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/pyproject.toml +2 -1
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/api.py +120 -29
- ytstudio_cli-0.2.0/src/ytstudio/commands/livestreams.py +694 -0
- ytstudio_cli-0.2.0/src/ytstudio/commands/profile.py +96 -0
- ytstudio_cli-0.2.0/src/ytstudio/config.py +262 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/demo.py +80 -0
- ytstudio_cli-0.2.0/src/ytstudio/demo_data/broadcasts.json +83 -0
- ytstudio_cli-0.2.0/src/ytstudio/demo_data/live_streams.json +34 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/main.py +14 -4
- ytstudio_cli-0.2.0/src/ytstudio/services.py +14 -0
- ytstudio_cli-0.2.0/tests/test_api.py +188 -0
- ytstudio_cli-0.2.0/tests/test_config.py +208 -0
- ytstudio_cli-0.2.0/tests/test_livestreams.py +460 -0
- ytstudio_cli-0.2.0/tests/test_profile.py +126 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/uv.lock +1 -1
- ytstudio_cli-0.1.1/README.md +0 -69
- ytstudio_cli-0.1.1/src/ytstudio/config.py +0 -74
- ytstudio_cli-0.1.1/src/ytstudio/services.py +0 -14
- ytstudio_cli-0.1.1/tests/test_api.py +0 -60
- ytstudio_cli-0.1.1/tests/test_config.py +0 -47
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/.editorconfig +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/.github/workflows/ci.yml +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/.gitignore +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/.pre-commit-config.yaml +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/.python-version +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/LICENSE +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/demo.gif +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/__init__.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/commands/__init__.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/commands/analytics.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/commands/comments.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/commands/videos.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/demo_data/analytics_metrics.json +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/demo_data/channel.json +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/demo_data/comments.json +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/demo_data/playlist_items.json +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/demo_data/videos.json +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/registry.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/ui.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/src/ytstudio/version.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/tests/__init__.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/tests/conftest.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/tests/test_analytics.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/tests/test_comments.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/tests/test_registry.py +0 -0
- {ytstudio_cli-0.1.1 → ytstudio_cli-0.2.0}/tests/test_videos.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ytstudio-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: CLI tool to manage and analyze your YouTube channel from the terminal
|
|
5
5
|
Project-URL: Homepage, https://github.com/jdwit/ytstudio-cli
|
|
6
6
|
Project-URL: Repository, https://github.com/jdwit/ytstudio-cli
|
|
@@ -51,6 +51,8 @@ I recommend the excellent [uv](https://uv.io/) tool for installation:
|
|
|
51
51
|
uv tool install ytstudio-cli
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
The CLI is installed as `ytstudio`; `yts` is a short alias for the same entry point.
|
|
55
|
+
|
|
54
56
|
## Setup
|
|
55
57
|
|
|
56
58
|
1. Create a [Google Cloud project](https://console.cloud.google.com/)
|
|
@@ -73,7 +75,73 @@ ytstudio init --client-secrets path/to/client_secret_<id>.json
|
|
|
73
75
|
ytstudio login
|
|
74
76
|
```
|
|
75
77
|
|
|
76
|
-
Credentials stored in `~/.config/ytstudio/`.
|
|
78
|
+
Credentials stored in `~/.config/ytstudio-cli/`.
|
|
79
|
+
|
|
80
|
+
### Headless Linux login
|
|
81
|
+
|
|
82
|
+
If you are logging in from a server without a browser, run:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
ytstudio login --headless
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The command prints a Google OAuth URL. Open that URL in a browser on any machine and approve
|
|
89
|
+
access. The browser will then fail to load a `127.0.0.1` page; this is expected. Copy the full URL
|
|
90
|
+
from the browser address bar, paste it back into the terminal, and ytstudio will finish the login.
|
|
91
|
+
|
|
92
|
+
## Managing multiple channels
|
|
93
|
+
|
|
94
|
+
If you run more than one YouTube channel, ytstudio stores each login under its own named profile.
|
|
95
|
+
Every command (`videos`, `analytics`, `comments`, ...) operates on the active channel.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
ytstudio profile add work # authenticate a new channel and make it active
|
|
99
|
+
ytstudio profile add personal # add another
|
|
100
|
+
ytstudio profile list # show profiles; the active one is marked with *
|
|
101
|
+
ytstudio profile use work # switch the active profile
|
|
102
|
+
ytstudio profile status work # auth status for one profile (defaults to active)
|
|
103
|
+
ytstudio profile remove personal
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
For scripting you can override the active channel per command without switching:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
YTSTUDIO_PROFILE=work ytstudio videos list
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Credentials live in `~/.config/ytstudio-cli/profiles/<name>/` with owner-only (`600`) permissions.
|
|
113
|
+
The shared OAuth client secrets stay at the top level, so `ytstudio init` is only needed once.
|
|
114
|
+
A plain `ytstudio login` (no `profile add`) authenticates the active profile, which is `default`
|
|
115
|
+
on a fresh setup. Existing single-channel installs are migrated to the `default` profile
|
|
116
|
+
automatically on first run.
|
|
117
|
+
|
|
118
|
+
## Live broadcasts
|
|
119
|
+
|
|
120
|
+
Manage your YouTube livestream broadcasts from the CLI. This drives YouTube's
|
|
121
|
+
`liveBroadcasts` API (scheduling, starting, stopping, updating); the actual
|
|
122
|
+
video push to YouTube still goes through your encoder of choice (OBS, etc.).
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
ytstudio livestreams list --status upcoming # what is scheduled
|
|
126
|
+
ytstudio livestreams show <broadcast-id> # details for one broadcast
|
|
127
|
+
ytstudio livestreams show <id> --ingest # also fetch the bound stream's RTMP URL
|
|
128
|
+
ytstudio livestreams show <id> --show-key # reveal the stream key (treat as secret)
|
|
129
|
+
ytstudio livestreams schedule \
|
|
130
|
+
--title "Title" --scheduled-start 2026-06-01T19:00:00+02:00 # dry-run
|
|
131
|
+
ytstudio livestreams schedule ... --execute # actually create
|
|
132
|
+
ytstudio livestreams start <id> # transition to live
|
|
133
|
+
ytstudio livestreams start <id> --to testing # transition to the monitor stream first
|
|
134
|
+
ytstudio livestreams stop <id> # transition to complete
|
|
135
|
+
ytstudio livestreams update <id> --privacy unlisted --no-dvr # dry-run by default
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Notes:
|
|
139
|
+
|
|
140
|
+
- `schedule` and `update` default to a dry-run preview; pass `--execute` to apply.
|
|
141
|
+
- `--show-key` on `show` reveals the bound stream key; without it the key is
|
|
142
|
+
redacted. Stream keys are credentials.
|
|
143
|
+
- `liveBroadcasts.insert`/`update` are write operations (~50 quota units each);
|
|
144
|
+
see [API quota](#api-quota) below.
|
|
77
145
|
|
|
78
146
|
## API quota
|
|
79
147
|
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# YT Studio CLI
|
|
2
|
+
|
|
3
|
+
[](https://github.com/jdwit/ytstudio/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/ytstudio-cli/)
|
|
5
|
+
[](https://pypi.org/project/ytstudio-cli/)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
Manage and analyze your YouTube channel from the terminal. Ideal for automation and AI workflows.
|
|
9
|
+
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
## Motivation
|
|
13
|
+
|
|
14
|
+
I built this because I needed to bulk update video titles for a YouTube channel I manage with 300+
|
|
15
|
+
videos. YouTube Studio does not support bulk search-replace operations, which made it a tedious
|
|
16
|
+
manual process. This tool uses the YouTube Data API to perform bulk operations on video metadata.
|
|
17
|
+
Furthermore, it provides features for analytics and comment moderation, all accesible from the
|
|
18
|
+
command line.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
I recommend the excellent [uv](https://uv.io/) tool for installation:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
uv tool install ytstudio-cli
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The CLI is installed as `ytstudio`; `yts` is a short alias for the same entry point.
|
|
29
|
+
|
|
30
|
+
## Setup
|
|
31
|
+
|
|
32
|
+
1. Create a [Google Cloud project](https://console.cloud.google.com/)
|
|
33
|
+
1. Enable **YouTube Data API v3** and **YouTube Analytics API**
|
|
34
|
+
1. Configure OAuth consent screen:
|
|
35
|
+
- Go to **APIs & Services** → **OAuth consent screen**
|
|
36
|
+
- Select **External** and create
|
|
37
|
+
- Fill in app name and your email
|
|
38
|
+
- Skip scopes, then add yourself as a test user
|
|
39
|
+
- Leave the app in "Testing" mode (no verification needed)
|
|
40
|
+
1. Create OAuth credentials:
|
|
41
|
+
- Go to **APIs & Services** → **Credentials**
|
|
42
|
+
- Click **Create Credentials** → **OAuth client ID**how
|
|
43
|
+
- Select **Desktop app** as application type
|
|
44
|
+
- Download the JSON file
|
|
45
|
+
1. Configure ytstudio:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
ytstudio init --client-secrets path/to/client_secret_<id>.json
|
|
49
|
+
ytstudio login
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Credentials stored in `~/.config/ytstudio-cli/`.
|
|
53
|
+
|
|
54
|
+
### Headless Linux login
|
|
55
|
+
|
|
56
|
+
If you are logging in from a server without a browser, run:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
ytstudio login --headless
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The command prints a Google OAuth URL. Open that URL in a browser on any machine and approve
|
|
63
|
+
access. The browser will then fail to load a `127.0.0.1` page; this is expected. Copy the full URL
|
|
64
|
+
from the browser address bar, paste it back into the terminal, and ytstudio will finish the login.
|
|
65
|
+
|
|
66
|
+
## Managing multiple channels
|
|
67
|
+
|
|
68
|
+
If you run more than one YouTube channel, ytstudio stores each login under its own named profile.
|
|
69
|
+
Every command (`videos`, `analytics`, `comments`, ...) operates on the active channel.
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
ytstudio profile add work # authenticate a new channel and make it active
|
|
73
|
+
ytstudio profile add personal # add another
|
|
74
|
+
ytstudio profile list # show profiles; the active one is marked with *
|
|
75
|
+
ytstudio profile use work # switch the active profile
|
|
76
|
+
ytstudio profile status work # auth status for one profile (defaults to active)
|
|
77
|
+
ytstudio profile remove personal
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
For scripting you can override the active channel per command without switching:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
YTSTUDIO_PROFILE=work ytstudio videos list
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Credentials live in `~/.config/ytstudio-cli/profiles/<name>/` with owner-only (`600`) permissions.
|
|
87
|
+
The shared OAuth client secrets stay at the top level, so `ytstudio init` is only needed once.
|
|
88
|
+
A plain `ytstudio login` (no `profile add`) authenticates the active profile, which is `default`
|
|
89
|
+
on a fresh setup. Existing single-channel installs are migrated to the `default` profile
|
|
90
|
+
automatically on first run.
|
|
91
|
+
|
|
92
|
+
## Live broadcasts
|
|
93
|
+
|
|
94
|
+
Manage your YouTube livestream broadcasts from the CLI. This drives YouTube's
|
|
95
|
+
`liveBroadcasts` API (scheduling, starting, stopping, updating); the actual
|
|
96
|
+
video push to YouTube still goes through your encoder of choice (OBS, etc.).
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
ytstudio livestreams list --status upcoming # what is scheduled
|
|
100
|
+
ytstudio livestreams show <broadcast-id> # details for one broadcast
|
|
101
|
+
ytstudio livestreams show <id> --ingest # also fetch the bound stream's RTMP URL
|
|
102
|
+
ytstudio livestreams show <id> --show-key # reveal the stream key (treat as secret)
|
|
103
|
+
ytstudio livestreams schedule \
|
|
104
|
+
--title "Title" --scheduled-start 2026-06-01T19:00:00+02:00 # dry-run
|
|
105
|
+
ytstudio livestreams schedule ... --execute # actually create
|
|
106
|
+
ytstudio livestreams start <id> # transition to live
|
|
107
|
+
ytstudio livestreams start <id> --to testing # transition to the monitor stream first
|
|
108
|
+
ytstudio livestreams stop <id> # transition to complete
|
|
109
|
+
ytstudio livestreams update <id> --privacy unlisted --no-dvr # dry-run by default
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Notes:
|
|
113
|
+
|
|
114
|
+
- `schedule` and `update` default to a dry-run preview; pass `--execute` to apply.
|
|
115
|
+
- `--show-key` on `show` reveals the bound stream key; without it the key is
|
|
116
|
+
redacted. Stream keys are credentials.
|
|
117
|
+
- `liveBroadcasts.insert`/`update` are write operations (~50 quota units each);
|
|
118
|
+
see [API quota](#api-quota) below.
|
|
119
|
+
|
|
120
|
+
## API quota
|
|
121
|
+
|
|
122
|
+
The YouTube Data API enforces a default quota of 10_000 units per project per day. Most read
|
|
123
|
+
operations (listing videos, comments, channel info) cost 1 unit, while write operations like
|
|
124
|
+
updating video metadata or moderating comments cost 50 units each. Bulk updates can consume quota
|
|
125
|
+
quickly. When exceeded, the API returns a 403 error; quota resets at midnight Pacific Time.
|
|
126
|
+
|
|
127
|
+
You can request a quota increase via **IAM & Admin** → **Quotas** in the
|
|
128
|
+
[Google Cloud Console](https://console.cloud.google.com/). See the
|
|
129
|
+
[official quota documentation](https://developers.google.com/youtube/v3/getting-started#quota) for
|
|
130
|
+
full details.
|
|
131
|
+
|
|
132
|
+
## Disclaimer
|
|
133
|
+
|
|
134
|
+
This project is not affiliated with or endorsed by Google. YouTube and YouTube Studio are trademarks
|
|
135
|
+
of Google. All channel data is accessed exclusively through the official
|
|
136
|
+
[YouTube Data API](https://developers.google.com/youtube/v3) and
|
|
137
|
+
[YouTube Analytics API](https://developers.google.com/youtube/analytics).
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "ytstudio-cli"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.0"
|
|
4
4
|
description = "CLI tool to manage and analyze your YouTube channel from the terminal"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = { text = "MIT" }
|
|
@@ -55,6 +55,7 @@ exclude_lines = [
|
|
|
55
55
|
|
|
56
56
|
[project.scripts]
|
|
57
57
|
ytstudio = "ytstudio.main:app"
|
|
58
|
+
yts = "ytstudio.main:app"
|
|
58
59
|
|
|
59
60
|
[project.urls]
|
|
60
61
|
Homepage = "https://github.com/jdwit/ytstudio-cli"
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
from urllib.parse import parse_qs, urlparse
|
|
2
|
+
|
|
1
3
|
import typer
|
|
2
4
|
from google.auth.exceptions import RefreshError
|
|
3
5
|
from google.auth.transport.requests import Request
|
|
@@ -5,12 +7,15 @@ from google.oauth2.credentials import Credentials
|
|
|
5
7
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
6
8
|
from googleapiclient.discovery import build
|
|
7
9
|
from googleapiclient.errors import HttpError
|
|
10
|
+
from rich.prompt import Prompt
|
|
8
11
|
|
|
9
12
|
from ytstudio.config import (
|
|
10
13
|
CLIENT_SECRETS_FILE,
|
|
11
14
|
clear_credentials,
|
|
15
|
+
get_active_profile,
|
|
12
16
|
load_credentials,
|
|
13
17
|
save_credentials,
|
|
18
|
+
save_profile_meta,
|
|
14
19
|
)
|
|
15
20
|
from ytstudio.ui import console, success_message
|
|
16
21
|
|
|
@@ -60,27 +65,17 @@ SCOPES = [
|
|
|
60
65
|
"https://www.googleapis.com/auth/yt-analytics.readonly",
|
|
61
66
|
]
|
|
62
67
|
|
|
68
|
+
HEADLESS_REDIRECT_URI = "http://127.0.0.1:9876/"
|
|
63
69
|
|
|
64
|
-
def authenticate() -> None:
|
|
65
|
-
if not CLIENT_SECRETS_FILE.exists():
|
|
66
|
-
console.print("[red]No client secrets found. Run 'ytstudio init' first.[/red]")
|
|
67
|
-
raise SystemExit(1) from None
|
|
68
70
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
flow = InstalledAppFlow.from_client_secrets_file(
|
|
71
|
+
def _create_flow() -> InstalledAppFlow:
|
|
72
|
+
return InstalledAppFlow.from_client_secrets_file(
|
|
72
73
|
str(CLIENT_SECRETS_FILE),
|
|
73
74
|
scopes=SCOPES,
|
|
74
75
|
)
|
|
75
76
|
|
|
76
|
-
# Run local server for OAuth callback
|
|
77
|
-
credentials = flow.run_local_server(
|
|
78
|
-
port=9876,
|
|
79
|
-
prompt="consent",
|
|
80
|
-
open_browser=True,
|
|
81
|
-
)
|
|
82
77
|
|
|
83
|
-
|
|
78
|
+
def _save_credentials(credentials: Credentials, profile: str | None = None) -> None:
|
|
84
79
|
creds_data = {
|
|
85
80
|
"token": credentials.token,
|
|
86
81
|
"refresh_token": credentials.refresh_token,
|
|
@@ -89,22 +84,114 @@ def authenticate() -> None:
|
|
|
89
84
|
"client_secret": credentials.client_secret,
|
|
90
85
|
"scopes": credentials.scopes,
|
|
91
86
|
}
|
|
92
|
-
save_credentials(creds_data)
|
|
87
|
+
save_credentials(creds_data, profile)
|
|
93
88
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
89
|
+
|
|
90
|
+
def _fetch_channel_info(credentials: Credentials) -> dict | None:
|
|
91
|
+
# Best-effort: a quota/network error here must not fail a successful login.
|
|
92
|
+
try:
|
|
93
|
+
service = build("youtube", "v3", credentials=credentials)
|
|
94
|
+
response = service.channels().list(part="snippet", mine=True).execute()
|
|
95
|
+
except Exception:
|
|
96
|
+
return None
|
|
97
|
+
|
|
98
|
+
items = response.get("items")
|
|
99
|
+
if not items:
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
snippet = items[0]["snippet"]
|
|
103
|
+
return {
|
|
104
|
+
"id": items[0].get("id", ""),
|
|
105
|
+
"title": snippet.get("title", ""),
|
|
106
|
+
"custom_url": snippet.get("customUrl", ""),
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _show_login_success(credentials: Credentials, profile: str) -> None:
|
|
111
|
+
info = _fetch_channel_info(credentials)
|
|
97
112
|
|
|
98
113
|
console.print()
|
|
99
|
-
if
|
|
100
|
-
|
|
101
|
-
success_message(f"Logged in as: {
|
|
114
|
+
if info:
|
|
115
|
+
save_profile_meta(profile, info)
|
|
116
|
+
success_message(f"Logged in as: {info['title']}")
|
|
102
117
|
else:
|
|
103
118
|
success_message("Authentication successful")
|
|
104
119
|
|
|
105
120
|
|
|
106
|
-
def
|
|
107
|
-
|
|
121
|
+
def _parse_authorization_response(authorization_response: str, expected_state: str) -> str:
|
|
122
|
+
parsed_url = urlparse(authorization_response)
|
|
123
|
+
query = parse_qs(parsed_url.query)
|
|
124
|
+
|
|
125
|
+
if error := query.get("error"):
|
|
126
|
+
error_description = query.get("error_description", [""])[0]
|
|
127
|
+
message = error_description or error[0]
|
|
128
|
+
console.print(f"[red]Authorization failed: {message}[/red]")
|
|
129
|
+
raise SystemExit(1) from None
|
|
130
|
+
|
|
131
|
+
if not query.get("code"):
|
|
132
|
+
console.print("[red]Redirect URL is missing an authorization code.[/red]")
|
|
133
|
+
raise SystemExit(1) from None
|
|
134
|
+
|
|
135
|
+
state = query.get("state", [""])[0]
|
|
136
|
+
if state != expected_state:
|
|
137
|
+
console.print("[red]Redirect URL state does not match this login attempt.[/red]")
|
|
138
|
+
raise SystemExit(1) from None
|
|
139
|
+
|
|
140
|
+
return query["code"][0]
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _authenticate_headless() -> Credentials:
|
|
144
|
+
flow = _create_flow()
|
|
145
|
+
flow.redirect_uri = HEADLESS_REDIRECT_URI
|
|
146
|
+
authorization_url, state = flow.authorization_url(prompt="consent")
|
|
147
|
+
|
|
148
|
+
console.print("Open this URL in a browser on any machine:\n")
|
|
149
|
+
console.print(f"[bold]{authorization_url}[/bold]\n")
|
|
150
|
+
console.print(
|
|
151
|
+
"After approving access, the browser will fail to load a 127.0.0.1 page. "
|
|
152
|
+
"Copy the full failed redirect URL from the address bar and paste it below."
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
authorization_response = Prompt.ask("Redirect URL").strip()
|
|
156
|
+
code = _parse_authorization_response(authorization_response, state)
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
flow.fetch_token(code=code)
|
|
160
|
+
except Exception as error:
|
|
161
|
+
console.print(f"[red]Could not complete OAuth token exchange: {error}[/red]")
|
|
162
|
+
raise SystemExit(1) from None
|
|
163
|
+
|
|
164
|
+
return flow.credentials
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def authenticate(headless: bool = False, profile: str | None = None) -> None:
|
|
168
|
+
if not CLIENT_SECRETS_FILE.exists():
|
|
169
|
+
console.print("[red]No client secrets found. Run 'ytstudio init' first.[/red]")
|
|
170
|
+
raise SystemExit(1) from None
|
|
171
|
+
|
|
172
|
+
# Resolve the target profile up-front: a profile switch during a long OAuth
|
|
173
|
+
# flow must not redirect the freshly minted credentials to a different one.
|
|
174
|
+
profile = profile or get_active_profile()
|
|
175
|
+
|
|
176
|
+
console.print("[bold]Authenticating with YouTube...[/bold]\n")
|
|
177
|
+
|
|
178
|
+
if headless:
|
|
179
|
+
credentials = _authenticate_headless()
|
|
180
|
+
else:
|
|
181
|
+
flow = _create_flow()
|
|
182
|
+
credentials = flow.run_local_server(
|
|
183
|
+
port=9876,
|
|
184
|
+
prompt="consent",
|
|
185
|
+
open_browser=True,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
_save_credentials(credentials, profile)
|
|
189
|
+
_show_login_success(credentials, profile)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def get_credentials(profile: str | None = None) -> Credentials | None:
|
|
193
|
+
profile = profile or get_active_profile()
|
|
194
|
+
creds_data = load_credentials(profile)
|
|
108
195
|
if not creds_data:
|
|
109
196
|
return None
|
|
110
197
|
|
|
@@ -127,27 +214,31 @@ def get_credentials() -> Credentials | None:
|
|
|
127
214
|
raise SystemExit(1) from None
|
|
128
215
|
# Save refreshed credentials
|
|
129
216
|
creds_data["token"] = credentials.token
|
|
130
|
-
save_credentials(creds_data)
|
|
217
|
+
save_credentials(creds_data, profile)
|
|
131
218
|
|
|
132
219
|
return credentials
|
|
133
220
|
|
|
134
221
|
|
|
135
|
-
def get_authenticated_service(
|
|
136
|
-
|
|
222
|
+
def get_authenticated_service(
|
|
223
|
+
api_name: str = "youtube",
|
|
224
|
+
version: str = "v3",
|
|
225
|
+
profile: str | None = None,
|
|
226
|
+
):
|
|
227
|
+
credentials = get_credentials(profile)
|
|
137
228
|
if not credentials:
|
|
138
229
|
console.print("[red]Not authenticated. Run 'ytstudio login' first.[/red]")
|
|
139
230
|
raise typer.Exit(1)
|
|
140
231
|
return build(api_name, version, credentials=credentials)
|
|
141
232
|
|
|
142
233
|
|
|
143
|
-
def get_status() -> None:
|
|
144
|
-
creds_data = load_credentials()
|
|
234
|
+
def get_status(profile: str | None = None) -> None:
|
|
235
|
+
creds_data = load_credentials(profile)
|
|
145
236
|
|
|
146
237
|
if not creds_data:
|
|
147
238
|
console.print("[yellow]Not authenticated. Run 'ytstudio login' to authenticate.[/yellow]")
|
|
148
239
|
return
|
|
149
240
|
|
|
150
|
-
credentials = get_credentials()
|
|
241
|
+
credentials = get_credentials(profile)
|
|
151
242
|
if not credentials or not credentials.valid:
|
|
152
243
|
console.print(
|
|
153
244
|
"[yellow]Credentials expired. Run 'ytstudio login' to re-authenticate.[/yellow]"
|