Skip to content

Commit 21aacf2

Browse files
author
Meyn
committed
Implement PoToken for YouTube
1 parent ac2f76c commit 21aacf2

File tree

10 files changed

+69
-27
lines changed

10 files changed

+69
-27
lines changed

Tubifarry/Download/Clients/YouTube/YoutubeClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override void RemoveItem(DownloadClientItem item, bool deleteData)
5252

5353
protected override void Test(List<ValidationFailure> failures)
5454
{
55-
_dlManager.SetCookies(Settings.CookiePath);
55+
_dlManager.SetAuth(Settings);
5656
if (string.IsNullOrEmpty(Settings.DownloadPath))
5757
failures.AddRange(PermissionTester.TestAllPermissions(Settings.FFmpegPath, _logger));
5858
failures.AddIfNotNull(TestFFmpeg().Result);

Tubifarry/Download/Clients/YouTube/YoutubeDownloadManager.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IYoutubeDownloadManager
1919
public Task<string> Download(RemoteAlbum remoteAlbum, IIndexer indexer, NamingConfig namingConfig, YoutubeClient provider);
2020
public IEnumerable<DownloadClientItem> GetItems();
2121
public void RemoveItem(DownloadClientItem item);
22-
public void SetCookies(string path);
22+
public void SetAuth(YoutubeProviderSettings settings);
2323
}
2424

2525
public class YoutubeDownloadManager : IYoutubeDownloadManager
@@ -29,7 +29,7 @@ public class YoutubeDownloadManager : IYoutubeDownloadManager
2929
private YouTubeMusicClient _ytClient;
3030
private Task? _testTask;
3131
private string? _cookiePath;
32-
32+
private string? _poToken;
3333

3434
/// <summary>
3535
/// Private constructor to prevent external instantiation.
@@ -42,12 +42,19 @@ public YoutubeDownloadManager(Logger logger)
4242
_ytClient = new YouTubeMusicClient();
4343
}
4444

45-
public void SetCookies(string path)
45+
/// <summary>
46+
/// Sets cookies and poToken for the YouTube Music client.
47+
/// </summary>
48+
public void SetAuth(YoutubeProviderSettings settings)
4649
{
47-
if (string.IsNullOrEmpty(path) || path == _cookiePath)
50+
if (settings.CookiePath == _cookiePath && settings.PoToken == _poToken)
51+
return;
52+
if (string.IsNullOrEmpty(settings.CookiePath) && string.IsNullOrEmpty(settings.PoToken))
4853
return;
49-
_cookiePath = path;
50-
_ytClient = new(cookies: CookieManager.ParseCookieFile(path));
54+
_cookiePath = settings.CookiePath;
55+
_poToken = settings.PoToken;
56+
System.Net.Cookie[]? cookies = !string.IsNullOrEmpty(settings.CookiePath) ? CookieManager.ParseCookieFile(settings.CookiePath) : null;
57+
_ytClient = new YouTubeMusicClient(poToken: settings.PoToken, cookies: cookies);
5158
}
5259

5360
public Task<string> Download(RemoteAlbum remoteAlbum, IIndexer indexer, NamingConfig namingConfig, YoutubeClient provider)

Tubifarry/Download/Clients/YouTube/YoutubeProviderSettings.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public YoutubeProviderSettingsValidator()
6666
.WithMessage("Max download speed must be greater than or equal to 0.")
6767
.LessThanOrEqualTo(2_500)
6868
.WithMessage("Max download speed must be less than or equal to 20 Mbps (2,500 KB/s).");
69+
70+
// Validate poToken
71+
RuleFor(x => x.PoToken)
72+
.Length(32, 64).When(x => !string.IsNullOrEmpty(x.PoToken))
73+
.WithMessage("Proof of Origin (poToken) must be between 32 and 64 characters if provided.");
6974
}
7075
}
7176

@@ -109,6 +114,9 @@ public class YoutubeProviderSettings : IProviderConfig
109114
[FieldDefinition(11, Label = "Max Download Speed", Type = FieldType.Number, HelpText = "Set to 0 for unlimited speed. Limits the download speed per download.", Unit = "KB/s", Advanced = true)]
110115
public int MaxDownloadSpeed { get; set; }
111116

117+
[FieldDefinition(12, Label = "PoToken", Type = FieldType.Textbox, HelpText = "A unique token to verify the origin of the request.", Advanced = true)]
118+
public string PoToken { get; set; } = string.Empty;
119+
112120
public NzbDroneValidationResult Validate() => new(Validator.Validate(this));
113121
}
114122

