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);
                }
            }
        }