Доработаны сервисы: уменьшенно кол-во создаваемых объектов
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using PlaylistShared.Api.Entities;
|
||||
using PlaylistShared.Api.Entities;
|
||||
using PlaylistShared.Api.Extensions;
|
||||
using PlaylistShared.Shared.Enums;
|
||||
using PlaylistShared.Shared.Yandex;
|
||||
using YandexMusic;
|
||||
using YandexMusic.API.Extensions.API;
|
||||
using YandexMusic.API;
|
||||
using YandexMusic.API.Models.Playlist;
|
||||
using YandexMusic.API.Models.Track;
|
||||
|
||||
@@ -12,56 +10,45 @@ namespace PlaylistShared.Api.Services;
|
||||
|
||||
public class YandexMusicService
|
||||
{
|
||||
private readonly IDataProtector _dataProtector;
|
||||
private readonly YandexApiService _yandexApiService;
|
||||
private YandexMusicApi Api => _yandexApiService.Client.Api;
|
||||
private bool IsAuthorized => _yandexApiService.Client.IsAuthorized;
|
||||
|
||||
public YandexMusicService(IDataProtectionProvider provider)
|
||||
public YandexMusicService(YandexApiService yandexApiService)
|
||||
{
|
||||
_dataProtector = provider.CreateProtector("YandexTokens");
|
||||
_yandexApiService = yandexApiService;
|
||||
}
|
||||
|
||||
private async Task<YandexMusicClient?> CreateClientAsync(ApplicationUser user)
|
||||
private async Task AuthorizeIfNot(ApplicationUser user)
|
||||
{
|
||||
if (string.IsNullOrEmpty(user.YandexAccessToken))
|
||||
return null;
|
||||
|
||||
string decryptedToken;
|
||||
try
|
||||
if (!IsAuthorized)
|
||||
{
|
||||
decryptedToken = _dataProtector.Unprotect(user.YandexAccessToken);
|
||||
var authResult = await _yandexApiService.AuthAsync(user);
|
||||
if (authResult == null || authResult == false)
|
||||
throw new Exception("Не удалось авторизоваться в Яндекс.Музыке. Проверьте токен.");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var client = new YandexMusicClient();
|
||||
var success = await client.Authorize(decryptedToken);
|
||||
return success ? client : null;
|
||||
}
|
||||
|
||||
public async Task<YPlaylist?> GetPlaylistAsync(ApplicationUser user, string ownerUid, string kind)
|
||||
{
|
||||
var client = await CreateClientAsync(user);
|
||||
if (client == null) return null;
|
||||
return await client.GetPlaylistAsync(ownerUid, kind);
|
||||
await AuthorizeIfNot(user);
|
||||
return await Api.Playlist.GetAsync(ownerUid, kind);
|
||||
}
|
||||
|
||||
public async Task<YPlaylist?> CreatePlaylistAsync(ApplicationUser user, string title)
|
||||
{
|
||||
var client = await CreateClientAsync(user);
|
||||
if (client == null) return null;
|
||||
return await client.CreatePlaylistAsync(title);
|
||||
await AuthorizeIfNot(user);
|
||||
return await Api.Playlist.CreateAsync(title);
|
||||
}
|
||||
|
||||
public async Task<YPlaylist?> AddTracksAsync(ApplicationUser user, string ownerUid, string kind, IEnumerable<string> trackIds)
|
||||
{
|
||||
var client = await CreateClientAsync(user);
|
||||
if (client == null) return null;
|
||||
await AuthorizeIfNot(user);
|
||||
|
||||
var playlist = await client.GetPlaylistAsync(ownerUid, kind);
|
||||
var playlist = await Api.Playlist.GetAsync(ownerUid, kind);
|
||||
if (playlist == null) return null;
|
||||
|
||||
var tracks = await client.GetTracksAsync(trackIds);
|
||||
var tracks = await Api.Track.GetAsync(trackIds);
|
||||
if (tracks == null || !tracks.Any()) return null;
|
||||
|
||||
var insertedTracks = tracks.Where(t => !playlist.Tracks.Any(p => p.Track.Id == t.Id)).ToArray();
|
||||
@@ -71,27 +58,32 @@ public class YandexMusicService
|
||||
|
||||
public async Task<YPlaylist?> RemoveTracksAsync(ApplicationUser user, string ownerUid, string kind, IEnumerable<string> trackIds)
|
||||
{
|
||||
var client = await CreateClientAsync(user);
|
||||
if (client == null) return null;
|
||||
var tracks = await client.GetTracksAsync(trackIds);
|
||||
await AuthorizeIfNot(user);
|
||||
|
||||
var tracks = await Api.Track.GetAsync(trackIds);
|
||||
if (tracks == null || !tracks.Any()) return null;
|
||||
var playlist = await client.GetPlaylistAsync(ownerUid, kind);
|
||||
|
||||
var playlist = await Api.Playlist.GetAsync(ownerUid, kind);
|
||||
if (playlist == null) return null;
|
||||
|
||||
return await playlist.RemoveTracksAsync(tracks.ToArray());
|
||||
}
|
||||
|
||||
public async Task<string?> GetTrackFileUrlAsync(ApplicationUser user, string trackId)
|
||||
{
|
||||
await AuthorizeIfNot(user);
|
||||
|
||||
var track = await GetYTrackAsync(user, trackId);
|
||||
if (track == null) return null;
|
||||
|
||||
return await track.GetLinkAsync();
|
||||
}
|
||||
|
||||
public async Task<YTrack?> GetYTrackAsync(ApplicationUser user, string trackId)
|
||||
{
|
||||
using var client = await CreateClientAsync(user);
|
||||
if (client == null) return null;
|
||||
var track = await client.GetTrackAsync(trackId);
|
||||
await AuthorizeIfNot(user);
|
||||
|
||||
var track = await Api.Track.GetAsync(trackId);
|
||||
return track;
|
||||
}
|
||||
|
||||
@@ -102,8 +94,7 @@ public class YandexMusicService
|
||||
int limit = 20
|
||||
)
|
||||
{
|
||||
var client = await CreateClientAsync(user);
|
||||
if (client == null) return new YandexSearchResult();
|
||||
await AuthorizeIfNot(user);
|
||||
|
||||
var ySerchType = searchType switch
|
||||
{
|
||||
@@ -114,7 +105,7 @@ public class YandexMusicService
|
||||
_ => YandexMusic.API.Models.Common.YSearchType.All
|
||||
};
|
||||
|
||||
var searchResult = await client.SearchAsync(query, ySerchType, page: 0, pageSize: limit);
|
||||
var searchResult = await Api.Search.SearchAsync(query, ySerchType, page: 0, pageSize: limit);
|
||||
if (searchResult?.Tracks?.Results == null) return new YandexSearchResult();
|
||||
|
||||
return new YandexSearchResult
|
||||
@@ -179,8 +170,7 @@ public class YandexMusicService
|
||||
{
|
||||
YandexSearchResult result = new();
|
||||
|
||||
var client = await CreateClientAsync(user);
|
||||
if (client == null) return result;
|
||||
await AuthorizeIfNot(user);
|
||||
|
||||
if (searchType == TrackSearchType.All)
|
||||
{
|
||||
@@ -189,7 +179,7 @@ public class YandexMusicService
|
||||
|
||||
else if (searchType == TrackSearchType.Track)
|
||||
{
|
||||
var track = await client.GetTrackAsync(id);
|
||||
var track = await Api.Track.GetAsync(id);
|
||||
|
||||
if (track != null)
|
||||
{
|
||||
@@ -215,7 +205,7 @@ public class YandexMusicService
|
||||
|
||||
else if (searchType == TrackSearchType.Album)
|
||||
{
|
||||
var album = await client.GetAlbumAsync(id);
|
||||
var album = await Api.Album.GetAsync(id);
|
||||
|
||||
result.Tracks = album?.Volumes.SelectMany(v => v).Select(t => new YandexTrack
|
||||
{
|
||||
@@ -235,7 +225,7 @@ public class YandexMusicService
|
||||
|
||||
else if (searchType == TrackSearchType.Artist)
|
||||
{
|
||||
var artist = await client.GetArtistAsync(id);
|
||||
var artist = await Api.Artist.GetAsync(id);
|
||||
if (artist != null)
|
||||
{
|
||||
result.Albums = artist.Albums.Select(a => new YandexAlbum()
|
||||
@@ -284,7 +274,7 @@ public class YandexMusicService
|
||||
|
||||
else if (searchType == TrackSearchType.Playlist)
|
||||
{
|
||||
var playlist = await client.GetPlaylistAsync(id);
|
||||
var playlist = await Api.Playlist.GetAsync(id);
|
||||
|
||||
result.Tracks = playlist?.Tracks.Select(p => new YandexTrack
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user