Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Jan 26, 2025
1 parent 97fbe02 commit 5df2370
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 7 additions & 12 deletions YoutubeExplode/Bridge/VisitorData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@
using System.Linq;
using System.Text;
using YoutubeExplode.Utils;
using YoutubeExplode.Utils.Extensions;

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();
}
private static readonly char[] SessionIdAllowedChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".ToCharArray();

public static string Generate() =>
new ProtoBuilder()
.AddString(1, GenerateRandomString(11))
// Session ID
.AddString(1, Random.NextString(SessionIdAllowedChars, 11))
// Timestamp
.AddNumber(
5,
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000 - Random.Next(600000)
)
// Country info
.AddBytes(
6,
new ProtoBuilder()
Expand Down
15 changes: 15 additions & 0 deletions YoutubeExplode/Utils/Extensions/RandomExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace YoutubeExplode.Utils.Extensions;

internal static class RandomExtensions
{
public static string NextString(this Random random, char[] allowedChars, int length)
{
var buffer = new char[length];
for (var i = 0; i < length; i++)
buffer[i] = allowedChars[random.Next(allowedChars.Length)];

return new string(buffer);
}
}

0 comments on commit 5df2370

Please sign in to comment.