Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="videoid"></param>
        /// <returns>string empty if everything's fine. Otherwise the error code</returns>
        public async Task <string> LoadVideoStreamData()
        {
            if (VideoID == null)
            {
                throw new Exception("Video ID is not set.");
            }

            // clear etc
            VideoInfo.Clear();
            VideoInfo_adaptive_fmts.Clear();

            JavascriptEngine = null;
            DecodeFuncName   = string.Empty;
            DecodeScript     = string.Empty;

            // string requestUri = string.Format("https://www.youtube.com/watch?v={0}&nomobile=1&gl=US&hl=en&has_verified=1&bpctr=9999999999", this.VideoID);
            string requestUri = string.Format("https://www.youtube.com/watch?v={0}", this.VideoID);

            try
            {
                HttpClientHandler handler = new HttpClientHandler();
                var req2     = new HttpClient(handler);
                var message2 = new HttpRequestMessage(HttpMethod.Get, requestUri);

                message2.Headers.Add("Host", "www.youtube.com");
                message2.Headers.Add("User-Agent", YoutubeStreamAPIConstants.RandomHTTPUserAgent());

                //bool TokenHeaderAdded2 = YouTubeUserHandler.AddAccessTokenIfAvailable(message2);

                try
                {
                    HttpResponseMessage data = await req2.SendAsync(message2);

                    HttpContent content = data.Content;
                    data.EnsureSuccessStatusCode();

                    string SourceCode = await content.ReadAsStringAsync();

                    string retResult = await ParseStreamsInternal(SourceCode);

                    return(retResult);
                }
                catch (Exception eex)
                {
                    Debug.WriteLine(eex.ToString());
                }
            }
            catch (Exception exp)
            {
                Debug.WriteLine(exp.ToString());
            }
            return("");
        }
Пример #2
0
        /// <summary>
        /// Gets the HTML5 source code of the underlying youtube page.
        /// </summary>
        /// <param name="SourceCode"></param>
        /// <returns></returns>
        public static async Task <string> GetHTML5PlayerJSFromYouTubePageSource(string SourceCode)
        {
            string HTML5JavascriptURL = null;

            const string StringSearch = "\"js\":\"\\";
            int          IndexOf      = SourceCode.IndexOf(StringSearch);

            if (IndexOf != -1)
            {
                int EndJSIndex = SourceCode.IndexOf(@"""", IndexOf + StringSearch.Length);

                HTML5JavascriptURL = SourceCode.Substring(IndexOf + StringSearch.Length, EndJSIndex - (IndexOf + StringSearch.Length));
                HTML5JavascriptURL = HTML5JavascriptURL.Replace("\\/", "/");

                if (!HTML5JavascriptURL.StartsWith("http:"))
                {
                    HTML5JavascriptURL = "http:" + HTML5JavascriptURL;
                }
                //Debug.WriteLine(HTML5JavascriptURL);

                /////// Load HTML5 player item
                HttpClientHandler handler = new HttpClientHandler();
                var req2     = new HttpClient(handler);
                var message2 = new HttpRequestMessage(HttpMethod.Get, HTML5JavascriptURL);

                message2.Headers.Add("User-Agent", YoutubeStreamAPIConstants.RandomHTTPUserAgent());
                message2.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                message2.Headers.Add("Accept-Language", "en-US,en;q=0.5");
                //message.Headers.Add("Accept-Encoding", "gzip, deflate");
                message2.Headers.Add("Connection", "keep-alive");

                try
                {
                    HttpResponseMessage data = await req2.SendAsync(message2);

                    HttpContent content = data.Content;
                    data.EnsureSuccessStatusCode();

                    string HTML5SrcPlayer = await content.ReadAsStringAsync();

                    return(HTML5SrcPlayer);
                }
                catch (Exception exp)
                {
                    Debug.WriteLine(exp.ToString());
                }
            }
            return(null);
        }