Ejemplo n.º 1
0
        public bool LoadFromHtml(HtmlNode rootNode, bool loggedIn)
        {
            if (rootNode.Name != "a")
            {
                return false;
            }

            if (!base.LoadFromLinkNode(rootNode))
            {
                return false;
            }

            if (!IsTopicUrl(Url))
            {
                return false;
            }

            HtmlNode pTD = rootNode.GetParentOfType("td");
            HtmlNode prevTD = pTD.GetPrevSiblingOfType("td");
            HtmlNode prevTD2 = prevTD.GetPrevSiblingOfType("td");
            HtmlNode nextTD = pTD.GetNextSiblingOfType("td");

            String prevText = HtmlUtilities.GetPlainHtmlText(prevTD.InnerText).Trim();
            IsOnTop = (prevText == "提示");

            if (!prevText.Contains("*"))
            {
                IsRead = true;
            }
            else
            {
                IsRead = false;
            }

            if (prevText.Contains("@"))
            {
                HasImage = true;
            }
            else
            {
                HasImage = false;
            }

            if (prevText.Contains("m"))
            {
                MFlag = true;
                IsRead = true;
            }
            else if (prevText.Contains("M"))
            {
                MFlag = true;
                IsRead = false;
            }
            else
            {
                MFlag = false;
            }

            if (prevText.Contains("b"))
            {
                BFlag = true;
                IsRead = true;
            }
            else if (prevText.Contains("B"))
            {
                BFlag = true;
                IsRead = false;
            }
            else
            {
                BFlag = false;
            }

            if (prevText.Contains("g"))
            {
                GFlag = true;
                IsRead = true;
            }
            else if (prevText.Contains("G"))
            {
                GFlag = true;
                IsRead = false;
            }
            else
            {
                GFlag = false;
            }

            if (IsOnTop || !App.Settings.KeepHistory || !App.WebSession.IsLoggedIn || !loggedIn)
            {
                IsRead = false;
            }

            ReplyCount = null;
            Author = null;
            foreach (HtmlNode authorNode in nextTD.Descendants("a"))
            {
                Author = HtmlUtilities.GetPlainHtmlText(authorNode.InnerText).Trim();
                break;
            }

            if (Author == null)
            {
                ReplyCount = nextTD.GetPlainInnerText().Trim();
                if (!ReplyCount.Contains("/"))
                {
                    ReplyCount = "";
                }

                nextTD = nextTD.GetNextSiblingOfType("td");
                foreach (HtmlNode authorNode in nextTD.Descendants("a"))
                {
                    Author = HtmlUtilities.GetPlainHtmlText(authorNode.InnerText).Trim();
                    break;
                }
            }

            IssueDate = null;
            foreach (HtmlNode dateNode in nextTD.Descendants("span"))
            {
                IssueDate = dateNode.GetPlainInnerText().Trim();
                break;
            }

            if (IssueDate == null)
            {
                nextTD = nextTD.GetNextSiblingOfType("td");
                IssueDate = nextTD.GetPlainInnerText().Trim();
            }

            Author2 = null;
            IssueDate2 = null;

            HtmlNode nextTD2 = nextTD.GetNextSiblingOfType("td");
            if (nextTD2 != null)
            {

                foreach (HtmlNode authorNode in nextTD2.Descendants("a"))
                {
                    Author2 = HtmlUtilities.GetPlainHtmlText(authorNode.InnerText).Trim();
                    break;
                }

                foreach (HtmlNode dateNode in nextTD2.Descendants("span"))
                {
                    IssueDate2 = dateNode.GetPlainInnerText().Trim();
                    break;
                }
            }

            if (Author2 != null)
            {
                Author = Author + " / " + Author2;
            }

            if (IssueDate2 != null)
            {
                IssueDate = IssueDate2;
            }

            if ((Author == null) || (IssueDate == null))
            {
                return false;
            }

            if ((prevTD2 != null) && !IsOnTop)
            {
                try
                {
                    Number = long.Parse(prevTD2.InnerText);
                }
                catch (Exception)
                {
                    Number = 0;
                }
            }

            Name = Name.Trim('●').Trim();

            return true;
        }
