コード例 #1
0
        private static bool ResponseYoutube(ExecutionInformation info)
        {
            string[] command = info.TextMessage.Message.SplitNoEmpty(' ');
            if (command[0] != "!f")
            {
                return(false);
            }
            if (command.Length != 2)
            {
                return(true);
            }
            int entry;

            if (int.TryParse(command[1], out entry))
            {
                PlayData data = info.Session.UserResource;
                if (data == null || data.Resource as YoutubeResource == null)
                {
                    info.Session.Write("An unexpected error with the ytresource occured: null.");
                    return(true);
                }
                YoutubeResource ytResource = (YoutubeResource)data.Resource;
                if (entry < 0 || entry >= ytResource.AvailableTypes.Count)
                {
                    return(true);
                }
                ytResource.Selected = entry;
                if (ValidateMedia(info.Session, ytResource))
                {
                    info.Session.Bot.FactoryManager.Play(data);
                }
            }
            return(true);
        }
コード例 #2
0
        private static bool ValidateMedia(BotSession session, YoutubeResource resource)
        {
            switch (ValidateMedia(resource))
            {
            case ValidateCode.Ok: return(true);

            case ValidateCode.Restricted: session.Write("The video cannot be played due to youtube restrictions."); return(false);

            case ValidateCode.Timeout: session.Write("No connection could be established to youtube. Please try again later."); return(false);

            case ValidateCode.UnknownError: session.Write("Unknown error occoured"); return(false);

            default: throw new InvalidOperationException();
            }
        }
コード例 #3
0
        public void PostProcess(PlayData data, out bool abortPlay)
        {
            YoutubeResource ytResource = (YoutubeResource)data.Resource;

#if DEBUG
            StringBuilder dbg = new StringBuilder("YT avail codecs: ");
            foreach (var yd in ytResource.AvailableTypes)
            {
                dbg.Append(yd.qualitydesciption).Append(" @ ").Append(yd.codec).Append(", ");
            }
            Log.Write(Log.Level.Debug, dbg.ToString());
#endif

            var availList       = ytResource.AvailableTypes;
            int autoselectIndex = availList.FindIndex(t => t.codec == VideoCodec.M4A);
            if (autoselectIndex == -1)
            {
                autoselectIndex = availList.FindIndex(t => t.audioOnly);
            }
            if (autoselectIndex != -1)
            {
                ytResource.Selected = autoselectIndex;
                abortPlay           = !ValidateMedia(data.Session, ytResource);
                return;
            }

            StringBuilder strb = new StringBuilder();
            strb.AppendLine("\nMultiple formats found please choose one with !f <number>");
            int count = 0;
            foreach (var videoType in ytResource.AvailableTypes)
            {
                strb.Append("[")
                .Append(count++)
                .Append("] ")
                .Append(videoType.codec.ToString())
                .Append(" @ ")
                .AppendLine(videoType.qualitydesciption);
            }

            abortPlay = true;
            data.Session.Write(strb.ToString());
            data.Session.UserResource = data;
            data.Session.SetResponse(ResponseYoutube, null);
        }
コード例 #4
0
		public RResultCode GetResourceById(string ytID, string name, out AudioResource result)
		{
			string resulthtml;
			if (!WebWrapper.DownloadString(out resulthtml, new Uri($"http://www.youtube.com/get_video_info?video_id={ytID}&el=info")))
			{
				result = null;
				return RResultCode.NoConnection;
			}

			var videoTypes = new List<VideoData>();
			NameValueCollection dataParse = HttpUtility.ParseQueryString(resulthtml);

			string videoDataUnsplit = dataParse["url_encoded_fmt_stream_map"];
			if (videoDataUnsplit != null)
			{
				string[] videoData = videoDataUnsplit.Split(',');

				foreach (string vdat in videoData)
				{
					NameValueCollection videoparse = HttpUtility.ParseQueryString(vdat);

					string vLink = videoparse["url"];
					if (vLink == null)
						continue;

					string vType = videoparse["type"];
					if (vType == null)
						continue;

					string vQuality = videoparse["quality"];
					if (vQuality == null)
						continue;

					var vt = new VideoData();
					vt.link = vLink;
					vt.codec = GetCodec(vType);
					vt.qualitydesciption = vQuality;
					videoTypes.Add(vt);
				}
			}

			videoDataUnsplit = dataParse["adaptive_fmts"];
			if (videoDataUnsplit != null)
			{
				string[] videoData = videoDataUnsplit.Split(',');

				foreach (string vdat in videoData)
				{
					NameValueCollection videoparse = HttpUtility.ParseQueryString(vdat);

					string vType = videoparse["type"];
					if (vType == null)
						continue;

					bool audioOnly = false;
					if (vType.StartsWith("video/"))
						continue;
					else if (vType.StartsWith("audio/"))
						audioOnly = true;

					string vLink = videoparse["url"];
					if (vLink == null)
						continue;

					var vt = new VideoData();
					vt.codec = GetCodec(vType);
					vt.qualitydesciption = vType;
					vt.link = vLink;
					if (audioOnly)
						vt.audioOnly = true;
					else
						vt.videoOnly = true;
					videoTypes.Add(vt);
				}
			}

			string finalName = name ?? dataParse["title"] ?? $"<YT - no title : {ytID}>";
			var ytResult = new YoutubeResource(ytID, finalName, videoTypes);
			result = ytResult;
			return ytResult.AvailableTypes.Count > 0 ? RResultCode.Success : RResultCode.YtNoVideosExtracted;
		}
