fix(openverse): decode HTML entities in titles and creators

Openverse sends some texts HTML-encoded, so "Angus & Friends" showed up
as it came. Error messages are in German like the rest of the interface.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
fraujulianandClaude Fable 5.2 committed 2026-11-23 14:22:49 +01:00
1 parent f0726bc9fa
commit 44d07a10df
4 files changed
+30 -19

No files matched your search

@@ -16,9 +16,9 @@ public class OpenverseController(OpenverseClient client, SongRepository songs)
public async Task<Song> ImportAsync(OpenverseTrack track, CancellationToken cancellationToken = default)
{
if (track.Duration is not { } duration)
throw new OpenverseException("This result has no duration and cannot be imported.");
throw new OpenverseException("Dieses Ergebnis hat keine Dauer und kann nicht importiert werden.");
if (track.Id.Length > 0 && ImportedIds().Contains(track.Id))
throw new OpenverseException($"\"{track.Title}\" is already in the library.");
throw new OpenverseException($"\"{track.Title}\" ist bereits in der Bibliothek.");
var path = await client.DownloadAsync(track, cancellationToken);
var song = new Song
+5 -4
View File
@@ -3,17 +3,18 @@ using System.Text.Json.Serialization;
namespace Radiosender.Models;
/// <summary>One audio result of the Openverse search API (fields as documented at api.openverse.org/v1).</summary>
/// <remarks>Openverse sends some texts HTML-encoded (&amp;amp;), so the text fields decode on assignment.</remarks>
public class OpenverseTrack
{
public string Id { get; init; } = "";
public string Title { get; init; } = "";
public string? Creator { get; init; }
public string Title { get; init => field = System.Net.WebUtility.HtmlDecode(value); } = "";
public string? Creator { get; init => field = System.Net.WebUtility.HtmlDecode(value); }
public string Url { get; init; } = "";
[JsonPropertyName("filetype")]
public string? FileType { get; init; }
public string License { get; init; } = "";
public string? LicenseVersion { get; init; }
public string? Attribution { get; init; }
public string? Attribution { get; init => field = System.Net.WebUtility.HtmlDecode(value); }
/// <summary>Length in milliseconds; Openverse leaves it empty for some results.</summary>
[JsonPropertyName("duration")]
@@ -25,7 +26,7 @@ public class OpenverseTrack
/// <summary>Songs need a duration for scheduling, so results without one cannot be imported.</summary>
public bool CanImport => Duration is not null;
public string DurationText => Duration?.ToString(@"m\:ss") ?? "unknown";
public string DurationText => Duration?.ToString(@"m\:ss") ?? "unbekannt";
/// <summary>Readable license name, for example "CC BY 4.0", "CC0 1.0" or "Public Domain Mark 1.0".</summary>
public string LicenseName => License.ToLowerInvariant() switch
+10 -10
View File
@@ -46,7 +46,7 @@ public class OpenverseClient(HttpClient http, string musicFolder)
}
catch (Exception e) when (e is not OpenverseException && !cancellationToken.IsCancellationRequested)
{
throw Wrap(e, "Openverse search failed");
throw Wrap(e, "Openverse-Suche fehlgeschlagen");
}
}
@@ -58,7 +58,7 @@ public class OpenverseClient(HttpClient http, string musicFolder)
public async Task<string> DownloadAsync(OpenverseTrack track, CancellationToken cancellationToken = default)
{
if (!Uri.TryCreate(track.Url, UriKind.Absolute, out var uri) || uri.Scheme != Uri.UriSchemeHttps)
throw new OpenverseException("Only https downloads are allowed.");
throw new OpenverseException("Nur https-Downloads sind erlaubt.");
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeout.CancelAfter(DownloadTimeout);
@@ -68,7 +68,7 @@ public class OpenverseClient(HttpClient http, string musicFolder)
using var response = await http.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
EnsureSuccess(response);
if (response.Content.Headers.ContentLength > MaxDownloadBytes)
throw new OpenverseException("The file is larger than 50 MB.");
throw new OpenverseException("Die Datei ist größer als 50 MB.");
Directory.CreateDirectory(musicFolder);
await using var file = CreateUniqueFile(track, out path);
@@ -80,11 +80,11 @@ public class OpenverseClient(HttpClient http, string musicFolder)
{
total += read;
if (total > MaxDownloadBytes)
throw new OpenverseException("The file is larger than 50 MB.");
throw new OpenverseException("Die Datei ist größer als 50 MB.");
await file.WriteAsync(buffer.AsMemory(0, read), timeout.Token);
}
if (total == 0)
throw new OpenverseException("The download was empty.");
throw new OpenverseException("Der Download war leer.");
return path;
}
catch (Exception e)
@@ -94,7 +94,7 @@ public class OpenverseClient(HttpClient http, string musicFolder)
File.Delete(path);
if (e is OpenverseException || cancellationToken.IsCancellationRequested)
throw;
throw Wrap(e, "Download failed");
throw Wrap(e, "Download fehlgeschlagen");
}
}
@@ -120,15 +120,15 @@ public class OpenverseClient(HttpClient http, string musicFolder)
private static void EnsureSuccess(HttpResponseMessage response)
{
if (response.StatusCode == HttpStatusCode.TooManyRequests)
throw new OpenverseException("Openverse rate limit reached (anonymous: 20 requests per minute, 200 per day). Please try again later.");
throw new OpenverseException("Openverse-Limit erreicht (anonym: 20 Anfragen pro Minute, 200 pro Tag). Bitte später erneut versuchen.");
if (!response.IsSuccessStatusCode)
throw new OpenverseException($"Openverse returned HTTP {(int)response.StatusCode}.");
throw new OpenverseException($"Openverse hat HTTP {(int)response.StatusCode} geliefert.");
}
private static OpenverseException Wrap(Exception e, string action) => e switch
{
OperationCanceledException => new OpenverseException($"{action}: the request timed out.", e),
JsonException => new OpenverseException($"{action}: unexpected response.", e),
OperationCanceledException => new OpenverseException($"{action}: Zeitüberschreitung der Anfrage.", e),
JsonException => new OpenverseException($"{action}: unerwartete Antwort.", e),
_ => new OpenverseException($"{action}: {e.Message}", e),
};
+13 -3
View File
@@ -52,6 +52,16 @@ public sealed class OpenverseTests : IDisposable
private string[] MusicFiles() => Directory.Exists(MusicFolder) ? Directory.GetFiles(MusicFolder) : [];
[Fact]
public void Track_DecodesHtmlEntitiesInTexts()
{
var track = new OpenverseTrack { Title = "Rock &amp; Roll", Creator = "Angus &amp; Friends", Attribution = "&quot;Rock&quot; by Angus" };
Assert.Equal("Rock & Roll", track.Title);
Assert.Equal("Angus & Friends", track.Creator);
Assert.Equal("\"Rock\" by Angus", track.Attribution);
}
[Fact]
public async Task Search_ParsesResults()
{
@@ -87,7 +97,7 @@ public sealed class OpenverseTests : IDisposable
var error = await Assert.ThrowsAsync<OpenverseException>(() => client.SearchAsync("x"));
Assert.Contains("rate limit", error.Message);
Assert.Contains("Limit erreicht", error.Message);
}
[Fact]
@@ -101,7 +111,7 @@ public sealed class OpenverseTests : IDisposable
var error = await Assert.ThrowsAsync<OpenverseException>(() => client.SearchAsync("x"));
Assert.Contains("timed out", error.Message);
Assert.Contains("Zeitüberschreitung", error.Message);
}
[Fact]
@@ -228,7 +238,7 @@ public sealed class OpenverseTests : IDisposable
var error = await Assert.ThrowsAsync<OpenverseException>(() => controller.ImportAsync(Track()));
Assert.Contains("already in the library", error.Message);
Assert.Contains("bereits in der Bibliothek", error.Message);
Assert.Equal(1, downloads);
Assert.Single(songs.GetAll());
Assert.Single(MusicFiles());