public void IsBooruAuthSet() { var b = new Gelbooru(); Assert.True(b.Auth == null); b.Auth = new BooruAuth("AAA", "AAA"); Assert.False(b.Auth == null); }
public async Task SetHttpClient() { var booru = new Gelbooru(); HttpClient hc = new HttpClient(); hc.DefaultRequestHeaders.Add("User-Agent", "BooruSharp.Unit-Tests"); booru.HttpClient = hc; await General.CheckGetRandom(booru, "kantai_collection"); Assert.Single(hc.DefaultRequestHeaders.GetValues("User-Agent")); Assert.Contains("BooruSharp.Unit-Tests", hc.DefaultRequestHeaders.GetValues("User-Agent")); }
public static async Task <GelStruct> GetGelImageAsync(string search) { try { var results = await Gelbooru.SearchPostsAsync(search, 10000); return(results[r.Next(results.Count)]); } catch { return(null); } }
static BooruHelper() { HttpClient = new HttpClient(new SocketsHttpHandler { AutomaticDecompression = DecompressionMethods.All, }); Danbooru = new Danbooru(HttpClient); Gelbooru = new Gelbooru(HttpClient); Konachan = new Konachan(HttpClient); SankakuComplex = new SankakuComplex(HttpClient); Yandere = new Yandere(HttpClient); TaskCancellationDelay = TimeSpan.FromMilliseconds(50); }
public AddResult Add(ulong ChannelId, List <string> Tags) { SubTag n = new SubTag() { Tags = Tags }; if (this.Tags.ContainsKey(ChannelId)) { for (int i = 0; i < this.Tags[ChannelId].Count; i++) { SubTag current = this.Tags[ChannelId][i]; if (current.Identical(n)) { return(AddResult.AlreadyContains); } } List <Post> posts = Gelbooru.FetchImages(Tags); if (posts.Count == 0) { return(AddResult.NoImages); } n.PostIds = posts.Select(t => t.PostId).ToList(); this.Tags[ChannelId].Add(n); } else { List <Post> posts = Gelbooru.FetchImages(Tags); if (posts.Count == 0) { return(AddResult.NoImages); } n.PostIds = posts.Select(t => t.PostId).ToList(); this.Tags.Add(ChannelId, new List <SubTag>() { n }); } Save(); return(AddResult.Success); }
private void listb_Click(object sender, EventArgs e) { double num; if (pageb.Value == 0 || !double.TryParse(pageb.Text, out num)) { pageb.ForeColor = Color.Black; pageb.Value = 1; } if (tagsb.Text == "Tags...") { tagsb.Text = ""; } statusl.Text = "Loading posts..."; //list.Rows.Clear(); list.AutoGenerateColumns = false; Site site = null; if (serverlist.Text == "Konachan") { site = new Moebooru("http://konachan.com", "page=" + pageb.Text + "&tags=" + tagsb.Text); } else if (serverlist.Text == "Danbooru") { site = new Danbooru("http://danbooru.donmai.us", "page=" + pageb.Text + "&tags=" + tagsb.Text); } else if (serverlist.Text == "Gelbooru") { site = new Gelbooru("http://gelbooru.com", tagsb.Text); } list.DataSource = site.bs; site.ProgressChange += site_ProgressChange; statusl.Text = "Posts loaded!"; }
public static List <Post> FetchImages(string Tags, bool RandomPage = false) { string baseLink = $"https://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=42&json=1&tags={ Tags }"; if (RandomPage) { baseLink += "&pid=" + Gelbooru.RandomPage(Tags); } if (!Global.Settings.Credentials.IsEmpty(Models.Settings.Credentials.CredentialType.Gelbooru)) { baseLink += $"&api_key={ Global.Settings.Credentials.Gelbooru.APIKey }&user_id={ Global.Settings.Credentials.Gelbooru.UserId }"; } HttpClientHandler handler = new HttpClientHandler(); CookieContainer container = new CookieContainer(); container.Add(new Cookie("fringeBenefits", "yup", "/", "gelbooru.com")); handler.CookieContainer = container; HttpClient client = new HttpClient(handler); HttpResponseMessage message = client.GetAsync(baseLink).Result; string Content = message.Content.ReadAsStringAsync().Result; List <Post> posts; try { posts = JsonConvert.DeserializeObject <DAPI>(Content).Posts; } catch (Exception e) { Logger.Log(LogType.Error, ConsoleColor.Red, "SubTag", "Base Link: " + baseLink); Logger.Log(LogType.Error, ConsoleColor.Red, "SubTag", "Content: " + Content); throw e; } return(posts); }
public async Task <CharacInfo> GetAnime(string query) { Gelbooru booru = new Gelbooru(); Dictionary <string, int> tags = new Dictionary <string, int>(); for (int i = 1; i <= 100; i++) { try { foreach (string s in (await booru.GetImage(i, query, "1girl")).tags) { if (tags.ContainsKey(s)) { tags[s]++; } else { tags.Add(s, 1); } } } catch (BooruSharp.Search.InvalidTags) { break; } } string source = null; int sourcePercent = 0; foreach (var item in tags) { try { if ((await booru.GetTag(item.Key)).type == BooruSharp.Search.Tag.TagType.Copyright && item.Value > sourcePercent) { source = item.Key; sourcePercent = item.Value; } } catch (BooruSharp.Search.InvalidTags) { } } string hair = null, eyes = null; int hairPercent = 0, eyesPercent = 0; foreach (var item in tags) { string[] parts = item.Key.Split('_'); if (parts.Length == 2) { if (parts[1] == "hair" && colors.Contains(parts[0]) && item.Value > hairPercent) { hair = parts[0]; hairPercent = item.Value; } else if (parts[1] == "eyes" && colors.Contains(parts[0]) && item.Value > eyesPercent) { eyes = parts[0]; eyesPercent = item.Value; } } } return(new CharacInfo(source, eyes, hair)); }
protected override void Init() { base.Init(); _booru = new Gelbooru(); }
private void ProcessImages(object sender, EventArgs e) { bool SaveTags = false; foreach (ulong key in Tags.Keys) { if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested) { break; } if (Global.Client.GetChannel(key) is ITextChannel channel) { LanguageEntry language = channel.GetSettings().GetLanguage(); List <SubTag> SubTags = Tags[key]; for (int i = 0; i < SubTags.Count; i++) { if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested) { break; } List <Post> CurrentImages = new List <Post>(); try { CurrentImages.AddRange(Gelbooru.FetchImages(SubTags[i].Tags)); } catch (Exception ex) { SendToErrorChannel(ex, $"Couldn't fetch images with tags: `{ string.Join(", ", SubTags[i].Tags) }`!\nWaiting 30 seconds..."); if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested) { break; } Thread.Sleep(30 * 1000); } if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested) { break; } Thread.Sleep(2000); if (CurrentImages.Count == 0) { continue; } bool PerformSave = false; for (int j = 0; j < CurrentImages.Count; j++) { Post post = CurrentImages[j]; if (!SubTags[i].PostIds.Contains(post.PostId)) { PerformSave = true; string joined_tags = SubTags[i].Tags.Count == 0 ? language.GetEntry("SubTagHandler:NoTag") : string.Join(" ", SubTags[i].Tags); string mentions = ""; if (SubTags[i].UserMentions.Count != 0) { mentions = string.Join(", ", SubTags[i].UserMentions.Select(t => $"<@{ t }>")); } channel.SendMessageAsync(mentions, embed: new EmbedBuilder() { Color = Color.Magenta, Title = language.GetEntry("SubTagHandler:NewImage", "TAGS", joined_tags) + (post.IsAnimated ? "[ANIMATED]" : "") + " - " + post.Filename, ImageUrl = (!post.IsAnimated || post.Filename.EndsWith(".gif") ? post.Link : post.ThumbnailUrl), Url = post.PostLink }.Build()); if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested) { break; } Thread.Sleep(2000); } } if (PerformSave) { Tags[key][i].PostIds = CurrentImages.Select(t => t.PostId).ToList(); Save(); } } } else { Tags.Remove(key); SaveTags = true; } } if (SaveTags) { Save(); } }
static async Task Main(string[] args) { var entryCharacter = "ikazuchi_(kantai_collection)"; // First search var entryAnime = "kantai_collection"; Dictionary <string, Json> allEntries = new Dictionary <string, Json>(); List <int> alreadyAsked = new List <int>();// Ids already asked List <string> charactersAlreadyAsked = new List <string>(); var booru = new Gelbooru(); var rand = new Random(); Dictionary <string, TagType> tags = new Dictionary <string, TagType>(); List <(string, string)> remainingCharacters = new List <(string, string)> { (entryCharacter, entryAnime) }; if (Directory.Exists("Result")) { foreach (var file in Directory.GetFiles("Result")) { File.Delete(file); } } else { Directory.CreateDirectory("Result"); } next: var current = remainingCharacters[0]; charactersAlreadyAsked.Add(current.Item1); remainingCharacters.RemoveAt(0); var result = await booru.GetRandomPostsAsync(int.MaxValue, new[] { "yuri", "2girls", current.Item1 }); foreach (var r in result) { if (alreadyAsked.Contains(r.id)) // We already parsed this image { continue; } alreadyAsked.Add(r.id); List <string> licences = new List <string>(); List <string> characters = new List <string>(); foreach (var t in r.tags) { if (tags.ContainsKey(t)) // TODO: Manage series { var tag = tags[t]; if (tag == TagType.Character) { characters.Add(t); } else if (tag == TagType.Copyright) { licences.Add(t); } continue; } SearchResult tagRes; try { tagRes = await booru.GetTagAsync(t); } catch (Exception e) { if (e is ArgumentException || e is InvalidTags) { continue; } throw; } tags.Add(t, tagRes.type); if (tagRes.type == TagType.Character) { characters.Add(t); } else if (tagRes.type == TagType.Copyright) { licences.Add(t); } } if (characters.Any(x => x.Contains("admiral") || licences.Contains("original"))) // TODO: Put that elsewhere { continue; } if (characters.Count == 2 && licences.Count <= 2 && characters.Contains(current.Item1) && licences.Contains(current.Item2)) { characters.OrderBy(x => x); var otherLicence = licences.Where(x => x != current.Item2).FirstOrDefault()?.Replace("_", ""); // Will be null if it's not a crossover string licenceName = otherLicence == null?current.Item2.Replace("_", "") : "crossover"; string c1, c2; if (otherLicence != null) // Crossover { var a = Regex.Replace(characters[0], "\\([^\\)]+\\)", "").Replace('_', ' ').Trim(); var b = Regex.Replace(characters[1], "\\([^\\)]+\\)", "").Replace('_', ' ').Trim(); if (a == current.Item1) { a = current.Item2.Replace("_", "") + "_" + a; b = otherLicence + "_" + b; } else { a = otherLicence + "_" + a; b = current.Item2.Replace("_", "") + "_" + b; } c1 = string.Compare(a, b) < 0 ? a : b; c2 = string.Compare(a, b) < 0 ? b : a; } else { c1 = Regex.Replace(characters[0], "\\([^\\)]+\\)", "").Replace('_', ' ').Trim(); c2 = Regex.Replace(characters[1], "\\([^\\)]+\\)", "").Replace('_', ' ').Trim(); } if (!allEntries.ContainsKey(licenceName)) { allEntries.Add(licenceName, new Json() { name = licenceName, ships = new Dictionary <string, Dictionary <string, List <Entry> > >(), color = $"#{rand.Next(127, 256).ToString("x2")}{rand.Next(127, 256).ToString("x2")}{rand.Next(127, 256).ToString("x2")}" }); } if (!allEntries[licenceName].ships.ContainsKey(c1)) { allEntries[licenceName].ships.Add(c1, new Dictionary <string, List <Entry> >()); } if (!allEntries[licenceName].ships[c1].ContainsKey(c2)) { allEntries[licenceName].ships[c1].Add(c2, new List <Entry>()); } if (allEntries[licenceName].ships[c1][c2].Count < 3) { allEntries[licenceName].ships[c1][c2].Add( new Entry { link = r.postUrl.AbsoluteUri, imageId = r.fileUrl.AbsoluteUri, linkType = "gelbooru", nsfw = (int)r.rating }); } foreach (var s in characters) { if (!charactersAlreadyAsked.Contains(s)) { charactersAlreadyAsked.Add(s); remainingCharacters.Add((s, otherLicence ?? licences[0])); } } Console.WriteLine($"Found relation between {c1} and {c2}. ({licenceName})"); } } foreach (var j in allEntries) { File.WriteAllText("Result/" + j.Key + ".json", JsonConvert.SerializeObject(j.Value)); } var d = new Dictionary <string, string[]>(); d.Add("names", allEntries.Select(x => x.Key).Where(x => x != "crossover").ToArray()); File.WriteAllText("Result/names.json", JsonConvert.SerializeObject(d)); Console.WriteLine("JSON saved\n\n"); if (remainingCharacters.Count > 0) { goto next; } Console.WriteLine("Press any key to continue"); Console.ReadKey(); } }
public static void Setup() { StartedTime = new TimeSpan(DateTime.Now.Ticks); Random = new TRandom(new NR3Generator(TMath.Seed())); Logger = new BaseLogger(); Logger.Log(ConsoleColor.Cyan, LogType.NoDisplay, "Info", "Welcome to Chino-chan!"); CheckExternalLibs(); LoadSettings(); Gelbooru = new Gelbooru(); Danbooru = new Danbooru(); Yandere = new Yandere(); if (!Settings.Credentials.IsEmpty(CredentialType.Sankaku)) { Sankaku = new Sankaku(Settings.Credentials.Sankaku.Username, Settings.Credentials.Sankaku.Password); } if (!Settings.Credentials.IsEmpty(CredentialType.Imgur)) { Imgur = new Image.Imgur(); } if (!string.IsNullOrWhiteSpace(Settings.Credentials.Google.ClientSecret) && !string.IsNullOrWhiteSpace(Settings.Credentials.Google.ClientId)) { Logger.Log(ConsoleColor.Green, LogType.GoogleDrive, null, "Logging into GoogleDrive..."); var Credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets() { ClientId = Settings.Credentials.Google.ClientId, ClientSecret = Settings.Credentials.Google.ClientSecret }, new string[] { DriveService.Scope.Drive }, "user", CancellationToken.None).Result; GoogleDrive = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = Credential, ApplicationName = "Chino-chan" }); Logger.Log(ConsoleColor.Green, LogType.GoogleDrive, null, "Logged in!"); } if (!string.IsNullOrWhiteSpace(Settings.Credentials.Google.Token)) { Logger.Log(ConsoleColor.Red, LogType.YouTubeAPI, null, "Creating YouTube service..."); YouTube = new YouTubeService(new BaseClientService.Initializer() { ApiKey = Settings.Credentials.Google.Token, ApplicationName = "Chino-chan" }); Logger.Log(ConsoleColor.Red, LogType.YouTubeAPI, null, "Done!"); } LanguageHandler = new LanguageHandler(); GuildSettings = new GuildSettings(); Updater = new Updater(); Client = new DiscordSocketClient(new DiscordSocketConfig() { AlwaysDownloadUsers = true, DefaultRetryMode = RetryMode.AlwaysRetry, LargeThreshold = 250, LogLevel = LogSeverity.Verbose }); Client.Log += (Log) => { Logger.Log(ConsoleColor.White, LogType.Discord, Log.Severity.ToString(), Log.Message); return(Task.CompletedTask); }; Client.Ready += () => { if (Sankaku != null) { Logger.Log(ConsoleColor.DarkYellow, LogType.Sankaku, "Login", "Logging in..."); var LoggedIn = Sankaku.Login(out bool TooManyRequests); if (LoggedIn) { Logger.Log(ConsoleColor.DarkYellow, LogType.Sankaku, "Login", "Logged in!"); } else { Logger.Log(ConsoleColor.Red, LogType.Sankaku, "Login", "Couldn't log in due to " + (TooManyRequests ? "rate limitation!" : "wrong credentials!")); } } return(Task.CompletedTask); }; if (osuAPIEnabled) { osuAPI = new osuApi(); } CommandService = new CommandService(new CommandServiceConfig() { CaseSensitiveCommands = false, LogLevel = LogSeverity.Verbose, DefaultRunMode = RunMode.Async }); Logger.Log(ConsoleColor.Cyan, LogType.Commands, null, "Loading Commands..."); Services = new ServiceCollection().BuildServiceProvider(); CommandService.AddModulesAsync(Assembly.GetEntryAssembly(), Services).ContinueWith((ModuleInfo) => { Logger.Log(ConsoleColor.Cyan, LogType.Commands, null, "Loaded Commands!"); var Modules = ModuleInfo.Result; var Text = ""; var HelpNotFound = new List <string>(); var English = LanguageHandler.GetLanguage("en"); foreach (var Module in Modules) { Text += Environment.NewLine + "- " + Module.Name + Environment.NewLine; foreach (var Command in Module.Commands) { Text += "-- " + Command.Name + Environment.NewLine; if (!English.Help.ContainsKey(Command.Name)) { HelpNotFound.Add(Command.Name); } } } Logger.Log(ConsoleColor.Cyan, LogType.Commands, null, "Available modules and commands: " + Text); if (HelpNotFound.Count != 0) { Logger.Log(ConsoleColor.Cyan, LogType.Commands, "Warning", "Help not found for these commands: " + string.Join(", ", HelpNotFound)); } }); Client.MessageReceived += async(ReceivedMessage) => { var Message = ReceivedMessage as SocketUserMessage; if (Message == null) { return; } var Context = new CommandContext(Client, Message); var Settings = GuildSettings.GetSettings(Context.Guild != null ? Context.Guild.Id : Context.User.Id); if (Global.Settings.SayPreferences.ContainsKey(Context.User.Id) && !Message.Content.StartsWith(Settings.Prefix + "say")) { var Prefs = Global.Settings.SayPreferences[Context.User.Id]; if (Prefs.Listening.ContainsKey(Context.Channel.Id)) { if (Client.GetChannel(Prefs.Listening[Context.Channel.Id]) is ITextChannel Channel) { await Channel.SendMessageAsync(Message.Content); if (Prefs.AutoDel) { var Dm = await Context.User.GetOrCreateDMChannelAsync(); if (Dm.Id != Context.Channel.Id) { await Message.DeleteAsync(); } } } else { Global.Settings.SayPreferences[Context.User.Id].Listening.Remove(Context.Channel.Id); SaveSettings(); } } } if (Message.Content == "/gamerescape") { var Name = Message.Author.Username; if (!Context.IsPrivate) { await Message.DeleteAsync(); Name = (await Context.Guild.GetUserAsync(Message.Author.Id)).Nickname ?? Name; } await Message.Channel.SendMessageAsync($"{ Name } ¯\\_(ツ)_/¯"); return; } else if (Message.Content == "/lenny") { var Name = Message.Author.Username; if (!Context.IsPrivate) { await Message.DeleteAsync(); Name = (await Context.Guild.GetUserAsync(Message.Author.Id)).Nickname ?? Name; } await Message.Channel.SendMessageAsync($"{ Name } ( ͡° ͜ʖ ͡°)"); return; } int Position = 0; if (!(Message.HasStringPrefix(Settings.Prefix, ref Position) || Message.HasMentionPrefix(Client.CurrentUser, ref Position))) { return; } var MessageCommand = Message.Content.Substring(Position).ToLower(); if (Images.Images.ContainsKey(MessageCommand)) { new Task(async() => { var Pair = Images.Images[MessageCommand]; if (Pair.IsNsfw && !(Context.IsPrivate || IsNsfwChannel(Settings, Message.Channel.Id))) { await Message.Channel.SendMessageAsync(LanguageHandler.GetLanguage(Settings.LanguageId).OnlyNsfw); return; } bool Success = false; var File = ""; do { if (!string.IsNullOrWhiteSpace(File)) { Images.Images[MessageCommand].Files.Remove(File); if (Images.Images[MessageCommand].Files.Count == 0) { await Message.Channel.SendMessageAsync(LanguageHandler.GetLanguage(Settings.LanguageId).CantUploadImage); break; } } File = Pair.RandomFile(); Success = await SendImageAsync(File, Context.Channel, Pair.TitleIncludeName ? Pair.Name : null); }while (!Success); }).Start(); return; } var Result = await CommandService.ExecuteAsync(Context, Position, Services); if (!Result.IsSuccess) { switch (Result.Error) { case CommandError.BadArgCount: break; case CommandError.ParseFailed: break; case CommandError.UnmetPrecondition: if (Result.ErrorReason == "Owner") { await Context.Channel.SendMessageAsync(Context.GetLanguage().NoOwnerPermission); } else if (Result.ErrorReason == "ServerSide") { await Context.Channel.SendMessageAsync(Context.GetLanguage().OnlyServer); } else { await Context.Channel.SendMessageAsync(Context.GetLanguage().NoPermission); } break; case CommandError.UnknownCommand: if (IsOwner(Context.User.Id)) { var Command = Tools.ConvertHighlightsBack(Message.Content.Substring(Position)); new Thread(() => Entrance.HandleCommand(Command, Context.Channel as ITextChannel)).Start(); } break; default: await Context.Channel.SendMessageAsync($"```css\nAn error happened! Please report this to { Global.Settings.OwnerName }!\n\nError type: { Result.Error }\nReason: { Result.ErrorReason }```"); break; } } else { Logger.Log(ConsoleColor.DarkYellow, LogType.Commands, Context.Guild != null ? Context.Guild.Name : Context.Channel.Name, $"#{ Context.Channel.Name } { Context.User.Username } executed { Context.Message.Content }"); } }; MusicClients = new Dictionary <ulong, Modules.Music>(); SysInfo = new SysInfo(); SysInfo.Load(); GC.Collect(); GC.WaitForPendingFinalizers(); }