コード例 #1
0
ファイル: 4chan.cs プロジェクト: jdpurcell/ChanThreadWatch
        public override GetFilesResult GetFiles(List <ReplaceInfo> replaceList)
        {
            GetFilesResult result      = new GetFilesResult();
            bool           seenSpoiler = false;

            foreach (HtmlTagRange postTagRange in Parser.FindStartTags("div").Where(t => HtmlParser.ClassAttributeValueHas(t, "post"))
                     .Select(t => Parser.CreateTagRange(t)).Where(r => r != null))
            {
                HtmlTagRange fileTextDivTagRange = Parser.CreateTagRange(Parser.FindStartTags(postTagRange, "div")
                                                                         .Where(t => HtmlParser.ClassAttributeValueHas(t, "fileText")).FirstOrDefault());
                if (fileTextDivTagRange == null)
                {
                    continue;
                }

                HtmlTagRange fileThumbLinkTagRange = Parser.CreateTagRange(Parser.FindStartTags(postTagRange, "a")
                                                                           .Where(t => HtmlParser.ClassAttributeValueHas(t, "fileThumb")).FirstOrDefault());
                if (fileThumbLinkTagRange == null)
                {
                    continue;
                }

                HtmlTag fileTextLinkStartTag = Parser.FindStartTag(fileTextDivTagRange, "a");
                if (fileTextLinkStartTag == null)
                {
                    continue;
                }

                HtmlTag fileThumbImageTag = Parser.FindStartTag(fileThumbLinkTagRange, "img");
                if (fileThumbImageTag == null)
                {
                    continue;
                }

                string imageUrl = fileTextLinkStartTag.GetAttributeValue("href");
                if (imageUrl == null)
                {
                    continue;
                }

                string thumbUrl = fileThumbImageTag.GetAttributeValue("src");
                if (thumbUrl == null)
                {
                    continue;
                }

                bool isSpoiler = HtmlParser.ClassAttributeValueHas(fileThumbLinkTagRange.StartTag, "imgspoiler");

                string originalFileName;
                if (isSpoiler)
                {
                    originalFileName = fileTextDivTagRange.StartTag.GetAttributeValue("title");
                }
                else
                {
                    // If the filename is shortened, the original filename is in the title attribute
                    originalFileName = fileTextLinkStartTag.GetAttributeValue("title");
                    // Otherwise, the link's innerHTML contains the original filename
                    if (originalFileName == null)
                    {
                        HtmlTagRange fileTextLinkTagRange = Parser.CreateTagRange(fileTextLinkStartTag);
                        if (fileTextLinkTagRange == null)
                        {
                            continue;
                        }
                        originalFileName = Parser.GetInnerHtml(fileTextLinkTagRange);
                    }
                }
                if (originalFileName == null)
                {
                    continue;
                }

                string imageMD5 = fileThumbImageTag.GetAttributeValue("data-md5");
                if (imageMD5 == null)
                {
                    continue;
                }

                ImageInfo image = new ImageInfo {
                    Url     = General.GetAbsoluteUrl(Uri, HttpUtility.HtmlDecode(imageUrl)),
                    Referer = Url,
                    UnsanitizedOriginalFileName = HttpUtility.HtmlDecode(originalFileName),
                    HashType = HashType.MD5,
                    Hash     = General.TryBase64Decode(imageMD5)
                };
                if (image.Url.Length == 0 || image.FileName.Length == 0 || image.Hash == null)
                {
                    continue;
                }

                ThumbnailInfo thumb = new ThumbnailInfo {
                    Url     = General.GetAbsoluteUrl(Uri, HttpUtility.HtmlDecode(thumbUrl)),
                    Referer = Url
                };
                if (thumb.Url == null || thumb.FileName.Length == 0)
                {
                    continue;
                }

                if (replaceList != null)
                {
                    HtmlAttribute attribute;

                    attribute = fileTextLinkStartTag.GetAttribute("href");
                    if (attribute != null)
                    {
                        replaceList.Add(
                            new ReplaceInfo {
                            Offset = attribute.Offset,
                            Length = attribute.Length,
                            Type   = ReplaceType.ImageLinkHref,
                            Tag    = image.FileName
                        });
                    }

                    attribute = fileThumbLinkTagRange.StartTag.GetAttribute("href");
                    if (attribute != null)
                    {
                        replaceList.Add(
                            new ReplaceInfo {
                            Offset = attribute.Offset,
                            Length = attribute.Length,
                            Type   = ReplaceType.ImageLinkHref,
                            Tag    = image.FileName
                        });
                    }

                    attribute = fileThumbImageTag.GetAttribute("src");
                    if (attribute != null)
                    {
                        replaceList.Add(
                            new ReplaceInfo {
                            Offset = attribute.Offset,
                            Length = attribute.Length,
                            Type   = ReplaceType.ImageSrc,
                            Tag    = thumb.FileName
                        });
                    }
                }

                result.Images.Add(image);

                if (!isSpoiler || !seenSpoiler)
                {
                    result.Thumbnails.Add(thumb);
                    if (isSpoiler)
                    {
                        seenSpoiler = true;
                    }
                }
            }

            return(result);
        }
コード例 #2
0
 public IEnumerable <HtmlTag> FindStartTags(HtmlTagRange containingTagRange, params string[] names)
 {
     return(FindStartTags(containingTagRange.StartTag, containingTagRange.EndTag, names));
 }
コード例 #3
0
 public IEnumerable <HtmlTag> EnumerateTags(HtmlTagRange containingTagRange)
 {
     return(EnumerateTags(containingTagRange.StartTag, containingTagRange.EndTag));
 }
コード例 #4
0
 public HtmlTag FindTag(bool isEndTag, HtmlTagRange containingTagRange, params string[] names)
 {
     return(FindTag(isEndTag, containingTagRange.StartTag, containingTagRange.EndTag, names));
 }
コード例 #5
0
 public string GetInnerHtml(HtmlTagRange tagRange)
 {
     return(GetInnerHtml(tagRange.StartTag, tagRange.EndTag));
 }