97 lines
3.2 KiB
C#
97 lines
3.2 KiB
C#
using Microsoft.AspNetCore.DataProtection;
|
|
using PlaylistShared.Api.Entities;
|
|
using YandexMusic;
|
|
using YandexMusic.API.Extensions.API;
|
|
using YandexMusic.API.Models.Playlist;
|
|
|
|
namespace PlaylistShared.Api.Services;
|
|
|
|
public class YandexMusicService
|
|
{
|
|
private readonly IDataProtector _dataProtector;
|
|
|
|
public YandexMusicService(IDataProtectionProvider provider)
|
|
{
|
|
_dataProtector = provider.CreateProtector("YandexTokens");
|
|
}
|
|
|
|
private async Task<YandexMusicClient?> CreateClientAsync(ApplicationUser user)
|
|
{
|
|
if (string.IsNullOrEmpty(user.YandexAccessToken))
|
|
return null;
|
|
|
|
string decryptedToken;
|
|
try
|
|
{
|
|
decryptedToken = _dataProtector.Unprotect(user.YandexAccessToken);
|
|
}
|
|
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);
|
|
}
|
|
|
|
public async Task<YPlaylist?> CreatePlaylistAsync(ApplicationUser user, string title)
|
|
{
|
|
var client = await CreateClientAsync(user);
|
|
if (client == null) return null;
|
|
return await client.CreatePlaylistAsync(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;
|
|
// Получаем треки по ID
|
|
var tracks = await client.GetTracksAsync(trackIds);
|
|
if (tracks == null || !tracks.Any()) return null;
|
|
var playlist = await client.GetPlaylistAsync(ownerUid, kind);
|
|
if (playlist == null) return null;
|
|
return await playlist.InsertTracksAsync(tracks.ToArray());
|
|
}
|
|
|
|
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);
|
|
if (tracks == null || !tracks.Any()) return null;
|
|
var playlist = await client.GetPlaylistAsync(ownerUid, kind);
|
|
if (playlist == null) return null;
|
|
return await playlist.RemoveTracksAsync(tracks.ToArray());
|
|
}
|
|
|
|
public async Task<string?> GetTrackFileUrlAsync(ApplicationUser user, string trackId)
|
|
{
|
|
using var client = await CreateClientAsync(user);
|
|
if (client == null) return null;
|
|
var track = await client.GetTrackAsync(trackId);
|
|
if (track == null) return null;
|
|
return await track.GetLinkAsync();
|
|
}
|
|
|
|
public string EncryptToken(string token) => _dataProtector.Protect(token);
|
|
|
|
public string DecryptToken(string encryptedToken)
|
|
{
|
|
try
|
|
{
|
|
return _dataProtector.Unprotect(encryptedToken);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
} |