Repository navigation
Expand file tree
/
Copy pathserver.mjs
More file actions
437 lines (392 loc) · 12.9 KB
/
Copy pathserver.mjs
File metadata and controls
437 lines (392 loc) · 12.9 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import http from "node:http";
import { URL } from "node:url";
import "/home/alex/codingcat-social-agent/chrome-extension/shared/sanity-posts.js";
const PORT = parseInt(process.env.PORT || "4005", 10);
const AUTH_TOKEN = process.env.BRIDGE_AUTH_TOKEN || "e06fdac10c7c18aed27e47e3d18860121a0ca378288a3e04";
const DEFAULT_HASHNODE_DRAFT = "https://hashnode.com/draft/6adff331943e682eec05f06c";
function escapeHtml(str = "") {
return String(str)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
function renderMobileHashnodeCopyPage(post) {
const safeTitle = escapeHtml(post.title);
const safeSubtitle = escapeHtml(post.subtitle || "");
const safeCanonical = escapeHtml(post.canonicalUrl);
const safeCover = escapeHtml(post.coverImageUrl || "");
const safeTags = escapeHtml((post.tags || []).join(", "));
const safeMarkdown = escapeHtml(post.markdown || "");
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Copy for Hashnode: ${safeTitle}</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: #0B1326;
color: #FAF9FC;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
padding: 16px;
max-width: 600px;
margin: 0 auto;
}
.card {
background: #131B2E;
border: 2px solid #7C3AED;
border-radius: 16px;
padding: 16px;
box-shadow: 4px 4px 0px #7C3AED;
display: flex;
flex-direction: column;
gap: 12px;
}
.badge {
display: inline-block;
background: #2A1155;
color: #C084FC;
border: 1px solid #7C3AED;
border-radius: 999px;
padding: 4px 10px;
font-size: 12px;
font-weight: 700;
width: fit-content;
}
h1 {
font-size: 18px;
line-height: 1.3;
color: #FFFFFF;
}
.meta {
font-size: 12px;
color: #86EFAC;
word-break: break-all;
font-family: monospace;
}
.btn-hero {
background: #7C3AED;
color: #FFFFFF;
border: 2px solid #C084FC;
border-radius: 14px;
padding: 16px;
font-size: 16px;
font-weight: 800;
text-align: center;
cursor: pointer;
box-shadow: 3px 3px 0px #C5E86C;
width: 100%;
}
.btn-hero:active {
transform: translate(1px, 1px);
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.btn {
background: #0B1326;
color: #FAF9FC;
border: 1.5px solid #4B2E83;
border-radius: 12px;
padding: 13px 10px;
font-size: 14px;
font-weight: 700;
text-align: center;
text-decoration: none;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}
.btn:active {
border-color: #C5E86C;
color: #C5E86C;
}
.toast {
display: none;
background: rgba(20, 83, 45, 0.7);
border: 1.5px solid #86EFAC;
color: #86EFAC;
padding: 12px;
border-radius: 10px;
font-weight: 700;
font-size: 14px;
text-align: center;
}
.toast.show { display: block; }
textarea {
width: 100%;
height: 180px;
background: #0B1326;
color: #CBD5E1;
border: 1px solid #334155;
border-radius: 10px;
padding: 10px;
font-family: monospace;
font-size: 12px;
}
</style>
</head>
<body>
<div class="card">
<span class="badge">🐾 CodingCat.dev Mobile Syndicator (${post.markdown.length.toLocaleString()} chars)</span>
<h1>${safeTitle}</h1>
<div class="meta">${safeCanonical}</div>
<div id="toast" class="toast"></div>
<button class="btn-hero" id="copy-and-open">
📋 1-Tap Copy Full Markdown & Open Hashnode →
</button>
<div class="grid">
<button class="btn" id="copy-md">📝 Copy Markdown Only</button>
<button class="btn" id="copy-title">📌 Copy Title</button>
<button class="btn" id="copy-canonical">🔗 Copy Canonical URL</button>
<button class="btn" id="copy-subtitle">💬 Copy Subtitle</button>
<a class="btn" href="/hashnode/${encodeURIComponent(post.slug)}.md" download="${encodeURIComponent(post.slug)}.md">⬇️ Download .md File</a>
<a class="btn" href="${DEFAULT_HASHNODE_DRAFT}" target="_blank" rel="noopener">🚀 Open Hashnode Draft</a>
</div>
<textarea id="raw-md" readonly>${safeMarkdown}</textarea>
</div>
<script>
const DATA = {
title: ${JSON.stringify(post.title)},
subtitle: ${JSON.stringify(post.subtitle || "")},
canonicalUrl: ${JSON.stringify(post.canonicalUrl)},
tags: ${JSON.stringify(safeTags)},
coverImageUrl: ${JSON.stringify(safeCover)},
draftUrl: ${JSON.stringify(DEFAULT_HASHNODE_DRAFT)}
};
const mdEl = document.getElementById("raw-md");
const toastEl = document.getElementById("toast");
function showToast(msg) {
toastEl.textContent = msg;
toastEl.classList.add("show");
}
async function copyText(text, label) {
try {
await navigator.clipboard.writeText(text);
showToast("✅ Copied " + label + " to clipboard!");
} catch {
mdEl.value = text;
mdEl.select();
document.execCommand("copy");
showToast("✅ Copied " + label + "!");
}
}
document.getElementById("copy-and-open").addEventListener("click", async () => {
await copyText(mdEl.value, "Full Markdown (" + mdEl.value.length + " chars)");
setTimeout(() => {
window.location.href = DATA.draftUrl;
}, 350);
});
document.getElementById("copy-md").addEventListener("click", () => copyText(mdEl.value, "Full Markdown"));
document.getElementById("copy-title").addEventListener("click", () => copyText(DATA.title, "Title"));
document.getElementById("copy-canonical").addEventListener("click", () => copyText(DATA.canonicalUrl, "Canonical URL"));
document.getElementById("copy-subtitle").addEventListener("click", () => copyText(DATA.subtitle, "Subtitle"));
</script>
</body>
</html>`;
}
function decodeHtmlEntities(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/'/g, "'")
.replace(/&#x([0-9a-fA-F]+);/g, (_, hex) =>
String.fromCodePoint(parseInt(hex, 16)),
)
.replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(parseInt(dec, 10)))
.replace(/\s+/g, " ")
.trim();
}
async function fetchYouTubeCaptions(videoId) {
const INNERTUBE_API_URL = "https://www.youtube.com/youtubei/v1/player?prettyPrint=false";
const INNERTUBE_CLIENT_VERSION = "20.10.38";
try {
let visitorData;
try {
const pageResp = await fetch(`https://www.youtube.com/watch?v=${videoId}`, {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"Accept-Language": "en-US,en;q=0.9",
},
});
if (pageResp.ok) {
const html = await pageResp.text();
const match = html.match(/"visitorData":"([^"]+)"/);
if (match) {
visitorData = match[1];
}
}
} catch {}
const requestHeaders = {
"Content-Type": "application/json",
"User-Agent": `com.google.android.youtube/${INNERTUBE_CLIENT_VERSION} (Linux; U; Android 14)`,
};
if (visitorData) {
requestHeaders["X-Goog-Visitor-Id"] = visitorData;
}
const response = await fetch(INNERTUBE_API_URL, {
method: "POST",
headers: requestHeaders,
body: JSON.stringify({
context: {
client: {
clientName: "ANDROID",
clientVersion: INNERTUBE_CLIENT_VERSION,
...(visitorData ? { visitorData } : {}),
},
},
videoId,
}),
});
if (!response.ok) {
return { cues: [], fullText: "", status: "no_caption_available", error: `innertube_http_${response.status}` };
}
const data = await response.json();
const playability = data?.playabilityStatus?.status;
const captionTracks = data?.captions?.playerCaptionsTracklistRenderer?.captionTracks;
if (!Array.isArray(captionTracks) || captionTracks.length === 0) {
return { cues: [], fullText: "", status: "no_caption_available", error: `no_tracks_status_${playability}` };
}
const track =
captionTracks.find(
(t) =>
t?.languageCode?.toLowerCase().startsWith("en") ||
t?.name?.runs?.[0]?.text?.toLowerCase().includes("english"),
) || captionTracks[0];
if (!track?.baseUrl) {
return { cues: [], fullText: "", status: "no_caption_available", error: "missing_track_baseUrl" };
}
const captionResp = await fetch(track.baseUrl);
if (!captionResp.ok) {
return { cues: [], fullText: "", status: "no_caption_available", error: `caption_fetch_http_${captionResp.status}` };
}
const xml = await captionResp.text();
const cues = [];
const pRegex = /<p\s+t="(\d+)"(?:\s+d="(\d+)")?[^>]*>([\s\S]*?)<\/p>/g;
let match;
while ((match = pRegex.exec(xml)) !== null) {
const startMs = parseInt(match[1], 10);
const durMs = match[2] ? parseInt(match[2], 10) : 3000;
const inner = match[3];
let text = "";
const sRegex = /<s[^>]*>([^<]*)<\/s>/g;
let sMatch;
while ((sMatch = sRegex.exec(inner)) !== null) {
text += sMatch[1];
}
if (!text) {
text = inner.replace(/<[^>]+>/g, "");
}
text = decodeHtmlEntities(text);
if (text && !text.startsWith("[")) {
cues.push({
start: Math.round((startMs / 1000) * 100) / 100,
duration: Math.round((durMs / 1000) * 100) / 100,
text,
});
}
}
if (cues.length === 0) {
const textRegex = /<text\s+start="([\d.]+)"(?:\s+dur="([\d.]+)")?[^>]*>([\s\S]*?)<\/text>/g;
while ((match = textRegex.exec(xml)) !== null) {
const start = parseFloat(match[1]);
const dur = match[2] ? parseFloat(match[2]) : 3.0;
const text = decodeHtmlEntities(match[3].replace(/<[^>]+>/g, ""));
if (text && !text.startsWith("[")) {
cues.push({
start: Math.round(start * 100) / 100,
duration: Math.round(dur * 100) / 100,
text,
});
}
}
}
cues.sort((a, b) => a.start - b.start);
const fullText = cues.map((c) => c.text).join(" ");
return {
cues,
fullText,
status: cues.length > 0 ? "completed" : "no_caption_available",
};
} catch (err) {
return {
cues: [],
fullText: "",
status: "no_caption_available",
error: err?.message || String(err),
};
}
}
const server = http.createServer(async (req, res) => {
const reqUrl = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
if (reqUrl.pathname === "/health") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ status: "healthy", timestamp: new Date().toISOString() }));
return;
}
if (reqUrl.pathname.startsWith("/hashnode/")) {
try {
const rawSlug = decodeURIComponent(reqUrl.pathname.replace(/^\/hashnode\//, "")).trim();
const isRawMd = rawSlug.endsWith(".md");
const slug = isRawMd ? rawSlug.slice(0, -3) : rawSlug;
const posts = await globalThis.CodingCatSanity.fetchCodingCatPosts({ forceRefresh: true });
const matched = posts.find((p) => p.slug === slug) || posts[0];
if (!matched) {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Post not found");
return;
}
if (isRawMd) {
res.writeHead(200, {
"Content-Type": "text/markdown; charset=utf-8",
"Content-Disposition": `inline; filename="${matched.slug}.md"`,
});
res.end(matched.markdown);
return;
}
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(renderMobileHashnodeCopyPage(matched));
return;
} catch (err) {
res.writeHead(500, { "Content-Type": "text/plain; charset=utf-8" });
res.end(`Error rendering mobile Hashnode copy page: ${err?.message || err}`);
return;
}
}
if (reqUrl.pathname === "/transcript") {
const auth = req.headers["authorization"] || req.headers["x-bridge-token"];
const token = typeof auth === "string" ? auth.replace(/^Bearer\s+/i, "") : "";
if (AUTH_TOKEN && token !== AUTH_TOKEN) {
res.writeHead(401, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Unauthorized" }));
return;
}
const videoId = reqUrl.searchParams.get("videoId") || reqUrl.searchParams.get("v");
if (!videoId) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Missing videoId parameter" }));
return;
}
const result = await fetchYouTubeCaptions(videoId);
res.writeHead(200, {
"Content-Type": "application/json",
"Cache-Control": "public, max-age=86400",
});
res.end(JSON.stringify(result));
return;
}
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Not found" }));
});
server.listen(PORT, "127.0.0.1", () => {
console.log(`[Transcript Bridge] Listening on http://127.0.0.1:${PORT}`);
});