<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.Audio;
using NadekoBot.Modules;
using System.IO;
using System.Diagnostics;
using NadekoBot.Extensions;
using System.Threading;
using Timer = System.Timers.Timer;
using YoutubeExtractor;

namespace NadekoBot.Classes.Music {
    public enum StreamState {
        Resolving,
        Queued,
        Buffering, //not using it atm
        Playing,
        Completed
    }

    public class StreamRequest {
        public Channel Channel { get; }
        public Server Server { get; }
        public User User { get; }
        public string Query { get; }

        public string Title { get; internal set; } = String.Empty;
        public IAudioClient VoiceClient { get; private set; }

        private MusicStreamer musicStreamer = null;
        public StreamState State => musicStreamer?.State ?? privateState;
        private StreamState privateState = StreamState.Resolving;

        public bool IsPaused => MusicControls.IsPaused;

        private MusicControls MusicControls;

        public StreamRequest(CommandEventArgs e, string query, MusicControls mc) {
            if (e == null)
                throw new ArgumentNullException(nameof(e));
            if (query == null)
                throw new ArgumentNullException(nameof(query));
            if (mc.VoiceClient == null)
                throw new NullReferenceException($"{nameof(mc.VoiceClient)} is null, bot didn't join any server.");
            this.MusicControls = mc;
            this.VoiceClient = mc.VoiceClient;
            this.Server = e.Server;
            this.Query = query;
            Task.Run(async () => await ResolveStreamLink());
        }

        private async Task ResolveStreamLink() {
            VideoInfo video = null;
            try {
                if (OnResolving != null)
                    OnResolving();
                Console.WriteLine("Resolving video link");

                video = DownloadUrlResolver.GetDownloadUrls(Searches.FindYoutubeUrlByKeywords(Query))
                        .Where(v => v.AdaptiveType == AdaptiveType.Audio)
                        .OrderByDescending(v => v.AudioBitrate)
                        .FirstOrDefault();

                if (video == null) // do something with this error
                    throw new Exception("Could not load any video elements based on the query.");

                Title = video.Title.Substring(0,video.Title.Length-10); // removing trailing "- You Tube"
            } catch (Exception ex) {
                privateState = StreamState.Completed;
                if (OnResolvingFailed != null)
                    OnResolvingFailed(ex.Message);
                Console.WriteLine($"Failed resolving the link.{ex.Message}");
                return;
            }

            musicStreamer = new MusicStreamer(this, video.DownloadUrl, Channel);
            if (OnQueued != null)
                OnQueued();
            return;
        }

        internal string PrintStats() => musicStreamer?.Stats();

        public Action OnQueued = null;
        public Action OnBuffering = null;
        public Action OnStarted = null;
        public Action OnCompleted = null;<commit_msg>song title string fixed, sry joshu it was my fault >.><commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.Audio;
using NadekoBot.Modules;
using System.IO;
using System.Diagnostics;
using NadekoBot.Extensions;
using System.Threading;
using Timer = System.Timers.Timer;
using YoutubeExtractor;

namespace NadekoBot.Classes.Music {
    public enum StreamState {
        Resolving,
        Queued,
        Buffering, //not using it atm
        Playing,
        Completed
    }

    public class StreamRequest {
        public Channel Channel { get; }
        public Server Server { get; }
        public User User { get; }
        public string Query { get; }

        public string Title { get; internal set; } = String.Empty;
        public IAudioClient VoiceClient { get; private set; }

        private MusicStreamer musicStreamer = null;
        public StreamState State => musicStreamer?.State ?? privateState;
        private StreamState privateState = StreamState.Resolving;

        public bool IsPaused => MusicControls.IsPaused;

        private MusicControls MusicControls;

        public StreamRequest(CommandEventArgs e, string query, MusicControls mc) {
            if (e == null)
                throw new ArgumentNullException(nameof(e));
            if (query == null)
                throw new ArgumentNullException(nameof(query));
            if (mc.VoiceClient == null)
                throw new NullReferenceException($"{nameof(mc.VoiceClient)} is null, bot didn't join any server.");
            this.MusicControls = mc;
            this.VoiceClient = mc.VoiceClient;
            this.Server = e.Server;
            this.Query = query;
            Task.Run(async () => await ResolveStreamLink());
        }

        private async Task ResolveStreamLink() {
            VideoInfo video = null;
            try {
                if (OnResolving != null)
                    OnResolving();
                Console.WriteLine("Resolving video link");

                video = DownloadUrlResolver.GetDownloadUrls(Searches.FindYoutubeUrlByKeywords(Query))
                        .Where(v => v.AdaptiveType == AdaptiveType.Audio)
                        .OrderByDescending(v => v.AudioBitrate)
                        .FirstOrDefault();

                if (video == null) // do something with this error
                    throw new Exception("Could not load any video elements based on the query.");

                Title = video.Title; //.Substring(0,video.Title.Length-10); // removing trailing "- You Tube"
            } catch (Exception ex) {
                privateState = StreamState.Completed;
                if (OnResolvingFailed != null)
                    OnResolvingFailed(ex.Message);
                Console.WriteLine($"Failed resolving the link.{ex.Message}");
                return;
            }

            musicStreamer = new MusicStreamer(this, video.DownloadUrl, Channel);
            if (OnQueued != null)
                OnQueued();
            return;
        }

        internal string PrintStats() => musicStreamer?.Stats();

        public Action OnQueued = null;
        public Action OnBuffering = null;
        public Action OnStarted = null;
        public Action OnCompleted = null;
{'fraction_non_alphanumeric': 0.08688500074771946, 'fraction_numerical': 0.0008972633467922835, 'mean_word_length': 2.0248756218905473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3092331', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1632, 'n_words': 476}