Ejemplo n.º 2
0
        public override bool LoadFromHtml(HtmlNode RootNode)
        {
            try
            {
                uint textIndex = 0;
                bool hasQuote = false;
                ContentBlock prevContentBlock = null;
                bool startContent = false;

                foreach (HtmlNode node in RootNode.Descendants())
                {
                    ContentBlock contentBlock = null;
                    String text = "";

                    if (node.Name == "#text")
                    {
                        text = node.GetPlainInnerText();
                        Match match;
                        switch (textIndex)
                        {
                            case 1:
                                match = Regex.Match(text, _headingTemplate);
                                if (match.Success)
                                {
                                    Author = match.Groups[1].ToString();
                                    BoardName = match.Groups[2].ToString();

                                    CalculateAuthorId();
                                }
                                break;

                            case 2:
                                match = Regex.Match(text, _titleTemplate);
                                if (match.Success)
                                {
                                    Title = HtmlUtilities.GetPlainHtmlText(match.Groups[1].ToString());
                                }
                                break;

                            case 3:
                            case 4:
                                match = Regex.Match(text, _source1Template);
                                if (match.Success)
                                {
                                    Source1 = match.Groups[1].ToString();
                                    IssueDate = match.Groups[2].ToString();
                                }

                                if (!text.StartsWith("关键字:"))
                                {
                                    if ((Author == null) ||
                                        (BoardName == null) ||
                                        (Title == null) ||
                                        (Source1 == null) ||
                                        (IssueDate == null))
                                    {
                                        return false;
                                    }
                                }

                                break;

                            default:
                                if (text.StartsWith("※"))
                                 {
                                    match = Regex.Match(text, _source2Template);
                                    if (match.Success)
                                    {
                                        Source2 = match.Groups[1].ToString();
                                        break;
                                    }
                                }

                                if (node.ParentNode.Name == "a")
                                {
                                    break;
                                }

                                if (startContent && !String.IsNullOrEmpty(text))
                                {
                                    contentBlock = ContentBlock.CreateContentBlockFromHtml(node, ParentUrl);
                                }

                                break;
                        }

                        if (textIndex > 3)
                        {
                            startContent = true;
                        }

                        textIndex++;
                    }
                    else if (startContent)
                    {
                        if (node.Name == "a")
                        {
                            String link = node.Attributes["href"].Value;
                            if (link.StartsWith(@"javascript:"))
                            {
                                continue;
                            }
                        }
                        else if (node.Name == "img")
                        {
                            if (node.ParentNode.Name == "a")
                            {
                                continue;
                            }
                        }

                        contentBlock = ContentBlock.CreateContentBlockFromHtml(node, ParentUrl);
                    }

                    if (contentBlock != null)
                    {
                        bool isSignatureStart = false;
                        if (text == "--")
                        {
                            hasQuote = false;
                            isSignatureStart = true;
                            contentBlock.NoMerge = true;
                        }

                        if (prevContentBlock != null && SignatureStart >= 0 && contentBlock is TextContentBlock && (contentBlock as TextContentBlock).Text == "\n")
                        {
                            prevContentBlock.NoMerge = true;
                        }

                        if ((prevContentBlock == null) || !(prevContentBlock.MergeWith(contentBlock)))
                        {
                            if (!hasQuote || !(contentBlock is QuoteBlock) || !App.Settings.HideFullQuote)
                            {
                                Contents.Add(contentBlock);
                                prevContentBlock = contentBlock;

                                if (prevContentBlock != null)
                                {
                                    if (prevContentBlock is QuoteBlock)
                                    {
                                        hasQuote = true;
                                    }
                                }

                                if (isSignatureStart)
                                {
                                    _signatureLastPos = SignatureStart;
                                    SignatureStart = Contents.Count - 1;
                                }
                            }
                        }
                    }
                }

                if (_signatureLastPos >= 0)
                {
                    SignatureStart = _signatureLastPos;
                }

                HtmlNode pTable = RootNode.GetParentOfType("table");
                HtmlNode prevTable = pTable.GetPrevSiblingOfType("table");
                if (prevTable != null)
                {
                    foreach (HtmlNode linkNode in prevTable.Descendants("a"))
                    {
                        String linkText = linkNode.GetLinkText();
                        String linkUrl = linkNode.GetLinkUrl(ParentUrl);

                        if (linkText == "本篇全文")
                        {
                            PostUrl = linkUrl;
                        }
                        else if (linkText == "回复")
                        {
                            ReplyPostUrl = linkUrl;
                        }
                        else if (linkText == "修改")
                        {
                            ModifyPostUrl = linkUrl;
                        }
                        else if (linkText == "删除")
                        {
                            if (linkNode.Attributes.Contains("onclick"))
                            {
                                DeletePostUrl = linkNode.Attributes["onclick"].Value;
                            }
                        }
                        else if (linkText == "转贴")
                        {
                            ForwardUrl = linkUrl;
                        }
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }