File size: 6,858 Bytes
5ec2e9b 714bc4f 5ec2e9b 714bc4f 5ec2e9b 714bc4f 5ec2e9b 714bc4f 5ec2e9b | 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 | import { ApiResponse, Innertube, YT } from "youtubei.js";
import { generateRandomString } from "youtubei.js/Utils";
import { compress, decompress } from "brotli";
import type { TokenMinter } from "../jobs/potoken.ts";
import { Metrics } from "../helpers/metrics.ts";
let youtubePlayerReqLocation = "youtubePlayerReq";
if (Deno.env.get("YT_PLAYER_REQ_LOCATION")) {
if (Deno.env.has("DENO_COMPILED")) {
youtubePlayerReqLocation = Deno.mainModule.replace("src/main.ts", "") +
Deno.env.get("YT_PLAYER_REQ_LOCATION");
} else {
youtubePlayerReqLocation = Deno.env.get(
"YT_PLAYER_REQ_LOCATION",
) as string;
}
}
const { youtubePlayerReq } = await import(youtubePlayerReqLocation);
import type { Config } from "./config.ts";
const kv = await Deno.openKv();
export const youtubePlayerParsing = async ({
innertubeClient,
videoId,
config,
tokenMinter,
metrics,
overrideCache = false,
}: {
innertubeClient: Innertube;
videoId: string;
config: Config;
tokenMinter: TokenMinter;
metrics: Metrics | undefined;
overrideCache?: boolean;
}): Promise<object> => {
const cacheEnabled = overrideCache ? false : config.cache.enabled;
const videoCached = (await kv.get(["video_cache", videoId]))
.value as Uint8Array;
if (videoCached != null && cacheEnabled) {
return JSON.parse(new TextDecoder().decode(decompress(videoCached)));
} else {
const youtubePlayerResponse = await youtubePlayerReq(
innertubeClient,
videoId,
config,
tokenMinter,
);
const videoData = youtubePlayerResponse.data;
if (videoData.playabilityStatus.status === "ERROR") {
return videoData;
}
const video = new YT.VideoInfo(
[youtubePlayerResponse],
innertubeClient.actions,
generateRandomString(16),
);
const streamingData = video.streaming_data;
// Modify the original YouTube response to include deciphered URLs
if (streamingData && videoData && videoData.streamingData) {
const ecatcherServiceTracking = videoData.responseContext
?.serviceTrackingParams.find((o: { service: string }) =>
o.service === "ECATCHER"
);
const clientNameUsed = ecatcherServiceTracking?.params?.find((
o: { key: string },
) => o.key === "client.name");
// no need to decipher on IOS nor ANDROID
if (
!clientNameUsed?.value.includes("IOS") &&
!clientNameUsed?.value.includes("ANDROID")
) {
for (const [index, format] of streamingData.formats.entries()) {
videoData.streamingData.formats[index].url = await format
.decipher(
innertubeClient.session.player,
);
if (
videoData.streamingData.formats[index]
.signatureCipher !==
undefined
) {
delete videoData.streamingData.formats[index]
.signatureCipher;
}
if (
videoData.streamingData.formats[index].url.includes(
"alr=yes",
)
) {
videoData.streamingData.formats[index].url = videoData.streamingData.formats[index].url.replace(
"alr=yes",
"alr=no",
);
} else {
videoData.streamingData.formats[index].url += "&alr=no";
}
}
for (
const [index, adaptive_format] of streamingData
.adaptive_formats
.entries()
) {
videoData.streamingData.adaptiveFormats[index].url =
await adaptive_format
.decipher(
innertubeClient.session.player,
);
if (
videoData.streamingData.adaptiveFormats[index]
.signatureCipher !==
undefined
) {
delete videoData.streamingData.adaptiveFormats[index]
.signatureCipher;
}
if (
videoData.streamingData.adaptiveFormats[index].url
.includes("alr=yes")
) {
videoData.streamingData.adaptiveFormats[index].url = videoData.streamingData.adaptiveFormats[index].url
.replace("alr=yes", "alr=no");
} else {
videoData.streamingData.adaptiveFormats[index].url +=
"&alr=no";
}
}
}
}
const videoOnlyNecessaryInfo = ((
{
captions,
playabilityStatus,
storyboards,
streamingData,
videoDetails,
microformat,
},
) => ({
captions,
playabilityStatus,
storyboards,
streamingData,
videoDetails,
microformat,
}))(videoData);
if (videoData.playabilityStatus?.status == "OK") {
metrics?.innertubeSuccessfulRequest.inc();
if (cacheEnabled) {
(async () => {
await kv.set(
["video_cache", videoId],
compress(
new TextEncoder().encode(
JSON.stringify(videoOnlyNecessaryInfo),
),
),
{
expireIn: 1000 * 60 * 60,
},
);
})();
}
} else {
metrics?.checkInnertubeResponse(videoData);
}
return videoOnlyNecessaryInfo;
}
};
export const youtubeVideoInfo = (
innertubeClient: Innertube,
youtubePlayerResponseJson: object,
): YT.VideoInfo => {
const playerResponse = {
success: true,
status_code: 200,
data: youtubePlayerResponseJson,
} as ApiResponse;
return new YT.VideoInfo(
[playerResponse],
innertubeClient.actions,
"",
);
};
|