示例#1
0
        private static List <DanbooruTag> ReadTagsFromTextFile(string filename)
        {
            var list = new List <DanbooruTag>();

            // Read the file and display it line by line.
            using (var file = new System.IO.StreamReader(filename))
            {
                var line = "";
                while ((line = file.ReadLine()) != null)
                {
                    var tag = new DanbooruTag()
                    {
                        Name  = line,
                        Id    = "-1",
                        Count = -1,
                        Type  = DanbooruTagType.Unknown
                    };

                    list.Add(tag);
                }
                ;
            }

            return(list);
        }
        private static void ReparseTags(DanbooruPost post, HtmlDocument doc)
        {
            post.TagsEntity.Clear();
            var tagsElement = doc.DocumentNode.SelectNodes("//ul[@id='tag-sidebar']/li");

            foreach (var tag in tagsElement)
            {
                HtmlDocument tagDoc = new HtmlDocument();
                tagDoc.LoadHtml(tag.OuterHtml);

                var tagEntity = new DanbooruTag();
                var el        = tagDoc.DocumentNode.SelectSingleNode("//li");
                var cls       = el.Attributes["class"].Value;
                switch (cls)
                {
                case "tag-type-general":
                    tagEntity.Type = DanbooruTagType.General;
                    break;

                case "tag-type-artist":
                    tagEntity.Type = DanbooruTagType.Artist;
                    break;

                case "tag-type-copyright":
                    tagEntity.Type = DanbooruTagType.Copyright;
                    break;

                case "tag-type-character":
                    tagEntity.Type = DanbooruTagType.Character;
                    break;

                //case "tag-type-medium":
                //    tagEntity.Type = DanbooruTagType.Circle;
                //    break;

                case "tag-type-medium":
                    tagEntity.Type = DanbooruTagType.Faults;
                    break;

                default:
                    tagEntity.Type = DanbooruTagType.Unknown;
                    break;
                }
                tagEntity.Name = tagDoc.DocumentNode.SelectSingleNode("//li/a").InnerText;
                var countStr = tagDoc.DocumentNode.SelectSingleNode("//li//span[@class='post-count']").InnerText.Trim();
                tagEntity.Count = Int32.Parse(countStr);

                post.TagsEntity.Add(tagEntity);
            }
        }
        public DanbooruTag GetTag(string tag, DanbooruTagCollection tagCollection)
        {
            // TODO: Hot spot for perfomance
            var result = tagCollection.Tag.FirstOrDefault <DanbooruTag>(x => x.Name == tag);

            if (result != null)
            {
                return(result);
            }
            else
            {
                var unknownTag = new DanbooruTag()
                {
                    Name  = tag,
                    Type  = DanbooruTagType.Unknown,
                    Count = -1,
                    Id    = "-1"
                };
                return(unknownTag);
            }
        }
        private static void ReparseTags(DanbooruPost post, HtmlDocument doc)
        {
            post.TagsEntity.Clear();
            var tagsElement = doc.DocumentNode.SelectNodes("//ul[@id='tag-sidebar']/li");
            foreach (var tag in tagsElement)
            {
                HtmlDocument tagDoc = new HtmlDocument();
                tagDoc.LoadHtml(tag.OuterHtml);

                var tagEntity = new DanbooruTag();
                var el = tagDoc.DocumentNode.SelectSingleNode("//li");
                var cls = el.Attributes["class"].Value;
                switch (cls)
                {
                    case "tag-type-general":
                        tagEntity.Type = DanbooruTagType.General;
                        break;

                    case "tag-type-artist":
                        tagEntity.Type = DanbooruTagType.Artist;
                        break;

                    case "tag-type-copyright":
                        tagEntity.Type = DanbooruTagType.Copyright;
                        break;

                    case "tag-type-character":
                        tagEntity.Type = DanbooruTagType.Character;
                        break;

                    //case "tag-type-medium":
                    //    tagEntity.Type = DanbooruTagType.Circle;
                    //    break;

                    case "tag-type-medium":
                        tagEntity.Type = DanbooruTagType.Faults;
                        break;

                    default:
                        tagEntity.Type = DanbooruTagType.Unknown;
                        break;
                }
                tagEntity.Name = tagDoc.DocumentNode.SelectSingleNode("//li/a").InnerText;
                var countStr = tagDoc.DocumentNode.SelectSingleNode("//li//span[@class='post-count']").InnerText.Trim();
                tagEntity.Count = Int32.Parse(countStr);

                post.TagsEntity.Add(tagEntity);
            }
        }
        public DanbooruTagCollection parseTagsPage(string data, int page)
        {
            DanbooruTagCollection tagCol = new DanbooruTagCollection();
            List<DanbooruTag> tags = new List<DanbooruTag>();
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(data);

            int index = 1 + ((page - 1) * 50);
            // select all tags
            var tables = doc.DocumentNode.SelectNodes("//table[contains(@class,'highlightable')]");
            foreach (var table in tables)
            {
                if (!(table.Attributes["class"].Value == "highlightable"))
                {
                    table.Remove();
                    continue;
                }

                var rows = table.SelectNodes("//table[contains(@class,'highlightable')]//tr");
                int countIndex = 1, nameIndex = 3, typeIndex = 9;
                foreach (var row in rows)
                {
                    //if (row.ChildNodes.Count != 11 && row.ChildNodes.Count != 7) continue;
                    var cols = row.ChildNodes;
                    if (cols[1].Name == "th")
                    {
                        for (int i = 0; i < cols.Count; ++i)
                        {
                            if (cols[i].Name == "th")
                            {
                                if (cols[i].InnerText.Replace("\n", "") == "Posts")
                                {
                                    countIndex = i;
                                    continue;
                                }
                                if (cols[i].InnerText.Replace("\n", "") == "Name")
                                {
                                    nameIndex = i;
                                    continue;
                                }
                                if (cols[i].InnerText.Replace("\n", "") == "Type")
                                {
                                    typeIndex = i;
                                    continue;
                                }
                            }
                        }
                        continue;
                    }
                    if (cols[1].Name != "td") continue;

                    DanbooruTag tag = new DanbooruTag();
                    tag.Id = index.ToString();
                    tag.Count = Int32.Parse(cols[countIndex].InnerText);
                    tag.Name = Helper.RemoveControlCharacters(System.Net.WebUtility.HtmlDecode(cols[nameIndex].ChildNodes[3].InnerText.Replace("\n", "")));

                    string tagType = cols[typeIndex].InnerText.Replace("\n", "");
                    if (tagType.EndsWith("(edit)")) tagType = tagType.Substring(0, tagType.Length - 6);
                    tagType = tagType.ToLowerInvariant();
                    if (tagType == "general")
                        tag.Type = DanbooruTagType.General;
                    else if (tagType == "character")
                        tag.Type = DanbooruTagType.Character;
                    else if (tagType == "artist")
                        tag.Type = DanbooruTagType.Artist;
                    else if (tagType == "copyright")
                        tag.Type = DanbooruTagType.Copyright;
                    else if (tagType == "idol")
                        tag.Type = DanbooruTagType.Artist;
                    else if (tagType == "photo_set")
                        tag.Type = DanbooruTagType.Circle;
                    else
                        tag.Type = DanbooruTagType.Faults;

                    tags.Add(tag);
                    ++index;
                }
            }

            tagCol.Tag = tags.ToArray();
            return tagCol;
        }
