Доработаны сервисы: уменьшенно кол-во создаваемых объектов
This commit is contained in:
@@ -8,7 +8,6 @@ using PlaylistShared.Shared;
|
|||||||
using PlaylistShared.Shared.Enums;
|
using PlaylistShared.Shared.Enums;
|
||||||
using PlaylistShared.Shared.SharedPlaylist;
|
using PlaylistShared.Shared.SharedPlaylist;
|
||||||
using PlaylistShared.Shared.Yandex;
|
using PlaylistShared.Shared.Yandex;
|
||||||
using YandexMusic;
|
|
||||||
|
|
||||||
namespace PlaylistShared.Api.Controllers;
|
namespace PlaylistShared.Api.Controllers;
|
||||||
|
|
||||||
@@ -45,13 +44,12 @@ public class PlaylistsController : ControllerBase
|
|||||||
if (string.IsNullOrEmpty(decryptedToken))
|
if (string.IsNullOrEmpty(decryptedToken))
|
||||||
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 400, Message = "Токен Яндекс.Музыки не установлен или недействителен" }));
|
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 400, Message = "Токен Яндекс.Музыки не установлен или недействителен" }));
|
||||||
|
|
||||||
var yandexClient = new YandexMusicClient();
|
var authSuccess = await _yandexApiService.AuthAsync(decryptedToken);
|
||||||
var authSuccess = await yandexClient.Authorize(decryptedToken);
|
|
||||||
if (!authSuccess)
|
if (!authSuccess)
|
||||||
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 400, Message = "Не удалось авторизоваться в Яндекс.Музыке. Проверьте токен." }));
|
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 400, Message = "Не удалось авторизоваться в Яндекс.Музыке. Проверьте токен." }));
|
||||||
|
|
||||||
var favorites = await yandexClient.GetFavoritesAsync();
|
var favorites = await _yandexApiService.Client.Api.Playlist.FavoritesAsync();
|
||||||
var ownPlaylists = favorites.Where(p => p.Owner.Uid == yandexClient.Account.Uid).ToList();
|
var ownPlaylists = favorites.Where(p => p.Owner.Uid == _yandexApiService.Client.Account.Uid).ToList();
|
||||||
|
|
||||||
var sharedPlaylists = await _sharedService.GetAllByUserAsync(userId);
|
var sharedPlaylists = await _sharedService.GetAllByUserAsync(userId);
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="10.1.7" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="10.1.7" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" 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>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -44,7 +44,28 @@ public class YandexApiService : IDisposable
|
|||||||
if (decryptedToken == null)
|
if (decryptedToken == null)
|
||||||
return 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>
|
/// <summary>
|
||||||
@@ -100,14 +121,6 @@ public class YandexApiService : IDisposable
|
|||||||
return cookie?.Value;
|
return cookie?.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Авторизуется с помощью OAuth-токена.
|
|
||||||
/// </summary>
|
|
||||||
public async Task<bool> AuthorizeAsync(string token)
|
|
||||||
{
|
|
||||||
return await _client.Authorize(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_client.Dispose();
|
_client.Dispose();
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using PlaylistShared.Api.Entities;
|
|||||||
using PlaylistShared.Shared.Yandex;
|
using PlaylistShared.Shared.Yandex;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using YandexMusic.API;
|
||||||
|
|
||||||
namespace PlaylistShared.Api.Services;
|
namespace PlaylistShared.Api.Services;
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ public class YandexAuthService
|
|||||||
private readonly ApplicationDbContext _dbContext;
|
private readonly ApplicationDbContext _dbContext;
|
||||||
|
|
||||||
public YandexApiService Service => _apiService;
|
public YandexApiService Service => _apiService;
|
||||||
|
public YandexMusicApi Api => _apiService.Client.Api;
|
||||||
|
|
||||||
public YandexAuthService(YandexApiService apiService, ApplicationDbContext dbContext)
|
public YandexAuthService(YandexApiService apiService, ApplicationDbContext dbContext)
|
||||||
{
|
{
|
||||||
@@ -41,12 +43,11 @@ public class YandexAuthService
|
|||||||
|
|
||||||
internal async Task<YandexAuthQr> GenerateQrAsync(ApplicationUser user)
|
internal async Task<YandexAuthQr> GenerateQrAsync(ApplicationUser user)
|
||||||
{
|
{
|
||||||
var client = _apiService.Client;
|
var qr = await Api.Passport.GetAuthQRLinkAsync();
|
||||||
var qr = await client.GetAuthQRLink();
|
var trackId = Service.Client.AuthStorage.AuthToken.TrackId;
|
||||||
var trackId = client.AuthStorage.AuthToken.TrackId;
|
var csfrToken = Service.Client.AuthStorage.AuthToken.CsfrToken;
|
||||||
var csfrToken = client.AuthStorage.AuthToken.CsfrToken;
|
var headerProcessUuid = Service.Client.AuthStorage.HeaderToken.ProcessUuid;
|
||||||
var headerProcessUuid = client.AuthStorage.HeaderToken.ProcessUuid;
|
var headerCsfrToken = Service.Client.AuthStorage.HeaderToken.CsfrToken;
|
||||||
var headerCsfrToken = client.AuthStorage.HeaderToken.CsfrToken;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(qr))
|
if (string.IsNullOrEmpty(qr))
|
||||||
throw new Exception("Не удалось получить QR-ссылку");
|
throw new Exception("Не удалось получить QR-ссылку");
|
||||||
@@ -82,24 +83,24 @@ public class YandexAuthService
|
|||||||
var session = await _dbContext.YandexAuthSessions.FindAsync(sessionId);
|
var session = await _dbContext.YandexAuthSessions.FindAsync(sessionId);
|
||||||
if (session == null) return null;
|
if (session == null) return null;
|
||||||
|
|
||||||
RestoreCookies(_apiService.CookieContainer, session.SerializedCookies);
|
RestoreCookies(Service.CookieContainer, session.SerializedCookies);
|
||||||
if (_apiService.Client.AuthStorage.AuthToken is null)
|
if (Service.Client.AuthStorage.AuthToken is null)
|
||||||
{
|
{
|
||||||
_apiService.Client.AuthStorage.AuthToken = new();
|
Service.Client.AuthStorage.AuthToken = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
_apiService.Client.AuthStorage.AuthToken.CsfrToken = session?.CsfrToken ?? "";
|
Service.Client.AuthStorage.AuthToken.CsfrToken = session?.CsfrToken ?? "";
|
||||||
_apiService.Client.AuthStorage.AuthToken.TrackId = session?.TrackId ?? "";
|
Service.Client.AuthStorage.AuthToken.TrackId = session?.TrackId ?? "";
|
||||||
_apiService.Client.AuthStorage.HeaderToken.CsfrToken = session?.HeaderCsfrToken ?? "";
|
Service.Client.AuthStorage.HeaderToken.CsfrToken = session?.HeaderCsfrToken ?? "";
|
||||||
_apiService.Client.AuthStorage.HeaderToken.ProcessUuid = session?.HeaderProcessId ?? "";
|
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")
|
if (status?.State == "otp_auth_finished")
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var auth = await _apiService.Client.AuthorizeByQR();
|
var auth = await Api.Passport.AuthorizeByQRAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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.Api.Extensions;
|
||||||
using PlaylistShared.Shared.Enums;
|
using PlaylistShared.Shared.Enums;
|
||||||
using PlaylistShared.Shared.Yandex;
|
using PlaylistShared.Shared.Yandex;
|
||||||
using YandexMusic;
|
using YandexMusic.API;
|
||||||
using YandexMusic.API.Extensions.API;
|
|
||||||
using YandexMusic.API.Models.Playlist;
|
using YandexMusic.API.Models.Playlist;
|
||||||
using YandexMusic.API.Models.Track;
|
using YandexMusic.API.Models.Track;
|
||||||
|
|
||||||
@@ -12,56 +10,45 @@ namespace PlaylistShared.Api.Services;
|
|||||||
|
|
||||||
public class YandexMusicService
|
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))
|
if (!IsAuthorized)
|
||||||
return null;
|
|
||||||
|
|
||||||
string decryptedToken;
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
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)
|
public async Task<YPlaylist?> GetPlaylistAsync(ApplicationUser user, string ownerUid, string kind)
|
||||||
{
|
{
|
||||||
var client = await CreateClientAsync(user);
|
await AuthorizeIfNot(user);
|
||||||
if (client == null) return null;
|
return await Api.Playlist.GetAsync(ownerUid, kind);
|
||||||
return await client.GetPlaylistAsync(ownerUid, kind);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<YPlaylist?> CreatePlaylistAsync(ApplicationUser user, string title)
|
public async Task<YPlaylist?> CreatePlaylistAsync(ApplicationUser user, string title)
|
||||||
{
|
{
|
||||||
var client = await CreateClientAsync(user);
|
await AuthorizeIfNot(user);
|
||||||
if (client == null) return null;
|
return await Api.Playlist.CreateAsync(title);
|
||||||
return await client.CreatePlaylistAsync(title);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<YPlaylist?> AddTracksAsync(ApplicationUser user, string ownerUid, string kind, IEnumerable<string> trackIds)
|
public async Task<YPlaylist?> AddTracksAsync(ApplicationUser user, string ownerUid, string kind, IEnumerable<string> trackIds)
|
||||||
{
|
{
|
||||||
var client = await CreateClientAsync(user);
|
await AuthorizeIfNot(user);
|
||||||
if (client == null) return null;
|
|
||||||
|
|
||||||
var playlist = await client.GetPlaylistAsync(ownerUid, kind);
|
var playlist = await Api.Playlist.GetAsync(ownerUid, kind);
|
||||||
if (playlist == null) return null;
|
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;
|
if (tracks == null || !tracks.Any()) return null;
|
||||||
|
|
||||||
var insertedTracks = tracks.Where(t => !playlist.Tracks.Any(p => p.Track.Id == t.Id)).ToArray();
|
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)
|
public async Task<YPlaylist?> RemoveTracksAsync(ApplicationUser user, string ownerUid, string kind, IEnumerable<string> trackIds)
|
||||||
{
|
{
|
||||||
var client = await CreateClientAsync(user);
|
await AuthorizeIfNot(user);
|
||||||
if (client == null) return null;
|
|
||||||
var tracks = await client.GetTracksAsync(trackIds);
|
var tracks = await Api.Track.GetAsync(trackIds);
|
||||||
if (tracks == null || !tracks.Any()) return null;
|
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;
|
if (playlist == null) return null;
|
||||||
|
|
||||||
return await playlist.RemoveTracksAsync(tracks.ToArray());
|
return await playlist.RemoveTracksAsync(tracks.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string?> GetTrackFileUrlAsync(ApplicationUser user, string trackId)
|
public async Task<string?> GetTrackFileUrlAsync(ApplicationUser user, string trackId)
|
||||||
{
|
{
|
||||||
|
await AuthorizeIfNot(user);
|
||||||
|
|
||||||
var track = await GetYTrackAsync(user, trackId);
|
var track = await GetYTrackAsync(user, trackId);
|
||||||
if (track == null) return null;
|
if (track == null) return null;
|
||||||
|
|
||||||
return await track.GetLinkAsync();
|
return await track.GetLinkAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<YTrack?> GetYTrackAsync(ApplicationUser user, string trackId)
|
public async Task<YTrack?> GetYTrackAsync(ApplicationUser user, string trackId)
|
||||||
{
|
{
|
||||||
using var client = await CreateClientAsync(user);
|
await AuthorizeIfNot(user);
|
||||||
if (client == null) return null;
|
|
||||||
var track = await client.GetTrackAsync(trackId);
|
var track = await Api.Track.GetAsync(trackId);
|
||||||
return track;
|
return track;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,8 +94,7 @@ public class YandexMusicService
|
|||||||
int limit = 20
|
int limit = 20
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var client = await CreateClientAsync(user);
|
await AuthorizeIfNot(user);
|
||||||
if (client == null) return new YandexSearchResult();
|
|
||||||
|
|
||||||
var ySerchType = searchType switch
|
var ySerchType = searchType switch
|
||||||
{
|
{
|
||||||
@@ -114,7 +105,7 @@ public class YandexMusicService
|
|||||||
_ => YandexMusic.API.Models.Common.YSearchType.All
|
_ => 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();
|
if (searchResult?.Tracks?.Results == null) return new YandexSearchResult();
|
||||||
|
|
||||||
return new YandexSearchResult
|
return new YandexSearchResult
|
||||||
@@ -179,8 +170,7 @@ public class YandexMusicService
|
|||||||
{
|
{
|
||||||
YandexSearchResult result = new();
|
YandexSearchResult result = new();
|
||||||
|
|
||||||
var client = await CreateClientAsync(user);
|
await AuthorizeIfNot(user);
|
||||||
if (client == null) return result;
|
|
||||||
|
|
||||||
if (searchType == TrackSearchType.All)
|
if (searchType == TrackSearchType.All)
|
||||||
{
|
{
|
||||||
@@ -189,7 +179,7 @@ public class YandexMusicService
|
|||||||
|
|
||||||
else if (searchType == TrackSearchType.Track)
|
else if (searchType == TrackSearchType.Track)
|
||||||
{
|
{
|
||||||
var track = await client.GetTrackAsync(id);
|
var track = await Api.Track.GetAsync(id);
|
||||||
|
|
||||||
if (track != null)
|
if (track != null)
|
||||||
{
|
{
|
||||||
@@ -215,7 +205,7 @@ public class YandexMusicService
|
|||||||
|
|
||||||
else if (searchType == TrackSearchType.Album)
|
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
|
result.Tracks = album?.Volumes.SelectMany(v => v).Select(t => new YandexTrack
|
||||||
{
|
{
|
||||||
@@ -235,7 +225,7 @@ public class YandexMusicService
|
|||||||
|
|
||||||
else if (searchType == TrackSearchType.Artist)
|
else if (searchType == TrackSearchType.Artist)
|
||||||
{
|
{
|
||||||
var artist = await client.GetArtistAsync(id);
|
var artist = await Api.Artist.GetAsync(id);
|
||||||
if (artist != null)
|
if (artist != null)
|
||||||
{
|
{
|
||||||
result.Albums = artist.Albums.Select(a => new YandexAlbum()
|
result.Albums = artist.Albums.Select(a => new YandexAlbum()
|
||||||
@@ -284,7 +274,7 @@ public class YandexMusicService
|
|||||||
|
|
||||||
else if (searchType == TrackSearchType.Playlist)
|
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
|
result.Tracks = playlist?.Tracks.Select(p => new YandexTrack
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user