Пример #1
0
 public static async Task <Search.Post.SearchResult> GetRandomPost(ABooru booru)
 {
     if (booru.NoEmptyPostSearch())
     {
         return(await booru.GetRandomPostAsync("スク水")); // Pixiv doesn't handle random search with no tag
     }
     return(await booru.GetRandomPostAsync());
 }
Пример #2
0
        private async Task SearchBooruAsync(ABooru booru, string[] tags, BooruType booruId)
        {
            // GetRandomImageAsync crash if we send it something null
            tags ??= new string[0];

            BooruSharp.Search.Post.SearchResult post;
            List <string> newTags = null;

            try
            {
                post = await booru.GetRandomPostAsync(tags);
            }
            catch (InvalidTags)
            {
                // On invalid tags we try to get guess which one the user wanted to use
                newTags = new List <string>();
                foreach (string s in tags)
                {
                    var related = await new Konachan().GetTagsAsync(s); // Konachan have a feature where it can "autocomplete" a tag so we use it to guess what the user meant
                    if (related.Length == 0)
                    {
                        throw new CommandFailed("There is no image with those tags.");
                    }
                    newTags.Add(related.OrderBy(x => GetStringDistance(x.name, s)).First().name);
                }
                try
                {
                    // Once we got our new tags, we try doing a new search with them
                    post = await booru.GetRandomPostAsync(newTags.ToArray());
                }
                catch (InvalidTags)
                {
                    // Might happens if the Konachan tags don't exist in the current booru
                    throw new CommandFailed("There is no image with those tags.");
                }
            }

            int id = int.Parse("" + (int)booruId + post.id);

            StaticObjects.Tags.AddTag(id, booru, post);

            if (post.fileUrl == null)
            {
                throw new CommandFailed("A post was found but no image was available.");
            }
            await ReplyAsync(embed : new EmbedBuilder
            {
                Color    = RatingToColor(post.rating),
                ImageUrl = post.fileUrl.AbsoluteUri,
                Url      = post.postUrl.AbsoluteUri,
                Title    = "From " + Utils.ToWordCase(booru.ToString().Split('.').Last()),
                Footer   = new EmbedFooterBuilder
                {
                    Text = (newTags == null ? "" : "Some of your tags were invalid, the current search was done with: " + string.Join(", ", newTags) + "\n") +
                           "Do the 'Tags' command with then id '" + id + "' to have more information about this image."
                }
            }.Build());
        }
Пример #3
0
        public static async Task CheckGetRandom(ABooru booru, string s1)
        {
            Search.Post.SearchResult result = await booru.GetRandomPostAsync(s1);

            Search.Post.SearchResult result2;
            int i = 0;

            do
            {
                result2 = await booru.GetRandomPostAsync(s1);

                i++;
            } while (result.id == result2.id && i < 5);
            Assert.NotEqual(result.id, result2.id);
            await CheckResult(result, s1);
        }
Пример #4
0
        public async Task CheckIsSafe(Type t, string explicitTag = "pussy")
        {
            ABooru b = await General.CreateBooru(t);

            bool isSafe        = b.IsSafe();
            bool foundExplicit = false;

            for (int i = 0; i < 10; i++)
            {
                var image = await b.GetRandomPostAsync(explicitTag);

                if (isSafe && image.fileUrl != null)
                {
                    Assert.NotEqual(Search.Post.Rating.Explicit, image.rating);
                }
                if (image.rating == Search.Post.Rating.Explicit)
                {
                    foundExplicit = true;
                }
            }
            if (!isSafe)
            {
                Assert.True(foundExplicit);
            }
        }