Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Infrastructure/Interaction/NetCordInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ public async Task<string> Play()
return "The requested song is blacklisted and cannot be played.";
}

var title = await youtubeService.GetVideoTitleAsync(selectedValue, CancellationToken.None);
string title;
try
{
title = await youtubeService.GetVideoTitleAsync(selectedValue, CancellationToken.None);
}
catch (Exception ex)
{
logger.LogWarning(ex, "Failed to fetch title for {Url}, enqueueing without a title", selectedValue);
title = "the requested track";
}

message = $"Added {title} to the queue!";
}
else
Expand Down
33 changes: 33 additions & 0 deletions src/Infrastructure/Services/YoutubeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,39 @@ public async Task<string> GetAudioStreamUrlAsync(string url, CancellationToken c

public async Task<string> GetVideoTitleAsync(string url, CancellationToken cancellationToken)
{
var (success, title) = await ExecuteWithTimeout(TryGetTitleWithYtDlpAsync, url, TimeSpan.FromSeconds(15), cancellationToken);
if (success)
{
return title!;
}

return (await _youtubeClient.Videos.GetAsync(url, cancellationToken)).Title;
}

private async Task<(bool Success, string? Url)> TryGetTitleWithYtDlpAsync(string url, CancellationToken cancellationToken)
{
try
{
var ytdl = new YoutubeDL { YoutubeDLPath = "yt-dlp" };
var result = await ytdl.RunVideoDataFetch(url, ct: cancellationToken);

if (!result.Success || string.IsNullOrWhiteSpace(result.Data?.Title))
{
_logger.LogWarning("YT-DLP title fetch failed for: {Url}. Errors: {Errors}", url,
string.Join("; ", result.ErrorOutput ?? []));
return (false, null);
}

return (true, result.Data.Title);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "YT-DLP title fetch failed for: {Url}", url);
return (false, null);
}
}
}
Loading