Skip to content

Commit b0f6942

Browse files
author
Meyn
committed
Implement YouTube SOCKS5 proxy
1 parent f45008e commit b0f6942

File tree

13 files changed

+224
-30
lines changed

13 files changed

+224
-30
lines changed

Tubifarry/Core/Model/AudioMetadataHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public async Task<bool> TryExtractAudioFromVideoAsync()
245245

246246
File.Move(tempOutputPath, finalOutputPath, true);
247247
TrackPath = finalOutputPath;
248+
await EnsureFileExtAsync();
248249

249250
_logger?.Trace($"Successfully extracted audio to {Path.GetFileName(TrackPath)}");
250251
return true;

Tubifarry/Core/Utilities/TrustedSessionHelper.cs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class TrustedSessionHelper
2020
private static readonly Logger _logger = NzbDroneLogger.GetLogger(typeof(TrustedSessionHelper));
2121

2222
private static SessionTokens? _cachedTokens;
23+
private static bool? _nodeJsAvailable;
24+
private static object _nodeJsCheckLock = new();
2325
private static readonly SemaphoreSlim _semaphore = new(1, 1);
2426

2527
// Constants for retry logic
@@ -41,14 +43,14 @@ public static async Task<SessionTokens> GetTrustedSessionTokensAsync(string? ser
4143
return _cachedTokens;
4244
}
4345

44-
SessionTokens newTokens;
46+
SessionTokens newTokens = new("", "", DateTime.UtcNow.AddHours(12));
4547

