fix: fixed search of tidal tracks using WebUtils
Some checks failed
Deploy / update (push) Has been cancelled
Build Project .NET / build (push) Has been cancelled

This commit is contained in:
Lisoveliy
2025-05-12 21:37:29 +03:00
parent fd7a274e82
commit 4a95ba0d86
10 changed files with 59 additions and 24 deletions

View File

@@ -117,5 +117,9 @@ public class LinksService
{
return (null, ServiceResult.NoResponse);
}
catch (TooManyRequestsException)
{
return (null, ServiceResult.TooManyRequests);
}
}
}

View File

@@ -53,7 +53,7 @@ public class TidalService : ApiService
clientHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.api+json"));
searchUri.Scheme = "https";
searchUri.Path = Path.Combine(searchUri.Path, Config.ApiPaths.Search, WebUtility.UrlEncode(GetQuery(query)
searchUri.Path = Path.Combine(searchUri.Path, Config.ApiPaths.Search, Uri.EscapeDataString(GetQuery(query)
//TODO: Это нужно, разработчики TIDAL дауны
));
var url = new UriBuilder(searchUri.Uri)

View File

@@ -1,4 +1,5 @@
using System.Net;
using System.Text.Json;
using System.Web;
using Microsoft.Extensions.Options;
using SWAD.API.Consts;
@@ -39,12 +40,12 @@ public class YandexService : ApiService
var json = await response.Content.ReadFromJsonAsync<YandexSearchResponse>();
// ReSharper disable once NullableWarningSuppressionIsUsed
if (json.Entities.Count < 1)
if (json?.Entities.Count < 1)
{
throw new TrackNotFoundException($"Track is not found in {ServiceType}");
}
var results = json.Entities.FirstOrDefault(x => x.Type == "track").Results;
var results = json?.Entities.FirstOrDefault(x => x.Type == "track")?.Results;
/*
* ВНИМАНИЕ
* Yandex API писали шизы поэтому когда будешь добавлять поля в YandexSearchResponse и здесь их юзать
@@ -52,9 +53,10 @@ public class YandexService : ApiService
* ЭТО ВЫЗОВЕТ ИСКЛЮЧЕНИЕ
* Думой
*/
var resultTrack = results.FirstOrDefault(x => x.Track.Title == query.Name) ??
results[0];
return $"{Config.Endpoints.MusicLink[0]}{resultTrack.Track.Id}";
var resultTrack = results?.FirstOrDefault(x => x.Track.Title == query.Name) ??
results?[0];
return $"{Config.Endpoints.MusicLink[0]}{resultTrack?.Track.Id ??
throw new TrackNotFoundException($"Track is not found in {ServiceType}")}";
}
public override async Task<TrackDto> GetQueryObject(string link, string countryCode = "RU")
@@ -80,9 +82,16 @@ public class YandexService : ApiService
else
throw new HttpRequestException(ErrorResources.Unsuccessful);
var json = await response.Content.ReadFromJsonAsync<YandexTrackResponse>();
// ReSharper disable once NullableWarningSuppressionIsUsed
var artists = string.Join(", ", json!.Artists.ToList().ConvertAll(x => x.Name));
return new TrackDto(json.Track.Title, artists, json.Track.Albums[0].Title, ServiceType);
try
{
var json = await response.Content.ReadFromJsonAsync<YandexTrackResponse>();
// ReSharper disable once NullableWarningSuppressionIsUsed
var artists = string.Join(", ", json!.Artists.ToList().ConvertAll(x => x.Name));
return new TrackDto(json.Track.Title, artists, json.Track.Albums[0].Title, ServiceType);
}
catch (JsonException ex)
{
throw new TooManyRequestsException("Яндекс, похоже, считает что мы робот, что по сути является правдой. Попробуйте позже.");
}
}
}