示例#6
0
        public DanbooruTagCollection parseTagsPage(string data, int page)
        {
            DanbooruTagCollection tagCol = new DanbooruTagCollection();
            List <DanbooruTag>    tags   = new List <DanbooruTag>();
            HtmlDocument          doc    = new HtmlDocument();

            doc.LoadHtml(data);

            int index = 1 + ((page - 1) * 50);
            // select all tags
            var tables = doc.DocumentNode.SelectNodes("//table[contains(@class,'highlightable')]");

            foreach (var table in tables)
            {
                if (!(table.Attributes["class"].Value == "highlightable"))
                {
                    table.Remove();
                    continue;
                }

                var rows = table.SelectNodes("//table[contains(@class,'highlightable')]//tr");
                int countIndex = 1, nameIndex = 3, typeIndex = 9;
                foreach (var row in rows)
                {
                    //if (row.ChildNodes.Count != 11 && row.ChildNodes.Count != 7) continue;
                    var cols = row.ChildNodes;
                    if (cols[1].Name == "th")
                    {
                        for (int i = 0; i < cols.Count; ++i)
                        {
                            if (cols[i].Name == "th")
                            {
                                if (cols[i].InnerText.Replace("\n", "") == "Posts")
                                {
                                    countIndex = i;
                                    continue;
                                }
                                if (cols[i].InnerText.Replace("\n", "") == "Name")
                                {
                                    nameIndex = i;
                                    continue;
                                }
                                if (cols[i].InnerText.Replace("\n", "") == "Type")
                                {
                                    typeIndex = i;
                                    continue;
                                }
                            }
                        }
                        continue;
                    }
                    if (cols[1].Name != "td")
                    {
                        continue;
                    }

                    DanbooruTag tag = new DanbooruTag();
                    tag.Id    = index.ToString();
                    tag.Count = Int32.Parse(cols[countIndex].InnerText);
                    tag.Name  = Helper.RemoveControlCharacters(System.Net.WebUtility.HtmlDecode(cols[nameIndex].ChildNodes[3].InnerText.Replace("\n", "")));

                    string tagType = cols[typeIndex].InnerText.Replace("\n", "");
                    if (tagType.EndsWith("(edit)"))
                    {
                        tagType = tagType.Substring(0, tagType.Length - 6);
                    }
                    tagType = tagType.ToLowerInvariant();
                    if (tagType == "general")
                    {
                        tag.Type = DanbooruTagType.General;
                    }
                    else if (tagType == "character")
                    {
                        tag.Type = DanbooruTagType.Character;
                    }
                    else if (tagType == "artist")
                    {
                        tag.Type = DanbooruTagType.Artist;
                    }
                    else if (tagType == "copyright")
                    {
                        tag.Type = DanbooruTagType.Copyright;
                    }
                    else if (tagType == "idol")
                    {
                        tag.Type = DanbooruTagType.Artist;
                    }
                    else if (tagType == "photo_set")
                    {
                        tag.Type = DanbooruTagType.Circle;
                    }
                    else
                    {
                        tag.Type = DanbooruTagType.Faults;
                    }

                    tags.Add(tag);
                    ++index;
                }
            }

            tagCol.Tag = tags.ToArray();
            return(tagCol);
        }
