Exemplo n.º 1
0
        public void TestLoadAsync()
        {
            var web = new WebGet();
            if (!AwfulWebRequest.CanAuthenticate)
            {
                Assert.Fail("Failed to login to SA.");
            }

            string url = "http://forums.somethingawful.com/forumdisplay.php?forumid=1";
            string target = "<title>General Bullshit - The Something Awful Forums</title>";
            web.LoadAsync(url, (result, args) =>
                {
                    switch (result)
                    {
                        case Models.ActionResult.Failure:
                            Assert.Fail("LoadAsync failed to load the page.");
                            break;

                        default:
                            try
                            {
                                string html = args.Document.DocumentNode.OuterHtml;
                                bool expected = true;
                                bool actual = html.Contains(target);
                                Assert.AreEqual(expected, actual);
                            }
                            catch (Exception) { Assert.Fail("LoadAsync failed to parse the web document."); }
                            break;
                    }
                    EnqueueTestComplete();
                });
        }
Exemplo n.º 2
0
 public void FetchAllForums(Action<ActionResult, IEnumerable<ForumData>> action)
 {
     string url = string.Format("{0}/{1}?forumid=1", Constants.BASE_URL, Constants.FORUM_PAGE_URI);
     var web = new WebGet();
     web.LoadAsync(url, (ar, doc) =>
         {
             IEnumerable<ForumData> result = null;
             if (ar == ActionResult.Success)
             {
                 result = AwfulForumParser.ParseForumList(doc.Document);
             }
             action(ar, result);
         });
 }
Exemplo n.º 3
0
        public void FetchForumPage(ForumData forum, int pageNumber, Action<ForumPageData> action)
        {
            var url = new StringBuilder();
            // http://forums.somethingawful.com/forumdisplay.php
            url.AppendFormat("{0}/{1}", Constants.BASE_URL, Constants.FORUM_PAGE_URI);
            // ?forumid=<FORUMID>
            url.AppendFormat("?forumid={0}", forum.ID);
            // &daysprune=30&perpage=30&posticon=0sortorder=desc&sortfield=lastpost
            url.Append("&daysprune=30&perpage=30&posticon=0sortorder=desc&sortfield=lastpost");
            // &pagenumber=<PAGENUMBER>
            url.AppendFormat("&pagenumber={0}", pageNumber);

            var web = new WebGet();
            web.LoadAsync(url.ToString(), (ar, doc) =>
            {
                ForumPageData result = null;
                if (ar == ActionResult.Success)
                {
                    result = AwfulForumParser.ParseForumPage(doc.Document);
                }
                action(result);
            });
        }
Exemplo n.º 4
0
        void OnBackgroundDoWork(object sender, DoWorkEventArgs e)
        {
            var url = e.Argument as string;
            web = new WebGet();
            HtmlAgilityPack.HtmlDocument doc = null;
            web.LoadAsync(url, (obj, loadDocumentArgs) =>
            {
                doc = loadDocumentArgs.Document;
                signal.Set();
            });

            signal.WaitOne(10000);

            if (doc != null)
                html = doc.DocumentNode.OuterHtml;
            else
                e.Cancel = true;
        }
Exemplo n.º 5
0
        private ThreadReplyData? GetReplyData(string threadID, string text)
        {
            string url = String.Format("http://forums.somethingawful.com/newreply.php?action=newreply&threadid={0}",
                threadID);

            AutoResetEvent replyDataSignal = new AutoResetEvent(false);
            HtmlDocument doc = null;
            bool success = false;
            WebGet web = new WebGet();
            web.LoadAsync(url, (obj, loadAsyncArgs) =>
                {
                    if (loadAsyncArgs.Document != null)
                    {
                        doc = loadAsyncArgs.Document;
                        success = true;
                    }
                    replyDataSignal.Set();
                });

            replyDataSignal.WaitOne(this._defaultTimeout);

            if (success == true)
            {
                ThreadReplyData data = GetReplyFormInfo(threadID, doc);
                data.TEXT = text;
                return data;
            }

            return null;
        }
Exemplo n.º 6
0
        private void BeginGetTextFromWebForm(string webUrl, Action<ActionResult, string> result)
        {
            var web = new WebGet();
            var url = webUrl;

            Logger.AddEntry(string.Format("ThreadReplyServer - Retrieving text from '{0}'.", url));

            ActionResult success = ActionResult.Failure;
            string bodyText = String.Empty;
            HtmlNode docNode = null;

            web.LoadAsync(url, (obj, args) =>
            {
                switch (obj)
                {
                    case ActionResult.Success:
                        if (args.Document != null)
                        {
                            success = ActionResult.Success;
                            docNode = args.Document.DocumentNode;
                        }
                        else { success = ActionResult.Failure; }
                        break;
                }

                getTextSignal.Set();
            });

            getTextSignal.WaitOne(this._defaultTimeout);

            if (getTextCancelled)
            {
                getTextCancelled = false;
                success = ActionResult.Cancelled;
            }

            if (success == ActionResult.Success)
            {
                bodyText = GetFormText(docNode);
                bodyText = HttpUtility.HtmlDecode(bodyText);
            }

            Logger.AddEntry(string.Format("ThreadReplyService - Get text result: success: {0}", success));
            var dispatcher = Deployment.Current.Dispatcher;

            dispatcher.BeginInvoke(() =>
            {
                result(success, bodyText);
            });
        }