Tubifarry/ILRepack.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<InputAssemblies Include="$(OutputPath)Microsoft.Extensions.Logging.Abstractions.dll" />
1313
<InputAssemblies Include="$(OutputPath)Acornima.dll" />
1414
<InputAssemblies Include="$(OutputPath)Jint.dll" />
15+
<InputAssemblies Include="$(OutputPath)System.Text.Json.dll" />
1516
</ItemGroup>
1617

1718
<ILRepack

Tubifarry/Indexers/Spotify/SpotifyToYoutubeParser.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using NzbDrone.Core.Parser.Model;
55
using Requests;
66
using Requests.Options;
7+
using System.Net;
78
using System.Text.Json;
89
using Tubifarry.Core.Model;
910
using Tubifarry.Core.Utilities;
11+
using Tubifarry.Indexers.Youtube;
1012
using YouTubeMusicAPI.Client;
1113
using YouTubeMusicAPI.Models.Search;
1214
using YouTubeMusicAPI.Models.Streaming;
@@ -15,7 +17,7 @@ namespace Tubifarry.Indexers.Spotify
1517
{
1618
public interface ISpotifyToYoutubeParser : IParseIndexerResponse
1719
{
18-
public void SetCookies(string path);
20+
public void SetAuth(YoutubeIndexerSettings settings);
1921
}
2022

2123
/// <summary>
@@ -27,19 +29,25 @@ public class SpotifyToYoutubeParser : ISpotifyToYoutubeParser
2729

2830
private readonly Logger _logger;
2931
private string? _cookiePath;
32+
private string? _poToken;
3033

3134
public SpotifyToYoutubeParser()
3235
{
3336
_logger = NzbDroneLogger.GetLogger(this);
3437
_ytClient = new YouTubeMusicClient();
3538
}
3639

37-
public void SetCookies(string path)
40+
public void SetAuth(YoutubeIndexerSettings settings)
3841
{
39-
if (string.IsNullOrEmpty(path) || path == _cookiePath)
42+
if (settings.CookiePath == _cookiePath && settings.PoToken == _poToken)
4043
return;
41-
_cookiePath = path;
42-
_ytClient = new(cookies: CookieManager.ParseCookieFile(path));
44+
if (string.IsNullOrEmpty(settings.CookiePath) && string.IsNullOrEmpty(settings.PoToken))
45+
return;
46+
47+
_cookiePath = settings.CookiePath;
48+
_poToken = settings.PoToken;
49+
Cookie[]? cookies = !string.IsNullOrEmpty(settings.CookiePath) ? CookieManager.ParseCookieFile(settings.CookiePath) : null;
50+
_ytClient = new YouTubeMusicClient(cookies: cookies, poToken: settings.PoToken);
4351
}
4452

4553
/// <summary>

Tubifarry/Indexers/Spotify/TubifarryIndexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TubifarryIndexer(ISpotifyToYoutubeParser parser, ISpotifyRequestGenerator
3535

3636
protected override async Task Test(List<ValidationFailure> failures)
3737
{
38-
_parseIndexerResponse.SetCookies(Settings.CookiePath);
38+
_parseIndexerResponse.SetAuth(Settings);
3939
failures.AddIfNotNull(await TestConnection());
4040
}
4141

Tubifarry/Indexers/Youtube/YoutubeIndexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public YoutubeIndexer(IYoutubeParser parser, IYoutubeRequestGenerator generator,
4242

4343
protected override Task Test(List<ValidationFailure> failures)
4444
{
45-
_parseIndexerResponse.SetCookies(Settings.CookiePath);
45+
_parseIndexerResponse.SetAuth(Settings);
4646
_indexerRequestGenerator.SetCookies(Settings.CookiePath);
4747
return Task.CompletedTask;
4848
}

Tubifarry/Indexers/Youtube/YoutubeIndexerSettings.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-

2-
using FluentValidation;
1+
using FluentValidation;
32
using NzbDrone.Core.Annotations;
43
using NzbDrone.Core.Indexers;
54
using NzbDrone.Core.Validation;
65
using Tubifarry.Core.Utilities;
76

87
namespace Tubifarry.Indexers.Youtube
98
{
10-
119
public class YoutubeIndexerSettingsValidator : AbstractValidator<YoutubeIndexerSettings>
1210
{
1311
public YoutubeIndexerSettingsValidator()
@@ -18,6 +16,12 @@ public YoutubeIndexerSettingsValidator()
1816
.WithMessage("Cookie file does not exist. Please provide a valid path to the cookies file.")
1917
.Must(path => string.IsNullOrEmpty(path) || CookieManager.ParseCookieFile(path).Any())
2018
.WithMessage("Cookie file is invalid or contains no valid cookies.");
19+
20+
// Validate poToken (optional)
21+
RuleFor(x => x.PoToken)
22+
.Length(32, 64)
23+
.When(x => !string.IsNullOrEmpty(x.PoToken))
24+
.WithMessage("Proof of Origin (poToken) must be between 32 and 64 characters if provided.");
2125
}
2226
}
2327

@@ -31,8 +35,11 @@ public class YoutubeIndexerSettings : IIndexerSettings
3135
[FieldDefinition(1, Label = "Cookie Path", Type = FieldType.FilePath, Hidden = HiddenType.Visible, Placeholder = "/path/to/cookies.txt", HelpText = "Specify the path to the Spotify cookies file. This is optional but required for accessing restricted content.", Advanced = true)]
3236
public string CookiePath { get; set; } = string.Empty;
3337

38+
[FieldDefinition(2, Label = "PoToken)", Type = FieldType.Textbox, HelpText = "A unique token to verify the origin of the request.", Advanced = true)]
39+
public string PoToken { get; set; } = string.Empty;
40+
3441
public string BaseUrl { get; set; } = string.Empty;
3542

3643
public NzbDroneValidationResult Validate() => new(Validator.Validate(this));
3744
}
38-
}
45+
}

