Skip to content

Commit

Permalink
Clean up last PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Jan 25, 2025
1 parent 5690324 commit 8d64e0a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 113 deletions.
44 changes: 44 additions & 0 deletions YoutubeExplode/Bridge/VisitorData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Linq;
using System.Text;
using YoutubeExplode.Utils;

namespace YoutubeExplode.Bridge;

internal static class VisitorData
{
private static readonly Random Random = new();

private static string GenerateRandomString(int length)
{
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

var buffer = new StringBuilder();
for (var i = 0; i < length; i++)
buffer.Append(alphabet.ElementAt(Random.Next(alphabet.Length)));

return buffer.ToString();
}

public static string Generate() =>
new ProtoBuilder()
.AddString(1, GenerateRandomString(11))
.AddNumber(
5,
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000 - Random.Next(600000)
)
.AddBytes(
6,
new ProtoBuilder()
.AddString(1, "US")
.AddBytes(
2,
new ProtoBuilder()
.AddString(2, "")
.AddNumber(4, Random.Next(255) + 1)
.ToBytes()
)
.ToBytes()
)
.ToUrlEncodedBase64();
}
114 changes: 52 additions & 62 deletions YoutubeExplode/Utils/ProtoBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,71 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;

namespace YoutubeExplode.Utils
namespace YoutubeExplode.Utils;

internal class ProtoBuilder
{
internal class ProtoBuilder
{
private MemoryStream _byteBuffer;
private readonly MemoryStream _byteBuffer = new();

public ProtoBuilder()
private void WriteNumber(long val)
{
if (val == 0)
{
_byteBuffer = new MemoryStream();
_byteBuffer.WriteByte(0);
return;
}

public byte[] ToBytes()
var v = val;
while (v != 0)
{
return _byteBuffer.ToArray();
}
var b = (byte)(v & 0x7F);
v >>= 7;

public string ToUrlencodedBase64()
{
var b64 = Convert
.ToBase64String(ToBytes())
.Replace('+', '-')
.Replace('/', '_')
.TrimEnd('=');
return Uri.EscapeDataString(b64);
}
if (v != 0)
b |= 0x80;

private void WriteVarint(long val)
{
if (val == 0)
{
_byteBuffer.WriteByte(0);
}
else
{
long v = val;
while (v != 0)
{
byte b = (byte)(v & 0x7F);
v >>= 7;

if (v != 0)
{
b |= 0x80;
}
_byteBuffer.WriteByte(b);
}
}
_byteBuffer.WriteByte(b);
}
}

private void Field(int field, byte wire)
{
long fbits = ((long)field) << 3;
long wbits = ((long)wire) & 0x07;
long val = fbits | wbits;
WriteVarint(val);
}
private void WriteField(int field, byte wire)
{
var fBits = (long)field << 3;
var wBits = (long)wire & 0x07;
var val = fBits | wBits;

public void Varint(int field, long val)
{
Field(field, 0);
WriteVarint(val);
}
WriteNumber(val);
}

public void String(int field, string str)
{
var strBytes = Encoding.UTF8.GetBytes(str);
Bytes(field, strBytes);
}
public ProtoBuilder AddNumber(int field, long val)
{
WriteField(field, 0);
WriteNumber(val);

public void Bytes(int field, byte[] bytes)
{
Field(field, 2);
WriteVarint(bytes.Length);
_byteBuffer.Write(bytes, 0, bytes.Length);
}
return this;
}

public ProtoBuilder AddString(int field, string str) =>
AddBytes(field, Encoding.UTF8.GetBytes(str));

public ProtoBuilder AddBytes(int field, byte[] bytes)
{
WriteField(field, 2);
WriteNumber(bytes.Length);
_byteBuffer.Write(bytes, 0, bytes.Length);

return this;
}

public byte[] ToBytes() => _byteBuffer.ToArray();

public string ToUrlEncodedBase64() =>
Uri.EscapeDataString(
Convert.ToBase64String(ToBytes()).Replace('+', '-').Replace('/', '_').TrimEnd('=')
);

[ExcludeFromCodeCoverage]
public override string ToString() => ToUrlEncodedBase64();
}
45 changes: 0 additions & 45 deletions YoutubeExplode/Utils/YoutubeParsingHelper.cs

This file was deleted.

7 changes: 1 addition & 6 deletions YoutubeExplode/Videos/VideoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ internal class VideoController(HttpClient http)
{
protected HttpClient Http { get; } = http;

private string _visitorData = null!;

public async ValueTask<VideoWatchPage> GetVideoWatchPageAsync(
VideoId videoId,
CancellationToken cancellationToken = default
Expand Down Expand Up @@ -63,9 +61,6 @@ public async ValueTask<PlayerResponse> GetPlayerResponseAsync(
"https://www.youtube.com/youtubei/v1/player"
);

if (_visitorData == null)
_visitorData = YoutubeParsingHelper.GetRandomVisitorData();

request.Content = new StringContent(
// lang=json
$$"""
Expand All @@ -81,7 +76,7 @@ public async ValueTask<PlayerResponse> GetPlayerResponseAsync(
"platform": "MOBILE",
"osName": "IOS",
"osVersion": "18.1.0.22B83",
"visitorData": {{Json.Serialize(_visitorData)}},
"visitorData": {{Json.Serialize(VisitorData.Generate())}},
"hl": "en",
"gl": "US",
"utcOffsetMinutes": 0
Expand Down

0 comments on commit 8d64e0a

Please sign in to comment.