mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-25 15:52:13 +00:00
refactor: Migrate to ts-proto (#752)
* refactor: Add extracted protos * refactor: Remove old stuff and update affected code * chore(package): Update `build:proto` script * chore(ClientInfo): Rename `androidSdkVersion` to `android_sdk_version` * chore: remove refs to old proto file * refactor(sabr_request): Rename `Gw` to `media_type` * chore(sabr_request): Fix typo in field num * feat(parser): Parse `video_playback_ustreamer_config` and `server_abr_streaming_url` * refactor: update protos * chore(package): streamline clean and build scripts * chore: update package.json * chore: update npmignore * chore(protos): Remove unneeded definitions See https://github.com/LuanRT/googlevideo for video playback proto definitions. * chore(package): add `rimraf` dependency
This commit is contained in:
48
src/utils/ProtoUtils.ts
Normal file
48
src/utils/ProtoUtils.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { base64ToU8, u8ToBase64 } from './Utils.js';
|
||||
import { VisitorData, PeformCommentActionParams } from '../../protos/generated/misc/params.js';
|
||||
|
||||
export function encodeVisitorData(id: string, timestamp: number): string {
|
||||
const writer = VisitorData.encode({ id, timestamp });
|
||||
return encodeURIComponent(u8ToBase64(writer.finish()).replace(/\+/g, '-').replace(/\//g, '_'));
|
||||
}
|
||||
|
||||
export function decodeVisitorData(visitor_data: string): VisitorData {
|
||||
const data = VisitorData.decode(base64ToU8(decodeURIComponent(visitor_data).replace(/-/g, '+').replace(/_/g, '/')));
|
||||
return data;
|
||||
}
|
||||
|
||||
export function encodeCommentActionParams(type: number, args: {
|
||||
comment_id?: string,
|
||||
video_id?: string,
|
||||
text?: string,
|
||||
target_language?: string
|
||||
} = {}): string {
|
||||
const data: PeformCommentActionParams = {
|
||||
type,
|
||||
commentId: args.comment_id || ' ',
|
||||
videoId: args.video_id || ' ',
|
||||
channelId: ' ',
|
||||
unkNum: 2
|
||||
};
|
||||
|
||||
if (args.hasOwnProperty('text')) {
|
||||
if (typeof args.target_language !== 'string')
|
||||
throw new Error('target_language must be a string');
|
||||
|
||||
if (args.comment_id)
|
||||
delete data.unkNum;
|
||||
|
||||
data.translateCommentParams = {
|
||||
params: {
|
||||
comment: {
|
||||
text: args.text as string
|
||||
}
|
||||
},
|
||||
commentId: args.comment_id || ' ',
|
||||
targetLanguage: args.target_language
|
||||
};
|
||||
}
|
||||
|
||||
const writer = PeformCommentActionParams.encode(data);
|
||||
return encodeURIComponent(u8ToBase64(writer.finish()));
|
||||
}
|
||||
@@ -234,7 +234,9 @@ export function u8ToBase64(u8: Uint8Array): string {
|
||||
}
|
||||
|
||||
export function base64ToU8(base64: string): Uint8Array {
|
||||
return new Uint8Array(atob(base64).split('').map((char) => char.charCodeAt(0)));
|
||||
const standard_base64 = base64.replace(/-/g, '+').replace(/_/g, '/');
|
||||
const padded_base64 = standard_base64.padEnd(standard_base64.length + (4 - standard_base64.length % 4) % 4, '=');
|
||||
return new Uint8Array(atob(padded_base64).split('').map((char) => char.charCodeAt(0)));
|
||||
}
|
||||
|
||||
export function isTextRun(run: TextRun | EmojiRun): run is TextRun {
|
||||
|
||||
@@ -15,5 +15,7 @@ export * as Utils from './Utils.js';
|
||||
export { default as Log } from './Log.js';
|
||||
export * as LZW from './LZW.js';
|
||||
|
||||
export * as ProtoUtils from './ProtoUtils.js';
|
||||
|
||||
export { default as UMP } from './UMP.js';
|
||||
export * from './UMP.js';
|
||||
Reference in New Issue
Block a user