コード例 #5
0
		private static ValidateCode ValidateMedia(YoutubeResource resource)
		{
			var media = resource.AvailableTypes[resource.Selected];
			return WebWrapper.GetResponse(new Uri(media.link), TimeSpan.FromSeconds(1));
		}
コード例 #6
0
		private static bool ValidateMedia(BotSession session, YoutubeResource resource)
		{
			switch (ValidateMedia(resource))
			{
			case ValidateCode.Ok: return true;
			case ValidateCode.Restricted: session.Write("The video cannot be played due to youtube restrictions."); return false;
			case ValidateCode.Timeout: session.Write("No connection could be established to youtube. Please try again later."); return false;
			case ValidateCode.UnknownError: session.Write("Unknown error occoured"); return false;
			default: throw new InvalidOperationException();
			}
		}
コード例 #7
0
        public RResultCode GetResourceById(string ytID, string name, out AudioResource result)
        {
            string resulthtml;

            if (!WebWrapper.DownloadString(out resulthtml, new Uri($"http://www.youtube.com/get_video_info?video_id={ytID}&el=info")))
            {
                result = null;
                return(RResultCode.NoConnection);
            }

            var videoTypes = new List <VideoData>();
            NameValueCollection dataParse = HttpUtility.ParseQueryString(resulthtml);

            string videoDataUnsplit = dataParse["url_encoded_fmt_stream_map"];

            if (videoDataUnsplit != null)
            {
                string[] videoData = videoDataUnsplit.Split(',');

                foreach (string vdat in videoData)
                {
                    NameValueCollection videoparse = HttpUtility.ParseQueryString(vdat);

                    string vLink = videoparse["url"];
                    if (vLink == null)
                    {
                        continue;
                    }

                    string vType = videoparse["type"];
                    if (vType == null)
                    {
                        continue;
                    }

                    string vQuality = videoparse["quality"];
                    if (vQuality == null)
                    {
                        continue;
                    }

                    var vt = new VideoData();
                    vt.link              = vLink;
                    vt.codec             = GetCodec(vType);
                    vt.qualitydesciption = vQuality;
                    videoTypes.Add(vt);
                }
            }

            videoDataUnsplit = dataParse["adaptive_fmts"];
            if (videoDataUnsplit != null)
            {
                string[] videoData = videoDataUnsplit.Split(',');

                foreach (string vdat in videoData)
                {
                    NameValueCollection videoparse = HttpUtility.ParseQueryString(vdat);

                    string vType = videoparse["type"];
                    if (vType == null)
                    {
                        continue;
                    }

                    bool audioOnly = false;
                    if (vType.StartsWith("video/"))
                    {
                        continue;
                    }
                    else if (vType.StartsWith("audio/"))
                    {
                        audioOnly = true;
                    }

                    string vLink = videoparse["url"];
                    if (vLink == null)
                    {
                        continue;
                    }

                    var vt = new VideoData();
                    vt.codec             = GetCodec(vType);
                    vt.qualitydesciption = vType;
                    vt.link = vLink;
                    if (audioOnly)
                    {
                        vt.audioOnly = true;
                    }
                    else
                    {
                        vt.videoOnly = true;
                    }
                    videoTypes.Add(vt);
                }
            }

            string finalName = name ?? dataParse["title"] ?? $"<YT - no title : {ytID}>";
            var    ytResult  = new YoutubeResource(ytID, finalName, videoTypes);

            result = ytResult;
            return(ytResult.AvailableTypes.Count > 0 ? RResultCode.Success : RResultCode.YtNoVideosExtracted);
        }
コード例 #8
0
        private static ValidateCode ValidateMedia(YoutubeResource resource)
        {
            var media = resource.AvailableTypes[resource.Selected];

            return(WebWrapper.GetResponse(new Uri(media.link), TimeSpan.FromSeconds(1)));
        }