add /api/player/play endpoint
fix not being able to play track with a playlist reference ``&list=`` attached
This commit is contained in:
parent
05d8af4d0e
commit
ed33e5d76d
@ -1,6 +1,7 @@
|
||||
using DSharpPlus.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TomatenMusic;
|
||||
using TomatenMusic.Music;
|
||||
using TomatenMusic_Api;
|
||||
using TomatenMusic_Api.Auth.Helpers;
|
||||
using TomatenMusic_Api.Models;
|
||||
@ -112,4 +113,34 @@ public class PlayerController : ControllerBase
|
||||
return Ok();
|
||||
|
||||
}
|
||||
|
||||
[HttpPost("play")]
|
||||
public async Task<IActionResult> PostPlay(TrackPlayRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _tomatenMusicDataService.GetGuildAsync(request.GuildId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return NotFound("That Guild was not found");
|
||||
}
|
||||
|
||||
if (!await _tomatenMusicDataService.IsConnectedAsync(request.GuildId) == true)
|
||||
return BadRequest("The Bot is not connected.");
|
||||
|
||||
MusicActionResponse response;
|
||||
|
||||
try
|
||||
{
|
||||
response = await _tomatenMusicDataService.TrackProvider.SearchAsync(request.TrackUri);
|
||||
}catch (Exception ex)
|
||||
{
|
||||
return NotFound(ex.Message + "\n" + ex.StackTrace);
|
||||
}
|
||||
|
||||
_eventBus.OnPlayRequestEvent(new TrackPlayArgs(response, request.GuildId, TimeSpan.FromSeconds(request.StartTimeSeconds), request.Now));
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
22
TomatenMusic/Models/EventArgs/TrackPlayArgs.cs
Normal file
22
TomatenMusic/Models/EventArgs/TrackPlayArgs.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Emzi0767.Utilities;
|
||||
using Lavalink4NET.Player;
|
||||
using TomatenMusic.Music;
|
||||
|
||||
namespace TomatenMusic_Api.Models.EventArgs
|
||||
{
|
||||
public class TrackPlayArgs : AsyncEventArgs
|
||||
{
|
||||
public MusicActionResponse Response { get; set; }
|
||||
public ulong GuildId { get; set; }
|
||||
public TimeSpan StartTime { get; set; }
|
||||
public bool Now { get; set; }
|
||||
|
||||
public TrackPlayArgs(MusicActionResponse response, ulong guildId, TimeSpan startTime, bool now)
|
||||
{
|
||||
Response = response;
|
||||
GuildId = guildId;
|
||||
StartTime = startTime;
|
||||
Now = now;
|
||||
}
|
||||
}
|
||||
}
|
10
TomatenMusic/Models/TrackPlayRequest.cs
Normal file
10
TomatenMusic/Models/TrackPlayRequest.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace TomatenMusic_Api.Models
|
||||
{
|
||||
public class TrackPlayRequest
|
||||
{
|
||||
public ulong GuildId { get; set; }
|
||||
public string TrackUri { get; set; }
|
||||
public bool Now { get; set; }
|
||||
public int StartTimeSeconds { get; set; }
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ public class InProcessEventBus
|
||||
|
||||
public event AsyncEventHandler<InProcessEventBus, ChannelDisconnectArgs>? OnDisconnectRequest;
|
||||
|
||||
|
||||
public event AsyncEventHandler<InProcessEventBus, TrackPlayArgs> OnPlayRequest;
|
||||
public void OnConnectRequestEvent(ChannelConnectArgs e)
|
||||
{
|
||||
_ = OnConnectRequest?.Invoke(this, e);
|
||||
@ -22,5 +22,10 @@ public class InProcessEventBus
|
||||
{
|
||||
_ = OnDisconnectRequest?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public void OnPlayRequestEvent(TrackPlayArgs e)
|
||||
{
|
||||
_ = OnPlayRequest?.Invoke(this, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,12 +11,14 @@ namespace TomatenMusic_Api
|
||||
public class TomatenMusicDataService : IHostedService
|
||||
{
|
||||
private ILogger<TomatenMusicDataService> _logger;
|
||||
public IServiceProvider _serviceProvider { get; set; } = TomatenMusicBot.ServiceProvider;
|
||||
private IServiceProvider _serviceProvider { get; set; } = TomatenMusicBot.ServiceProvider;
|
||||
public IAudioService _audioService { get; set; }
|
||||
public TrackProvider TrackProvider { get; set; }
|
||||
public TomatenMusicDataService(ILogger<TomatenMusicDataService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_audioService = _serviceProvider.GetRequiredService<IAudioService>();
|
||||
TrackProvider = _serviceProvider.GetRequiredService<TrackProvider>();
|
||||
}
|
||||
|
||||
public async Task<PlayerConnectionInfo> GetConnectionInfoAsync(ulong guild_id)
|
||||
|
@ -26,9 +26,40 @@ namespace TomatenMusic_Api
|
||||
{
|
||||
_inProcessEventBus.OnConnectRequest += _inProcessEventBus_OnConnectRequest;
|
||||
_inProcessEventBus.OnDisconnectRequest += _inProcessEventBus_OnDisconnectRequest;
|
||||
_inProcessEventBus.OnPlayRequest += _inProcessEventBus_OnPlayRequest;
|
||||
}
|
||||
|
||||
private async Task _inProcessEventBus_OnDisconnectRequest(InProcessEventBus sender, ChannelDisconnectArgs e)
|
||||
private async Task _inProcessEventBus_OnPlayRequest(InProcessEventBus sender, TrackPlayArgs e)
|
||||
{
|
||||
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(e.GuildId);
|
||||
|
||||
if (e.Response.Tracks != null && e.Response.Tracks.Any())
|
||||
{
|
||||
if (e.Now)
|
||||
await player.PlayTracksNowAsync(e.Response.Tracks);
|
||||
else
|
||||
await player.PlayTracksAsync(e.Response.Tracks);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Response.IsPlaylist)
|
||||
{
|
||||
if (e.Now)
|
||||
await player.PlayPlaylistNowAsync(e.Response.Playlist);
|
||||
else
|
||||
await player.PlayPlaylistAsync(e.Response.Playlist);
|
||||
}else
|
||||
{
|
||||
if (e.Now)
|
||||
await player.PlayNowAsync(e.Response.Track, e.StartTime);
|
||||
else
|
||||
await player.PlayAsync(e.Response.Track, e.StartTime);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async Task _inProcessEventBus_OnDisconnectRequest(InProcessEventBus sender, ChannelDisconnectArgs e)
|
||||
{
|
||||
GuildPlayer player = _audioService.GetPlayer<GuildPlayer>(e.GuildId);
|
||||
player.DisconnectAsync();
|
||||
|
@ -79,7 +79,7 @@ namespace TomatenMusic.Commands
|
||||
|
||||
try
|
||||
{
|
||||
if (response.isPlaylist)
|
||||
if (response.IsPlaylist)
|
||||
{
|
||||
ILavalinkPlaylist playlist = response.Playlist;
|
||||
await player.PlayPlaylistNowAsync(playlist);
|
||||
@ -232,7 +232,7 @@ namespace TomatenMusic.Commands
|
||||
|
||||
try
|
||||
{
|
||||
if (response.isPlaylist)
|
||||
if (response.IsPlaylist)
|
||||
{
|
||||
ILavalinkPlaylist playlist = response.Playlist;
|
||||
await player.PlayPlaylistAsync(playlist);
|
||||
|
@ -28,12 +28,12 @@ namespace TomatenMusic.Music.Entitites
|
||||
public Playlist YoutubeItem { get; set; }
|
||||
public Uri AuthorThumbnail { get; set; }
|
||||
|
||||
public YoutubePlaylist(string name, IEnumerable<LavalinkTrack> tracks, Uri uri)
|
||||
public YoutubePlaylist(string name, IEnumerable<LavalinkTrack> tracks, string id)
|
||||
{
|
||||
Identifier = uri.ToString().Replace("https://www.youtube.com/playlist?list=", "").Replace("https://youtube.com/playlist?list=", "");
|
||||
Identifier = id;
|
||||
Name = name;
|
||||
Tracks = tracks;
|
||||
Url = uri;
|
||||
Url = new Uri($"https://youtube.com/playlist?list={id}");
|
||||
TrackCount = tracks.Count();
|
||||
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ namespace TomatenMusic.Music
|
||||
QueuePrompt.UpdateFor(GuildId);
|
||||
}
|
||||
|
||||
public async Task PlayTracksAsync(List<LavalinkTrack> tracks)
|
||||
public async Task PlayTracksAsync(IEnumerable<LavalinkTrack> tracks)
|
||||
{
|
||||
EnsureNotDestroyed();
|
||||
EnsureConnected();
|
||||
|
@ -11,12 +11,12 @@ namespace TomatenMusic.Music
|
||||
public ILavalinkPlaylist Playlist { get; }
|
||||
public LavalinkTrack Track { get; }
|
||||
public IEnumerable<LavalinkTrack> Tracks { get; }
|
||||
public bool isPlaylist { get; }
|
||||
public bool IsPlaylist { get; }
|
||||
public MusicActionResponse(LavalinkTrack track = null, ILavalinkPlaylist playlist = null, IEnumerable<LavalinkTrack> tracks = null)
|
||||
{
|
||||
Playlist = playlist;
|
||||
Track = track;
|
||||
isPlaylist = playlist != null;
|
||||
IsPlaylist = playlist != null;
|
||||
Tracks = tracks;
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ namespace TomatenMusic.Music
|
||||
|
||||
}
|
||||
|
||||
public Task QueueTracksAsync(List<LavalinkTrack> tracks)
|
||||
public Task QueueTracksAsync(IEnumerable<LavalinkTrack> tracks)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
|
@ -5,6 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using TomatenMusic.Music.Entitites;
|
||||
using TomatenMusic.Services;
|
||||
|
||||
@ -57,7 +58,8 @@ namespace TomatenMusic.Music
|
||||
|
||||
if (loadResult.LoadType == TrackLoadType.PlaylistLoaded && !isSearch)
|
||||
return new MusicActionResponse(
|
||||
playlist: await _youtubeService.PopulatePlaylistAsync(new YoutubePlaylist(loadResult.PlaylistInfo.Name, await FullTrackContext.PopulateTracksAsync(loadResult.Tracks), uri)));
|
||||
playlist: await _youtubeService.PopulatePlaylistAsync(
|
||||
new YoutubePlaylist(loadResult.PlaylistInfo.Name, await FullTrackContext.PopulateTracksAsync(loadResult.Tracks), ParseListId(query))));
|
||||
else
|
||||
return new MusicActionResponse(await FullTrackContext.PopulateAsync(loadResult.Tracks.First()));
|
||||
|
||||
@ -79,5 +81,27 @@ namespace TomatenMusic.Music
|
||||
|
||||
}
|
||||
|
||||
public string ParseListId(string url)
|
||||
{
|
||||
var uri = new Uri(url, UriKind.Absolute);
|
||||
|
||||
// you can check host here => uri.Host <= "www.youtube.com"
|
||||
|
||||
var query = HttpUtility.ParseQueryString(uri.Query);
|
||||
|
||||
var videoId = string.Empty;
|
||||
|
||||
if (query.AllKeys.Contains("list"))
|
||||
{
|
||||
videoId = query["list"];
|
||||
}
|
||||
else
|
||||
{
|
||||
videoId = uri.Segments.Last();
|
||||
}
|
||||
|
||||
return videoId;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
@ -11,17 +11,17 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DSharpPlus" Version="4.2.0-nightly-01088" />
|
||||
<PackageReference Include="DSharpPlus.Interactivity" Version="4.2.0-nightly-01084" />
|
||||
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.2.0-nightly-01088" />
|
||||
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.56.0.2617" />
|
||||
<PackageReference Include="DSharpPlus" Version="4.2.0-nightly-01101" />
|
||||
<PackageReference Include="DSharpPlus.Interactivity" Version="4.2.0-nightly-01101" />
|
||||
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.2.0-nightly-01101" />
|
||||
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.56.0.2630" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.42" />
|
||||
<PackageReference Include="Lavalink4NET" Version="2.1.1" />
|
||||
<PackageReference Include="Lavalink4NET.DSharpPlus" Version="2.1.1" />
|
||||
<PackageReference Include="Lavalink4NET.Logging.Microsoft" Version="2.1.1-preview.5" />
|
||||
<PackageReference Include="Lavalink4NET.Logging.Microsoft" Version="2.1.1-preview.6" />
|
||||
<PackageReference Include="Lavalink4NET.MemoryCache" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.7" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0-preview.2.22152.2" />
|
||||
<PackageReference Include="SpotifyAPI.Web" Version="6.2.2" />
|
||||
<PackageReference Include="SpotifyAPI.Web.Auth" Version="6.2.2" />
|
||||
</ItemGroup>
|
||||
|
Loading…
x
Reference in New Issue
Block a user