示例#7
0
        /// <summary>
        /// Reparse tags from post details.
        /// </summary>
        /// <param name="post"></param>
        /// <param name="doc"></param>
        private static void ReparseTags(DanbooruPost post, HtmlDocument doc)
        {
            post.TagsEntity.Clear();
            var tags = doc.DocumentNode.SelectNodes("//ul[@id='tag-sidebar']/li");

            foreach (var tag in tags)
            {
                var tagEntity = new DanbooruTag();
                var cls       = tag.Attributes["class"].Value; // Fix Issue #146
                switch (cls)
                {
                case "tag-type-idol":
                    // idol complex
                    tagEntity.Type = DanbooruTagType.Artist;
                    break;

                case "tag-type-artist":
                    // sankaku
                    tagEntity.Type = DanbooruTagType.Artist;
                    break;

                case "tag-type-photo_set":
                    // idol complex: usually album name
                    tagEntity.Type = DanbooruTagType.Circle;
                    break;

                case "tag-type-studio":
                    // sankaku: circlename
                    tagEntity.Type = DanbooruTagType.Circle;
                    break;

                case "tag-type-meta":
                    // both
                    tagEntity.Type = DanbooruTagType.Faults;
                    break;

                case "tag-type-medium":
                    // both
                    tagEntity.Type = DanbooruTagType.Faults;
                    break;

                case "tag-type-general":
                    // both
                    tagEntity.Type = DanbooruTagType.General;
                    break;

                case "tag-type-copyright":
                    // both
                    tagEntity.Type = DanbooruTagType.Copyright;
                    break;

                case "tag-type-character":
                    // both
                    tagEntity.Type = DanbooruTagType.Character;
                    break;

                default:
                    tagEntity.Type = DanbooruTagType.Unknown;
                    break;
                }
                tagEntity.Name = Helper.DecodeEncodedNonAsciiCharacters(tag.SelectSingleNode("//ul[@id='tag-sidebar']/li/a").InnerText);
                var countStr = tag.SelectSingleNode("//ul[@id='tag-sidebar']/li//span[@class='post-count']").InnerText.Trim();
                tagEntity.Count = Int32.Parse(countStr);

                post.TagsEntity.Add(tagEntity);
                tag.Remove();
            }
            post.TagsEntity = post.TagsEntity.OrderByDescending(x => x.Type).ThenBy(x => x.Name).ToList();
        }
        /// <summary>
        /// Reparse tags from post details.
        /// </summary>
        /// <param name="post"></param>
        /// <param name="doc"></param>
        private static void ReparseTags(DanbooruPost post, HtmlDocument doc)
        {
            post.TagsEntity.Clear();
            var tagsElement = doc.DocumentNode.SelectNodes("//ul[@id='tag-sidebar']/li");
            foreach (var tag in tagsElement)
            {
                HtmlDocument tagDoc = new HtmlDocument();
                tagDoc.LoadHtml(tag.OuterHtml);

                var tagEntity = new DanbooruTag();
                var el = tagDoc.DocumentNode.SelectSingleNode("//li");
                var cls = el.Attributes["class"].Value;
                switch (cls)
                {
                    case "tag-type-idol":
                        // idol complex
                        tagEntity.Type = DanbooruTagType.Artist;
                        break;
                    case "tag-type-artist":
                        // sankaku
                        tagEntity.Type = DanbooruTagType.Artist;
                        break;

                    case "tag-type-photo_set":
                        // idol complex: usually album name
                        tagEntity.Type = DanbooruTagType.Circle;
                        break;
                    case "tag-type-studio":
                        // sankaku: circlename
                        tagEntity.Type = DanbooruTagType.Circle;
                        break;

                    case "tag-type-meta":
                        // both
                        tagEntity.Type = DanbooruTagType.Faults;
                        break;
                    case "tag-type-medium":
                        // both
                        tagEntity.Type = DanbooruTagType.Faults;
                        break;
                    case "tag-type-general":
                        // both
                        tagEntity.Type = DanbooruTagType.General;
                        break;
                    case "tag-type-copyright":
                        // both
                        tagEntity.Type = DanbooruTagType.Copyright;
                        break;
                    case "tag-type-character":
                        // both
                        tagEntity.Type = DanbooruTagType.Character;
                        break;

                    default:
                        tagEntity.Type = DanbooruTagType.Unknown;
                        break;
                }
                tagEntity.Name = tagDoc.DocumentNode.SelectSingleNode("//li/a").InnerText;
                var countStr = tagDoc.DocumentNode.SelectSingleNode("//li//span[@class='post-count']").InnerText.Trim();
                tagEntity.Count = Int32.Parse(countStr);

                post.TagsEntity.Add(tagEntity);
            }
            post.TagsEntity = post.TagsEntity.OrderByDescending(x => x.Type).ThenBy(x => x.Name).ToList();
        }
示例#9
0
        private static List<DanbooruTag> ReadTagsFromTextFile(string filename)
        {
            var list = new List<DanbooruTag>();

            // Read the file and display it line by line.
            using (var file = new System.IO.StreamReader(filename))
            {
                var line = "";
                while ((line = file.ReadLine()) != null)
                {
                    var tag = new DanbooruTag()
                    {
                        Name = line,
                        Id = "-1",
                        Count = -1,
                        Type = DanbooruTagType.Unknown
                    };

                    list.Add(tag);
                };
            }

            return list;
        }
 public DanbooruTag GetTag(string tag, DanbooruTagCollection tagCollection)
 {
     // TODO: Hot spot for perfomance
     var result = tagCollection.Tag.FirstOrDefault<DanbooruTag>(x => x.Name == tag);
     if (result != null) return result;
     else
     {
         var unknownTag = new DanbooruTag()
         {
             Name = tag,
             Type = DanbooruTagType.Unknown,
             Count = -1,
             Id = "-1"
         };
         return unknownTag;
     }
 }