示例#1
0
        public IEnumerator GetDownloadUrls(Action callback, string videoUrl, bool decryptSignature = true)
        {
            if (videoUrl != null)
            {
                Debug.Log("Youtube: " + videoUrl);
            }
            else
            {
                Debug.Log("Youtube url null!");
            }
            if (videoUrl == null)
            {
                throw new ArgumentNullException("videoUrl");
            }

#if UNITY_WSA
            videoUrl = "https://youtube.com/watch?v=" + videoUrl;
#else
            Uri  uriResult;
            bool result = Uri.TryCreate(videoUrl, UriKind.Absolute, out uriResult) &&
                          (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
            if (!result)
            {
                videoUrl = "https://youtube.com/watch?v=" + videoUrl;
            }
#endif


            bool isYoutubeUrl = TryNormalizeYoutubeUrl(videoUrl, out videoUrl);
            if (!isYoutubeUrl)
            {
                throw new ArgumentException("URL is not a valid youtube URL!");
            }
            yield return(StartCoroutine(DownloadUrl(videoUrl)));

            if (downloadUrlResponse.isValid)
            {
                if (IsVideoUnavailable(downloadUrlResponse.data))
                {
                    throw new VideoNotAvailableException();
                }

                try
                {
                    var     dataRegex     = new Regex(@"ytplayer\.config\s*=\s*(\{.+?\});", RegexOptions.Multiline);
                    string  extractedJson = dataRegex.Match(downloadUrlResponse.data).Result("$1");
                    JObject json          = JObject.Parse(extractedJson);
                    string  videoTitle    = GetVideoTitle(json);
                    IEnumerable <ExtractionInfo> downloadUrls      = ExtractDownloadUrls(json);
                    List <VideoInfo>             infos             = GetVideoInfos(downloadUrls, videoTitle).ToList();
                    Html5PlayerResult            htmlPlayerVersion = GetHtml5PlayerVersion(json);
                    if (htmlPlayerVersion.isValid)
                    {
                        foreach (VideoInfo info in infos)
                        {
                            info.HtmlPlayerVersion = htmlPlayerVersion.result;
                            info.HtmlscriptName    = htmlPlayerVersion.scriptName;
                        }
                        videoInfos = infos;
                        callback.Invoke();
                    }
                }
                catch (Exception e)
                {
                    Debug.Log("Resolver Exception!: " + e.Message);
                    //string filePath = Application.persistentDataPath + "/log_download_exception_" + DateTime.Now.ToString("ddMMyyyyhhmmssffff") + ".txt";
                    //Debug.Log("DownloadUrl content saved to " + filePath);
                    //File.WriteAllText(filePath, downloadUrlResponse.data);
                    Debug.Log("retry!");
                    if (GameObject.FindObjectOfType <HighQualityPlayback>() != null)
                    {
                        GameObject.FindObjectOfType <HighQualityPlayback>().RetryPlayYoutubeVideo();
                    }
                    else if (GameObject.FindObjectOfType <SimplePlayback>() != null)
                    {
                        GameObject.FindObjectOfType <SimplePlayback>().RetryPlayback();
                    }
                }
            }
        }
示例#2
0
        //--------------------------------------------------------------------------------------------------------------------------//
        public IEnumerator GetDownloadUrls(Action callback, string url, bool decryptSignature = true, Action failedCallback = null)
        //--------------------------------------------------------------------------------------------------------------------------//
        {
            //If our passed in Youtube URL is null or empty, stop here!
            if (url == null || (url != null && url == ""))
            {
                if (failedCallback != null)
                {
                    failedCallback.Invoke();
                }
                throw new ArgumentException("RequestResolver.cs GetDownloadUrls() ERROR: Passed in Youtube url is null or empty");
            }
            else
            {
#if UNITY_WSA
                //If this is videoID instead of the full URL, attach it to a normal Youtube URL
                if (!videoUrl.Contains("youtube"))
                {
                    videoUrl = "https://youtube.com/watch?v=" + videoUrl;
                }
#else
                //Create a new uri from the passed in url
                Uri  uriResult;
                bool result = Uri.TryCreate(url, UriKind.Absolute, out uriResult) &&
                              (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

                //If the uri could not be created, try using the url as a videoID instead
                //If this is videoID instead of the full URL, attach it to a normal Youtube URL
                if (!result)
                {
                    url = "https://youtube.com/watch?v=" + url;
                }
#endif


                bool isYoutubeUrl = TryNormalizeYoutubeUrl(url, out url);

                if (!isYoutubeUrl)
                {
                    Debug.Log("RequestResolver.cs GetDownloadUrls() ERROR: Passed in Youtube url is invalid");
                    if (failedCallback != null)
                    {
                        failedCallback.Invoke();
                    }
                    throw new ArgumentException("RequestResolver.cs GetDownloadUrls() ERROR: Unable to normalize passed in YouTube URL");
                }

                //Let's parse the Youtube url for our list of videos we can play
                yield return(StartCoroutine(DownloadUrl(url)));

                if (downloadUrlResponse.isValid)
                {
                    if (IsVideoUnavailable(downloadUrlResponse.data))
                    {
                        if (failedCallback != null)
                        {
                            failedCallback.Invoke();
                        }
                        throw new VideoNotAvailableException("RequestResolver.cs GetDownloadUrls() ERROR: Youtube video is unavailable");
                    }
                    else
                    {
                        try
                        {
                            var     dataRegex     = new Regex(@"ytplayer\.config\s*=\s*(\{.+?\});", RegexOptions.Multiline);
                            string  extractedJson = dataRegex.Match(downloadUrlResponse.data).Result("$1");
                            JObject json          = JObject.Parse(extractedJson);
                            string  videoTitle    = GetVideoTitle(json);
                            IEnumerable <ExtractionInfo> downloadUrls      = ExtractDownloadUrls(json);
                            List <VideoInfo>             infos             = GetVideoInfos(downloadUrls, videoTitle).ToList();
                            Html5PlayerResult            htmlPlayerVersion = GetHtml5PlayerVersion(json);

                            if (htmlPlayerVersion.isValid)
                            {
                                foreach (VideoInfo info in infos)
                                {
                                    info.HtmlPlayerVersion = htmlPlayerVersion.result;
                                    info.HtmlscriptName    = htmlPlayerVersion.scriptName;
                                }

                                videoInfos = infos;
                                if (callback != null)
                                {
                                    callback.Invoke();
                                }
                                //Debug.Log( "RequestResolver.cs GetDownloadUrls() Success! Found valid VideoInfo for url" );
                            }
                            else
                            {
                                if (failedCallback != null)
                                {
                                    failedCallback.Invoke();
                                }
                                throw new ArgumentException("RequestResolver.cs GetDownloadUrls() ERROR: Unable to create valid VideoInfo for url");
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Log("Resolver Exception!: " + e.Message);
                            //string filePath = Application.persistentDataPath + "/log_download_exception_" + DateTime.Now.ToString("ddMMyyyyhhmmssffff") + ".txt";
                            //Debug.Log("DownloadUrl content saved to " + filePath);
                            //File.WriteAllText(filePath, downloadUrlResponse.data);
                            Debug.Log("retry!");

                            if (GameObject.FindObjectOfType <HighQualityPlayback>() != null)
                            {
                                GameObject.FindObjectOfType <HighQualityPlayback>().RetryPlayYoutubeVideo();
                            }
                            else if (GameObject.FindObjectOfType <SimplePlayback>() != null)
                            {
                                GameObject.FindObjectOfType <SimplePlayback>().RetryPlayback();
                            }
                        }
                    }
                }
                else
                {
                    if (failedCallback != null)
                    {
                        failedCallback.Invoke();
                    }
                    throw new ArgumentException("RequestResolver.cs GetDownloadUrls() ERROR: Unable to get list of Youtube videos from url");
                }
            }
        } //END GetDownloadUrls