Exemplo n.º 7
0
        private void ReportLoginSuccess()
        {
            this.Status = "Success! Welcome to the forums.";
            this.IsLoading = false;
            List<Cookie> cookies = null;
            bool cookiesGrabbed = false;
            int attempts = 0;
            while (!cookiesGrabbed && attempts < MAX_LOGIN_ATTEMPTS)
            {
                // try to get the cookies from the browser. browser.GetCookies() throws an
                // index out of range exception often...
                try
                {

                    cookies = this.ManageCookies(browser.GetCookies());
                    cookiesGrabbed = true;
                }
                catch (Exception ex)
                {
                    Logger.AddEntry("An error occurred: ", ex);
                    attempts = attempts + 1;
                }
            }

            //  try to parse username from index //
            var web = new WebGet();
            web.LoadAsync(SUCCESS_URL, (a, args) =>
                {
                    if (a == ActionResult.Success)
                    {
                        string username = AwfulIndexParser.ParseUserSessionName(args.Document);
                        this.Username = username;
                        this._currentState = LoginStates.SUCCESS;
                        this.Result.Fire(this, new ValueChangedEventArgs<LoginResult>(LoginResult.LOGIN_SUCCESSFUL));
                        var profile = new AwfulProfile() { Username = this.Username, Password = this.Password };
                        LoginSuccessful.Fire(this, new ProfileChangedEventArgs(profile, cookies));
                    }
                });

            this.Status = "Grabbing username...";
        }
Exemplo n.º 8
0
        public void RefreshThreadPage(ThreadPageData page, Action<ThreadPageData> action)
        {
            string url = page.Url;
            var web = new WebGet();
            web.LoadAsync(url, (a, e) =>
                {
                    ThreadPageData result = null;
                    if (a == ActionResult.Success)
                    {
                        result = AwfulThreadParser.ParseFromThreadPage(e.Document);
                    }

                    action(result);
                });
        }
Exemplo n.º 9
0
        public void FetchThreadPage(ThreadData thread, int pageNumber, Action<ThreadPageData> action)
        {
            // http://forums.somethingawful.com/showthread.php?noseen=0&threadid=3439182&pagenumber=69
            var url = new StringBuilder();
            // http://forums.somethingawful.com/showthread.php
            url.AppendFormat("{0}/{1}", Constants.BASE_URL, Constants.THREAD_PAGE_URI);
            // noseen=0&threadid=<THREADID>&pagenumber=<PAGENUMBER>
            url.AppendFormat("noseen=0&threadid={0}&pagenumber={1}", thread.ID, pageNumber);

            var web = new WebGet();
            web.LoadAsync(url.ToString(), (ar, doc) =>
               {
                ThreadPageData result = null;
                if (ar == ActionResult.Success)
                {
                    result = AwfulThreadParser.ParseFromThreadPage(doc.Document);
                }
                action(result);
            });
        }
Exemplo n.º 10
0
        private HtmlDocument LoadHtmlFromWeb(SAThreadPage page)
        {
            HtmlDocument doc = null;
            var signal = new AutoResetEvent(false);
            var web = new WebGet();
            web.LoadAsync(page.Url, (result, docArgs) =>
            {
                switch (result)
                {
                    case Awful.Core.Models.ActionResult.Success:
                        doc = docArgs.Document;
                        if (page.PageNumber == 0) { page.PageNumber = this.ProcessPageNumber(docArgs.Url); }
                        break;

                    case Awful.Core.Models.ActionResult.Busy:
                        break;

                    case Awful.Core.Models.ActionResult.Failure:
                        break;
                }

                signal.Set();
            });

            signal.WaitOne();

            return doc;
        }
Exemplo n.º 11
0
        public void RunURLTaskAsync(string url, Action<ActionResult> result)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback((state) =>
            {
                var web = new WebGet();
                var dispatch = Deployment.Current.Dispatcher;
                var signal = new AutoResetEvent(false);
                bool success = false;
                web.LoadAsync(url, (obj, args) =>
                {
                    if (args.Document != null)
                        success = true;

                    signal.Set();
                });

                signal.WaitOne(this._defaultServiceTimeout);

                if (success)
                {
                    dispatch.BeginInvoke(() => {result(ActionResult.Success); });
                }
                else
                {
                    dispatch.BeginInvoke(() => { result(ActionResult.Failure); });
                }
            }), null);
        }