Example #1
0
        private static async Task Modify(Form f, ImageView view, WebClient c, ABooru booru, string[] args)
        {
            SearchResult result = await booru.GetRandomImageAsync(args);

            Console.Clear();
            Console.WriteLine($"Image preview URL: {result.previewUrl}");
            Console.WriteLine($"Image URL: {result.fileUrl}");
            Console.WriteLine($"Image Source: {result.score}");
            Console.WriteLine($"Image rating: {result.rating}");
            Console.WriteLine($"Tags on the image: {string.Join(", ", result.tags)}");
            f.Title =
                $"{result.fileUrl} - {result.rating}";
            await using Stream s = c.OpenRead(result.fileUrl);
            view.Image           = new Bitmap(s);
            SetFormSize(f, view.Image.Size);
        }
Example #2
0
        public async Task Booru(CommandContext ctx, [Description("Booru to select image from")]
                                ABooru booru,
                                [Description("Tags for image selection")]
                                params string[] tags)
        {
            if (ctx.Channel.Get(ConfigManager.Enabled)
                .And(ctx.Channel.GetMethodEnabled()))
            {
                await ctx.TriggerTypingAsync();

                SearchResult result;
                int          triesLeft = 10;
                do
                {
                    if (triesLeft == 0)
                    {
                        throw new Exception("Failed to find image in a reasonable amount of tries");
                    }
                    result = await booru.GetRandomImageAsync(tags);

                    triesLeft--;
                } while (result.rating != (
#if !NO_NSFW
                             ctx.Channel.GetEvaluatedNsfw()
                        ? Rating.Explicit
                        :
#endif
                             Rating.Safe));
                string val = Program.Rnd.Next(10000, 99999).ToString();
                using WebClient wClient = new WebClient();
                await ctx.RespondWithFileAsync($"{val}_img.jpg",
                                               wClient.OpenRead(result.fileUrl ?? result.previewUrl), embed : new DiscordEmbedBuilder
                {
                    Description = $"Tags: {string.Join(", ", result.tags)}",
                    Title       = result.source ?? "Unknown source",
                    Url         = result.fileUrl.ToString()
                }.Build());
            }
        }
Example #3
0
        public static async Task <FeatureRequest <Response.Booru, Error.Booru> > SearchBooru(bool isChanSafe, string[] tags, ABooru booru, Random r, int remainingTries = 5)
        {
            if (isChanSafe && !booru.IsSafe())
            {
                return(new FeatureRequest <Response.Booru, Error.Booru>(null, Error.Booru.ChanNotNSFW));
            }
            BooruSharp.Search.Post.SearchResult res;
            List <string> newTags = null;

            try
            {
                res = await booru.GetRandomImageAsync(tags);
            }
            catch (BooruSharp.Search.InvalidTags)
            {
                newTags = new List <string>();
                foreach (string s in tags)
                {
                    string tag     = s;
                    var    related = await new Konachan().GetTagsAsync(s);
                    if (related.Length == 0)
                    {
                        return(new FeatureRequest <Response.Booru, Error.Booru>(null, Error.Booru.NotFound));
                    }
                    newTags.Add(tag = related.OrderBy(x => GetStringDistance(x.name, s)).First().name);
                }
                try
                {
                    res = await booru.GetRandomImageAsync(newTags.ToArray());
                }
                catch (BooruSharp.Search.InvalidTags)
                {
                    return(new FeatureRequest <Response.Booru, Error.Booru>(null, Error.Booru.NotFound));
                }
            }
            Error.Booru error = Error.Booru.None;
            if (res.fileUrl == null)
            {
                if (remainingTries == 0)
                {
                    return(new FeatureRequest <Response.Booru, Error.Booru>(null, Error.Booru.NotFound));
                }
                else
                {
                    return(await SearchBooru(isChanSafe, tags, booru, r, remainingTries - 1));
                }
            }
            string url    = res.fileUrl.AbsoluteUri;
            Color  color  = GetColorFromRating(res.rating);
            string saveId = null;

            if (!(booru is E621) && !(booru is E926))
            {
                saveId = (tagInfos.Count + 1) + Utilities.GenerateRandomCode(4, r);
                tagInfos.Add(saveId, new Tuple <Type, BooruSharp.Search.Post.SearchResult>(booru.GetType(), res));
            }
            return(new FeatureRequest <Response.Booru, Error.Booru>(new Response.Booru()
            {
                url = url,
                colorRating = color,
                saveId = saveId,
                tags = res.tags,
                newTags = newTags?.ToArray()
            }, error));
        }