Доработаны сервисы: уменьшенно кол-во создаваемых объектов
This commit is contained in:
@@ -8,7 +8,6 @@ using PlaylistShared.Shared;
|
||||
using PlaylistShared.Shared.Enums;
|
||||
using PlaylistShared.Shared.SharedPlaylist;
|
||||
using PlaylistShared.Shared.Yandex;
|
||||
using YandexMusic;
|
||||
|
||||
namespace PlaylistShared.Api.Controllers;
|
||||
|
||||
@@ -45,13 +44,12 @@ public class PlaylistsController : ControllerBase
|
||||
if (string.IsNullOrEmpty(decryptedToken))
|
||||
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 400, Message = "Токен Яндекс.Музыки не установлен или недействителен" }));
|
||||
|
||||
var yandexClient = new YandexMusicClient();
|
||||
var authSuccess = await yandexClient.Authorize(decryptedToken);
|
||||
var authSuccess = await _yandexApiService.AuthAsync(decryptedToken);
|
||||
if (!authSuccess)
|
||||
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 400, Message = "Не удалось авторизоваться в Яндекс.Музыке. Проверьте токен." }));
|
||||
|
||||
var favorites = await yandexClient.GetFavoritesAsync();
|
||||
var ownPlaylists = favorites.Where(p => p.Owner.Uid == yandexClient.Account.Uid).ToList();
|
||||
var favorites = await _yandexApiService.Client.Api.Playlist.FavoritesAsync();
|
||||
var ownPlaylists = favorites.Where(p => p.Owner.Uid == _yandexApiService.Client.Account.Uid).ToList();
|
||||
|
||||
var sharedPlaylists = await _sharedService.GetAllByUserAsync(userId);
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="10.1.7" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.7" />
|
||||
<PackageReference Include="YandexMusic" Version="0.0.14" />
|
||||
<PackageReference Include="YandexMusic" Version="0.0.15" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -44,7 +44,28 @@ public class YandexApiService : IDisposable
|
||||
if (decryptedToken == null)
|
||||
return null;
|
||||
|
||||
return await _client.Authorize(decryptedToken);
|
||||
return await AuthorizeAsync(decryptedToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Авторизуется с помощью OAuth-токена.
|
||||
/// </summary>
|
||||
public async Task<bool> AuthAsync(string token)
|
||||
{
|
||||
return await AuthorizeAsync(token);
|
||||
}
|
||||
|
||||
private async Task<bool> AuthorizeAsync(string token)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _client.Api.Auth.AuthorizeAsync(token);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -100,14 +121,6 @@ public class YandexApiService : IDisposable
|
||||
return cookie?.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Авторизуется с помощью OAuth-токена.
|
||||
/// </summary>
|
||||
public async Task<bool> AuthorizeAsync(string token)
|
||||
{
|
||||
return await _client.Authorize(token);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
|
||||
@@ -4,6 +4,7 @@ using PlaylistShared.Api.Entities;
|
||||
using PlaylistShared.Shared.Yandex;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using YandexMusic.API;
|
||||
|
||||
namespace PlaylistShared.Api.Services;
|
||||
|
||||
@@ -13,6 +14,7 @@ public class YandexAuthService
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
public YandexApiService Service => _apiService;
|
||||
public YandexMusicApi Api => _apiService.Client.Api;
|
||||
|
||||
public YandexAuthService(YandexApiService apiService, ApplicationDbContext dbContext)
|
||||
{
|
||||
@@ -41,12 +43,11 @@ public class YandexAuthService
|
||||
|
||||
internal async Task<YandexAuthQr> GenerateQrAsync(ApplicationUser user)
|
||||
{
|
||||
var client = _apiService.Client;
|
||||
var qr = await client.GetAuthQRLink();
|
||||
var trackId = client.AuthStorage.AuthToken.TrackId;
|
||||
var csfrToken = client.AuthStorage.AuthToken.CsfrToken;
|
||||
var headerProcessUuid = client.AuthStorage.HeaderToken.ProcessUuid;
|
||||
var headerCsfrToken = client.AuthStorage.HeaderToken.CsfrToken;
|
||||
var qr = await Api.Passport.GetAuthQRLinkAsync();
|
||||
var trackId = Service.Client.AuthStorage.AuthToken.TrackId;
|
||||
var csfrToken = Service.Client.AuthStorage.AuthToken.CsfrToken;
|
||||
var headerProcessUuid = Service.Client.AuthStorage.HeaderToken.ProcessUuid;
|
||||
var headerCsfrToken = Service.Client.AuthStorage.HeaderToken.CsfrToken;
|
||||
|
||||
if (string.IsNullOrEmpty(qr))
|
||||
throw new Exception("Не удалось получить QR-ссылку");
|
||||
@@ -82,24 +83,24 @@ public class YandexAuthService
|
||||
var session = await _dbContext.YandexAuthSessions.FindAsync(sessionId);
|
||||
if (session == null) return null;
|
||||
|
||||
RestoreCookies(_apiService.CookieContainer, session.SerializedCookies);
|
||||
if (_apiService.Client.AuthStorage.AuthToken is null)
|
||||
RestoreCookies(Service.CookieContainer, session.SerializedCookies);
|
||||
if (Service.Client.AuthStorage.AuthToken is null)
|
||||
{
|
||||
_apiService.Client.AuthStorage.AuthToken = new();
|
||||
Service.Client.AuthStorage.AuthToken = new();
|
||||
}
|
||||
|
||||
_apiService.Client.AuthStorage.AuthToken.CsfrToken = session?.CsfrToken ?? "";
|
||||
_apiService.Client.AuthStorage.AuthToken.TrackId = session?.TrackId ?? "";
|
||||
_apiService.Client.AuthStorage.HeaderToken.CsfrToken = session?.HeaderCsfrToken ?? "";
|
||||
_apiService.Client.AuthStorage.HeaderToken.ProcessUuid = session?.HeaderProcessId ?? "";
|
||||
Service.Client.AuthStorage.AuthToken.CsfrToken = session?.CsfrToken ?? "";
|
||||
Service.Client.AuthStorage.AuthToken.TrackId = session?.TrackId ?? "";
|
||||
Service.Client.AuthStorage.HeaderToken.CsfrToken = session?.HeaderCsfrToken ?? "";
|
||||
Service.Client.AuthStorage.HeaderToken.ProcessUuid = session?.HeaderProcessId ?? "";
|
||||
|
||||
var status = await _apiService.Client.CheckQRStatusAsync();
|
||||
var status = await Api.Passport.CheckQRStatusAsync();
|
||||
|
||||
if (status?.State == "otp_auth_finished")
|
||||
{
|
||||
try
|
||||
{
|
||||
var auth = await _apiService.Client.AuthorizeByQR();
|
||||
var auth = await Api.Passport.AuthorizeByQRAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -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