@wizardwarlocks/tubex 0.0.0-stage → 0.1.1
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.
- package/LICENSE +21 -0
- package/README.md +200 -2
- package/bin/tubex.js +8 -0
- package/package.json +50 -4
- package/src/auth.js +382 -0
- package/src/cli.js +112 -0
- package/src/commands/auth.js +80 -0
- package/src/commands/download.js +101 -0
- package/src/commands/playlists.js +87 -0
- package/src/commands/search.js +50 -0
- package/src/downloader.js +254 -0
- package/src/ui.js +12 -0
- package/src/youtube.js +238 -0
package/src/youtube.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { getValidAccessToken, loadEnvironment } from "./auth.js";
|
|
2
|
+
|
|
3
|
+
function decodeHtmlEntities(str) {
|
|
4
|
+
if (!str) return "";
|
|
5
|
+
return str
|
|
6
|
+
.replace(/&/g, "&")
|
|
7
|
+
.replace(/</g, "<")
|
|
8
|
+
.replace(/>/g, ">")
|
|
9
|
+
.replace(/"/g, '"')
|
|
10
|
+
.replace(/'/g, "'")
|
|
11
|
+
.replace(/'/g, "'");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getApiKey() {
|
|
15
|
+
if (!process.env.YOUTUBE_API_KEY) {
|
|
16
|
+
loadEnvironment();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return process.env.YOUTUBE_API_KEY?.trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function fetchWithAuth(url) {
|
|
23
|
+
const accessToken = await getValidAccessToken();
|
|
24
|
+
|
|
25
|
+
let response;
|
|
26
|
+
try {
|
|
27
|
+
response = await fetch(url, {
|
|
28
|
+
headers: {
|
|
29
|
+
Authorization: `Bearer ${accessToken}`,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
} catch (networkError) {
|
|
33
|
+
const error = new Error(
|
|
34
|
+
`Network error: Unable to connect to YouTube API (${networkError.message})`,
|
|
35
|
+
);
|
|
36
|
+
error.code = "NETWORK_ERROR";
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
let errorData;
|
|
42
|
+
try {
|
|
43
|
+
errorData = await response.json();
|
|
44
|
+
} catch {
|
|
45
|
+
errorData = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const apiError = errorData?.error;
|
|
49
|
+
const reason = apiError?.errors?.[0]?.reason;
|
|
50
|
+
const message = apiError?.message || response.statusText || "Unknown error";
|
|
51
|
+
|
|
52
|
+
if (response.status === 401) {
|
|
53
|
+
const error = new Error(
|
|
54
|
+
"Authentication expired or invalid. Please run 'tubex auth login' again.",
|
|
55
|
+
);
|
|
56
|
+
error.code = "AUTH_REVOKED";
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (response.status === 404 || reason === "playlistNotFound") {
|
|
61
|
+
const error = new Error("Playlist not found.");
|
|
62
|
+
error.code = "NOT_FOUND";
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (
|
|
67
|
+
response.status === 403 &&
|
|
68
|
+
(reason === "quotaExceeded" ||
|
|
69
|
+
reason === "dailyLimitExceeded" ||
|
|
70
|
+
message.toLowerCase().includes("quota"))
|
|
71
|
+
) {
|
|
72
|
+
const error = new Error(
|
|
73
|
+
"YouTube API quota exceeded. Please check your quota in the Google Cloud Console or try again later.",
|
|
74
|
+
);
|
|
75
|
+
error.code = "QUOTA_EXCEEDED";
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const error = new Error(
|
|
80
|
+
`YouTube API error (${response.status}): ${message}`,
|
|
81
|
+
);
|
|
82
|
+
error.code = "API_ERROR";
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return response.json();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function searchYouTube(query, limit = 10) {
|
|
90
|
+
const apiKey = getApiKey();
|
|
91
|
+
|
|
92
|
+
if (!apiKey) {
|
|
93
|
+
const error = new Error("YOUTUBE_API_KEY is not configured.");
|
|
94
|
+
error.code = "MISSING_API_KEY";
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const url = new URL("https://www.googleapis.com/youtube/v3/search");
|
|
99
|
+
url.searchParams.set("part", "snippet");
|
|
100
|
+
url.searchParams.set("type", "video");
|
|
101
|
+
url.searchParams.set("q", query);
|
|
102
|
+
url.searchParams.set("maxResults", String(limit));
|
|
103
|
+
url.searchParams.set("key", apiKey);
|
|
104
|
+
|
|
105
|
+
let response;
|
|
106
|
+
try {
|
|
107
|
+
response = await fetch(url);
|
|
108
|
+
} catch (networkError) {
|
|
109
|
+
const error = new Error(
|
|
110
|
+
`Network error: Unable to connect to YouTube API (${networkError.message})`,
|
|
111
|
+
);
|
|
112
|
+
error.code = "NETWORK_ERROR";
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!response.ok) {
|
|
117
|
+
let errorData;
|
|
118
|
+
try {
|
|
119
|
+
errorData = await response.json();
|
|
120
|
+
} catch {
|
|
121
|
+
errorData = null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const apiError = errorData?.error;
|
|
125
|
+
const reason = apiError?.errors?.[0]?.reason;
|
|
126
|
+
const message = apiError?.message || response.statusText || "Unknown error";
|
|
127
|
+
|
|
128
|
+
if (
|
|
129
|
+
response.status === 403 &&
|
|
130
|
+
(reason === "quotaExceeded" ||
|
|
131
|
+
reason === "dailyLimitExceeded" ||
|
|
132
|
+
message.toLowerCase().includes("quota"))
|
|
133
|
+
) {
|
|
134
|
+
const error = new Error(
|
|
135
|
+
"YouTube API quota exceeded. Please check your quota in the Google Cloud Console or try again later.",
|
|
136
|
+
);
|
|
137
|
+
error.code = "QUOTA_EXCEEDED";
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (reason === "badRequest" && message.toLowerCase().includes("key")) {
|
|
142
|
+
const error = new Error(
|
|
143
|
+
"YouTube API key is invalid. Please check your YOUTUBE_API_KEY.",
|
|
144
|
+
);
|
|
145
|
+
error.code = "INVALID_API_KEY";
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const error = new Error(
|
|
150
|
+
`YouTube API error (${response.status}): ${message}`,
|
|
151
|
+
);
|
|
152
|
+
error.code = "API_ERROR";
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const data = await response.json();
|
|
157
|
+
const items = data.items || [];
|
|
158
|
+
|
|
159
|
+
return items
|
|
160
|
+
.filter((item) => item.id?.videoId)
|
|
161
|
+
.map((item) => ({
|
|
162
|
+
id: item.id.videoId,
|
|
163
|
+
title: decodeHtmlEntities(item.snippet?.title || ""),
|
|
164
|
+
channelTitle: decodeHtmlEntities(item.snippet?.channelTitle || ""),
|
|
165
|
+
url: `https://www.youtube.com/watch?v=${item.id.videoId}`,
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export async function getUserPlaylists() {
|
|
170
|
+
const playlists = [];
|
|
171
|
+
let pageToken = null;
|
|
172
|
+
|
|
173
|
+
do {
|
|
174
|
+
const url = new URL("https://www.googleapis.com/youtube/v3/playlists");
|
|
175
|
+
url.searchParams.set("part", "snippet,contentDetails");
|
|
176
|
+
url.searchParams.set("mine", "true");
|
|
177
|
+
url.searchParams.set("maxResults", "50");
|
|
178
|
+
if (pageToken) {
|
|
179
|
+
url.searchParams.set("pageToken", pageToken);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const data = await fetchWithAuth(url);
|
|
183
|
+
const items = data.items || [];
|
|
184
|
+
|
|
185
|
+
for (const item of items) {
|
|
186
|
+
playlists.push({
|
|
187
|
+
id: item.id,
|
|
188
|
+
title: decodeHtmlEntities(item.snippet?.title || "Untitled"),
|
|
189
|
+
description: decodeHtmlEntities(item.snippet?.description || "").trim(),
|
|
190
|
+
itemCount: item.contentDetails?.itemCount ?? 0,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
pageToken = data.nextPageToken;
|
|
195
|
+
} while (pageToken);
|
|
196
|
+
|
|
197
|
+
return playlists;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export async function getPlaylistVideos(playlistId) {
|
|
201
|
+
const videos = [];
|
|
202
|
+
let pageToken = null;
|
|
203
|
+
|
|
204
|
+
do {
|
|
205
|
+
const url = new URL("https://www.googleapis.com/youtube/v3/playlistItems");
|
|
206
|
+
url.searchParams.set("part", "snippet,contentDetails");
|
|
207
|
+
url.searchParams.set("playlistId", playlistId);
|
|
208
|
+
url.searchParams.set("maxResults", "50");
|
|
209
|
+
if (pageToken) {
|
|
210
|
+
url.searchParams.set("pageToken", pageToken);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const data = await fetchWithAuth(url);
|
|
214
|
+
const items = data.items || [];
|
|
215
|
+
|
|
216
|
+
for (const item of items) {
|
|
217
|
+
const videoId =
|
|
218
|
+
item.snippet?.resourceId?.videoId || item.contentDetails?.videoId;
|
|
219
|
+
|
|
220
|
+
videos.push({
|
|
221
|
+
id: videoId || "Unavailable",
|
|
222
|
+
title: decodeHtmlEntities(item.snippet?.title || "Untitled"),
|
|
223
|
+
channelTitle: decodeHtmlEntities(
|
|
224
|
+
item.snippet?.videoOwnerChannelTitle ||
|
|
225
|
+
item.snippet?.channelTitle ||
|
|
226
|
+
"Unknown",
|
|
227
|
+
),
|
|
228
|
+
url: videoId
|
|
229
|
+
? `https://www.youtube.com/watch?v=${videoId}`
|
|
230
|
+
: "Unavailable",
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
pageToken = data.nextPageToken;
|
|
235
|
+
} while (pageToken);
|
|
236
|
+
|
|
237
|
+
return videos;
|
|
238
|
+
}
|