Tubifarry/Indexers/Youtube/YoutubeParser.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using NLog;
33
using NzbDrone.Core.Indexers;
44
using NzbDrone.Core.Parser.Model;
5+
using System.Net;
56
using Tubifarry.Core.Model;
67
using Tubifarry.Core.Utilities;
78
using YouTubeMusicAPI.Client;
@@ -14,7 +15,7 @@ namespace Tubifarry.Indexers.Youtube
1415
{
1516
public interface IYoutubeParser : IParseIndexerResponse
1617
{
17-
public void SetCookies(string path);
18+
public void SetAuth(YoutubeIndexerSettings settings);
1819
}
1920

2021
/// <summary>
@@ -26,19 +27,29 @@ public class YoutubeParser : IYoutubeParser
2627

2728
private readonly Logger _logger;
2829
private string? _cookiePath;
30+
private string? _poToken;
2931

3032
public YoutubeParser(Logger logger)
3133
{
3234
_logger = logger;
3335
_ytClient = new YouTubeMusicClient();
3436
}
3537

36-
public void SetCookies(string path)
38+
/// <summary>
39+
/// Sets cookies and poToken for the YouTube Music client.
40+
/// </summary>
41+
/// <param name="settings">The settings containing cookie path and poToken.</param>
42+
public void SetAuth(YoutubeIndexerSettings settings)
3743
{
38-
if (string.IsNullOrEmpty(path) || path == _cookiePath)
44+
if (settings.CookiePath == _cookiePath && settings.PoToken == _poToken)
3945
return;
40-
_cookiePath = path;
41-
_ytClient = new(cookies: CookieManager.ParseCookieFile(path));
46+
if (string.IsNullOrEmpty(settings.CookiePath) && string.IsNullOrEmpty(settings.PoToken))
47+
return;
48+
49+
_cookiePath = settings.CookiePath;
50+
_poToken = settings.PoToken;
51+
Cookie[]? cookies = !string.IsNullOrEmpty(settings.CookiePath) ? CookieManager.ParseCookieFile(settings.CookiePath) : null;
52+
_ytClient = new YouTubeMusicClient(cookies: cookies, poToken: settings.PoToken);
4253
}
4354

4455
public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse)

Tubifarry/Tubifarry.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="FuzzySharp" Version="2.0.2" />
14-
<PackageReference Include="ILRepack.Lib.MSBuild.Task" Version="2.0.34.2">
14+
<PackageReference Include="ILRepack.Lib.MSBuild.Task" Version="2.0.37">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
1818
<PackageReference Include="Shard.DownloadAssistant" Version="1.1.0" />
19-
<PackageReference Include="Xabe.FFmpeg" Version="5.2.6" />
20-
<PackageReference Include="Xabe.FFmpeg.Downloader" Version="5.2.6" />
21-
<PackageReference Include="YouTubeMusicAPI" Version="2.2.1" />
19+
<PackageReference Include="Xabe.FFmpeg" Version="6.0.1" />
20+
<PackageReference Include="Xabe.FFmpeg.Downloader" Version="6.0.1" />
21+
<PackageReference Include="YouTubeMusicAPI" Version="2.2.2" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

0 commit comments

Comments
 (0)