Repull video with it's uri if it's a youtube song, every time a song is played, #1334
This commit is contained in:
		@@ -463,15 +463,15 @@ namespace NadekoBot.Modules.Music
 | 
			
		||||
        {
 | 
			
		||||
            var mp = await _music.GetOrCreatePlayer(Context);
 | 
			
		||||
 | 
			
		||||
            var songs = mp.QueueArray().Songs
 | 
			
		||||
                .Select(s => new PlaylistSong()
 | 
			
		||||
            var songs = await Task.WhenAll(mp.QueueArray().Songs
 | 
			
		||||
                .Select(async s => new PlaylistSong()
 | 
			
		||||
                {
 | 
			
		||||
                    Provider = s.Provider,
 | 
			
		||||
                    ProviderType = s.ProviderType,
 | 
			
		||||
                    Title = s.Title,
 | 
			
		||||
                    Uri = s.Uri,
 | 
			
		||||
                    Uri = await s.Uri(),
 | 
			
		||||
                    Query = s.Query,
 | 
			
		||||
                }).ToList();
 | 
			
		||||
                }).ToList());
 | 
			
		||||
 | 
			
		||||
            MusicPlaylist playlist;
 | 
			
		||||
            using (var uow = _db.UnitOfWork)
 | 
			
		||||
@@ -481,7 +481,7 @@ namespace NadekoBot.Modules.Music
 | 
			
		||||
                    Name = name,
 | 
			
		||||
                    Author = Context.User.Username,
 | 
			
		||||
                    AuthorId = Context.User.Id,
 | 
			
		||||
                    Songs = songs,
 | 
			
		||||
                    Songs = songs.ToList(),
 | 
			
		||||
                };
 | 
			
		||||
                uow.MusicPlaylists.Add(playlist);
 | 
			
		||||
                await uow.CompleteAsync().ConfigureAwait(false);
 | 
			
		||||
 
 | 
			
		||||
@@ -154,7 +154,7 @@ namespace NadekoBot.Services.Music
 | 
			
		||||
                         continue;
 | 
			
		||||
 | 
			
		||||
                     _log.Info("Starting");
 | 
			
		||||
                     using (var b = new SongBuffer(data.Song.Uri, ""))
 | 
			
		||||
                     using (var b = new SongBuffer(await data.Song.Uri(), ""))
 | 
			
		||||
                     {
 | 
			
		||||
                         AudioOutStream pcm = null;
 | 
			
		||||
                         try
 | 
			
		||||
 
 | 
			
		||||
@@ -234,23 +234,23 @@ namespace NadekoBot.Services.Music
 | 
			
		||||
            return await SongInfoFromSVideo(svideo, queuerName);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<SongInfo> SongInfoFromSVideo(SoundCloudVideo svideo, string queuerName) =>
 | 
			
		||||
            new SongInfo
 | 
			
		||||
        public Task<SongInfo> SongInfoFromSVideo(SoundCloudVideo svideo, string queuerName) =>
 | 
			
		||||
            Task.FromResult(new SongInfo
 | 
			
		||||
            {
 | 
			
		||||
                Title = svideo.FullName,
 | 
			
		||||
                Provider = "SoundCloud",
 | 
			
		||||
                Uri = await svideo.StreamLink().ConfigureAwait(false),
 | 
			
		||||
                Uri = () => svideo.StreamLink(),
 | 
			
		||||
                ProviderType = MusicType.Soundcloud,
 | 
			
		||||
                Query = svideo.TrackLink,
 | 
			
		||||
                AlbumArt = svideo.artwork_url,
 | 
			
		||||
                QueuerName = queuerName
 | 
			
		||||
            };
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
    public SongInfo ResolveLocalSong(string query, string queuerName)
 | 
			
		||||
        {
 | 
			
		||||
            return new SongInfo
 | 
			
		||||
            {
 | 
			
		||||
                Uri = "\"" + Path.GetFullPath(query) + "\"",
 | 
			
		||||
                Uri = () => Task.FromResult("\"" + Path.GetFullPath(query) + "\""),
 | 
			
		||||
                Title = Path.GetFileNameWithoutExtension(query),
 | 
			
		||||
                Provider = "Local File",
 | 
			
		||||
                ProviderType = MusicType.Local,
 | 
			
		||||
@@ -263,7 +263,7 @@ namespace NadekoBot.Services.Music
 | 
			
		||||
        {
 | 
			
		||||
            return new SongInfo
 | 
			
		||||
            {
 | 
			
		||||
                Uri = query,
 | 
			
		||||
                Uri = () => Task.FromResult(query),
 | 
			
		||||
                Title = query,
 | 
			
		||||
                Provider = "Radio Stream",
 | 
			
		||||
                ProviderType = MusicType.Radio,
 | 
			
		||||
@@ -282,18 +282,7 @@ namespace NadekoBot.Services.Music
 | 
			
		||||
 | 
			
		||||
        public async Task<SongInfo> ResolveYoutubeSong(string query, string queuerName)
 | 
			
		||||
        {
 | 
			
		||||
            var link = (await _google.GetVideoLinksByKeywordAsync(query).ConfigureAwait(false)).FirstOrDefault();
 | 
			
		||||
            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();
 | 
			
		||||
            var (link, video) = await GetYoutubeVideo(query);
 | 
			
		||||
 | 
			
		||||
            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"
 | 
			
		||||
                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,
 | 
			
		||||
                ProviderType = MusicType.YouTube,
 | 
			
		||||
                QueuerName = queuerName
 | 
			
		||||
@@ -317,6 +312,24 @@ namespace NadekoBot.Services.Music
 | 
			
		||||
            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) =>
 | 
			
		||||
            (query.StartsWith("http") ||
 | 
			
		||||
            query.StartsWith("ww"))
 | 
			
		||||
 
 | 
			
		||||
@@ -24,6 +24,7 @@ namespace NadekoBot.Services.Music
 | 
			
		||||
        public SongBuffer(string songUri, string skipTo)
 | 
			
		||||
        {
 | 
			
		||||
            _log = LogManager.GetCurrentClassLogger();
 | 
			
		||||
            _log.Warn(songUri);
 | 
			
		||||
            this.SongUri = songUri;
 | 
			
		||||
 | 
			
		||||
            this.p = StartFFmpegProcess(songUri, 0);
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ namespace NadekoBot.Services.Music
 | 
			
		||||
        public MusicType ProviderType { get; set; }
 | 
			
		||||
        public string Query { 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 QueuerName { get; set; }
 | 
			
		||||
        public TimeSpan TotalTime { get; set; } = TimeSpan.Zero;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user