Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
sourceObj.height);
const urlPrefix = (isVideo) ?
"https://grayjay.internal/video" :
"https://grayjay.internal/audio";
const dash = generateWEBMDash(webmHeader,
urlPrefix + "/internal/segment.webm?segIndex=$Number$",
urlPrefix + "/internal/init.webm");
return [dash, umpResp, webmHeader];
}
function generateWEBMDash(webm, templateUrl, initUrl) {
const duration = splitMS(webm.duration);
const durationFormatted = `PT${duration.hours}H${duration.minutes}M${duration.seconds}.${((duration.miliseconds + "").padStart(3, '0'))}S`;
let repCounter = 1;
let mpd = `<?xml version="1.0" encoding="UTF-8"?>\n`;
mpd += xmlTag("MPD", {
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns": "urn:mpeg:dash:schema:mpd:2011",
"xsi:schemaLocation": "urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd",
"profiles": "urn:mpeg:dash:profile:isoff-live:2011",
"minBufferTime": "PT1.5S",
"type": "static",
"mediaPresentationDuration": durationFormatted
}, (indent)=>
xmlTag("Period", {id: "0", duration: durationFormatted}, (indent) =>
xmlTag("AdaptationSet", {segmentAlignment: "true"}, (indent)=>
xmlTag("Representation",
(webm.mimeType.startsWith("video/")) ?
{id: "1", mimeType: webm.mimeType, codecs: webm.codec, startWithSAP: "1", bandwidth: "800000", width: webm.width, height: webm.height}:
{id: "2", mimeType: webm.mimeType, codecs: webm.codec, startWithSAP: "1", bandwidth: "800000", audioSamplingRate: webm.samplingFrequency},(indent)=>
xmlTag("SegmentTemplate", {timescale: webm.timescale / 1000, startNumber: "1",
media: templateUrl,
duration: webm.duration,
xmlTag("SegmentTimeline", {}, (indent)=>
webm.cues.map((cue, i)=>
xmlTag("S", {t: cue, d: (webm.cues.length > i + 1) ? webm.cues[i + 1] - cue : webm.durationCueTimescale - cue}, undefined, indent + " ")
).join("")
,indent + " ")
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
,indent + " ")
, indent + " ")
, indent + " ")
, "");
return mpd;
}
function splitMS(ms) {
const hours = Math.floor(ms / (60 * 60 * 1000));
ms -= hours * (60 * 60 * 1000);
const minutes = Math.floor(ms / (60 * 1000));
ms -= minutes * (60 * 1000);
const seconds = Math.floor(ms / 1000);
ms -= seconds * 1000;
return {
hours: hours,
minutes: minutes,
seconds: seconds,
miliseconds: ms
};
}
function xmlTag(tag, attributes, nested, indent) {
indent = indent ?? "";
let prefix = indent + "<" + tag;
const attrKeys = (attributes) ? Object.keys(attributes) : [];
if(attrKeys && attrKeys.length > 0) {
prefix += " " + attrKeys.map(x=>x + "=\"" + attributes[x] + "\"").join(" ");
}
if(!!nested) {
return prefix + ">\n" +
nested(indent + " ") +
indent + "</" + tag + ">\n";
}
else
return prefix + "/>\n";
}
class TestYTABRVideoSource extends DashManifestRawSource {
constructor(obj, url, sourceObj) {
super(obj);
this.url = url;
this.url = "https://dash.akamaized.net/akamai/bbb_30fps/bbb_30fps.mpd";
this.abrUrl = url;
this.sourceObj = sourceObj;
}
generate() {
const dash = http.GET("https://dash.akamaized.net/akamai/bbb_30fps/bbb_30fps.mpd", {});
return dash.body;
}
getRequestExecutor() {
return new YTABRExecutor(this.abrUrl, this.sourceObj);
}
}
const KB_SIZE = 1000;
const MB_SIZE = 1000 * KB_SIZE;
const GB_SIZE = 1000 * MB_SIZE;
class ReusableBuffers {
constructor(size, count) {
this.maxSize = size;
this.maxCount = count;
this.buffers = [];
}
freeAll() {
for(let buffer of this.buffers) {
buffer.data = undefined;
buffer.tag = undefined;
}
}
freeTag(tag) {
const buffers = this.buffers.filter(x=>x.tag == tag);
for(let buffer of buffers) {
buffer.data = undefined;
buffer.tag = undefined;
}
}
free(toFree) {
const buffer = this.buffers.find(x=>x.data == toFree);
if(buffer) {
buffer.data = undefined;
buffer.tag = undefined;
}
}
getBuffer(size, tag) {
log("Reusable Buffer [" + size + "]");
if(size > this.maxSize)
throw new ScriptException("Requested reusable buffer above the max buffer size (" + size + " > " + this.maxSize + ")");
for(let buffer of this.buffers) {
if(!buffer.data) {
buffer.data = new Uint8Array(buffer.buffer, 0, size);
buffer.tag = tag;
return buffer.data;
}
}
if(this.buffers.length < this.maxCount) {
log("Allocated new resuable buffer (total: " + ((this.buffers.length + 1) * this.maxSize)/MB_SIZE + "MB)");
const newBuffer = new ArrayBuffer(this.maxSize);
const newData = new Uint8Array(newBuffer, 0, size);
this.buffers.push({
buffer: newBuffer,
data: newData,
tag: tag
});
return newData;
}
throw new ScriptException("Ran out of reusable memory (" + this.maxCount + ")");
}
}
let _reusableBufferVideo = undefined;
let _reusableBufferAudio = undefined;
function getMediaReusableVideoBuffers() {
if(!_reusableBufferVideo)
_reusableBufferVideo = new ReusableBuffers(20 * MB_SIZE, 10);
return _reusableBufferVideo;
}
function getMediaReusableAudioBuffers() {
if(!_reusableBufferAudio)
_reusableBufferAudio = new ReusableBuffers(2 * MB_SIZE, 10);
return _reusableBufferAudio;
}
const useReusableBuffers = false;
let executorCounter = 0;
let _executorsVideo = [];
let _executorsAudio = [];
class YTABRExecutor {
constructor(url, source, ustreamerConfig, header, initialUmp) {
this.executorId = executorCounter++;
this.source = source;
this.itag = source.itag;
this.header = header;
this.initialUmp = initialUmp;
this.abrUrl = url;
this.ustreamerConfig = ustreamerConfig;
this.lastRequest = 0;
this.requestStarted = (new Date()).getTime();
this.lastAction = (new Date()).getTime() - (Math.random() * 1000 * 5);
log("UMP New executor: " + source.name + " - " + source.mimeType + " (segments: " + header?.cues?.length + ")");
log("UMP Cues: " + header?.cues?.join(", "));
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
if(source.mimeType.startsWith("video/")) {
this.urlPrefix = "https://grayjay.internal/video";
this.reusableBuffer = (useReusableBuffers) ?
getMediaReusableVideoBuffers() : undefined;
this.type = "video";
_executorsVideo.push(this);
if(_executorsVideo.length > 2) {
log("LEAKED EXECUTOR DETECTED?");
}
}
else {
this.urlPrefix = "https://grayjay.internal/audio";
this.reusableBuffer = (useReusableBuffers) ?
getMediaReusableAudioBuffers() : undefined;
this.type = "audio";
_executorsAudio.push(this);
}
this.segments = {};
if(initialUmp)
{
for(let segment of Object.keys(initialUmp.streams)) {
const stream = initialUmp.streams[segment];
if(stream.itag == this.itag) {
log(`Caching initial Segment: itag:${stream.itag}, segmentIndex: ${stream.segmentIndex}, segmentLength: ${stream.segmentSize}, completed: ${stream.completed}`)
this.cacheSegment(initialUmp.streams[segment]);
}
getOffset(index) {
if(this.segmentOffset && this.segmentOffset.actual <= index)
return this.segmentOffset.offset;
return 0;
}
registerOffset(index, found) {
this.segmentOffset = {index: index, actual: found, offset: found - index};
}
findSegmentTime(index) {
if(this.header && this.header.cues) {
if(this.header.cues.length > index) {
const time = this.header.cues[index];
if(index > 0 && time == 0) {
log("UMP Cues: " + this.header.cues.join(", "));
throw new ScriptException("Zero time for non-zero segment?");
}
return time;
}
else
throw new ScriptException("UMP: Segment index out of bound? " + this.header.cues.length + " > " + index)
}
throw new ScriptException("Missing initialHeader?");
}
cacheSegment(segment) {
this.segments[segment.segmentIndex - this.getOffset(segment.segmentIndex)] = segment;
}
getCachedSegmentCount() {
return Object.keys(this.segments).length;
}
getCachedSegment(index) {
return this.segments[index];
}
freeOldSegments(index) {
const reusable = this.reusableBuffer;
for(let key of Object.keys(this.segments)) {
log("UMP [" + this.type + "]: disposing segment " + key + " (<" + index + " || >" + (index + 6) + ")");
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
reusable?.free(this.segments[key].data);
const segment = this.segments[key];
if(segment) {
delete segment.data;
}
delete this.segments[key];
}
}
}
freeAllSegments() {
const reusable = this.reusableBuffer;
for(let key of Object.keys(this.segments)) {
reusable?.free(this.segments[key].data);
delete this.segments[key];
}
}
cleanup() {
log("UMP: Cleaning up!");
this.initialUmp = undefined;
this.header = undefined;
if(this.type == "video") {
const index = _executorsVideo.indexOf(this);
const removed = _executorsVideo.splice(index, 1);
if(removed)
log("Remaining video executors: " + _executorsVideo.length);
}
else {
const index = _executorsAudio.indexOf(this);
_executorsVideo.splice(index, 1);
log("Remaining audio executors: " + _executorsAudio.length);
}
this.freeAllSegments();
}
if(!retryCount)
retryCount = 0;
log("UMP: " + url + "");
const u = new URL(url);
const isInternal = u.pathname.startsWith('/internal');
const isInit = u.pathname.startsWith('/internal/init');
let segment = u.searchParams.has("segIndex") ? u.searchParams.get("segIndex") : 0;
let time = (segment > 0) ? this.findSegmentTime(segment - 1) : 0;
if(overrideSegment && overrideSegment > 0) {
const oldTime = time;
time = this.findSegmentTime(overrideSegment - 1);
log("UMP [" + this.type + "], overriding timestamp " + oldTime + " => " + time);
}
this.freeOldSegments(segment);
const cached = this.getCachedSegment(segment);
if(cached) {
if(cached.data) {
log("UMP [" + this.type + "] Cached segment " + segment + " (" + this.getCachedSegmentCount() + " remaining)");
return cached.data;
}
else
log("UMP [" + this.type + "] Cached segment " + segment + " was undefined, refetching");
}
log("UMP [" + this.type + "] requesting segment: " + segment + ", time: " + time + ", itag: " + this.itag);
if(overrideSegment)
log("UMP [" + this.type + "] requesting with overrided segment: " + overrideSegment)
const initialReq = getVideoPlaybackRequest(this.source, this.ustreamerConfig, time, (overrideSegment) ? overrideSegment : segment, this.lastRequest, this.lastAction, now, undefined, -6);
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
const postData = initialReq.serializeBinary();
const initialResp = http.POST(this.abrUrl, postData, {
"Origin": "https://www.youtube.com",
"Accept": "*/*",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}, false, true);
if(!initialResp.isOk)
throw new ScriptException("Failed initial stream request [ " + initialResp.code + "]");
const data = initialResp.body;
let byteArray = undefined;
if(data instanceof ArrayBuffer)
byteArray = new Uint8Array(data);
else if(data instanceof Int8Array)
byteArray = new Uint8Array(data.buffer);
else {
byteArray = Uint8Array.from(data);
}
const umpResp = new UMPResponse(byteArray, this.reusableBuffer);
let streamsArr = [];
for(let key of Object.keys(umpResp.streams)) {
const stream = umpResp.streams[key]
if(stream.itag == this.itag && stream.segmentIndex >= segment)
streamsArr.push(stream);
else
log(`IGNORING itag:${stream.itag}, segmentIndex: ${stream.segmentIndex}, segmentLength: ${stream.segmentSize}, completed: ${stream.completed}`)
}
log("UMP [" + this.type + "] stream resps: \n" + streamsArr
.map(x=>`itag:${x.itag}, segmentIndex: ${x.segmentIndex}, segmentLength: ${x.segmentSize}, completed: ${x.completed}`)
.join("\n"));
this.lastRequest = (new Date()).getTime();
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
const stream = streamsArr[0];
if(!stream)
throw new ScriptException("No streams for requesting segment " + segment + ((overrideSegment && overrideSegment > 0) ? (", override: " + overrideSegment) : ""));
const expectedSegment = parseInt(segment) + parseInt(this.getOffset(stream.segmentIndex));
log("Expected segment " + expectedSegment + " got " + stream.segmentIndex);
if(stream && stream.segmentIndex != expectedSegment) {
log("Retrieved wrong segment: " + stream.segmentIndex + " != " + segment + ", retrying (" + (retryCount + 1) + ")")
if(true) {
let diff = stream.segmentIndex - segment;
if(diff < 0)
throw new ScriptException("Illegal negative offset");
else {
const doBackrequests = false;
if(!doBackrequests) {
log("Segment offset detected of " + diff + " (" + stream.segmentIndex + " - " + segment + ")");
this.registerOffset(parseInt(segment), parseInt(stream.segmentIndex));
}
else {
log("Requesting older data using offset (" + diff + ")");
if(retryCount == 0) {
for(let stream of streamsArr) {
log("Caching future segment " + stream.segmentIndex);
if(stream.completed)
this.cacheSegment(stream);
}
}
if(retryCount < 3) {
return this.executeRequest(url, headers, retryCount + 1, (parseInt(segment) - diff));
}
else {
throw new ScriptException("Too many back-requests");
}
}
}
}
else {
if(true || retryCount >= 2)
throw new ScriptException("Retrieved wrong segment: " + stream.segmentIndex + " != " + segment + " (" + retryCount + " attempts)");
else { //Disabled retry for now, doesnt make a diff.
log("Retrieved wrong segment: " + stream.segmentIndex + " != " + segment + ", retrying (" + (retryCount + 1) + ")");
return this.executeRequest(url, headers, retryCount + 1);
}
}
}
for(let stream of streamsArr) {
if(stream.completed)
this.cacheSegment(stream);
}
if(data instanceof ArrayBuffer) {
log("Clearing POST ArrayBuffer?");
}
if(!stream || !stream.data)
throw new ScriptException("NO STREAMDATA FOUND (" + Object.keys(umpResp.streams).join(", ") + "): " + !!umpResp.streams[0]?.data);
log("UMP [" + this.type + "]: segment " + segment + " - " + stream.data?.length);
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
return stream.data;
}
}
function getVideoPlaybackRequest(source, ustreamerConfig, playerPosMs, segmentIndex, lastRequest, lastAction, requestStarted, playbackCookie) {
const vidReq = new pb.VideoPlaybackRequest_pb.VideoPlaybackRequest();
const ustreamerBytes = Uint8Array.from(atob(ustreamerConfig.replaceAll("_", "/").replaceAll("-", "+")), c => c.charCodeAt(0))
vidReq.setVideoplaybackustreamerconfig(ustreamerBytes);
const clientInfo = new pb.VideoPlaybackRequest_pb.ClientInfo();
clientInfo.setClientname(1);
clientInfo.setClientversion("2.20240808.00.00");
clientInfo.setOsname("Windows");
clientInfo.setOsversion("10.0");
//Info
const info = new pb.VideoPlaybackRequest_pb.VideoPlaybackRequestInfo();
if(source.width) {
info.setDesiredwidth(source.width);
info.setDesiredheight(source.height);
info.setVideoheightmaybe(source.height);
info.setVideoheight2maybe(source.height);
info.setSelectedqualityheight(source.height);
}
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
info.setCurrentvideopositionms(playerPosMs);
if(lastRequest > 0)
info.setTimesincelastrequestms((new Date().getTime() - lastRequest));
info.setTimesincelastactionms(Math.floor((new Date()).getTime() - lastAction));
info.setDynamicrangecompression(true);
info.setLatencymsmaybe(Math.floor(Math.random() * 90 + 7));
info.setLastmanualdirection(0);
info.setTimesincelastmanualformatselectionms(requestStarted);
info.setVisibility(0);
info.setVp9(false);
vidReq.setInfo(info);
//SessionInfo
const sessionInfo = new pb.VideoPlaybackRequest_pb.SessionInfo();
sessionInfo.setClientinfo(clientInfo);
//TODO: sessionInfo.setPot();
if(playbackCookie)
sessionInfo.setPlaybackcookie(playbackCookie);
vidReq.setSessioninfo(sessionInfo);
//Formats
const format = new pb.VideoPlaybackRequest_pb.FormatId();
format.setItag(source.itag);
format.setLmt(source.lastModified);
if(source.xtags)
format.setXtags(source.xtags);
if(segmentIndex > 0) {
const bufferedStream = new pb.VideoPlaybackRequest_pb.BufferedStreamInfo()
bufferedStream.setFormatid(format);
//TODO: bufferedStream.setBuffereddurationms();
bufferedStream.setBufferedsegmentstartindex(1);
bufferedStream.setBufferedsegmentendindex(segmentIndex - 1);
bufferedStream.setBufferedstarttimems(0);
vidReq.setBufferedstreamsList[bufferedStream];
vidReq.setDesiredstreamsList([format]);
}
if(source.mimeType.startsWith("video/")) {
vidReq.setSupportedvideostreamsList([format]);
info.setMediatypeflags(pb.VideoPlaybackRequest_pb.MediaType.VIDEO);
}
else if(source.mimeType.startsWith("audio/")) {
vidReq.setSupportedaudiostreamsList([format]);
info.setMediatypeflags(pb.VideoPlaybackRequest_pb.MediaType.AUDIO);
}
else throw new ScriptException("Unknown source format?");
return vidReq;
}
return new YTRequestModifier(this.originalUrl);
}
}
class YTRequestModifier extends RequestModifier {
this.originalUrl = originalUrl;
this.newUrl = null;
this.newUrlCount = 0;
}
/**
* Modifies the request
* @param {string} url The URL string used
* @param {{[key: string]: string}} headers The headers used
* @returns {Request}
*/
modifyRequest(url, headers) {
const u = new URL(url);
const actualUrl = (this.newUrl) ? new URL(this.newUrl) : u;
const isVideoPlaybackUrl = u.pathname.startsWith('/videoplayback');
if (isVideoPlaybackUrl && !u.searchParams.has("rn")) {
actualUrl.searchParams.set("rn", this.requestNumber.toString());
if(this.newUrl) {
log("BYPASS: Using NewURL For sources");
log("BYPASS: OldUrl: " + u.toString());
log("BYPASS: NewUrl: " + actualUrl.toString());
log("BYPASS: Headers: " + JSON.stringify(headers));
}
let removedRangeHeader = undefined;
if (headers["Range"] && !u.searchParams.has("range")) {
let range = headers["Range"];
if (range.startsWith("bytes=")) {
range = range.substring("bytes=".length);
}
}
const c = u.searchParams.get("c");
if (c === "WEB" || c === "TVHTML5_SIMPLY_EMBEDDED_PLAYER") {
headers["Origin"] = URL_BASE;
headers["Referer"] = URL_BASE;
headers["Sec-Fetch-Dest"] = "empty";
headers["Sec-Fetch-Mode"] = "cors";
headers["Sec-Fetch-Site"] = "cross-site";
}
headers['TE'] = "trailers";
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
//I hate this
//Workaround for seemingly active blocking
/*
const isValid = refetchClient.request("HEAD", actualUrl.toString(), headers);
if(isValid.code == 403 && this.newUrlCount < 3) {
const itag = actualUrl.searchParams.get("itag");
bridge.toast("Youtube block detected (" + (this.newUrlCount + 1) + "), bypassing..");
log("Detected 403, attempting bypass");
try {
const newDetailsResp = source.getContentDetails(this.originalUrl, false, true);
if(newDetailsResp) {
let source = newDetailsResp.video.videoSources.find(x=>x.itagId == itag);
if(!source)
source = newDetailsResp.video.audioSources.find(x=>x.itagId == itag);
if(source) {
this.newUrl = source.url;
this.newUrlCount++;
this.requestNumber = 0;
log("Injecting new source url[" + source.name + "]: " + source.url);
bridge.toast("Injecting new source url");
if(removedRangeHeader)
headers["Range"] = removedRangeHeader;
return this.modifyRequest(url, headers);
}
}
else
bridge.toast("Bypass failed, couldn't reload [" + newDetailsResp.code + "]");
}
catch(ex) {
bridge.toast("Bypass failed\n" + ex);
}
}
*/
if (c) {
switch (c) {
case "ANDROID":
headers["User-Agent"] = USER_AGENT_ANDROID;
break;
case "IOS":
headers["User-Agent"] = USER_AGENT_IOS;
break;
default:
headers["User-Agent"] = USER_AGENT_WINDOWS;
break;
}
}
return {
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
headers: headers
}
}
}
class YTLiveEventPager extends LiveEventPager {
constructor(key, continuation) {
super([], continuation != null);
this.key = key;
this.continuation = continuation;
this.hasMore = true;
this.knownEmojis = {};
this.nextPage();
}
nextPage() {
const newResult = http.POST(URL_LIVE_CHAT + "?key=" + this.key + "&prettyPrint=false",
JSON.stringify({
context: {
client: {
clientName: "WEB",
clientVersion: "2.20220901.00.00",
clientFormFactor: "UNKNOWN_FORM_FACTOR",
utcOffsetMinutes: 0,
memoryTotalKbytes: 100000,
timeZone: "ETC/UTC"
},
user: {
lockedSafetyMode: false
}
},
continuation: this.continuation,
webClientInfo: {
isDocumentHidden: false
}
}), {
"Content-Type": "application/json",
"User-Agent": USER_AGENT_WINDOWS
}, false);
if(!newResult.isOk)
throw new ScriptException("Failed chat: " + newResult.body);
const json = JSON.parse(newResult.body);
//if(IS_TESTING)
// console.log("Live Chat Json:", json);
const continuationArr = json?.continuationContents?.liveChatContinuation?.continuations;
if(!continuationArr || continuationArr.length == 0) {
this.hasMore = false;
throw new ScriptException("No chat continuation found");
}
const continuation = continuationArr[0]?.timedContinuationData?.continuation ?? continuationArr[0]?.invalidationContinuationData?.continuation
if(!continuation) {
this.hasMore = false;
throw new ScriptException("No chat continuation found");
}
this.continuation = continuation;
const actions = json.continuationContents?.liveChatContinuation?.actions;
if(IS_TESTING)
console.log("Live Chat Actions:", actions);
const actionResults = handleYoutubeLiveEvents(actions);
const emojiMap = actionResults.emojis;
events = actionResults.events;
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
let newEmojiCount = 0;
for(let kv in emojiMap) {
if(this.knownEmojis[kv])
delete emojiMap[kv];
else {
this.knownEmojis[kv] = emojiMap[kv];
newEmojiCount++;
}
}
if(newEmojiCount > 0) {
console.log("New Emojis:", emojiMap);
events.unshift(new LiveEventEmojis(emojiMap));
}
}
this.results = events;
//if(IS_TESTING)
// console.log("LiveEvents:", this.results);
return this;
}
}
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
function handleYoutubeLiveEvents(actions) {
let emojiMap = {};
let events = [];
for(let action of actions) {
try {
if(action.addChatItemAction) {
const obj = action.addChatItemAction;
const isPaid = !!obj.item?.liveChatPaidMessageRenderer
const renderer = (isPaid) ? obj.item?.liveChatPaidMessageRenderer : obj.item?.liveChatTextMessageRenderer;
const msgObj = extractLiveMessage_Obj(renderer);
if(!msgObj)
continue;
if(msgObj.emojis)
for(let emojiKey in msgObj.emojis)
emojiMap[emojiKey] = msgObj.emojis[emojiKey];
if(msgObj && msgObj.name && (msgObj.message || isPaid)) {
if(!isPaid)
events.push(new LiveEventComment(msgObj.name, msgObj.message, msgObj.thumbnail, msgObj.colorName, msgObj.badges));
else {
const amount = extractText_String(renderer.amount ?? renderer.purchaseAmountText ?? paidMessageRenderer?.amount ?? paidMessageRenderer?.purchaseAmountText);
events.push(new LiveEventDonation(amount, msgObj.name, msgObj.message ?? "", msgObj.thumbnail, 0, renderer.bodyBackgroundColor ? "#" + Number(renderer.bodyBackgroundColor).toString(16) : null));
}
}
}
else if(action.ReplaceChatItemAction) {}
else if(action.RemoveChatItemAction) {}
else if(action.addLiveChatTickerItemAction) {
const obj = action.addLiveChatTickerItemAction;
if(obj.item?.liveChatTickerSponsorItemRenderer) {
const renderer = obj.item?.liveChatTickerSponsorItemRenderer;
const membershipRenderer = renderer.showItemEndpoint?.showLiveChatItemEndpoint?.renderer?.liveChatMembershipItemRenderer;
const msgObj = extractLiveMessage_Obj(membershipRenderer);
if(msgObj && msgObj.name)
events.push(new LiveEventDonation("Member", msgObj.name, msgObj.message, msgObj.thumbnail, (renderer.durationSec ?? 10) * 1000, membershipRenderer.bodyBackgroundColor ? "#" + Number(membershipRenderer.bodyBackgroundColor).toString(16) : null));
}
else if(obj.item?.liveChatTickerPaidMessageItemRenderer) {
const renderer = obj.item?.liveChatTickerPaidMessageItemRenderer
const paidMessageRenderer = renderer.showItemEndpoint?.showLiveChatItemEndpoint?.renderer?.liveChatPaidMessageRenderer;
const msgObj = extractLiveMessage_Obj(paidMessageRenderer);
const amount = extractText_String(renderer.amount ?? renderer.purchaseAmountText ?? paidMessageRenderer?.amount ?? paidMessageRenderer?.purchaseAmountText);
if(msgObj && msgObj.name)
events.push(new LiveEventDonation(amount, msgObj.name, msgObj.message, msgObj.thumbnail, (renderer.durationSec ?? 10) * 1000, paidMessageRenderer.bodyBackgroundColor ? "#" + Number(paidMessageRenderer.bodyBackgroundColor).toString(16) : null));
}
}
else if(action.addBannerToLiveChatCommand) {
const bannerRenderer = action.addBannerToLiveChatCommand?.bannerRenderer?.liveChatBannerRenderer;
const redirectRenderer = bannerRenderer?.contents?.liveChatBannerRedirectRenderer;
if(bannerRenderer && redirectRenderer && bannerRenderer.bannerType == "LIVE_CHAT_BANNER_TYPE_CROSS_CHANNEL_REDIRECT") {
const url = redirectRenderer.inlineActionButton?.buttonRenderer?.command?.commandMetadata?.webCommandMetadata?.url;
const name = redirectRenderer.bannerMessage?.runs?.find(x=>x.bold)?.text;
const thumbnails = redirectRenderer.authorPhoto?.thumbnails;
if(url && name && thumbnails && thumbnails.length && thumbnails.length > 0)
events.push(new LiveEventRaid(URL_BASE + url, name, thumbnails[thumbnails.length - 1]?.url));
}
}
else {
const keys = Object.keys(action);
log("Unknown Event: " + keys.join(",") + JSON.stringify(action, null, " "));
}
}
catch(ex) {
log("Failed Youtube live action parse due to [" + ex + "]: " + JSON.stringify(action, null, " "));
}
}
return {
events: events,
emojis: emojiMap
};
}
source.handleYoutubeLiveEvents = handleYoutubeLiveEvents;
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
function extractLiveMessage_Obj(obj) {
if(!obj)
return null;
const name = extractText_String(obj.authorName);
const thumbnails = obj?.authorPhoto?.thumbnails;
let thumbnail = null;
for(let thumb of thumbnails){
if(thumb?.url) {
thumbnail = thumb.url;
break;
}
}
let message = extractText_String(obj.message);
const headerMessage = extractText_String(obj.headerPrimaryText);
const emojiMap = {};
let isMember = false;
const badges = [];
if(obj.authorBadges) {
for(let badge of obj.authorBadges) {
const badgeImages = badge.liveChatAuthorBadgeRenderer?.customThumbnail?.thumbnails;
const badgeName = badge.liveChatAuthorBadgeRenderer?.tooltip;
if(badgeImages && badgeImages.length > 0 && badgeName) {
emojiMap[badgeName] = badgeImages[badgeImages.length - 1].url;
badges.push(badgeName);
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
isMember = true;
}
}
}
if(obj?.message?.runs) {
for(let part of obj?.message?.runs) {
if(part.emoji?.image?.accessibility?.accessibilityData?.label && part.emoji?.image?.thumbnails) {
const label = part.emoji?.image?.accessibility?.accessibilityData?.label;
if(label && !emojiMap[label]) {
emojiMap[label] = part.emoji?.image?.thumbnails[0]?.url;
}
}
}
}
return {
name: name,
thumbnail: thumbnail,
message: message,
headerMessage: headerMessage,
emojis: emojiMap,
colorName: isMember ? "#2ba640" : null,
badges: badges
};
}
class YTCommentPager extends CommentPager {
constructor(comments, continuation, contextUrl, useLogin, useMobile) {
this.useLogin = !!useLogin;
this.useMobile = !!useMobile;
this.continuation = continuation;
}
nextPage() {
if(!this.continuation)
return new CommentPager([], false);
return requestCommentPager(this.context, this.continuation, this.useLogin, this.useMobile) ?? new CommentPager([], false);
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
}
}
class YTComment extends Comment {
constructor(obj) {
super(obj);
}
}
class RichGridPager extends VideoPager {
constructor(tab, context, useMobile = false, useAuth = false) {
super(tab.videos, tab.videos.length > 0 && !!tab.continuation, context);
this.continuation = tab.continuation;
this.useMobile = useMobile;
this.useAuth = useAuth;
}
nextPage() {
this.context.page = this.context.page + 1;
if(this.continuation) {
const newData = validateContinuation(()=>requestBrowse({
continuation: this.continuation.token
}, !!this.useMobile, !!this.useAuth));
if(newData && newData.length > 0) {
const fakeRichGrid = {
contents: newData
};
const newItemSection = extractRichGridRenderer_Shelves(fakeRichGrid, this.context);
if(newItemSection.videos && newItemSection.videos.length == 0 && newItemSection.shelves && newItemSection.shelves.length > 0) {
if(IS_TESTING)
console.log("No videos in root found, checking shelves", newItemSection);
let vids = [];
for(let i = 0; i < newItemSection.shelves.length; i++) {
const shelf = newItemSection.shelves[i];
vids = vids.concat(shelf.videos);
}
newItemSection.videos = vids;
}
if(newItemSection.videos)
return new RichGridPager(newItemSection, this.context, this.useMobile, this.useAuth);
}
else
log("Call [RichGridPager.nextPage] continuation gave no appended items, setting empty page with hasMore to false");
}
this.hasMore = false;
this.results = [];
return this;
}
}
class RichGridPlaylistPager extends PlaylistPager {
constructor(tab, context, useMobile = false, useAuth = false) {
super(tab.playlists, tab.playlists.length > 0 && !!tab.continuation, context);
if(!this.continuation && tab.subContinuations && tab.subContinuations.length == 1) {
this.continuation = tab.subContinuations[0];
this.hasMore = true;
}
this.useMobile = useMobile;
this.useAuth = useAuth;
}
nextPage() {
this.context.page = this.context.page + 1;
if(this.continuation) {
const newData = validateContinuation(()=>requestBrowse({
continuation: (this.continuation.token) ? this.continuation.token : this.continuation
}, !!this.useMobile, !!this.useAuth));
if(newData && newData.length > 0) {
const fakeRichGrid = {
contents: newData
};
const newItemSection = extractRichGridRenderer_Shelves(fakeRichGrid, this.context);
if(newItemSection.playlists && newItemSection.playlists.length == 0 && newItemSection.shelves && newItemSection.shelves.length > 0) {
if(IS_TESTING)
console.log("No playlists in root found, checking shelves", newItemSection);
let vids = [];
for(let i = 0; i < newItemSection.shelves.length; i++) {
const shelf = newItemSection.shelves[i];
vids = vids.concat(shelf.playlists);
}
newItemSection.playlists = vids;
}
if(newItemSection.playlists)
return new RichGridPlaylistPager(newItemSection, this.context, this.useMobile, this.useAuth);
if(!newItemSection.playlists) {
log("No results from RichGridRenderer extraction, trying single-shelf");
const shelf = extractGridRenderer_Shelf({
items: newData
}, this.context);
if(shelf.playlists && shelf.playlists.length > 0) {
return new RichGridPlaylistPager(shelf, this.context, this.useMobile, this.useAuth);
}
}
}
else
log("Call [RichGridPager.nextPage] continuation gave no appended items, setting empty page with hasMore to false");
}
this.hasMore = false;
this.results = [];
return this;
}
}
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
class SearchItemSectionVideoPager extends VideoPager {
constructor(itemSection) {
super(itemSection.videos, itemSection.videos.length > 0 && !!itemSection.continuation);
this.continuation = itemSection.continuation;
}
nextPage() {
this.context.page = this.context.page + 1;
if(this.continuation) {
const continueItems = validateContinuation(()=>
requestSearchContinuation(this.continuation.token));
if(continueItems.length > 0) {
const fakeSectionList = {
contents: continueItems
};
const newItemSection = extractSectionListRenderer_Sections(fakeSectionList, this.context);
if(newItemSection.videos)
return new SearchItemSectionVideoPager(newItemSection);
}
}
this.hasMore = false;
this.results = [];
return this;
}
}
class SearchItemSectionChannelPager extends ChannelPager {
constructor(itemSection) {
super(itemSection.channels, itemSection.channels.length > 0 && !!itemSection.continuation);
this.continuation = itemSection.continuation;
}
nextPage() {
this.context.page = this.context.page + 1;
if(this.continuation) {
const continueItems = validateContinuation(()=>
requestSearchContinuation(this.continuation.token));
if(continueItems.length > 0) {
const fakeSectionList = {
contents: continueItems
};
const newItemSection = extractSectionListRenderer_Sections(fakeSectionList, this.context);
if(newItemSection.channels)
return new SearchItemSectionChannelPager(newItemSection);
}
}
this.hasMore = false;
this.results = [];
return this;
}
}
class SearchItemSectionPlaylistPager extends ChannelPager {
constructor(itemSection) {
super(itemSection.playlists, itemSection.playlists.length > 0 && !!itemSection.continuation);