4648
if (!string.IsNullOrEmpty(serviceUrl))
4749
{
4850
_logger.Trace($"Using web service approach with URL: {serviceUrl}");
4951
newTokens = await GetTokensFromWebServiceAsync(serviceUrl, forceRefresh, token);
5052
}
51-
else
53+
else if (IsNodeJsAvailable())
5254
{
5355
_logger.Trace("Using local YouTubeSessionGenerator");
5456
newTokens = await GetTokensFromLocalGeneratorAsync(token);
@@ -301,25 +303,8 @@ private static SessionTokens ParseTokenResponse(string responseJson, string sour
301303
/// </summary>
302304
public static async Task ValidateAuthenticationSettingsAsync(string? trustedSessionGeneratorUrl = null, string? cookiePath = null)
303305
{
304-
if (string.IsNullOrEmpty(trustedSessionGeneratorUrl) && string.IsNullOrEmpty(cookiePath))
305-
{
306-
NodeEnvironment? testEnv = null;
307-
try
308-
{
309-
testEnv = new NodeEnvironment();
310-
_logger.Trace("Node.js environment test successful");
311-
}
312-
catch (Exception ex)
313-
{
314-
throw new ArgumentException($"Node.js environment is not available for local token generation: {ex.Message}");
315-
}
316-
finally
317-
{
318-
testEnv?.Dispose();
319-
}
320-
321-
return;
322-
}
306+
if (string.IsNullOrEmpty(trustedSessionGeneratorUrl) && !IsNodeJsAvailable())
307+
_logger.Warn("Node.js environment is not available for local token generation.");
323308

324309
if (!string.IsNullOrEmpty(trustedSessionGeneratorUrl))
325310
{
@@ -359,6 +344,37 @@ public static async Task ValidateAuthenticationSettingsAsync(string? trustedSess
359344
}
360345
}
361346

347+
/// <summary>
348+
/// Checks if Node.js is available for local token generation
349+
/// </summary>
350+
private static bool IsNodeJsAvailable()
351+
{
352+
lock (_nodeJsCheckLock)
353+
{
354+
if (_nodeJsAvailable.HasValue)
355+
return _nodeJsAvailable.Value;
356+
NodeEnvironment? testEnv = null;
357+
try
358+
{
359+
_logger.Trace("Checking Node.js availability...");
360+
testEnv = new();
361+
_nodeJsAvailable = true;
362+
_logger.Debug("Node.js environment is available for local token generation");
363+
return true;
364+
}
365+
catch (Exception ex)
366+
{
367+
_nodeJsAvailable = false;
368+
_logger.Warn(ex, "Node.js environment is not available for local token generation: {Message}", ex.Message);
369+
return false;
370+
}
371+
finally
372+
{
373+
testEnv?.Dispose();
374+
}
375+
}
376+
}
377+
362378
public static void ClearCache()
363379
{
364380
_cachedTokens = null;

Tubifarry/Download/Clients/YouTube/YouTubeAlbumRequest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ private void AddTrackDownloadRequests(AlbumInfo albumInfo, AlbumSong trackInfo,
198198
private async Task<bool> SongDownloadCompletedAsync(AlbumInfo albumInfo, AlbumSong trackInfo, LoadRequest req, CancellationToken token)
199199
{
200200
string trackPath = req.Destination;
201+
await Task.Delay(100, token);
201202
if (!File.Exists(trackPath))
202203
return false;
203204

Tubifarry/Download/Clients/YouTube/YoutubeClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected override void Test(List<ValidationFailure> failures)
5858
{
5959
TrustedSessionHelper.ValidateAuthenticationSettingsAsync(Settings.TrustedSessionGeneratorUrl, Settings.CookiePath).Wait();
6060
SessionTokens session = TrustedSessionHelper.GetTrustedSessionTokensAsync(Settings.TrustedSessionGeneratorUrl, true).Result;
61-
if (!session.IsValid)
61+
if (!session.IsValid && !session.IsEmpty)
6262
failures.Add(new ValidationFailure("TrustedSessionGeneratorUrl", "Failed to retrieve valid tokens from the session generator service"));
6363
}
6464
catch (Exception ex)

Tubifarry/Indexers/Spotify/TubifarryIndexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected override async Task Test(List<ValidationFailure> failures)
5454
{
5555
await TrustedSessionHelper.ValidateAuthenticationSettingsAsync(Settings.TrustedSessionGeneratorUrl, Settings.CookiePath);
5656
SessionTokens session = await TrustedSessionHelper.GetTrustedSessionTokensAsync(Settings.TrustedSessionGeneratorUrl, true);
57-
if (!session.IsValid)
57+
if (!session.IsValid && !session.IsEmpty)
5858
failures.Add(new ValidationFailure("TrustedSessionGeneratorUrl", "Failed to retrieve valid tokens from the session generator service"));
5959
}
6060
catch (Exception ex)

Tubifarry/Indexers/Youtube/YoutubeIndexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected override async Task Test(List<ValidationFailure> failures)
5050
{
5151
await TrustedSessionHelper.ValidateAuthenticationSettingsAsync(Settings.TrustedSessionGeneratorUrl, Settings.CookiePath);
5252
SessionTokens session = await TrustedSessionHelper.GetTrustedSessionTokensAsync(Settings.TrustedSessionGeneratorUrl, true);
53-
if (!session.IsValid)
53+
if (!session.IsValid && !session.IsEmpty)
5454
failures.Add(new ValidationFailure("TrustedSessionGeneratorUrl", "Failed to retrieve valid tokens from the session generator service"));
5555
}
5656
catch (Exception ex)

Tubifarry/Notifications/Queue/ImportFailureNotificationService.cs renamed to Tubifarry/Notifications/QueueCleaner/ImportFailureNotificationService.cs

File renamed without changes.

Tubifarry/Notifications/Queue/QueueCleaner.cs renamed to Tubifarry/Notifications/QueueCleaner/QueueCleaner.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ internal void CleanImport(AlbumImportIncompleteEvent message)
5656
if (!trackedDownload.IsTrackable || trackedDownload.State != TrackedDownloadState.ImportFailed || !trackedDownload.DownloadItem.CanMoveFiles)
5757
return;
5858

59-
ImportFailureReason failureReason = CheckImport(trackedDownload);
60-
61-
switch (failureReason)
59+
switch (CheckImport(trackedDownload))
6260
{
6361
case ImportFailureReason.FailedBecauseOfMissingTracks:
6462
HandleFailure(trackedDownload, Settings.ImportCleaningOption, ImportCleaningOptions.WhenMissingTracks);
@@ -101,8 +99,7 @@ private void Remove(TrackedDownload item)
10199
{
102100
if (!item.DownloadItem.CanBeRemoved)
103101
return;
104-
List<IFileInfo> filesOnDisk = _diskProvider.GetFileInfos(item.DownloadItem.OutputPath.FullPath, true);
105-
foreach (IFileInfo file in filesOnDisk)
102+
foreach (IFileInfo file in (List<IFileInfo>)_diskProvider.GetFileInfos(item.DownloadItem.OutputPath.FullPath, true))
106103
_diskProvider.DeleteFile(file.FullName);
107104
_eventAggregator.PublishEvent(new DownloadCanBeRemovedEvent(item));
108105
}

Tubifarry/Notifications/Queue/QueueCleanerSettings.cs renamed to Tubifarry/Notifications/QueueCleaner/QueueCleanerSettings.cs

File renamed without changes.

0 commit comments

Comments
 (0)