Ejemplo n.º 1
0
        /// <summary>
        /// Downloads a webpage from a blog and searches for TEMPORARY_POST_TITLE_GUID.
        /// </summary>
        /// <param name="blogPageUrl"></param>
        /// <param name="progress"></param>
        /// <returns>Stream containing document which contains TEMPORARY_POST_TITLE_GUID.</returns>
        private Stream DownloadBlogPage(string blogPageUrl, IProgressHost progress)
        {
            ProgressTick   tick      = new ProgressTick(progress, 50, 100);
            MemoryStream   memStream = new MemoryStream();
            IHTMLDocument2 doc2      = null;

            // WinLive 221984: Theme detection timing out intermittently on WordPress.com
            // The temp post *often* takes more than a minute to show up on the blog home page.
            // The download progress dialog has a cancel button, we'll try a lot before giving up.
            for (int i = 0; i < 30 && doc2 == null; i++)
            {
                if (progress.CancelRequested)
                {
                    throw new OperationCancelledException();
                }
                tick.UpdateProgress(0, 0, Res.Get(StringId.ProgressDownloadingWeblogEditingStyle));
                // Sleep to give the post enough time to show up.
                // We'll make 10 attempts with a 1 second delay.
                // Subsequent attempts will use a 10 second delay.
                // This means we'll try for 5 minutes (10s + 290s = 300s) before we consider the operation timed out.
                Thread.Sleep(i < 10 ? 1000 : 10000);

                // Add random parameter to URL to bypass cache
                var urlRandom = UrlHelper.AppendQueryParameters(blogPageUrl, new string[] { Guid.NewGuid().ToString() });

                HttpWebResponse resp = _pageDownloader(urlRandom, 60000);
                memStream = new MemoryStream();
                using (Stream respStream = resp.GetResponseStream())
                    StreamHelper.Transfer(respStream, memStream);

                //read in the HTML file and determine if it contains the title element
                memStream.Seek(0, SeekOrigin.Begin);
                doc2 = HTMLDocumentHelper.GetHTMLDocumentFromStream(memStream, urlRandom);
                if (HTMLDocumentHelper.FindElementContainingText(doc2, TEMPORARY_POST_TITLE_GUID) == null)
                {
                    doc2 = null;
                }
            }
            if (doc2 == null)
            {
                throw new OperationTimedOutException();
            }
            tick.UpdateProgress(100, 100);

            //return the stream
            memStream.Seek(0, SeekOrigin.Begin);
            return(memStream);
        }