fix: canlı-tespitini yt-dlp'ye geri taşı — HTML regex yanlış videoId yakalıyordu
Kullanıcı panelindeki kayıtların kendi yayınıyla alakasız olduğunu bildirdi. Sebep: channel /live sayfası onlarca alakasız videoId içeriyor (kanal video listesi, öneriler); düz regex sayfadaki İLK videoId'yi alıyordu, bu da rastgele başka bir videoydu — isLive:true kontrolü de aynı bağlamda değildi. yt-dlp'nin extractor'ı kanalın asıl o anki canlı videosunu doğru çözüyor; -f (format) verilmediği için PO-token duvarını da tetiklemiyor (format çözümleme sadece gerçek capture adımında, -f ile, gerekiyor). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
|
import { execFile } from "node:child_process";
|
||||||
|
import { promisify } from "node:util";
|
||||||
import { prisma } from "@streamclipper/db";
|
import { prisma } from "@streamclipper/db";
|
||||||
import { env } from "./env";
|
import { env } from "./env";
|
||||||
import { streamIngestQueue } from "./queues";
|
import { streamIngestQueue } from "./queues";
|
||||||
import { sendTelegramMessage } from "./telegram";
|
import { sendTelegramMessage } from "./telegram";
|
||||||
|
import { ytdlpAntiBotArgs } from "./ytdlpCookies";
|
||||||
|
|
||||||
const BROWSER_USER_AGENT =
|
const execFileAsync = promisify(execFile);
|
||||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Live detection failures are kept per channel and surfaced via GET /status
|
* Live detection failures are kept per channel and surfaced via GET /status
|
||||||
@@ -13,33 +15,28 @@ const BROWSER_USER_AGENT =
|
|||||||
export const lastPollErrors = new Map<string, string>();
|
export const lastPollErrors = new Map<string, string>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Live detection is a plain page fetch, not a yt-dlp invocation. Resolving
|
* A channel's /live page HTML embeds dozens of unrelated `videoId`s (channel
|
||||||
* downloadable formats (what yt-dlp does even in --simulate mode) is what
|
* video list, recommendations, etc.) — a plain regex grabbed whichever one
|
||||||
* triggers YouTube's bot-check and proof-of-origin token requirements — but
|
* happened to appear first, which had nothing to do with the actual live
|
||||||
* we don't need stream bytes here, just whether the channel is live, which
|
* broadcast (confirmed: captured recordings didn't match the real stream).
|
||||||
* is embedded directly in the channel's /live page HTML (`isLive`/`videoId`
|
* yt-dlp's extractor correctly resolves the channel's *current* live video,
|
||||||
* in ytInitialData). This avoids cookies/PO-token entirely for polling;
|
* so the check goes back through it. Crucially, no `-f` format selector is
|
||||||
* those are only needed later, for the actual stream-ingest capture.
|
* passed here: format resolution is what triggers YouTube's PO-token wall,
|
||||||
|
* and this call only needs the video id / live flag, not downloadable
|
||||||
|
* formats — those are only resolved later, in the actual capture.
|
||||||
*/
|
*/
|
||||||
async function findLiveVideoId(channelId: string): Promise<string | null> {
|
async function findLiveVideoId(channelId: string): Promise<string | null> {
|
||||||
const liveUrl = `https://www.youtube.com/channel/${channelId}/live`;
|
const liveUrl = `https://www.youtube.com/channel/${channelId}/live`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(liveUrl, { headers: { "User-Agent": BROWSER_USER_AGENT } });
|
const { stdout } = await execFileAsync(
|
||||||
if (!res.ok) {
|
"yt-dlp",
|
||||||
lastPollErrors.set(channelId, `HTTP ${res.status} fetching ${liveUrl}`);
|
["--simulate", "--no-warnings", ...ytdlpAntiBotArgs(), "--print", "%(id)s", liveUrl],
|
||||||
return null;
|
{ timeout: 20_000 },
|
||||||
}
|
);
|
||||||
|
|
||||||
const html = await res.text();
|
|
||||||
lastPollErrors.delete(channelId);
|
lastPollErrors.delete(channelId);
|
||||||
|
const videoId = stdout.trim().split("\n")[0];
|
||||||
if (!/"isLive":true/.test(html)) {
|
return videoId || null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const match = html.match(/"videoId":"([^"]+)"/);
|
|
||||||
return match ? match[1] : null;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const message = err instanceof Error ? err.message : String(err);
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
lastPollErrors.set(channelId, message.slice(0, 500));
|
lastPollErrors.set(channelId, message.slice(0, 500));
|
||||||
|
|||||||
Reference in New Issue
Block a user