public static HitomiIndexMetadata?GetMetadataFromMagic(string magic)
        {
            HitomiIndexMetadata tmp = new HitomiIndexMetadata()
            {
                ID = Convert.ToInt32(magic)
            };
            var pos = HitomiIndex.Instance.metadata_collection.BinarySearch(tmp, new CompareMetadata());

            if (pos < 0)
            {
                return(null);
            }
            return(HitomiIndex.Instance.metadata_collection[pos]);
        }
Exemple #2
0
        public static HitomiArticle MetadataToArticle(HitomiIndexMetadata metadata)
        {
            HitomiArticle article = new HitomiArticle();

            if (metadata.Artists != null)
            {
                article.Artists = metadata.Artists.Select(x => HitomiIndex.Instance.index.Artists[x]).ToArray();
            }
            if (metadata.Characters != null)
            {
                article.Characters = metadata.Characters.Select(x => HitomiIndex.Instance.index.Characters[x]).ToArray();
            }
            if (metadata.Groups != null)
            {
                article.Groups = metadata.Groups.Select(x => HitomiIndex.Instance.index.Groups[x]).ToArray();
            }
            if (metadata.Language >= 0)
            {
                article.Language = HitomiIndex.Instance.index.Languages[metadata.Language];
            }
            article.Magic = metadata.ID.ToString();
            if (metadata.Parodies != null)
            {
                article.Series = metadata.Parodies.Select(x => HitomiIndex.Instance.index.Series[x]).ToArray();
            }
            if (metadata.Tags != null)
            {
                article.Tags = metadata.Tags.Select(x => HitomiIndex.Instance.index.Tags[x]).ToArray();
            }
            article.Title = metadata.Name;
            if (metadata.Type >= 0)
            {
                article.Type = HitomiIndex.Instance.index.Types[metadata.Type];
            }
            return(article);
        }
        public static bool match(object[] queries, HitomiIndexMetadata md)
        {
            bool[] checker = new bool[queries.Length];

            for (int i = 1; i < queries.Length; i++)
            {
                if (queries[i] is HitomiDataAdvancedQuery query)
                {
                    if (query.is_operator == false)
                    {
                        string token = query.token.Replace('_', ' ');
                        switch (query.token_type)
                        {
                        case HitomiDataAdvancedQueryTokenType.None:
                            Console.Console.Instance.WriteErrorLine("Query type not found!");
                            Console.Console.Instance.Write(Monitor.SerializeObject(query));
                            throw new Exception($"Query system error!");

                        case HitomiDataAdvancedQueryTokenType.Common:
                            if (md.Artists != null)
                            {
                                checker[i] = md.Artists.Any(x => HitomiIndex.Instance.index.Artists[x].Contains(token));
                            }
                            if (md.Name != null)
                            {
                                checker[i] = md.Name.ToLower().Contains(token);
                            }
                            if (md.Groups != null)
                            {
                                checker[i] = md.Groups.Any(x => HitomiIndex.Instance.index.Groups[x].Contains(token));
                            }
                            if (md.Parodies != null)
                            {
                                checker[i] = md.Parodies.Any(x => HitomiIndex.Instance.index.Series[x].Contains(token));
                            }
                            if (md.Characters != null)
                            {
                                checker[i] = md.Characters.Any(x => HitomiIndex.Instance.index.Characters[x].Contains(token));
                            }
                            if (md.Language >= 0 && HitomiIndex.Instance.index.Languages[md.Language] == token)
                            {
                                checker[i] = true;
                            }
                            if (md.Type >= 0 && HitomiIndex.Instance.index.Types[md.Type] == token)
                            {
                                checker[i] = true;
                            }
                            if (md.ID.ToString() == token)
                            {
                                checker[i] = true;
                            }
                            if (md.Tags != null)
                            {
                                checker[i] = md.Tags.Any(x => HitomiIndex.Instance.index.Tags[x].Contains(token));
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Artists:
                            if (md.Artists != null)
                            {
                                checker[i] = md.Artists.Select(x => HitomiIndex.Instance.index.Artists[x]).Contains(token);
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Title:
                            if (md.Name != null)
                            {
                                checker[i] = token.ToLower().Contains(token);
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Groups:
                            if (md.Groups != null)
                            {
                                checker[i] = md.Groups.Select(x => HitomiIndex.Instance.index.Groups[x]).Contains(token);
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Series:
                            if (md.Parodies != null)
                            {
                                checker[i] = md.Parodies.Select(x => HitomiIndex.Instance.index.Series[x]).Contains(token);
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Characters:
                            if (md.Characters != null)
                            {
                                checker[i] = md.Characters.Select(x => HitomiIndex.Instance.index.Characters[x]).Contains(token);
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Type:
                            if (md.Type >= 0 && HitomiIndex.Instance.index.Types[md.Type] == token)
                            {
                                checker[i] = true;
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Language:
                            var lang = "n/a";     //md.Language;
                            if (md.Language >= 0)
                            {
                                lang = HitomiIndex.Instance.index.Languages[md.Language];
                            }
                            if (lang == token)
                            {
                                checker[i] = true;
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Id:
                            if (md.ID.ToString() == token)
                            {
                                checker[i] = true;
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.Tag:
                            if (md.Tags != null)
                            {
                                checker[i] = md.Tags.Select(x => HitomiIndex.Instance.index.Tags[x]).Contains(token);
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.FemaleTag:
                            if (md.Tags != null)
                            {
                                checker[i] = md.Tags.Select(x => HitomiIndex.Instance.index.Tags[x]).Contains("female:" + token);
                            }
                            break;

                        case HitomiDataAdvancedQueryTokenType.MaleTag:
                            if (md.Tags != null)
                            {
                                checker[i] = md.Tags.Select(x => HitomiIndex.Instance.index.Tags[x]).Contains("male:" + token);
                            }
                            break;
                        }
                        if (query.option == HitomiDataAdvancedQueryTokenOption.Complement)
                        {
                            checker[i] = !checker[i];
                        }
                    }
                }
            }

            for (int i = queries.Length - 1; i >= 0; i--)
            {
                if (queries[i] is HitomiDataAdvancedQuery query)
                {
                    if (query.is_operator == true)
                    {
                        int s1 = i * 2;
                        int s2 = i * 2 + 1;

                        var qop = queries[i] as HitomiDataAdvancedQuery;

                        if (qop.combination == HitomiDataAdvancedQueryCombinationOption.Default)
                        {
                            checker[i] = checker[s1] && checker[s2];
                        }
                        else if (qop.combination == HitomiDataAdvancedQueryCombinationOption.Combination)
                        {
                            checker[i] = checker[s1] || checker[s2];
                        }
                        else if (qop.combination == HitomiDataAdvancedQueryCombinationOption.Difference)
                        {
                            checker[i] = checker[s1] && !checker[s2];
                        }

                        if (qop.option == HitomiDataAdvancedQueryTokenOption.Complement)
                        {
                            checker[i] = !checker[i];
                        }
                    }
                }
            }

            return(checker[1]);
        }
Exemple #4
0
        public static void MakeIndex()
        {
            var artists    = new Dictionary <string, int>();
            var groups     = new Dictionary <string, int>();
            var series     = new Dictionary <string, int>();
            var characters = new Dictionary <string, int>();
            var languages  = new Dictionary <string, int>();
            var types      = new Dictionary <string, int>();
            var tags       = new Dictionary <string, int>();

            foreach (var md in HitomiData.Instance.metadata_collection)
            {
                add(artists, md.Artists);
                add(groups, md.Groups);
                add(series, md.Parodies);
                add(characters, md.Characters);
                if (md.Language != null)
                {
                    add(languages, md.Language.ToLower());
                }
                else
                {
                    add(languages, md.Language);
                }
                if (md.Type != null)
                {
                    add(types, md.Type.ToLower());
                }
                else
                {
                    add(types, md.Type);
                }
                add(tags, md.Tags);
            }

            var index = new HitomiIndexModel();

            index.Artists    = pp(artists);
            index.Groups     = pp(groups);
            index.Series     = pp(series);
            index.Characters = pp(characters);
            index.Languages  = pp(languages);
            index.Types      = pp(types);
            index.Tags       = pp(tags);

            var mdl = new List <HitomiIndexMetadata>();

            foreach (var md in HitomiData.Instance.metadata_collection)
            {
                var him = new HitomiIndexMetadata();
                him.ID   = md.ID;
                him.Name = md.Name;
                if (md.Artists != null)
                {
                    him.Artists = md.Artists.Select(x => artists[x]).ToArray();
                }
                if (md.Groups != null)
                {
                    him.Groups = md.Groups.Select(x => groups[x]).ToArray();
                }
                if (md.Parodies != null)
                {
                    him.Parodies = md.Parodies.Select(x => series[x]).ToArray();
                }
                if (md.Characters != null)
                {
                    him.Characters = md.Characters.Select(x => characters[x]).ToArray();
                }
                if (md.Language != null)
                {
                    him.Language = languages[md.Language.ToLower()];
                }
                else
                {
                    him.Language = -1;
                }
                if (md.Type != null)
                {
                    him.Type = types[md.Type.ToLower()];
                }
                else
                {
                    him.Type = -1;
                }
                if (md.Tags != null)
                {
                    him.Tags = md.Tags.Select(x => tags[x]).ToArray();
                }
                mdl.Add(him);
            }

            var result = new HitomiIndexDataModel();

            result.index    = index;
            result.metadata = mdl;

            var bbb = MessagePackSerializer.Serialize(result);

            using (FileStream fsStream = new FileStream(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "index-metadata.json"), FileMode.Create))
                using (BinaryWriter sw = new BinaryWriter(fsStream))
                {
                    sw.Write(bbb);
                }
        }