Esempio n. 1
0
File: Web.cs Progetto: ta1H3n/Namiko
        public async Task SauceNao(string url = null, [Remainder] string str = "")
        {
            await Context.Channel.TriggerTypingAsync();

            url ??= Context.Message.Attachments.FirstOrDefault()?.Url;

            if (url == null)
            {
                await Context.Channel.SendMessageAsync("Can't get your attachment, there probably isn't one. *Heh, dummy...*");

                return;
            }

            var sauce = await WebUtil.SauceNETSearchAsync(url);

            if (sauce.Request.Status != 0)
            {
                await Context.Channel.SendMessageAsync($"An error occured. Server response: `{sauce.Message}`");

                return;
            }

            for (int i = sauce.Results.Count - 1; i >= 0; i--)
            {
                if (Double.Parse(sauce.Results[i].Similarity) < 42)
                {
                    sauce.Results.RemoveAt(i);
                }
            }

            if (sauce.Results.Count == 0)
            {
                await Context.Channel.SendMessageAsync("No matches. Sorry~");

                return;
            }

            await Context.Channel.SendMessageAsync("", false, WebUtil.SauceEmbed(sauce, url).Build());
        }
Esempio n. 2
0
        public static async Task Timer_RequestSauce(object sender, ElapsedEventArgs e)
        {
            Waifu        waifu  = null;
            List <Embed> embeds = new List <Embed>();
            SauceRequest req    = null;

            if (sender != null && sender is SauceRequest)
            {
                req   = sender as SauceRequest;
                waifu = req.Waifu;
            }

            try
            {
                using var db = new NamikoDbContext();
                if (waifu != null)
                {
                    waifu = await db.Waifus.AsQueryable().FirstOrDefaultAsync(x => x.Source.Equals(waifu.Source) && x.ImageSource.Equals("missing"));
                }
                if (waifu == null)
                {
                    waifu = await db.Waifus.AsQueryable().OrderBy(x => Guid.NewGuid()).FirstOrDefaultAsync(x => x.ImageSource.Equals("missing"));

                    if (waifu == null)
                    {
                        await WebhookClients.SauceRequestChannel.SendMessageAsync("`No unknown sauces. Idling...`");

                        return;
                    }
                }
                embeds.Add(WaifuUtil.WaifuEmbedBuilder(waifu).Build());

                var res = await WebUtil.SauceNETSearchAsync(waifu.HostImageUrl);

                if (res.Message.Contains("limit exceeded", StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Sauce limit exceeded");
                }
                else
                {
                    embeds.Add(WebUtil.SauceEmbed(res, waifu.HostImageUrl).Build());
                }

                var family = await db.Waifus.AsQueryable().Where(x => x.Source.Equals(waifu.Source) &&
                                                                 !(x.ImageSource == null || x.ImageSource.Equals("retry") || x.ImageSource.Equals("missing"))).ToListAsync();

                family = family.DistinctBy(x => x.ImageSource).ToList();

                string familySauces = "";
                foreach (var w in family)
                {
                    string add = $"**{w.Name}** - {w.ImageSource}\n";
                    if ((familySauces + add).Length < 1900)
                    {
                        familySauces += add;
                    }
                }
                if (familySauces != "")
                {
                    var eb = new EmbedBuilderPrepared();
                    eb.WithTitle("Possible sauces");
                    eb.WithDescription($"Image sauces of waifus from **{waifu.Source}**:\n{familySauces}");
                    embeds.Add(eb.Build());
                }

                if (req == null || req.Channel == null)
                {
                    await WebhookClients.SauceRequestChannel.SendMessageAsync("Missing waifu image sauce", embeds : embeds);
                }
                else
                {
                    foreach (var embed in embeds)
                    {
                        await req.Channel.SendMessageAsync(embed : embed);
                    }
                }
            }
            catch (Exception ex)
            {
                SentrySdk.WithScope(scope =>
                {
                    if (waifu != null)
                    {
                        scope.SetExtras(waifu.GetProperties());
                    }
                    SentrySdk.CaptureException(ex);
                });
                if (req == null || req.Channel == null)
                {
                    await WebhookClients.SauceRequestChannel.SendMessageAsync($"Broke on **{waifu.Name}** - please find source manually.");
                }
                else
                {
                    await req.Channel.SendMessageAsync($"Broke on **{waifu.Name}** - please find source manually.");
                }
            }
        }
Esempio n. 3
0
        private static async void Timer_GetSauce(object sender, ElapsedEventArgs e)
        {
            if (!NullSource && !RetrySource)
            {
                return;
            }

            Waifu waifu = null;

            try
            {
                using var db = new NamikoDbContext();

                if (NullSource)
                {
                    waifu = await db.Waifus.AsQueryable().FirstOrDefaultAsync(x => x.ImageSource == null);

                    Console.WriteLine(DateTime.Now + " New sauce - " + waifu?.Name);
                }
                if (RetrySource && waifu == null)
                {
                    NullSource = false;
                    waifu      = await db.Waifus.AsQueryable().AsQueryable().FirstOrDefaultAsync(x => x.ImageSource.Equals("retry"));

                    Console.WriteLine(DateTime.Now + " Retrying - " + waifu?.Name);
                }
                if (waifu == null)
                {
                    RetrySource = false;
                    await WebhookClients.SauceChannel.SendMessageAsync("`No missing sauces. Idling...`");

                    return;
                }

                var res = await WebUtil.SauceNETSearchAsync(waifu.HostImageUrl);

                if (res.Message.Contains("limit exceeded", StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Sauce limit exceeded");
                    return;
                }
                foreach (var result in res.Results.OrderByDescending(x => Double.Parse(x.Similarity)))
                {
                    if (Double.Parse(result.Similarity) > 80)
                    {
                        waifu.ImageSource = result.SourceURL;
                        await WebhookClients.SauceChannel.SendMessageAsync($"<:TickYes:577838859107303424> **{waifu.Name}** - {result.DatabaseName} {result.Similarity}% ({result.SourceURL})");

                        break;
                    }
                    else if ((result.DatabaseName == "Pixiv" ||
                              result.DatabaseName == "Danbooru" ||
                              result.DatabaseName == "Gelbooru" ||
                              result.DatabaseName == "AniDb" ||
                              result.DatabaseName == "Twitter") &&
                             Double.Parse(result.Similarity) > 60)
                    {
                        waifu.ImageSource = result.SourceURL;
                        await WebhookClients.SauceChannel.SendMessageAsync($":question: **{waifu.Name}** - {result.DatabaseName} {result.Similarity}% ({result.SourceURL})\n" +
                                                                           $"Verify: *{waifu.Source}* ({waifu.HostImageUrl})",
                                                                           embeds : new List <Embed> {
                            WebUtil.SauceEmbed(res, waifu.HostImageUrl).Build()
                        });

                        break;
                    }
                    else if (result.DatabaseName == "AniDb" && Double.Parse(result.Similarity) > 40)
                    {
                        waifu.ImageSource = result.SourceURL;
                        await WebhookClients.SauceChannel.SendMessageAsync($":question: **{waifu.Name}** - {result.DatabaseName} {result.Similarity}% ({result.SourceURL})\n" +
                                                                           $"Verify: *{waifu.Source}* ({waifu.HostImageUrl})",
                                                                           embeds : new List <Embed> {
                            WebUtil.SauceEmbed(res, waifu.HostImageUrl).Build()
                        });

                        break;
                    }
                }

                if (waifu.ImageSource == null || waifu.ImageSource == "")
                {
                    waifu.ImageSource = "retry";
                }
                else if (waifu.ImageSource == "retry")
                {
                    waifu.ImageSource = "missing";
                    await WebhookClients.SauceChannel.SendMessageAsync($"<:TickNo:577838859077943306> **{waifu.Name}** - missing sauce.");
                }

                db.Waifus.Update(waifu);
                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                SentrySdk.WithScope(scope =>
                {
                    if (waifu != null)
                    {
                        scope.SetExtras(waifu.GetProperties());
                    }
                    SentrySdk.CaptureException(ex);
                });
            }
        }