Пример #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();
                });
        }
Пример #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);
         });
 }
Пример #3
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);
            });
        }
Пример #4
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);
            });
        }
Пример #5
0
        private void OnTaskDoWork(object sender, DoWorkEventArgs e)
        {
            var request = e.Argument as AwfulSmileyRequest;
            if (request == null)
            {
                e.Result = new AwfulSmileyRequest() { Status = Awful.Core.Models.ActionResult.Failure };
                return;
            }

            request.Status = Awful.Core.Models.ActionResult.Cancelled;

            this._web = new WebGet();
            this._web.LoadAsync(SMILEY_REQUEST_URI, (result, args) =>
                {
                    switch (result)
                    {
                        case Awful.Core.Models.ActionResult.Success:
                            this.ProcessRequest(request, args);
                            break;

                        default:
                            request.Status = result;
                            break;
                    }

                    this._signal.Set();
                });

            int timeout = AwfulSettings.THREAD_TIMEOUT_DEFAULT;

            #if DEBUG
            timeout = -1;
            #endif
            if (timeout > 0) { this._signal.WaitOne(timeout); }
            else { this._signal.WaitOne(); }
            e.Result = request;
        }
Пример #6
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;
        }
        private void Thread_RequestNewThread(object sender, DoWorkEventArgs e)
        {
            var forum = e.Argument as ForumData;
            if (forum == null)
            {
                e.Cancel = true;
                return;
            }

            AutoResetEvent signal = new AutoResetEvent(false);

            string threadRequestUrl = string.Format("{0}?{1}&forumid={2}", NEW_THREAD_URL,
                NEW_THREAD_ACTION, forum.ID);

            NewThreadRequest threadRequest = null;
            WebGetDocumentArgs doc = null;

            this._web = new WebGet();

            this._web.LoadAsync(threadRequestUrl, (webResult, document) =>
            {
                switch (webResult)
                {
                    case ActionResult.Success:
                        doc = document;
                        break;

                    case ActionResult.Cancelled:
                        e.Cancel = true;
                        break;

                    case ActionResult.Busy:
                        e.Cancel = true;
                        break;
                }

                signal.Set();
            });

            signal.WaitOne();

            threadRequest = this.GenerateNewThreadRequest(doc);

            if (threadRequest != null)
            {
                threadRequest.Forum = forum;
                e.Result = threadRequest;
            }
        }
Пример #8
0
        private List<SAForum> LoadForumsFromWeb()
        {
            List<SAForum> result = null;
            this._webGet = new WebGet();
            AutoResetEvent waitSignal = new AutoResetEvent(false);
            HtmlDocument doc = null;

            this._webGet.LoadAsync("http://forums.somethingawful.com/forumdisplay.php?forumid=1", (action, docArgs) =>
            {
                switch (action)
                {
                    case Awful.Core.Models.ActionResult.Success:
                        doc = docArgs.Document;
                        break;

                    case Awful.Core.Models.ActionResult.Failure:
                        worker.CancelAsync();
                        break;
                }

                if (!worker.CancellationPending)
                {
                    result = ParseData(doc);

                    if (result == null) { worker.CancelAsync(); }
                    else
                    {
                        var args = new ForumsRefreshedEventArgs<SAForum>(result);
                        ForumsRefreshed.Fire(this, args);
                    }
                }

                waitSignal.Set();
            });

            waitSignal.WaitOne();
            return result;
        }
Пример #9
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;
        }
Пример #10
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);
            });
        }
Пример #11
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...";
        }
Пример #12
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);
                });
        }
Пример #13
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;
        }
Пример #14
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);
        }