Добавьте файлы проекта.

This commit is contained in:
FrigaT
2026-04-10 12:12:33 +03:00
parent 9615cf42ee
commit 11d0b0d72f
383 changed files with 9661 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
namespace YandexMusic.API.Common.Ynison
{
public class UpperSnakeCaseNamingStrategy : SnakeCaseNamingStrategy
{
protected override string ResolvePropertyName(string name) => base.ResolvePropertyName(name).ToUpper();
}
}

View File

@@ -0,0 +1,315 @@
using System.Net.WebSockets;
using YandexMusic.API.Models.Track;
using YandexMusic.API.Models.Ynison;
using YandexMusic.API.Models.Ynison.Messages;
namespace YandexMusic.API.Common.Ynison
{
public class YnisonPlayer : IDisposable
{
#region Поля
private readonly JsonSerializerSettings jsonSettings = new()
{
Converters = new List<JsonConverter> {
new StringEnumConverter(new UpperSnakeCaseNamingStrategy())
},
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new DefaultContractResolver
{
// Важно! Унисон отдаёт данные в SnakeCase
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
private AuthStorage storage;
private YnisonWebSocket redirector;
private YnisonWebSocket state;
#endregion Поля
#region Свойства
/// <summary>
/// API
/// </summary>
public YandexMusicApi API { get; internal set; }
/// <summary>
/// Состояние
/// </summary>
public YYnisonState State { get; internal set; }
/// <summary>
/// Текущий проигрываемый трек
/// </summary>
public YTrack Current => GetCurrent();
#endregion Свойства
#region События
public class ReceiveEventArgs
{
public YYnisonState State { get; internal set; }
}
public delegate void OnReceiveEventHandler(YnisonPlayer player, ReceiveEventArgs args);
/// <summary>
/// Получение данных
/// </summary>
public event OnReceiveEventHandler OnReceive;
public class CloseEventArgs
{
public WebSocketCloseStatus? Status { get; set; }
public string Description { get; set; }
}
public delegate void OnCloseEventHandler(YnisonPlayer player, CloseEventArgs args);
/// <summary>
/// Получение данных
/// </summary>
public event OnCloseEventHandler OnClose;
#endregion События
#region Вспомогательные функции
private string SerializeJson(object data)
{
return JsonConvert.SerializeObject(data, jsonSettings);
}
private T Deserialize<T>(YYnisonMessageType messageType, string data)
{
return storage.Debug != null
? storage.Debug.Deserialize<T>($"Ynison{messageType}", data, jsonSettings)
: JsonConvert.DeserializeObject<T>(data, jsonSettings);
}
private T DeserializeMessage<T>(YYnisonMessageType messageType, string data)
{
JObject o = JObject.Parse(data);
// Сообщение с ошибкой
if (o.ContainsKey("error"))
{
YYnisonErrorMessage exception = Deserialize<YYnisonErrorMessage>(YYnisonMessageType.Error, data);
throw exception ?? new Exception("Ошибка десериализации ответа с ошибкой.");
}
return Deserialize<T>(messageType, data);
}
private string DefaultState()
{
YYnisonVersion version = new()
{
DeviceId = storage.DeviceId,
Version = "0"
};
YYnisonUpdateFullStateMessage fullState = new()
{
UpdateFullState = new()
{
Device = new()
{
Capabilities = new()
{
CanBePlayer = true
},
Info = new()
{
DeviceId = storage.DeviceId,
AppName = "Yandex Music API",
AppVersion = "0.0.1",
Type = "WEB",
Title = "YandexMusicAPI"
},
IsShadow = true
},
PlayerState = new()
{
PlayerQueue = new()
{
Version = version
},
Status = new()
{
Version = version
}
}
}
};
return SerializeJson(fullState);
}
private YTrack GetCurrent()
{
if (State == null)
return null;
int index = State.PlayerState.PlayerQueue.CurrentPlayableIndex;
if (index < 0 || index > State.PlayerState.PlayerQueue.PlayableList.Count)
return null;
YYnisonPlayableItem item = State.PlayerState.PlayerQueue.PlayableList[index];
return API.Track.Get(storage, item.PlayableId)
.Result
.FirstOrDefault();
}
private void UpdateState()
{
YYnisonUpdatePlayerStateMessage update = new()
{
UpdatePlayerState = State.PlayerState
};
update.UpdatePlayerState.Status.Version = new()
{
DeviceId = storage.DeviceId
};
update.UpdatePlayerState.PlayerQueue.Version = new()
{
DeviceId = storage.DeviceId
};
try
{
state.Send(SerializeJson(update));
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
#endregion Вспомогательные функции
#region Подключение
public void Connect()
{
redirector.Connect(storage, "wss://ynison.music.yandex.ru/redirector.YnisonRedirectService/GetRedirectToYnison");
redirector.OnReceive += (socket, data) =>
{
YYnisonRedirect redirectInfo = Deserialize<YYnisonRedirect>(YYnisonMessageType.Redirect, data.Data);
if (state.IsConnected)
return;
state.Connect(storage, $"wss://{redirectInfo.Host}/ynison_state.YnisonStateService/PutYnisonState", redirectInfo.RedirectTicket);
state.OnReceive += (s, d) =>
{
YYnisonState message = DeserializeMessage<YYnisonState>(YYnisonMessageType.State, d.Data);
State = message;
OnReceive?.Invoke(this, new ReceiveEventArgs
{
State = State
});
};
state.OnClose += (s, args) =>
{
OnClose?.Invoke(this, new CloseEventArgs
{
Status = args.Status,
Description = args.Description
});
};
state.BeginReceive();
// Отправка изначального состояния
state.Send(DefaultState());
};
redirector.BeginReceive();
}
public void Disconnect()
{
state?.StopReceive();
redirector?.StopReceive();
}
#endregion Подключение
#region Плеер
/*
public void Play()
{
}
public void Stop()
{
}
public void Next()
{
List<YYnisonPlayableItem> list = State.PlayerState.PlayerQueue.PlayableList;
if (State.PlayerState.PlayerQueue.EntityType == YYnisonEntityType.Radio)
{
YYnisonPlayableItem next = State.PlayerState.PlayerQueue.Queue.WaveQueue.RecommendedPlayableList
.FirstOrDefault();
list.RemoveAt(0);
list.Add(next);
UpdateState();
}
if (State.PlayerState.PlayerQueue.CurrentPlayableIndex < list.Count - 1)
{
State.PlayerState.PlayerQueue.CurrentPlayableIndex++;
UpdateState();
}
}
public void Previous()
{
}
*/
#endregion Плеер
internal YnisonPlayer(YandexMusicApi api, AuthStorage authStorage)
{
API = api;
storage = authStorage;
redirector = new();
state = new();
}
#region IDisposable
public void Dispose()
{
redirector?.StopReceive();
redirector?.Dispose();
}
#endregion IDisposable
}
}

View File

@@ -0,0 +1,179 @@
using System.Net.WebSockets;
using System.Text;
namespace YandexMusic.API.Common.Ynison
{
public class YnisonWebSocket : IDisposable
{
#region Поля
private readonly JsonSerializerSettings jsonSettings = new()
{
Converters = new List<JsonConverter> {
new StringEnumConverter {
NamingStrategy = new CamelCaseNamingStrategy()
}
},
NullValueHandling = NullValueHandling.Ignore
};
private readonly ClientWebSocket socketClient = new();
private CancellationTokenSource cancellationTokenSource = new();
private CancellationToken cancellation;
private readonly StringBuilder data = new();
private readonly int size = 4096;
#endregion Поля
#region Свойства
public bool IsConnected => socketClient.State == WebSocketState.Open;
#endregion Свойства
#region События
public class ReceiveEventArgs
{
public string Data { get; internal set; }
}
public delegate void OnReceiveEventHandler(YnisonWebSocket socket, ReceiveEventArgs args);
/// <summary>
/// Получение данных
/// </summary>
public event OnReceiveEventHandler OnReceive;
public class CloseEventArgs
{
public WebSocketCloseStatus? Status { get; set; }
public string Description { get; set; }
}
public delegate void OnCloseEventHandler(YnisonWebSocket socket, CloseEventArgs args);
/// <summary>
/// Закрытие соединения
/// </summary>
public event OnCloseEventHandler OnClose;
#endregion События
#region Вспомогательные функции
private string SerializeJson(object obj)
{
return JsonConvert.SerializeObject(obj, jsonSettings);
}
private string GetProtocolData(string deviceId, string redirectTicket)
{
Dictionary<string, object> deviceInfo = new() {
{ "app_name", "Chrome" },
{ "type", 1 }
};
Dictionary<string, string> protocol = new() {
{ "Ynison-Device-Id", deviceId },
{ "Ynison-Device-Info", SerializeJson(deviceInfo) }
};
if (!string.IsNullOrEmpty(redirectTicket))
protocol.Add("Ynison-Redirect-Ticket", redirectTicket);
return SerializeJson(protocol);
}
private async Task<string> ReadSocketContent()
{
byte[] buffer = new byte[size];
WebSocketReceiveResult result;
do
{
result = await socketClient.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
data.Append(Encoding.UTF8.GetString(buffer, 0, result.Count));
} while (!result.EndOfMessage);
return data.ToString();
}
#endregion Вспомогательные функции
public bool Connect(AuthStorage storage, string url, string redirectTicket = null)
{
socketClient.Options.AddSubProtocol("Bearer");
socketClient.Options.SetRequestHeader("Sec-WebSocket-Protocol", $"Bearer, v2, {GetProtocolData(storage.DeviceId, redirectTicket)}");
socketClient.Options.SetRequestHeader("Origin", "https://music.yandex.ru");
socketClient.Options.SetRequestHeader("Authorization", $"OAuth {storage.Token}");
socketClient.Options.Proxy = storage.Context.WebProxy;
socketClient.ConnectAsync(new Uri(url), CancellationToken.None)
.GetAwaiter()
.GetResult();
cancellation = cancellationTokenSource.Token;
return socketClient.State == WebSocketState.Open;
}
public async Task BeginReceive()
{
if (socketClient.State != WebSocketState.Open)
return;
do
{
string content = await ReadSocketContent();
OnReceive?.Invoke(this, new ReceiveEventArgs
{
Data = content
});
data.Clear();
} while (!cancellation.IsCancellationRequested && socketClient.State == WebSocketState.Open);
OnClose?.Invoke(this, new CloseEventArgs
{
Status = socketClient.CloseStatus,
Description = socketClient.CloseStatusDescription
});
await socketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
public ValueTask Send(string json)
{
ReadOnlyMemory<byte> message = new(Encoding.UTF8.GetBytes(json));
return socketClient.SendAsync(message, WebSocketMessageType.Text, false, CancellationToken.None);
}
public Task StopReceive()
{
if (socketClient.State != WebSocketState.Open)
return Task.CompletedTask;
cancellationTokenSource.Cancel(false);
return Task.CompletedTask;
}
#region IDisposable
public void Dispose()
{
socketClient?.Dispose();
cancellationTokenSource?.Dispose();
}
#endregion IDisposable
}
}