Exemplo n.º 1
0
#pragma warning restore CS1998

        /// <summary>
        /// Get an image given various informations
        /// </summary>
        /// <param name="booru">Which booru is concerned (see above)</param>
        /// <param name="tags">Tags that need to be contain on the image</param>
        /// <param name="chan">Channel the image will be post in</param>
        /// <param name="currName">Temporary name of the file</param>
        /// <param name="isSfw">Is the channel safe for work ?</param>
        /// <param name="isGame">If the request from Game module (doesn't count dl for stats and don't get informations about tags)</param>
        public static async void getImage(Booru booru, string[] tags, ITextChannel chan, string currName, bool isSfw, bool isGame)
        {
            if (!isSfw && !chan.IsNsfw)
            {
                await chan.SendMessageAsync(Sentences.chanIsNotNsfw(chan.GuildId));

                return;
            }
            IGuildUser me = await chan.Guild.GetUserAsync(Sentences.myId);

            if (!me.GuildPermissions.AttachFiles)
            {
                await chan.SendMessageAsync(Sentences.needAttachFile(chan.GuildId));

                return;
            }
            if (!isGame)
            {
                await chan.SendMessageAsync(Sentences.prepareImage(chan.GuildId));
            }
            string url = getBooruUrl(booru, tags);

            if (url == null)
            {
                await chan.SendMessageAsync(Sentences.tagsNotFound(tags));
            }
            else
            {
                using (WebClient wc = new WebClient())
                {
                    wc.Headers.Add("User-Agent: Sanara");
                    string json      = wc.DownloadString(url);
                    string image     = booru.getFileUrl(json);
                    string imageName = currName + "." + image.Split('.')[image.Split('.').Length - 1];
                    wc.Headers.Add("User-Agent: Sanara");
                    wc.DownloadFile(image, imageName);
                    FileInfo file = new FileInfo(imageName);
                    Program.p.statsMonth[(int)booru.getId()] += file.Length;
                    if (file.Length >= 8000000)
                    {
                        await chan.SendMessageAsync(Sentences.fileTooBig(chan.GuildId));
                    }
                    else
                    {
                        while (true)
                        {
                            try
                            {
                                await chan.SendFileAsync(imageName);

                                break;
                            }
                            catch (RateLimitedException) { }
                        }
                        if (!isGame)
                        {
                            List <string> finalStr = getTagsInfos(json, booru);
                            foreach (string s in finalStr)
                            {
                                await chan.SendMessageAsync(s);
                            }
                        }
                    }
                    File.Delete(imageName);
                }
                if (!isGame)
                {
                    string finalStrModule = "";
                    foreach (long i in Program.p.statsMonth)
                    {
                        finalStrModule += i + "|";
                    }
                    finalStrModule = finalStrModule.Substring(0, finalStrModule.Length - 1);
                    File.WriteAllText("Saves/MonthModules.dat", finalStrModule + Environment.NewLine + Program.p.lastMonthSent);
                }
            }
        }
Exemplo n.º 2
0
        public async Task getNhentai(params string[] keywords)
        {
            p.doAction(Context.User, Context.Guild.Id, Program.Module.Doujinshi);
            if (!(Context.Channel as ITextChannel).IsNsfw)
            {
                await ReplyAsync(Sentences.chanIsNotNsfw(Context.Guild.Id));

                return;
            }
            string tags = "";

            if (keywords.Length != 0)
            {
                foreach (string s in keywords)
                {
                    tags += s + "+";
                }
                tags = tags.Substring(0, tags.Length - 1);
            }
            string xml;

            using (WebClient w = new WebClient())
            {
                w.Encoding = Encoding.UTF8;
                if (keywords.Length == 0)
                {
                    xml = w.DownloadString("https://nhentai.net/api/galleries/all?page=0");
                }
                else
                {
                    xml = w.DownloadString("https://nhentai.net/api/galleries/search?query=" + tags + "&page=8000");
                }
            }
            int page = p.rand.Next(Convert.ToInt32(Program.getElementXml("\"num_pages\":", xml, ','))) + 1;

            using (WebClient w = new WebClient())
            {
                w.Encoding = Encoding.UTF8;
                if (keywords.Length == 0)
                {
                    xml = w.DownloadString("https://nhentai.net/api/galleries/all?page=" + page);
                }
                else
                {
                    xml = w.DownloadString("https://nhentai.net/api/galleries/search?query=" + tags + "&page=" + page);
                }
            }
            List <string> allDoujinshi = xml.Split(new string[] { "title" }, StringSplitOptions.None).ToList();

            allDoujinshi.RemoveAt(0);
            if (allDoujinshi.Count == 0)
            {
                string[] allTags = tags.Split(new string[] { "+" }, StringSplitOptions.RemoveEmptyEntries);
                await ReplyAsync(Sentences.tagsNotFound(allTags));
            }
            else
            {
                string   curr      = allDoujinshi[p.rand.Next(allDoujinshi.Count)];
                string[] ids       = curr.Split(new string[] { "}]" }, StringSplitOptions.None);
                string   currBlock = "";
                for (int i = ids.Length - 1; i >= 0; i--)
                {
                    currBlock = Program.getElementXml("id\":", ids[i], ',');
                    if (currBlock != "")
                    {
                        if (keywords.Length == 0)
                        {
                            await ReplyAsync("https://nhentai.net/g/" + currBlock);
                        }
                        else
                        {
                            string finalOk = "";
                            foreach (string t in keywords)
                            {
                                bool isOk = false;
                                foreach (string s in ids[i - 1].Split(new string[] { "},{" }, StringSplitOptions.None))
                                {
                                    if (Program.getElementXml("\"name\":\"", s, '"').Contains(t))
                                    {
                                        isOk = true;
                                        break;
                                    }
                                }
                                if (!isOk)
                                {
                                    finalOk = t;
                                    break;
                                }
                            }
                            if (finalOk == "")
                            {
                                await ReplyAsync("https://nhentai.net/g/" + currBlock);
                            }
                            else
                            {
                                await ReplyAsync(Sentences.tagsNotFound(new string[] { finalOk }));
                            }
                        }
                        break;
                    }
                }
            }
        }