Repull video with it's uri if it's a youtube song, every time a song is played, #1334

This commit is contained in:
Master Kwoth 2017-07-05 16:59:59 +02:00
parent ab07199a1e
commit 78be4598cf
5 changed files with 40 additions and 26 deletions

View File

@ -463,15 +463,15 @@ namespace NadekoBot.Modules.Music
{ {
var mp = await _music.GetOrCreatePlayer(Context); var mp = await _music.GetOrCreatePlayer(Context);
var songs = mp.QueueArray().Songs var songs = await Task.WhenAll(mp.QueueArray().Songs
.Select(s => new PlaylistSong() .Select(async s => new PlaylistSong()
{ {
Provider = s.Provider, Provider = s.Provider,
ProviderType = s.ProviderType, ProviderType = s.ProviderType,
Title = s.Title, Title = s.Title,
Uri = s.Uri, Uri = await s.Uri(),
Query = s.Query, Query = s.Query,
}).ToList(); }).ToList());
MusicPlaylist playlist; MusicPlaylist playlist;
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
@ -481,7 +481,7 @@ namespace NadekoBot.Modules.Music
Name = name, Name = name,
Author = Context.User.Username, Author = Context.User.Username,
AuthorId = Context.User.Id, AuthorId = Context.User.Id,
Songs = songs, Songs = songs.ToList(),
}; };
uow.MusicPlaylists.Add(playlist); uow.MusicPlaylists.Add(playlist);
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);

View File

@ -154,7 +154,7 @@ namespace NadekoBot.Services.Music
continue; continue;
_log.Info("Starting"); _log.Info("Starting");
using (var b = new SongBuffer(data.Song.Uri, "")) using (var b = new SongBuffer(await data.Song.Uri(), ""))
{ {
AudioOutStream pcm = null; AudioOutStream pcm = null;
try try

View File

@ -234,23 +234,23 @@ namespace NadekoBot.Services.Music
return await SongInfoFromSVideo(svideo, queuerName); return await SongInfoFromSVideo(svideo, queuerName);
} }
public async Task<SongInfo> SongInfoFromSVideo(SoundCloudVideo svideo, string queuerName) => public Task<SongInfo> SongInfoFromSVideo(SoundCloudVideo svideo, string queuerName) =>
new SongInfo Task.FromResult(new SongInfo
{ {
Title = svideo.FullName, Title = svideo.FullName,
Provider = "SoundCloud", Provider = "SoundCloud",
Uri = await svideo.StreamLink().ConfigureAwait(false), Uri = () => svideo.StreamLink(),
ProviderType = MusicType.Soundcloud, ProviderType = MusicType.Soundcloud,
Query = svideo.TrackLink, Query = svideo.TrackLink,
AlbumArt = svideo.artwork_url, AlbumArt = svideo.artwork_url,
QueuerName = queuerName QueuerName = queuerName
}; });
public SongInfo ResolveLocalSong(string query, string queuerName) public SongInfo ResolveLocalSong(string query, string queuerName)
{ {
return new SongInfo return new SongInfo
{ {
Uri = "\"" + Path.GetFullPath(query) + "\"", Uri = () => Task.FromResult("\"" + Path.GetFullPath(query) + "\""),
Title = Path.GetFileNameWithoutExtension(query), Title = Path.GetFileNameWithoutExtension(query),
Provider = "Local File", Provider = "Local File",
ProviderType = MusicType.Local, ProviderType = MusicType.Local,
@ -263,7 +263,7 @@ namespace NadekoBot.Services.Music
{ {
return new SongInfo return new SongInfo
{ {
Uri = query, Uri = () => Task.FromResult(query),
Title = query, Title = query,
Provider = "Radio Stream", Provider = "Radio Stream",
ProviderType = MusicType.Radio, ProviderType = MusicType.Radio,
@ -282,18 +282,7 @@ namespace NadekoBot.Services.Music
public async Task<SongInfo> ResolveYoutubeSong(string query, string queuerName) public async Task<SongInfo> ResolveYoutubeSong(string query, string queuerName)
{ {
var link = (await _google.GetVideoLinksByKeywordAsync(query).ConfigureAwait(false)).FirstOrDefault(); var (link, video) = await GetYoutubeVideo(query);
if (string.IsNullOrWhiteSpace(link))
{
_log.Info("No song found.");
return null;
}
var allVideos = await Task.Run(async () => { try { return await YouTube.Default.GetAllVideosAsync(link).ConfigureAwait(false); } catch { return Enumerable.Empty<YouTubeVideo>(); } }).ConfigureAwait(false);
var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio);
var video = videos
.Where(v => v.AudioBitrate < 256)
.OrderByDescending(v => v.AudioBitrate)
.FirstOrDefault();
if (video == null) // do something with this error if (video == null) // do something with this error
{ {
@ -309,7 +298,13 @@ namespace NadekoBot.Services.Music
{ {
Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube" Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube"
Provider = "YouTube", Provider = "YouTube",
Uri = await video.GetUriAsync().ConfigureAwait(false), Uri = async () => {
var vid = await GetYoutubeVideo(query);
if (vid.Item2 == null)
throw new HttpRequestException();
return await vid.Item2.GetUriAsync();
},
Query = link, Query = link,
ProviderType = MusicType.YouTube, ProviderType = MusicType.YouTube,
QueuerName = queuerName QueuerName = queuerName
@ -317,6 +312,24 @@ namespace NadekoBot.Services.Music
return song; return song;
} }
private async Task<(string, YouTubeVideo)> GetYoutubeVideo(string query)
{
var link = (await _google.GetVideoLinksByKeywordAsync(query).ConfigureAwait(false)).FirstOrDefault();
if (string.IsNullOrWhiteSpace(link))
{
_log.Info("No song found.");
return (null, null);
}
var allVideos = await Task.Run(async () => { try { return await YouTube.Default.GetAllVideosAsync(link).ConfigureAwait(false); } catch { return Enumerable.Empty<YouTubeVideo>(); } }).ConfigureAwait(false);
var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio);
var video = videos
.Where(v => v.AudioBitrate < 256)
.OrderByDescending(v => v.AudioBitrate)
.FirstOrDefault();
return (link, video);
}
private bool IsRadioLink(string query) => private bool IsRadioLink(string query) =>
(query.StartsWith("http") || (query.StartsWith("http") ||
query.StartsWith("ww")) query.StartsWith("ww"))

View File

@ -24,6 +24,7 @@ namespace NadekoBot.Services.Music
public SongBuffer(string songUri, string skipTo) public SongBuffer(string songUri, string skipTo)
{ {
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
_log.Warn(songUri);
this.SongUri = songUri; this.SongUri = songUri;
this.p = StartFFmpegProcess(songUri, 0); this.p = StartFFmpegProcess(songUri, 0);

View File

@ -14,7 +14,7 @@ namespace NadekoBot.Services.Music
public MusicType ProviderType { get; set; } public MusicType ProviderType { get; set; }
public string Query { get; set; } public string Query { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Uri { get; set; } public Func<Task<string>> Uri { get; set; }
public string AlbumArt { get; set; } public string AlbumArt { get; set; }
public string QueuerName { get; set; } public string QueuerName { get; set; }
public TimeSpan TotalTime { get; set; } = TimeSpan.Zero; public TimeSpan TotalTime { get; set; } = TimeSpan.Zero;